blob: 27980bd541f30401b294487641320dad9bfa8ea7 [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 Hutchins79747e02012-04-23 16:45:01 +0000316 if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) {
317 // Ignore empty strings without warnings
318 if (StrLit->getLength() == 0)
319 continue;
320
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000321 // We allow constant strings to be used as a placeholder for expressions
322 // that are not valid C++ syntax, but warn that they are ignored.
323 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) <<
324 Attr.getName();
325 continue;
326 }
327
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000328 QualType ArgTy = ArgExp->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000329
DeLesley Hutchins79747e02012-04-23 16:45:01 +0000330 // A pointer to member expression of the form &MyClass::mu is treated
331 // specially -- we need to look at the type of the member.
332 if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp))
333 if (UOp->getOpcode() == UO_AddrOf)
334 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr()))
335 if (DRE->getDecl()->isCXXInstanceMember())
336 ArgTy = DRE->getDecl()->getType();
337
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000338 // First see if we can just cast to record type, or point to record type.
339 const RecordType *RT = getRecordType(ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000340
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000341 // Now check if we index into a record type function param.
342 if(!RT && ParamIdxOk) {
343 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000344 IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp);
345 if(FD && IL) {
346 unsigned int NumParams = FD->getNumParams();
347 llvm::APInt ArgValue = IL->getValue();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000348 uint64_t ParamIdxFromOne = ArgValue.getZExtValue();
349 uint64_t ParamIdxFromZero = ParamIdxFromOne - 1;
350 if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000351 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range)
352 << Attr.getName() << Idx + 1 << NumParams;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000353 continue;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000354 }
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000355 ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000356 }
357 }
358
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000359 checkForLockableRecord(S, D, Attr, ArgTy);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000360
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000361 Args.push_back(ArgExp);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000362 }
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000363}
364
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000365//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000366// Attribute Implementations
367//===----------------------------------------------------------------------===//
368
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000369// FIXME: All this manual attribute parsing code is gross. At the
370// least add some helper functions to check most argument patterns (#
371// and types of args).
372
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000373static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr,
374 bool pointer = false) {
375 assert(!Attr.isInvalid());
376
377 if (!checkAttributeNumArgs(S, Attr, 0))
378 return;
379
380 // D must be either a member field or global (potentially shared) variable.
381 if (!mayBeSharedVariable(D)) {
382 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000383 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000384 return;
385 }
386
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000387 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000388 return;
389
390 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000391 D->addAttr(::new (S.Context) PtGuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000392 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000393 D->addAttr(::new (S.Context) GuardedVarAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000394}
395
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000396static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000397 bool pointer = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000398 assert(!Attr.isInvalid());
399
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000400 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000401 return;
402
403 // D must be either a member field or global (potentially shared) variable.
404 if (!mayBeSharedVariable(D)) {
405 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000406 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000407 return;
408 }
409
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000410 if (pointer && !threadSafetyCheckIsPointer(S, D, Attr))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000411 return;
412
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000413 SmallVector<Expr*, 1> Args;
414 // check that all arguments are lockable objects
415 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
416 unsigned Size = Args.size();
417 if (Size != 1)
418 return;
419 Expr *Arg = Args[0];
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000420
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000421 if (pointer)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000422 D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000423 S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000424 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000425 D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000426}
427
428
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000429static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr,
430 bool scoped = false) {
431 assert(!Attr.isInvalid());
432
433 if (!checkAttributeNumArgs(S, Attr, 0))
434 return;
435
Caitlin Sadowski1748b122011-09-16 00:35:54 +0000436 // FIXME: Lockable structs for C code.
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000437 if (!isa<CXXRecordDecl>(D)) {
438 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
439 << Attr.getName() << ExpectedClass;
440 return;
441 }
442
443 if (scoped)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000444 D->addAttr(::new (S.Context) ScopedLockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000445 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000446 D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context));
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000447}
448
449static void handleNoThreadSafetyAttr(Sema &S, Decl *D,
450 const AttributeList &Attr) {
451 assert(!Attr.isInvalid());
452
453 if (!checkAttributeNumArgs(S, Attr, 0))
454 return;
455
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000456 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000457 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
458 << Attr.getName() << ExpectedFunctionOrMethod;
459 return;
460 }
461
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000462 D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(),
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +0000463 S.Context));
464}
465
Kostya Serebryany71efba02012-01-24 19:25:38 +0000466static void handleNoAddressSafetyAttr(Sema &S, Decl *D,
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000467 const AttributeList &Attr) {
Kostya Serebryany71efba02012-01-24 19:25:38 +0000468 assert(!Attr.isInvalid());
469
470 if (!checkAttributeNumArgs(S, Attr, 0))
471 return;
472
473 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
474 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
475 << Attr.getName() << ExpectedFunctionOrMethod;
476 return;
477 }
478
479 D->addAttr(::new (S.Context) NoAddressSafetyAnalysisAttr(Attr.getRange(),
480 S.Context));
481}
482
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000483static void handleAcquireOrderAttr(Sema &S, Decl *D, const AttributeList &Attr,
484 bool before) {
485 assert(!Attr.isInvalid());
486
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000487 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000488 return;
489
490 // D must be either a member field or global (potentially shared) variable.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000491 ValueDecl *VD = dyn_cast<ValueDecl>(D);
492 if (!VD || !mayBeSharedVariable(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000493 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000494 << Attr.getName() << ExpectedFieldOrGlobalVar;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000495 return;
496 }
497
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000498 // Check that this attribute only applies to lockable types.
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000499 QualType QT = VD->getType();
500 if (!QT->isDependentType()) {
501 const RecordType *RT = getRecordType(QT);
502 if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) {
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000503 S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable)
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000504 << Attr.getName();
505 return;
506 }
507 }
508
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000509 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000510 // Check that all arguments are lockable objects.
511 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000512 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000513 if (Size == 0)
514 return;
515 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000516
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000517 if (before)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000518 D->addAttr(::new (S.Context) AcquiredBeforeAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000519 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000520 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000521 D->addAttr(::new (S.Context) AcquiredAfterAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000522 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000523}
524
525static void handleLockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000526 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000527 assert(!Attr.isInvalid());
528
529 // zero or more arguments ok
530
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000531 // check that the attribute is applied to a function
532 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000533 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
534 << Attr.getName() << ExpectedFunctionOrMethod;
535 return;
536 }
537
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000538 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000539 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000540 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000541 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000542 Expr **StartArg = Size == 0 ? 0 : &Args[0];
543
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000544 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000545 D->addAttr(::new (S.Context) ExclusiveLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000546 S.Context, StartArg,
547 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000548 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000549 D->addAttr(::new (S.Context) SharedLockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000550 S.Context, StartArg,
551 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000552}
553
554static void handleTrylockFunAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000555 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000556 assert(!Attr.isInvalid());
557
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000558 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000559 return;
560
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000561 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000562 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
563 << Attr.getName() << ExpectedFunctionOrMethod;
564 return;
565 }
566
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000567 if (!isIntOrBool(Attr.getArg(0))) {
568 S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool)
569 << Attr.getName();
570 return;
571 }
572
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000573 SmallVector<Expr*, 2> Args;
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000574 // check that all arguments are lockable objects
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000575 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000576 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000577 Expr **StartArg = Size == 0 ? 0 : &Args[0];
578
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000579 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000580 D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000581 S.Context,
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000582 Attr.getArg(0),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000583 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000584 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000585 D->addAttr(::new (S.Context) SharedTrylockFunctionAttr(Attr.getRange(),
Caitlin Sadowski69f5d142011-09-15 17:50:19 +0000586 S.Context,
587 Attr.getArg(0),
588 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000589}
590
591static void handleLocksRequiredAttr(Sema &S, Decl *D, const AttributeList &Attr,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000592 bool exclusive = false) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000593 assert(!Attr.isInvalid());
594
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000595 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000596 return;
597
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000598 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000599 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
600 << Attr.getName() << ExpectedFunctionOrMethod;
601 return;
602 }
603
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000604 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000605 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000606 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000607 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000608 if (Size == 0)
609 return;
610 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000611
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000612 if (exclusive)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000613 D->addAttr(::new (S.Context) ExclusiveLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000614 S.Context, StartArg,
615 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000616 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000617 D->addAttr(::new (S.Context) SharedLocksRequiredAttr(Attr.getRange(),
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000618 S.Context, StartArg,
619 Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000620}
621
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000622static void handleUnlockFunAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000623 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000624 assert(!Attr.isInvalid());
625
626 // zero or more arguments ok
627
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000628 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000629 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
630 << Attr.getName() << ExpectedFunctionOrMethod;
631 return;
632 }
633
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000634 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000635 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000636 checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000637 unsigned Size = Args.size();
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000638 Expr **StartArg = Size == 0 ? 0 : &Args[0];
639
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000640 D->addAttr(::new (S.Context) UnlockFunctionAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000641 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000642}
643
644static void handleLockReturnedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000645 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000646 assert(!Attr.isInvalid());
647
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000648 if (!checkAttributeNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000649 return;
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000650 Expr *Arg = Attr.getArg(0);
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000651
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000652 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000653 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
654 << Attr.getName() << ExpectedFunctionOrMethod;
655 return;
656 }
657
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000658 if (Arg->isTypeDependent())
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000659 return;
660
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000661 // check that the argument is lockable object
DeLesley Hutchins83cad452012-04-06 20:02:30 +0000662 checkForLockableRecord(S, D, Attr, Arg->getType());
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000663
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000664 D->addAttr(::new (S.Context) LockReturnedAttr(Attr.getRange(), S.Context, Arg));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000665}
666
667static void handleLocksExcludedAttr(Sema &S, Decl *D,
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000668 const AttributeList &Attr) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000669 assert(!Attr.isInvalid());
670
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000671 if (!checkAttributeAtLeastNumArgs(S, Attr, 1))
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000672 return;
673
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000674 if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) {
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000675 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
676 << Attr.getName() << ExpectedFunctionOrMethod;
677 return;
678 }
679
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000680 // check that all arguments are lockable objects
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000681 SmallVector<Expr*, 1> Args;
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000682 checkAttrArgsAreLockableObjs(S, D, Attr, Args);
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000683 unsigned Size = Args.size();
DeLesley Hutchinsae519c42012-04-19 16:10:44 +0000684 if (Size == 0)
685 return;
686 Expr **StartArg = &Args[0];
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000687
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000688 D->addAttr(::new (S.Context) LocksExcludedAttr(Attr.getRange(), S.Context,
Caitlin Sadowski3ac1fbc2011-08-23 18:46:34 +0000689 StartArg, Size));
Caitlin Sadowskidb33e142011-07-28 20:12:35 +0000690}
691
692
Chandler Carruth1b03c872011-07-02 00:01:44 +0000693static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
694 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000695 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner545dd342008-06-28 23:36:30 +0000696 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000697 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000698 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000699 }
Mike Stumpbf916502009-07-24 19:02:52 +0000700
Chris Lattner6b6b5372008-06-26 18:38:35 +0000701 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000702
703 Expr *sizeExpr;
704
705 // Special case where the argument is a template id.
706 if (Attr.getParameterName()) {
John McCallf7a1a742009-11-24 19:00:30 +0000707 CXXScopeSpec SS;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000708 SourceLocation TemplateKWLoc;
John McCallf7a1a742009-11-24 19:00:30 +0000709 UnqualifiedId id;
710 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor4ac01402011-06-15 16:02:29 +0000711
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000712 ExprResult Size = S.ActOnIdExpression(scope, SS, TemplateKWLoc, id,
713 false, false);
Douglas Gregor4ac01402011-06-15 16:02:29 +0000714 if (Size.isInvalid())
715 return;
716
717 sizeExpr = Size.get();
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000718 } else {
719 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000720 if (!checkAttributeNumArgs(S, Attr, 1))
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000721 return;
Chandler Carruth1731e202011-07-11 23:30:35 +0000722
Peter Collingbourne7a730022010-11-23 20:45:58 +0000723 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000724 }
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000725
726 // Instantiate/Install the vector type, and let Sema build the type for us.
727 // This will run the reguired checks.
John McCall9ae2f072010-08-23 23:25:46 +0000728 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000729 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000730 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000731 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000732
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000733 // Remember this typedef decl, we will need it later for diagnostics.
734 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000735 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000736}
737
Chandler Carruth1b03c872011-07-02 00:01:44 +0000738static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000739 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000740 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +0000741 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000742
Chandler Carruth87c44602011-07-01 23:49:12 +0000743 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000744 TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chandler Carruth87c44602011-07-01 23:49:12 +0000745 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000746 // If the alignment is less than or equal to 8 bits, the packed attribute
747 // has no effect.
748 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000749 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000750 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000751 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000752 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000753 FD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000754 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000755 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000756}
757
Chandler Carruth1b03c872011-07-02 00:01:44 +0000758static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000759 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000760 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getRange(), S.Context));
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000761 else
762 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
763}
764
Chandler Carruth1b03c872011-07-02 00:01:44 +0000765static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000766 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000767 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek96329d42008-07-15 22:26:48 +0000768 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000769
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000770 // The IBAction attributes only apply to instance methods.
Chandler Carruth87c44602011-07-01 23:49:12 +0000771 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000772 if (MD->isInstanceMethod()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000773 D->addAttr(::new (S.Context) IBActionAttr(Attr.getRange(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000774 return;
775 }
776
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000777 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000778}
779
Ted Kremenek2f041d02011-09-29 07:02:25 +0000780static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) {
781 // The IBOutlet/IBOutletCollection attributes only apply to instance
782 // variables or properties of Objective-C classes. The outlet must also
783 // have an object reference type.
784 if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) {
785 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
Ted Kremenek0bfaf062011-11-01 18:08:35 +0000786 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000787 << Attr.getName() << VD->getType() << 0;
788 return false;
789 }
790 }
791 else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) {
792 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000793 S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type)
Ted Kremenek2f041d02011-09-29 07:02:25 +0000794 << Attr.getName() << PD->getType() << 1;
795 return false;
796 }
797 }
798 else {
799 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
800 return false;
801 }
Douglas Gregorf6b8b582012-03-14 16:55:17 +0000802
Ted Kremenek2f041d02011-09-29 07:02:25 +0000803 return true;
804}
805
Chandler Carruth1b03c872011-07-02 00:01:44 +0000806static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000807 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +0000808 if (!checkAttributeNumArgs(S, Attr, 0))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000809 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000810
811 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000812 return;
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000813
Ted Kremenek2f041d02011-09-29 07:02:25 +0000814 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getRange(), S.Context));
Ted Kremenek96329d42008-07-15 22:26:48 +0000815}
816
Chandler Carruth1b03c872011-07-02 00:01:44 +0000817static void handleIBOutletCollection(Sema &S, Decl *D,
818 const AttributeList &Attr) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000819
820 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000821 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000822 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
823 return;
824 }
825
Ted Kremenek2f041d02011-09-29 07:02:25 +0000826 if (!checkIBOutletCommon(S, D, Attr))
Ted Kremenek857e9182010-05-19 17:38:06 +0000827 return;
Ted Kremenek2f041d02011-09-29 07:02:25 +0000828
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000829 IdentifierInfo *II = Attr.getParameterName();
830 if (!II)
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000831 II = &S.Context.Idents.get("NSObject");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000832
John McCallb3d87482010-08-24 05:47:05 +0000833 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruth87c44602011-07-01 23:49:12 +0000834 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000835 if (!TypeRep) {
836 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
837 return;
838 }
John McCallb3d87482010-08-24 05:47:05 +0000839 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000840 // Diagnose use of non-object type in iboutletcollection attribute.
841 // FIXME. Gnu attribute extension ignores use of builtin types in
842 // attributes. So, __attribute__((iboutletcollection(char))) will be
843 // treated as __attribute__((iboutletcollection())).
Fariborz Jahanianf4072ae2011-10-18 19:54:31 +0000844 if (!QT->isObjCIdType() && !QT->isObjCObjectType()) {
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000845 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
846 return;
847 }
Argyrios Kyrtzidisf1e7af32011-09-13 18:41:59 +0000848 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getRange(),S.Context,
849 QT, Attr.getParameterLoc()));
Ted Kremenek857e9182010-05-19 17:38:06 +0000850}
851
Chandler Carruthd309c812011-07-01 23:49:16 +0000852static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000853 if (const RecordType *UT = T->getAsUnionType())
854 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
855 RecordDecl *UD = UT->getDecl();
856 for (RecordDecl::field_iterator it = UD->field_begin(),
857 itend = UD->field_end(); it != itend; ++it) {
858 QualType QT = it->getType();
859 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
860 T = QT;
861 return;
862 }
863 }
864 }
865}
866
Chandler Carruth1b03c872011-07-02 00:01:44 +0000867static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +0000868 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
869 // ignore it as well
Chandler Carruth87c44602011-07-01 23:49:12 +0000870 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000871 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000872 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000873 return;
874 }
Mike Stumpbf916502009-07-24 19:02:52 +0000875
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000876 // In C++ the implicit 'this' function parameter also counts, and they are
877 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +0000878 bool HasImplicitThisParam = isInstanceMethod(D);
879 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000880
881 // The nonnull attribute only applies to pointers.
Chris Lattner5f9e2722011-07-23 10:55:15 +0000882 SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000883
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000884 for (AttributeList::arg_iterator I=Attr.arg_begin(),
885 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000886
887
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000888 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000889 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000890 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000891 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
892 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000893 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
894 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000895 return;
896 }
Mike Stumpbf916502009-07-24 19:02:52 +0000897
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000898 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000899
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000900 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000901 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000902 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000903 return;
904 }
Mike Stumpbf916502009-07-24 19:02:52 +0000905
Ted Kremenek465172f2008-07-21 22:09:15 +0000906 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000907 if (HasImplicitThisParam) {
908 if (x == 0) {
909 S.Diag(Attr.getLoc(),
910 diag::err_attribute_invalid_implicit_this_argument)
911 << "nonnull" << Ex->getSourceRange();
912 return;
913 }
914 --x;
915 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000916
917 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +0000918 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000919 possibleTransparentUnionPointerType(T);
Fariborz Jahanian68fe96a2011-06-27 21:12:03 +0000920
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000921 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000922 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000923 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000924 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000925 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000926 }
Mike Stumpbf916502009-07-24 19:02:52 +0000927
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000928 NonNullArgs.push_back(x);
929 }
Mike Stumpbf916502009-07-24 19:02:52 +0000930
931 // If no arguments were specified to __attribute__((nonnull)) then all pointer
932 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000933 if (NonNullArgs.empty()) {
Chandler Carruth87c44602011-07-01 23:49:12 +0000934 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
935 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruthd309c812011-07-01 23:49:16 +0000936 possibleTransparentUnionPointerType(T);
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000937 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000938 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000939 }
Mike Stumpbf916502009-07-24 19:02:52 +0000940
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000941 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000942 if (NonNullArgs.empty()) {
943 // Warn the trivial case only if attribute is not coming from a
944 // macro instantiation.
945 if (Attr.getLoc().isFileID())
946 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000947 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000948 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000949 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000950
951 unsigned* start = &NonNullArgs[0];
952 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000953 llvm::array_pod_sort(start, start + size);
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +0000954 D->addAttr(::new (S.Context) NonNullAttr(Attr.getRange(), S.Context, start,
Sean Huntcf807c42010-08-18 23:23:40 +0000955 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000956}
957
Chandler Carruth1b03c872011-07-02 00:01:44 +0000958static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000959 // This attribute must be applied to a function declaration.
960 // The first argument to the attribute must be a string,
961 // the name of the resource, for example "malloc".
962 // The following arguments must be argument indexes, the arguments must be
963 // of integer type for Returns, otherwise of pointer type.
964 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000965 // after being held. free() should be __attribute((ownership_takes)), whereas
966 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000967
968 if (!AL.getParameterName()) {
969 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
970 << AL.getName()->getName() << 1;
971 return;
972 }
973 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000974 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000975 switch (AL.getKind()) {
976 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000977 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000978 if (AL.getNumArgs() < 1) {
979 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
980 return;
981 }
Jordy Rose2a479922010-08-12 08:54:03 +0000982 break;
983 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000984 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000985 if (AL.getNumArgs() < 1) {
986 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
987 return;
988 }
Jordy Rose2a479922010-08-12 08:54:03 +0000989 break;
990 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000991 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000992 if (AL.getNumArgs() > 1) {
993 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
994 << AL.getNumArgs() + 1;
995 return;
996 }
Jordy Rose2a479922010-08-12 08:54:03 +0000997 break;
998 default:
999 // This should never happen given how we are called.
1000 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001001 }
1002
Chandler Carruth87c44602011-07-01 23:49:12 +00001003 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall883cc2c2011-03-02 12:29:23 +00001004 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
1005 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001006 return;
1007 }
1008
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001009 // In C++ the implicit 'this' function parameter also counts, and they are
1010 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00001011 bool HasImplicitThisParam = isInstanceMethod(D);
1012 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001013
Chris Lattner5f9e2722011-07-23 10:55:15 +00001014 StringRef Module = AL.getParameterName()->getName();
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001015
1016 // Normalize the argument, __foo__ becomes foo.
1017 if (Module.startswith("__") && Module.endswith("__"))
1018 Module = Module.substr(2, Module.size() - 4);
1019
Chris Lattner5f9e2722011-07-23 10:55:15 +00001020 SmallVector<unsigned, 10> OwnershipArgs;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001021
Jordy Rose2a479922010-08-12 08:54:03 +00001022 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
1023 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001024
Peter Collingbourne7a730022010-11-23 20:45:58 +00001025 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001026 llvm::APSInt ArgNum(32);
1027 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1028 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1029 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
1030 << AL.getName()->getName() << IdxExpr->getSourceRange();
1031 continue;
1032 }
1033
1034 unsigned x = (unsigned) ArgNum.getZExtValue();
1035
1036 if (x > NumArgs || x < 1) {
1037 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
1038 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
1039 continue;
1040 }
1041 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001042 if (HasImplicitThisParam) {
1043 if (x == 0) {
1044 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1045 << "ownership" << IdxExpr->getSourceRange();
1046 return;
1047 }
1048 --x;
1049 }
1050
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001051 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +00001052 case OwnershipAttr::Takes:
1053 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001054 // Is the function argument a pointer type?
Chandler Carruth87c44602011-07-01 23:49:12 +00001055 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001056 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
1057 // FIXME: Should also highlight argument in decl.
1058 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +00001059 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001060 << "pointer"
1061 << IdxExpr->getSourceRange();
1062 continue;
1063 }
1064 break;
1065 }
Sean Huntcf807c42010-08-18 23:23:40 +00001066 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001067 if (AL.getNumArgs() > 1) {
1068 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +00001069 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001070 llvm::APSInt ArgNum(32);
1071 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
1072 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
1073 S.Diag(AL.getLoc(), diag::err_ownership_type)
1074 << "ownership_returns" << "integer"
1075 << IdxExpr->getSourceRange();
1076 return;
1077 }
1078 }
1079 break;
1080 }
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001081 } // switch
1082
1083 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +00001084 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00001085 i = D->specific_attr_begin<OwnershipAttr>(),
1086 e = D->specific_attr_end<OwnershipAttr>();
Sean Huntcf807c42010-08-18 23:23:40 +00001087 i != e; ++i) {
1088 if ((*i)->getOwnKind() != K) {
1089 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
1090 I!=E; ++I) {
1091 if (x == *I) {
1092 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
1093 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001094 }
1095 }
1096 }
1097 }
1098 OwnershipArgs.push_back(x);
1099 }
1100
1101 unsigned* start = OwnershipArgs.data();
1102 unsigned size = OwnershipArgs.size();
1103 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +00001104
1105 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
1106 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
1107 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001108 }
Sean Huntcf807c42010-08-18 23:23:40 +00001109
Chandler Carruth87c44602011-07-01 23:49:12 +00001110 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Sean Huntcf807c42010-08-18 23:23:40 +00001111 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +00001112}
1113
John McCall332bb2a2011-02-08 22:35:49 +00001114/// Whether this declaration has internal linkage for the purposes of
1115/// things that want to complain about things not have internal linkage.
1116static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
1117 switch (D->getLinkage()) {
1118 case NoLinkage:
1119 case InternalLinkage:
1120 return true;
1121
1122 // Template instantiations that go from external to unique-external
1123 // shouldn't get diagnosed.
1124 case UniqueExternalLinkage:
1125 return true;
1126
1127 case ExternalLinkage:
1128 return false;
1129 }
1130 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001131}
1132
Chandler Carruth1b03c872011-07-02 00:01:44 +00001133static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001134 // Check the attribute arguments.
1135 if (Attr.getNumArgs() > 1) {
1136 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1137 return;
1138 }
1139
Chandler Carruth87c44602011-07-01 23:49:12 +00001140 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall332bb2a2011-02-08 22:35:49 +00001141 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001142 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +00001143 return;
1144 }
1145
Chandler Carruth87c44602011-07-01 23:49:12 +00001146 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00001147
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001148 // gcc rejects
1149 // class c {
1150 // static int a __attribute__((weakref ("v2")));
1151 // static int b() __attribute__((weakref ("f3")));
1152 // };
1153 // and ignores the attributes of
1154 // void f(void) {
1155 // static int a __attribute__((weakref ("v2")));
1156 // }
1157 // we reject them
Chandler Carruth87c44602011-07-01 23:49:12 +00001158 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001159 if (!Ctx->isFileContext()) {
1160 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +00001161 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +00001162 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001163 }
1164
1165 // The GCC manual says
1166 //
1167 // At present, a declaration to which `weakref' is attached can only
1168 // be `static'.
1169 //
1170 // It also says
1171 //
1172 // Without a TARGET,
1173 // given as an argument to `weakref' or to `alias', `weakref' is
1174 // equivalent to `weak'.
1175 //
1176 // gcc 4.4.1 will accept
1177 // int a7 __attribute__((weakref));
1178 // as
1179 // int a7 __attribute__((weak));
1180 // This looks like a bug in gcc. We reject that for now. We should revisit
1181 // it if this behaviour is actually used.
1182
John McCall332bb2a2011-02-08 22:35:49 +00001183 if (!hasEffectivelyInternalLinkage(nd)) {
1184 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001185 return;
1186 }
1187
1188 // GCC rejects
1189 // static ((alias ("y"), weakref)).
1190 // Should we? How to check that weakref is before or after alias?
1191
1192 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001193 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001194 Arg = Arg->IgnoreParenCasts();
1195 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
1196
Douglas Gregor5cee1192011-07-27 05:40:30 +00001197 if (!Str || !Str->isAscii()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001198 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
1199 << "weakref" << 1;
1200 return;
1201 }
1202 // GCC will accept anything as the argument of weakref. Should we
1203 // check for an existing decl?
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001204 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001205 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001206 }
1207
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001208 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getRange(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001209}
1210
Chandler Carruth1b03c872011-07-02 00:01:44 +00001211static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001212 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001213 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001214 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001215 return;
1216 }
Mike Stumpbf916502009-07-24 19:02:52 +00001217
Peter Collingbourne7a730022010-11-23 20:45:58 +00001218 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001219 Arg = Arg->IgnoreParenCasts();
1220 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001221
Douglas Gregor5cee1192011-07-27 05:40:30 +00001222 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001223 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001224 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001225 return;
1226 }
Mike Stumpbf916502009-07-24 19:02:52 +00001227
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00001228 if (S.Context.getTargetInfo().getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +00001229 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
1230 return;
1231 }
1232
Chris Lattner6b6b5372008-06-26 18:38:35 +00001233 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +00001234
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001235 D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001236 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001237}
1238
Chandler Carruth1b03c872011-07-02 00:01:44 +00001239static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001240 // Check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001241 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001242 return;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001243
Chandler Carruth87c44602011-07-01 23:49:12 +00001244 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001245 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001246 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001247 return;
1248 }
1249
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001250 D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context));
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001251}
1252
Chandler Carruth1b03c872011-07-02 00:01:44 +00001253static void handleAlwaysInlineAttr(Sema &S, Decl *D,
1254 const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001255 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001256 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001257 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1258 return;
1259 }
1260
Chandler Carruth87c44602011-07-01 23:49:12 +00001261 if (!isa<FunctionDecl>(D)) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001262 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001263 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001264 return;
1265 }
Mike Stumpbf916502009-07-24 19:02:52 +00001266
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001267 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getRange(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001268}
1269
Chandler Carruth1b03c872011-07-02 00:01:44 +00001270static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +00001271 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001272 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +00001273 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1274 return;
1275 }
Mike Stump1eb44332009-09-09 15:08:12 +00001276
Chandler Carruth87c44602011-07-01 23:49:12 +00001277 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001278 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001279 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001280 D->addAttr(::new (S.Context) MallocAttr(Attr.getRange(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001281 return;
1282 }
Ryan Flynn76168e22009-08-09 20:07:29 +00001283 }
1284
Ted Kremenek2cff7d12009-08-15 00:51:46 +00001285 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +00001286}
1287
Chandler Carruth1b03c872011-07-02 00:01:44 +00001288static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohman34c26302010-11-17 00:03:07 +00001289 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001290 if (!checkAttributeNumArgs(S, Attr, 0))
Dan Gohman34c26302010-11-17 00:03:07 +00001291 return;
Dan Gohman34c26302010-11-17 00:03:07 +00001292
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001293 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getRange(), S.Context));
Dan Gohman34c26302010-11-17 00:03:07 +00001294}
1295
Chandler Carruth1b03c872011-07-02 00:01:44 +00001296static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001297 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001298 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001299 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001300 else
1301 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001302 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001303}
1304
Chandler Carruth1b03c872011-07-02 00:01:44 +00001305static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth56aeb402011-07-11 23:33:05 +00001306 assert(!Attr.isInvalid());
Chandler Carruth87c44602011-07-01 23:49:12 +00001307 if (isa<VarDecl>(D))
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001308 D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context));
Eric Christopher722109c2010-12-03 06:58:14 +00001309 else
1310 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001311 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +00001312}
1313
Chandler Carruth1b03c872011-07-02 00:01:44 +00001314static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001315 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00001316
1317 if (S.CheckNoReturnAttr(attr)) return;
1318
Chandler Carruth87c44602011-07-01 23:49:12 +00001319 if (!isa<ObjCMethodDecl>(D)) {
John McCall711c52b2011-01-05 12:14:39 +00001320 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001321 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00001322 return;
1323 }
1324
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001325 D->addAttr(::new (S.Context) NoReturnAttr(attr.getRange(), S.Context));
John McCall711c52b2011-01-05 12:14:39 +00001326}
1327
1328bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +00001329 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +00001330 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1331 attr.setInvalid();
1332 return true;
1333 }
1334
1335 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +00001336}
1337
Chandler Carruth1b03c872011-07-02 00:01:44 +00001338static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
1339 const AttributeList &Attr) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001340
1341 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
1342 // because 'analyzer_noreturn' does not impact the type.
1343
Chandler Carruth1731e202011-07-11 23:30:35 +00001344 if(!checkAttributeNumArgs(S, Attr, 0))
1345 return;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001346
Chandler Carruth87c44602011-07-01 23:49:12 +00001347 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
1348 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001349 if (VD == 0 || (!VD->getType()->isBlockPointerType()
1350 && !VD->getType()->isFunctionPointerType())) {
1351 S.Diag(Attr.getLoc(),
1352 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1353 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001354 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +00001355 return;
1356 }
1357 }
1358
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001359 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001360}
1361
John Thompson35cc9622010-08-09 21:53:52 +00001362// PS3 PPU-specific.
Chandler Carruth1b03c872011-07-02 00:01:44 +00001363static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompson35cc9622010-08-09 21:53:52 +00001364/*
1365 Returning a Vector Class in Registers
1366
Eric Christopherf48f3672010-12-01 22:13:54 +00001367 According to the PPU ABI specifications, a class with a single member of
1368 vector type is returned in memory when used as the return value of a function.
1369 This results in inefficient code when implementing vector classes. To return
1370 the value in a single vector register, add the vecreturn attribute to the
1371 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +00001372
1373 Example:
1374
1375 struct Vector
1376 {
1377 __vector float xyzw;
1378 } __attribute__((vecreturn));
1379
1380 Vector Add(Vector lhs, Vector rhs)
1381 {
1382 Vector result;
1383 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
1384 return result; // This will be returned in a register
1385 }
1386*/
Chandler Carruth87c44602011-07-01 23:49:12 +00001387 if (!isa<RecordDecl>(D)) {
John Thompson35cc9622010-08-09 21:53:52 +00001388 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001389 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +00001390 return;
1391 }
1392
Chandler Carruth87c44602011-07-01 23:49:12 +00001393 if (D->getAttr<VecReturnAttr>()) {
John Thompson35cc9622010-08-09 21:53:52 +00001394 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
1395 return;
1396 }
1397
Chandler Carruth87c44602011-07-01 23:49:12 +00001398 RecordDecl *record = cast<RecordDecl>(D);
John Thompson01add592010-09-18 01:12:07 +00001399 int count = 0;
1400
1401 if (!isa<CXXRecordDecl>(record)) {
1402 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1403 return;
1404 }
1405
1406 if (!cast<CXXRecordDecl>(record)->isPOD()) {
1407 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
1408 return;
1409 }
1410
Eric Christopherf48f3672010-12-01 22:13:54 +00001411 for (RecordDecl::field_iterator iter = record->field_begin();
1412 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +00001413 if ((count == 1) || !iter->getType()->isVectorType()) {
1414 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
1415 return;
1416 }
1417 count++;
1418 }
1419
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001420 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getRange(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +00001421}
1422
Chandler Carruth1b03c872011-07-02 00:01:44 +00001423static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001424 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Sean Huntbbd37c62009-11-21 08:43:09 +00001425 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001426 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +00001427 return;
1428 }
1429 // FIXME: Actually store the attribute on the declaration
1430}
1431
Chandler Carruth1b03c872011-07-02 00:01:44 +00001432static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek73798892008-07-25 04:39:19 +00001433 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001434 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001435 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +00001436 return;
1437 }
Mike Stumpbf916502009-07-24 19:02:52 +00001438
Chandler Carruth87c44602011-07-01 23:49:12 +00001439 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
1440 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001441 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001442 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +00001443 return;
1444 }
Mike Stumpbf916502009-07-24 19:02:52 +00001445
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001446 D->addAttr(::new (S.Context) UnusedAttr(Attr.getRange(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +00001447}
1448
Rafael Espindolaf87cced2011-10-03 14:59:42 +00001449static void handleReturnsTwiceAttr(Sema &S, Decl *D,
1450 const AttributeList &Attr) {
1451 // check the attribute arguments.
1452 if (Attr.hasParameterOrArguments()) {
1453 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1454 return;
1455 }
1456
1457 if (!isa<FunctionDecl>(D)) {
1458 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1459 << Attr.getName() << ExpectedFunction;
1460 return;
1461 }
1462
1463 D->addAttr(::new (S.Context) ReturnsTwiceAttr(Attr.getRange(), S.Context));
1464}
1465
Chandler Carruth1b03c872011-07-02 00:01:44 +00001466static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001467 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001468 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001469 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1470 return;
1471 }
Mike Stumpbf916502009-07-24 19:02:52 +00001472
Chandler Carruth87c44602011-07-01 23:49:12 +00001473 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +00001474 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001475 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
1476 return;
1477 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001478 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001479 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001480 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001481 return;
1482 }
Mike Stumpbf916502009-07-24 19:02:52 +00001483
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001484 D->addAttr(::new (S.Context) UsedAttr(Attr.getRange(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001485}
1486
Chandler Carruth1b03c872011-07-02 00:01:44 +00001487static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001488 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001489 if (Attr.getNumArgs() > 1) {
1490 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001491 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001492 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001493
1494 int priority = 65535; // FIXME: Do not hardcode such constants.
1495 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001496 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001497 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001498 if (E->isTypeDependent() || E->isValueDependent() ||
1499 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001500 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001501 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001502 return;
1503 }
1504 priority = Idx.getZExtValue();
1505 }
Mike Stumpbf916502009-07-24 19:02:52 +00001506
Chandler Carruth87c44602011-07-01 23:49:12 +00001507 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001508 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001509 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001510 return;
1511 }
1512
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001513 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001514 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001515}
1516
Chandler Carruth1b03c872011-07-02 00:01:44 +00001517static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001518 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001519 if (Attr.getNumArgs() > 1) {
1520 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001521 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001522 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001523
1524 int priority = 65535; // FIXME: Do not hardcode such constants.
1525 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001526 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001527 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001528 if (E->isTypeDependent() || E->isValueDependent() ||
1529 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001530 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001531 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001532 return;
1533 }
1534 priority = Idx.getZExtValue();
1535 }
Mike Stumpbf916502009-07-24 19:02:52 +00001536
Chandler Carruth87c44602011-07-01 23:49:12 +00001537 if (!isa<FunctionDecl>(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001538 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001539 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001540 return;
1541 }
1542
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001543 D->addAttr(::new (S.Context) DestructorAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00001544 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001545}
1546
Chandler Carruth1b03c872011-07-02 00:01:44 +00001547static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001548 unsigned NumArgs = Attr.getNumArgs();
1549 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001550 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001551 return;
1552 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001553
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001554 // Handle the case where deprecated attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001555 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001556 if (NumArgs == 1) {
1557 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001558 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001559 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1560 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001561 return;
1562 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001563 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001564 }
Mike Stumpbf916502009-07-24 19:02:52 +00001565
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001566 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001567}
1568
Chandler Carruth1b03c872011-07-02 00:01:44 +00001569static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001570 unsigned NumArgs = Attr.getNumArgs();
1571 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001572 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001573 return;
1574 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001575
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001576 // Handle the case where unavailable attribute has a text message.
Chris Lattner5f9e2722011-07-23 10:55:15 +00001577 StringRef Str;
Chris Lattner951bbb22011-02-24 05:42:24 +00001578 if (NumArgs == 1) {
1579 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001580 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001581 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001582 diag::err_attribute_not_string) << "unavailable";
1583 return;
1584 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001585 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001586 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001587 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getRange(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001588}
1589
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001590static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1591 const AttributeList &Attr) {
1592 unsigned NumArgs = Attr.getNumArgs();
1593 if (NumArgs > 0) {
1594 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1595 return;
1596 }
1597
1598 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001599 Attr.getRange(), S.Context));
Fariborz Jahanian742352a2011-07-06 19:24:05 +00001600}
1601
Patrick Beardb2f68202012-04-06 18:12:22 +00001602static void handleObjCRootClassAttr(Sema &S, Decl *D,
1603 const AttributeList &Attr) {
1604 if (!isa<ObjCInterfaceDecl>(D)) {
1605 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1606 return;
1607 }
1608
1609 unsigned NumArgs = Attr.getNumArgs();
1610 if (NumArgs > 0) {
1611 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1612 return;
1613 }
1614
1615 D->addAttr(::new (S.Context) ObjCRootClassAttr(Attr.getRange(), S.Context));
1616}
1617
Ted Kremenek71207fc2012-01-05 22:47:47 +00001618static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D,
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001619 const AttributeList &Attr) {
Fariborz Jahanian341b8be2012-01-03 22:52:32 +00001620 if (!isa<ObjCInterfaceDecl>(D)) {
1621 S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis);
1622 return;
1623 }
1624
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001625 unsigned NumArgs = Attr.getNumArgs();
1626 if (NumArgs > 0) {
1627 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1628 return;
1629 }
1630
Ted Kremenek71207fc2012-01-05 22:47:47 +00001631 D->addAttr(::new (S.Context) ObjCRequiresPropertyDefsAttr(
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00001632 Attr.getRange(), S.Context));
1633}
1634
Chandler Carruth1b03c872011-07-02 00:01:44 +00001635static void handleAvailabilityAttr(Sema &S, Decl *D,
1636 const AttributeList &Attr) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001637 IdentifierInfo *Platform = Attr.getParameterName();
1638 SourceLocation PlatformLoc = Attr.getParameterLoc();
1639
Chris Lattner5f9e2722011-07-23 10:55:15 +00001640 StringRef PlatformName
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001641 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1642 if (PlatformName.empty()) {
1643 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1644 << Platform;
1645
1646 PlatformName = Platform->getName();
1647 }
1648
1649 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1650 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1651 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001652 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001653
Douglas Gregorc90df6a2011-08-10 16:09:55 +00001654 // Ensure that Introduced <= Deprecated <= Obsoleted (although not all
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001655 // of these steps are needed).
1656 if (Introduced.isValid() && Deprecated.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001657 !(Introduced.Version <= Deprecated.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001658 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1659 << 1 << PlatformName << Deprecated.Version.getAsString()
1660 << 0 << Introduced.Version.getAsString();
1661 return;
1662 }
1663
1664 if (Introduced.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001665 !(Introduced.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001666 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1667 << 2 << PlatformName << Obsoleted.Version.getAsString()
1668 << 0 << Introduced.Version.getAsString();
1669 return;
1670 }
1671
1672 if (Deprecated.isValid() && Obsoleted.isValid() &&
Douglas Gregor3b6b7ac2011-08-10 15:31:35 +00001673 !(Deprecated.Version <= Obsoleted.Version)) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001674 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1675 << 2 << PlatformName << Obsoleted.Version.getAsString()
1676 << 1 << Deprecated.Version.getAsString();
1677 return;
1678 }
1679
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001680 StringRef Str;
1681 const StringLiteral *SE =
1682 dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr());
1683 if (SE)
1684 Str = SE->getString();
1685
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001686 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getRange(), S.Context,
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001687 Platform,
1688 Introduced.Version,
1689 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001690 Obsoleted.Version,
Fariborz Jahanian006e42f2011-12-10 00:28:41 +00001691 IsUnavailable,
1692 Str));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001693}
1694
Chandler Carruth1b03c872011-07-02 00:01:44 +00001695static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001696 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001697 if(!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00001698 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001699
Peter Collingbourne7a730022010-11-23 20:45:58 +00001700 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001701 Arg = Arg->IgnoreParenCasts();
1702 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001703
Douglas Gregor5cee1192011-07-27 05:40:30 +00001704 if (!Str || !Str->isAscii()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001705 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001706 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001707 return;
1708 }
Mike Stumpbf916502009-07-24 19:02:52 +00001709
Chris Lattner5f9e2722011-07-23 10:55:15 +00001710 StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001711 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001712
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001713 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001714 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001715 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001716 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001717 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001718 type = VisibilityAttr::Hidden; // FIXME
John McCall41887602012-01-29 01:20:30 +00001719 else if (TypeStr == "protected") {
1720 // Complain about attempts to use protected visibility on targets
1721 // (like Darwin) that don't support it.
1722 if (!S.Context.getTargetInfo().hasProtectedVisibility()) {
1723 S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility);
1724 type = VisibilityAttr::Default;
1725 } else {
1726 type = VisibilityAttr::Protected;
1727 }
1728 } else {
Chris Lattner08631c52008-11-23 21:45:46 +00001729 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001730 return;
1731 }
Mike Stumpbf916502009-07-24 19:02:52 +00001732
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001733 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getRange(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001734}
1735
Chandler Carruth1b03c872011-07-02 00:01:44 +00001736static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1737 const AttributeList &Attr) {
John McCalld5313b02011-03-02 11:33:24 +00001738 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1739 if (!method) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001740 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001741 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001742 return;
1743 }
1744
Chandler Carruth87c44602011-07-01 23:49:12 +00001745 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1746 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1747 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCalld5313b02011-03-02 11:33:24 +00001748 << "objc_method_family" << 1;
1749 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00001750 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCalld5313b02011-03-02 11:33:24 +00001751 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001752 Attr.setInvalid();
John McCalld5313b02011-03-02 11:33:24 +00001753 return;
1754 }
1755
Chris Lattner5f9e2722011-07-23 10:55:15 +00001756 StringRef param = Attr.getParameterName()->getName();
John McCalld5313b02011-03-02 11:33:24 +00001757 ObjCMethodFamilyAttr::FamilyKind family;
1758 if (param == "none")
1759 family = ObjCMethodFamilyAttr::OMF_None;
1760 else if (param == "alloc")
1761 family = ObjCMethodFamilyAttr::OMF_alloc;
1762 else if (param == "copy")
1763 family = ObjCMethodFamilyAttr::OMF_copy;
1764 else if (param == "init")
1765 family = ObjCMethodFamilyAttr::OMF_init;
1766 else if (param == "mutableCopy")
1767 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1768 else if (param == "new")
1769 family = ObjCMethodFamilyAttr::OMF_new;
1770 else {
1771 // Just warn and ignore it. This is future-proof against new
1772 // families being used in system headers.
Chandler Carruth87c44602011-07-01 23:49:12 +00001773 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCalld5313b02011-03-02 11:33:24 +00001774 return;
1775 }
1776
John McCallf85e1932011-06-15 23:02:42 +00001777 if (family == ObjCMethodFamilyAttr::OMF_init &&
1778 !method->getResultType()->isObjCObjectPointerType()) {
1779 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1780 << method->getResultType();
1781 // Ignore the attribute.
1782 return;
1783 }
1784
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001785 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(),
John McCallf85e1932011-06-15 23:02:42 +00001786 S.Context, family));
John McCalld5313b02011-03-02 11:33:24 +00001787}
1788
Chandler Carruth1b03c872011-07-02 00:01:44 +00001789static void handleObjCExceptionAttr(Sema &S, Decl *D,
1790 const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00001791 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner0db29ec2009-02-14 08:09:34 +00001792 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001793
Chris Lattner0db29ec2009-02-14 08:09:34 +00001794 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1795 if (OCI == 0) {
1796 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1797 return;
1798 }
Mike Stumpbf916502009-07-24 19:02:52 +00001799
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001800 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getRange(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001801}
1802
Chandler Carruth1b03c872011-07-02 00:01:44 +00001803static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001804 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001805 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001806 return;
1807 }
Richard Smith162e1c12011-04-15 14:24:37 +00001808 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001809 QualType T = TD->getUnderlyingType();
1810 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001811 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001812 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1813 return;
1814 }
1815 }
Ted Kremenekf6e88d72012-03-01 01:40:32 +00001816 else if (!isa<ObjCPropertyDecl>(D)) {
1817 // It is okay to include this attribute on properties, e.g.:
1818 //
1819 // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject));
1820 //
1821 // In this case it follows tradition and suppresses an error in the above
1822 // case.
Fariborz Jahanian9b2eb7b2011-11-29 01:48:40 +00001823 S.Diag(D->getLocation(), diag::warn_nsobject_attribute);
Ted Kremenekf6e88d72012-03-01 01:40:32 +00001824 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001825 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getRange(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001826}
1827
Mike Stumpbf916502009-07-24 19:02:52 +00001828static void
Chandler Carruth1b03c872011-07-02 00:01:44 +00001829handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregorf9201e02009-02-11 23:02:49 +00001830 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001831 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001832 return;
1833 }
1834
1835 if (!isa<FunctionDecl>(D)) {
1836 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1837 return;
1838 }
1839
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001840 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getRange(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001841}
1842
Chandler Carruth1b03c872011-07-02 00:01:44 +00001843static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00001844 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001845 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001846 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001847 return;
1848 }
Mike Stumpbf916502009-07-24 19:02:52 +00001849
Steve Naroff9eae5762008-09-18 16:44:58 +00001850 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001851 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001852 return;
1853 }
Mike Stumpbf916502009-07-24 19:02:52 +00001854
Sean Huntcf807c42010-08-18 23:23:40 +00001855 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001856 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001857 type = BlocksAttr::ByRef;
1858 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001859 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001860 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001861 return;
1862 }
Mike Stumpbf916502009-07-24 19:02:52 +00001863
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001864 D->addAttr(::new (S.Context) BlocksAttr(Attr.getRange(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001865}
1866
Chandler Carruth1b03c872011-07-02 00:01:44 +00001867static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson77091822008-10-05 18:05:59 +00001868 // check the attribute arguments.
1869 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001870 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001871 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001872 }
1873
John McCall3323fad2011-09-09 07:56:05 +00001874 unsigned sentinel = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001875 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001876 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001877 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001878 if (E->isTypeDependent() || E->isValueDependent() ||
1879 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001880 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001881 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001882 return;
1883 }
Mike Stumpbf916502009-07-24 19:02:52 +00001884
John McCall3323fad2011-09-09 07:56:05 +00001885 if (Idx.isSigned() && Idx.isNegative()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001886 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1887 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001888 return;
1889 }
John McCall3323fad2011-09-09 07:56:05 +00001890
1891 sentinel = Idx.getZExtValue();
Anders Carlsson77091822008-10-05 18:05:59 +00001892 }
1893
John McCall3323fad2011-09-09 07:56:05 +00001894 unsigned nullPos = 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001895 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001896 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001897 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001898 if (E->isTypeDependent() || E->isValueDependent() ||
1899 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001900 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001901 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001902 return;
1903 }
1904 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001905
John McCall3323fad2011-09-09 07:56:05 +00001906 if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) {
Anders Carlsson77091822008-10-05 18:05:59 +00001907 // FIXME: This error message could be improved, it would be nice
1908 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001909 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1910 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001911 return;
1912 }
1913 }
1914
Chandler Carruth87c44602011-07-01 23:49:12 +00001915 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall3323fad2011-09-09 07:56:05 +00001916 const FunctionType *FT = FD->getType()->castAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001917 if (isa<FunctionNoProtoType>(FT)) {
1918 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1919 return;
1920 }
Mike Stumpbf916502009-07-24 19:02:52 +00001921
Chris Lattner897cd902009-03-17 23:03:47 +00001922 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001923 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001924 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001925 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001926 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlsson77091822008-10-05 18:05:59 +00001927 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001928 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001929 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001930 }
Eli Friedmana0b2ba12012-01-06 01:23:10 +00001931 } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
1932 if (!BD->isVariadic()) {
1933 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1;
1934 return;
1935 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001936 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001937 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001938 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00001939 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherf48f3672010-12-01 22:13:54 +00001940 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001941 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001942 int m = Ty->isFunctionPointerType() ? 0 : 1;
1943 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001944 return;
1945 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001946 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001947 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001948 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001949 return;
1950 }
Anders Carlsson77091822008-10-05 18:05:59 +00001951 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001953 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001954 return;
1955 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001956 D->addAttr(::new (S.Context) SentinelAttr(Attr.getRange(), S.Context, sentinel,
Eric Christopherf48f3672010-12-01 22:13:54 +00001957 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001958}
1959
Chandler Carruth1b03c872011-07-02 00:01:44 +00001960static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner026dc962009-02-14 07:37:35 +00001961 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00001962 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner026dc962009-02-14 07:37:35 +00001963 return;
Chris Lattner026dc962009-02-14 07:37:35 +00001964
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001965 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001966 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001967 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001968 return;
1969 }
Mike Stumpbf916502009-07-24 19:02:52 +00001970
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001971 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1972 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1973 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001974 return;
1975 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001976 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1977 if (MD->getResultType()->isVoidType()) {
1978 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1979 << Attr.getName() << 1;
1980 return;
1981 }
1982
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00001983 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getRange(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001984}
1985
Chandler Carruth1b03c872011-07-02 00:01:44 +00001986static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001987 // check the attribute arguments.
Chandler Carruth87c44602011-07-01 23:49:12 +00001988 if (Attr.hasParameterOrArguments()) {
1989 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001990 return;
1991 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001992
Chandler Carruth87c44602011-07-01 23:49:12 +00001993 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
Fariborz Jahanian13c7fcc2011-10-21 22:27:12 +00001994 if (isa<CXXRecordDecl>(D)) {
1995 D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
1996 return;
1997 }
Chandler Carruth87c44602011-07-01 23:49:12 +00001998 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1999 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00002000 return;
2001 }
2002
Chandler Carruth87c44602011-07-01 23:49:12 +00002003 NamedDecl *nd = cast<NamedDecl>(D);
John McCall332bb2a2011-02-08 22:35:49 +00002004
2005 // 'weak' only applies to declarations with external linkage.
2006 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002007 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002008 return;
2009 }
Mike Stumpbf916502009-07-24 19:02:52 +00002010
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002011 nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002012}
2013
Chandler Carruth1b03c872011-07-02 00:01:44 +00002014static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002015 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002016 if (!checkAttributeNumArgs(S, Attr, 0))
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002017 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002018
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002019
2020 // weak_import only applies to variable & function declarations.
2021 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002022 if (!D->canBeWeakImported(isDef)) {
2023 if (isDef)
2024 S.Diag(Attr.getLoc(),
2025 diag::warn_attribute_weak_import_invalid_on_definition)
2026 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00002027 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002028 (S.Context.getTargetInfo().getTriple().isOSDarwin() &&
Fariborz Jahanian90eed212011-10-26 23:59:12 +00002029 (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) {
Douglas Gregordef86312011-03-23 13:27:51 +00002030 // Nothing to warn about here.
2031 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00002032 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002033 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002034
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002035 return;
2036 }
2037
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002038 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getRange(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00002039}
2040
Chandler Carruth1b03c872011-07-02 00:01:44 +00002041static void handleReqdWorkGroupSize(Sema &S, Decl *D,
2042 const AttributeList &Attr) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002043 // Attribute has 3 arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002044 if (!checkAttributeNumArgs(S, Attr, 3))
Nate Begeman6f3d8382009-06-26 06:32:41 +00002045 return;
Nate Begeman6f3d8382009-06-26 06:32:41 +00002046
2047 unsigned WGSize[3];
2048 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00002049 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00002050 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002051 if (E->isTypeDependent() || E->isValueDependent() ||
2052 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00002053 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2054 << "reqd_work_group_size" << E->getSourceRange();
2055 return;
2056 }
2057 WGSize[i] = (unsigned) ArgNum.getZExtValue();
2058 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002059 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context,
Sean Huntcf807c42010-08-18 23:23:40 +00002060 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00002061 WGSize[2]));
2062}
2063
Chandler Carruth1b03c872011-07-02 00:01:44 +00002064static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002065 // Attribute has no arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002066 if (!checkAttributeNumArgs(S, Attr, 1))
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002067 return;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002068
2069 // Make sure that there is a string literal as the sections's single
2070 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00002071 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002072 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002073 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002074 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002075 return;
2076 }
Mike Stump1eb44332009-09-09 15:08:12 +00002077
Chris Lattner797c3c42009-08-10 19:03:04 +00002078 // If the target wants to validate the section specifier, make it happen.
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002079 std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002080 if (!Error.empty()) {
2081 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
2082 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00002083 return;
2084 }
Mike Stump1eb44332009-09-09 15:08:12 +00002085
Chris Lattnera1e1dc72010-01-12 20:58:53 +00002086 // This attribute cannot be applied to local variables.
2087 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
2088 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
2089 return;
2090 }
2091
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002092 D->addAttr(::new (S.Context) SectionAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002093 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00002094}
2095
Chris Lattner6b6b5372008-06-26 18:38:35 +00002096
Chandler Carruth1b03c872011-07-02 00:01:44 +00002097static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002098 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002099 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002100 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002101 return;
2102 }
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002103
Chandler Carruth87c44602011-07-01 23:49:12 +00002104 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002105 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002106 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002107 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002108 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002109 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002110}
2111
Chandler Carruth1b03c872011-07-02 00:01:44 +00002112static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002113 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002114 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002115 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002116 return;
2117 }
Mike Stumpbf916502009-07-24 19:02:52 +00002118
Chandler Carruth87c44602011-07-01 23:49:12 +00002119 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002120 if (Existing->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002121 Existing->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002122 } else {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002123 D->addAttr(::new (S.Context) ConstAttr(Attr.getRange(), S.Context));
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002124 }
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002125}
2126
Chandler Carruth1b03c872011-07-02 00:01:44 +00002127static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002128 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002129 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002130 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002131
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002132 D->addAttr(::new (S.Context) PureAttr(Attr.getRange(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00002133}
2134
Chandler Carruth1b03c872011-07-02 00:01:44 +00002135static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpbf916502009-07-24 19:02:52 +00002136 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002137 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2138 return;
2139 }
Mike Stumpbf916502009-07-24 19:02:52 +00002140
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002141 if (Attr.getNumArgs() != 0) {
2142 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2143 return;
2144 }
Mike Stumpbf916502009-07-24 19:02:52 +00002145
Chandler Carruth87c44602011-07-01 23:49:12 +00002146 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpbf916502009-07-24 19:02:52 +00002147
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002148 if (!VD || !VD->hasLocalStorage()) {
2149 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
2150 return;
2151 }
Mike Stumpbf916502009-07-24 19:02:52 +00002152
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002153 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00002154 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00002155 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002156 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
2157 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002158 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002159 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002160 Attr.getParameterName();
2161 return;
2162 }
Mike Stumpbf916502009-07-24 19:02:52 +00002163
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002164 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
2165 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002166 S.Diag(Attr.getParameterLoc(),
2167 diag::err_attribute_cleanup_arg_not_function)
2168 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002169 return;
2170 }
2171
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002172 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002173 S.Diag(Attr.getParameterLoc(),
2174 diag::err_attribute_cleanup_func_must_take_one_arg)
2175 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002176 return;
2177 }
Mike Stumpbf916502009-07-24 19:02:52 +00002178
Anders Carlsson89941c12009-02-07 23:16:50 +00002179 // We're currently more strict than GCC about what function types we accept.
2180 // If this ever proves to be a problem it should be easy to fix.
2181 QualType Ty = S.Context.getPointerType(VD->getType());
2182 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00002183 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
2184 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00002185 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00002186 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
2187 Attr.getParameterName() << ParamTy << Ty;
2188 return;
2189 }
Mike Stumpbf916502009-07-24 19:02:52 +00002190
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002191 D->addAttr(::new (S.Context) CleanupAttr(Attr.getRange(), S.Context, FD));
Eli Friedman5f2987c2012-02-02 03:46:19 +00002192 S.MarkFunctionReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00002193}
2194
Mike Stumpbf916502009-07-24 19:02:52 +00002195/// Handle __attribute__((format_arg((idx)))) attribute based on
2196/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002197static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth1731e202011-07-11 23:30:35 +00002198 if (!checkAttributeNumArgs(S, Attr, 1))
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002199 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002200
Chandler Carruth87c44602011-07-01 23:49:12 +00002201 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002202 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002203 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002204 return;
2205 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002206
2207 // In C++ the implicit 'this' function parameter also counts, and they are
2208 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002209 bool HasImplicitThisParam = isInstanceMethod(D);
2210 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002211 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002212
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002213 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002214 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002215 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002216 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2217 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002218 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2219 << "format" << 2 << IdxExpr->getSourceRange();
2220 return;
2221 }
Mike Stumpbf916502009-07-24 19:02:52 +00002222
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002223 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
2224 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
2225 << "format" << 2 << IdxExpr->getSourceRange();
2226 return;
2227 }
Mike Stumpbf916502009-07-24 19:02:52 +00002228
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002229 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002230
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002231 if (HasImplicitThisParam) {
2232 if (ArgIdx == 0) {
2233 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
2234 << "format_arg" << IdxExpr->getSourceRange();
2235 return;
2236 }
2237 ArgIdx--;
2238 }
2239
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002240 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002241 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00002242
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002243 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
2244 if (not_nsstring_type &&
2245 !isCFStringType(Ty, S.Context) &&
2246 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002247 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002248 // FIXME: Should highlight the actual expression that has the wrong type.
2249 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002250 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002251 << IdxExpr->getSourceRange();
2252 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002253 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002254 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002255 if (!isNSStringType(Ty, S.Context) &&
2256 !isCFStringType(Ty, S.Context) &&
2257 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002258 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002259 // FIXME: Should highlight the actual expression that has the wrong type.
2260 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00002261 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002262 << IdxExpr->getSourceRange();
2263 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002264 }
2265
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002266 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getRange(), S.Context,
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002267 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00002268}
2269
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002270enum FormatAttrKind {
2271 CFStringFormat,
2272 NSStringFormat,
2273 StrftimeFormat,
2274 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00002275 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002276 InvalidFormat
2277};
2278
2279/// getFormatAttrKind - Map from format attribute names to supported format
2280/// types.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002281static FormatAttrKind getFormatAttrKind(StringRef Format) {
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002282 // Check for formats that get handled specially.
2283 if (Format == "NSString")
2284 return NSStringFormat;
2285 if (Format == "CFString")
2286 return CFStringFormat;
2287 if (Format == "strftime")
2288 return StrftimeFormat;
2289
2290 // Otherwise, check for supported formats.
2291 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
Jean-Daniel Dupas69d53842012-01-27 09:14:17 +00002292 Format == "strfmon" || Format == "cmn_err" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00002293 Format == "zcmn_err" ||
2294 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002295 return SupportedFormat;
2296
Duncan Sandsbc525952010-03-23 14:44:19 +00002297 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
2298 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00002299 return IgnoredFormat;
2300
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002301 return InvalidFormat;
2302}
2303
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002304/// Handle __attribute__((init_priority(priority))) attributes based on
2305/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002306static void handleInitPriorityAttr(Sema &S, Decl *D,
2307 const AttributeList &Attr) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002308 if (!S.getLangOpts().CPlusPlus) {
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002309 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
2310 return;
2311 }
2312
Chandler Carruth87c44602011-07-01 23:49:12 +00002313 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002314 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2315 Attr.setInvalid();
2316 return;
2317 }
Chandler Carruth87c44602011-07-01 23:49:12 +00002318 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002319 if (S.Context.getAsArrayType(T))
2320 T = S.Context.getBaseElementType(T);
2321 if (!T->getAs<RecordType>()) {
2322 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
2323 Attr.setInvalid();
2324 return;
2325 }
2326
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002327 if (Attr.getNumArgs() != 1) {
2328 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2329 Attr.setInvalid();
2330 return;
2331 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00002332 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00002333
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002334 llvm::APSInt priority(32);
2335 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
2336 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
2337 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
2338 << "init_priority" << priorityExpr->getSourceRange();
2339 Attr.setInvalid();
2340 return;
2341 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00002342 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002343 if (prioritynum < 101 || prioritynum > 65535) {
2344 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
2345 << priorityExpr->getSourceRange();
2346 Attr.setInvalid();
2347 return;
2348 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002349 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002350 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002351}
2352
Mike Stumpbf916502009-07-24 19:02:52 +00002353/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
2354/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruth1b03c872011-07-02 00:01:44 +00002355static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002356
Chris Lattner545dd342008-06-28 23:36:30 +00002357 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002358 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00002359 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002360 return;
2361 }
2362
Chris Lattner545dd342008-06-28 23:36:30 +00002363 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002364 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002365 return;
2366 }
2367
Chandler Carruth87c44602011-07-01 23:49:12 +00002368 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002369 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002370 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002371 return;
2372 }
2373
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002374 // In C++ the implicit 'this' function parameter also counts, and they are
2375 // counted from one.
Chandler Carruth87c44602011-07-01 23:49:12 +00002376 bool HasImplicitThisParam = isInstanceMethod(D);
2377 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002378 unsigned FirstIdx = 1;
2379
Chris Lattner5f9e2722011-07-23 10:55:15 +00002380 StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002381
2382 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002383 if (Format.startswith("__") && Format.endswith("__"))
2384 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002385
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002386 // Check for supported formats.
2387 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00002388
2389 if (Kind == IgnoredFormat)
2390 return;
2391
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002392 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002393 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002394 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002395 return;
2396 }
2397
2398 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002399 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00002400 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002401 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
2402 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002403 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002404 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002405 return;
2406 }
2407
2408 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002409 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002410 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002411 return;
2412 }
2413
2414 // FIXME: Do we need to bounds check?
2415 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00002416
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002417 if (HasImplicitThisParam) {
2418 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00002419 S.Diag(Attr.getLoc(),
2420 diag::err_format_attribute_implicit_this_format_string)
2421 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00002422 return;
2423 }
2424 ArgIdx--;
2425 }
Mike Stump1eb44332009-09-09 15:08:12 +00002426
Chris Lattner6b6b5372008-06-26 18:38:35 +00002427 // make sure the format string is really a string
Chandler Carruth87c44602011-07-01 23:49:12 +00002428 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002429
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002430 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002431 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002432 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2433 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00002434 return;
2435 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002436 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002437 // FIXME: do we need to check if the type is NSString*? What are the
2438 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00002439 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002440 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002441 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2442 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002443 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002444 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002445 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00002446 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00002447 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002448 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
2449 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002450 return;
2451 }
2452
2453 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00002454 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00002455 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002456 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
2457 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002458 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00002459 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002460 return;
2461 }
2462
2463 // check if the function is variadic if the 3rd argument non-zero
2464 if (FirstArg != 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00002465 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002466 ++NumArgs; // +1 for ...
2467 } else {
Chandler Carruth87c44602011-07-01 23:49:12 +00002468 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00002469 return;
2470 }
2471 }
2472
Chris Lattner3c73c412008-11-19 08:23:25 +00002473 // strftime requires FirstArg to be 0 because it doesn't read from any
2474 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002475 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002476 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002477 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
2478 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002479 return;
2480 }
2481 // if 0 it disables parameter checking (to use with e.g. va_list)
2482 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002483 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00002484 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00002485 return;
2486 }
2487
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002488 // Check whether we already have an equivalent format attribute.
2489 for (specific_attr_iterator<FormatAttr>
Chandler Carruth87c44602011-07-01 23:49:12 +00002490 i = D->specific_attr_begin<FormatAttr>(),
2491 e = D->specific_attr_end<FormatAttr>();
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002492 i != e ; ++i) {
2493 FormatAttr *f = *i;
2494 if (f->getType() == Format &&
2495 f->getFormatIdx() == (int)Idx.getZExtValue() &&
2496 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
2497 // If we don't have a valid location for this attribute, adopt the
2498 // location.
2499 if (f->getLocation().isInvalid())
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +00002500 f->setRange(Attr.getRange());
Douglas Gregorb30cd4a2011-06-15 05:45:11 +00002501 return;
2502 }
2503 }
2504
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002505 D->addAttr(::new (S.Context) FormatAttr(Attr.getRange(), S.Context, Format,
Sean Huntcf807c42010-08-18 23:23:40 +00002506 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00002507 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002508}
2509
Chandler Carruth1b03c872011-07-02 00:01:44 +00002510static void handleTransparentUnionAttr(Sema &S, Decl *D,
2511 const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002512 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002513 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002514 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002515
Chris Lattner6b6b5372008-06-26 18:38:35 +00002516
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002517 // Try to find the underlying union declaration.
2518 RecordDecl *RD = 0;
Chandler Carruth87c44602011-07-01 23:49:12 +00002519 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002520 if (TD && TD->getUnderlyingType()->isUnionType())
2521 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
2522 else
Chandler Carruth87c44602011-07-01 23:49:12 +00002523 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002524
2525 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002526 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002527 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002528 return;
2529 }
2530
John McCall5e1cdac2011-10-07 06:10:15 +00002531 if (!RD->isCompleteDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002532 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002533 diag::warn_transparent_union_attribute_not_definition);
2534 return;
2535 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00002536
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002537 RecordDecl::field_iterator Field = RD->field_begin(),
2538 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002539 if (Field == FieldEnd) {
2540 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2541 return;
2542 }
Eli Friedmanbc887452008-09-02 05:19:23 +00002543
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002544 FieldDecl *FirstField = *Field;
2545 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00002546 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00002547 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00002548 diag::warn_transparent_union_attribute_floating)
2549 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002550 return;
2551 }
2552
2553 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2554 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2555 for (; Field != FieldEnd; ++Field) {
2556 QualType FieldType = Field->getType();
2557 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2558 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2559 // Warn if we drop the attribute.
2560 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00002561 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002562 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00002563 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002564 diag::warn_transparent_union_attribute_field_size_align)
2565 << isSize << Field->getDeclName() << FieldBits;
2566 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00002567 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00002568 diag::note_transparent_union_first_field_size_align)
2569 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00002570 return;
2571 }
2572 }
2573
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002574 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getRange(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002575}
2576
Chandler Carruth1b03c872011-07-02 00:01:44 +00002577static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002578 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002579 if (!checkAttributeNumArgs(S, Attr, 1))
Chris Lattner6b6b5372008-06-26 18:38:35 +00002580 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002581
Peter Collingbourne7a730022010-11-23 20:45:58 +00002582 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00002583 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00002584
Chris Lattner6b6b5372008-06-26 18:38:35 +00002585 // Make sure that there is a string literal as the annotation's single
2586 // argument.
2587 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00002588 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00002589 return;
2590 }
Julien Lerouge77f68bb2011-09-09 22:41:49 +00002591
2592 // Don't duplicate annotations that are already set.
2593 for (specific_attr_iterator<AnnotateAttr>
2594 i = D->specific_attr_begin<AnnotateAttr>(),
2595 e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) {
2596 if ((*i)->getAnnotation() == SE->getString())
2597 return;
2598 }
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002599 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getRange(), S.Context,
Eric Christopherf48f3672010-12-01 22:13:54 +00002600 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002601}
2602
Chandler Carruth1b03c872011-07-02 00:01:44 +00002603static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002604 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002605 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002606 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002607 return;
2608 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002609
2610 //FIXME: The C++0x version of this attribute has more limited applicabilty
2611 // than GNU's, and should error out when it is used to specify a
2612 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002613
Chris Lattner545dd342008-06-28 23:36:30 +00002614 if (Attr.getNumArgs() == 0) {
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002615 D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002616 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002617 }
Mike Stumpbf916502009-07-24 19:02:52 +00002618
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002619 S.AddAlignedAttr(Attr.getRange(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002620}
2621
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002622void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E) {
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002623 // FIXME: Handle pack-expansions here.
2624 if (DiagnoseUnexpandedParameterPack(E))
2625 return;
2626
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002627 if (E->isTypeDependent() || E->isValueDependent()) {
2628 // Save dependent expressions in the AST to be instantiated.
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002629 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002630 return;
2631 }
2632
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002633 SourceLocation AttrLoc = AttrRange.getBegin();
Sean Huntcf807c42010-08-18 23:23:40 +00002634 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002635 llvm::APSInt Alignment(32);
Richard Smith282e7e62012-02-04 09:53:13 +00002636 ExprResult ICE =
2637 VerifyIntegerConstantExpression(E, &Alignment,
2638 PDiag(diag::err_attribute_argument_not_int) << "aligned",
2639 /*AllowFold*/ false);
2640 if (ICE.isInvalid())
Chris Lattner49e2d342008-06-28 23:50:44 +00002641 return;
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002642 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002643 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2644 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002645 return;
2646 }
2647
Richard Smith282e7e62012-02-04 09:53:13 +00002648 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, true, ICE.take()));
Sean Huntcf807c42010-08-18 23:23:40 +00002649}
2650
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002651void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS) {
Sean Huntcf807c42010-08-18 23:23:40 +00002652 // FIXME: Cache the number on the Attr object if non-dependent?
2653 // FIXME: Perform checking of type validity
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002654 D->addAttr(::new (Context) AlignedAttr(AttrRange, Context, false, TS));
Sean Huntcf807c42010-08-18 23:23:40 +00002655 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002656}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002657
Chandler Carruthd309c812011-07-01 23:49:16 +00002658/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpbf916502009-07-24 19:02:52 +00002659/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002660///
Mike Stumpbf916502009-07-24 19:02:52 +00002661/// Despite what would be logical, the mode attribute is a decl attribute, not a
2662/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2663/// HImode, not an intermediate pointer.
Chandler Carruth1b03c872011-07-02 00:01:44 +00002664static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002665 // This attribute isn't documented, but glibc uses it. It changes
2666 // the width of an int or unsigned int to the specified size.
2667
2668 // Check that there aren't any arguments
Chandler Carruth1731e202011-07-11 23:30:35 +00002669 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002670 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002671
Chris Lattnerfbf13472008-06-27 22:18:37 +00002672
2673 IdentifierInfo *Name = Attr.getParameterName();
2674 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002675 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002676 return;
2677 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002678
Chris Lattner5f9e2722011-07-23 10:55:15 +00002679 StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002680
2681 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002682 if (Str.startswith("__") && Str.endswith("__"))
2683 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002684
2685 unsigned DestWidth = 0;
2686 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002687 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002688 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002689 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002690 switch (Str[0]) {
2691 case 'Q': DestWidth = 8; break;
2692 case 'H': DestWidth = 16; break;
2693 case 'S': DestWidth = 32; break;
2694 case 'D': DestWidth = 64; break;
2695 case 'X': DestWidth = 96; break;
2696 case 'T': DestWidth = 128; break;
2697 }
2698 if (Str[1] == 'F') {
2699 IntegerMode = false;
2700 } else if (Str[1] == 'C') {
2701 IntegerMode = false;
2702 ComplexMode = true;
2703 } else if (Str[1] != 'I') {
2704 DestWidth = 0;
2705 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002706 break;
2707 case 4:
2708 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2709 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002710 if (Str == "word")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002711 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002712 else if (Str == "byte")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002713 DestWidth = S.Context.getTargetInfo().getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002714 break;
2715 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002716 if (Str == "pointer")
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002717 DestWidth = S.Context.getTargetInfo().getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002718 break;
2719 }
2720
2721 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002722 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002723 OldTy = TD->getUnderlyingType();
2724 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2725 OldTy = VD->getType();
2726 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002727 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002728 << "mode" << Attr.getRange();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002729 return;
2730 }
Eli Friedman73397492009-03-03 06:41:03 +00002731
John McCall183700f2009-09-21 23:43:11 +00002732 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002733 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2734 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002735 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002736 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2737 } else if (ComplexMode) {
2738 if (!OldTy->isComplexType())
2739 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2740 } else {
2741 if (!OldTy->isFloatingType())
2742 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2743 }
2744
Mike Stump390b4cc2009-05-16 07:39:55 +00002745 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2746 // and friends, at least with glibc.
2747 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2748 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002749 // FIXME: Make sure floating-point mappings are accurate
2750 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002751 QualType NewTy;
2752 switch (DestWidth) {
2753 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002754 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002755 return;
2756 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002757 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002758 return;
2759 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002760 if (!IntegerMode) {
2761 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2762 return;
2763 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002764 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002765 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002766 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002767 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002768 break;
2769 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002770 if (!IntegerMode) {
2771 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2772 return;
2773 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002774 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002775 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002776 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002777 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002778 break;
2779 case 32:
2780 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002781 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002782 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002783 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002784 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002785 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002786 break;
2787 case 64:
2788 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002789 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002790 else if (OldTy->isSignedIntegerType())
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002791 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002792 NewTy = S.Context.LongTy;
2793 else
2794 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002795 else
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00002796 if (S.Context.getTargetInfo().getLongWidth() == 64)
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002797 NewTy = S.Context.UnsignedLongTy;
2798 else
2799 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002800 break;
Eli Friedman73397492009-03-03 06:41:03 +00002801 case 96:
2802 NewTy = S.Context.LongDoubleTy;
2803 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002804 case 128:
2805 if (!IntegerMode) {
2806 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2807 return;
2808 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002809 if (OldTy->isSignedIntegerType())
2810 NewTy = S.Context.Int128Ty;
2811 else
2812 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002813 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002814 }
2815
Eli Friedman73397492009-03-03 06:41:03 +00002816 if (ComplexMode) {
2817 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002818 }
2819
2820 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002821 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002822 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002823 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002824 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002825 cast<ValueDecl>(D)->setType(NewTy);
2826}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002827
Chandler Carruth1b03c872011-07-02 00:01:44 +00002828static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssond87df372009-02-13 06:46:13 +00002829 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002830 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlssond87df372009-02-13 06:46:13 +00002831 return;
Anders Carlssone896d982009-02-13 08:11:52 +00002832
Chandler Carruth87c44602011-07-01 23:49:12 +00002833 if (!isFunctionOrMethod(D)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002834 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002835 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002836 return;
2837 }
Mike Stumpbf916502009-07-24 19:02:52 +00002838
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002839 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getRange(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002840}
2841
Chandler Carruth1b03c872011-07-02 00:01:44 +00002842static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002843 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002844 if (!checkAttributeNumArgs(S, Attr, 0))
Anders Carlsson5bab7882009-02-19 19:16:48 +00002845 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002846
Mike Stumpbf916502009-07-24 19:02:52 +00002847
Chandler Carruth87c44602011-07-01 23:49:12 +00002848 if (!isa<FunctionDecl>(D)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002849 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002850 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002851 return;
2852 }
Mike Stumpbf916502009-07-24 19:02:52 +00002853
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002854 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getRange(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002855}
2856
Chandler Carruth1b03c872011-07-02 00:01:44 +00002857static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2858 const AttributeList &Attr) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002859 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002860 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner7255a2d2010-06-22 00:03:40 +00002861 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002862
Chris Lattner7255a2d2010-06-22 00:03:40 +00002863
Chandler Carruth87c44602011-07-01 23:49:12 +00002864 if (!isa<FunctionDecl>(D)) {
Chris Lattner7255a2d2010-06-22 00:03:40 +00002865 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002866 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002867 return;
2868 }
2869
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002870 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002871 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002872}
2873
Chandler Carruth1b03c872011-07-02 00:01:44 +00002874static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002875 if (S.LangOpts.CUDA) {
2876 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002877 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002878 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2879 return;
2880 }
2881
Chandler Carruth87c44602011-07-01 23:49:12 +00002882 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002883 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002884 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002885 return;
2886 }
2887
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002888 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002889 } else {
2890 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2891 }
2892}
2893
Chandler Carruth1b03c872011-07-02 00:01:44 +00002894static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002895 if (S.LangOpts.CUDA) {
2896 // check the attribute arguments.
2897 if (Attr.getNumArgs() != 0) {
2898 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2899 return;
2900 }
2901
Chandler Carruth87c44602011-07-01 23:49:12 +00002902 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002903 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002904 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002905 return;
2906 }
2907
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002908 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002909 } else {
2910 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2911 }
2912}
2913
Chandler Carruth1b03c872011-07-02 00:01:44 +00002914static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002915 if (S.LangOpts.CUDA) {
2916 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002917 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002918 return;
Peter Collingbourneced76712010-12-01 03:15:31 +00002919
Chandler Carruth87c44602011-07-01 23:49:12 +00002920 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002921 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002922 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002923 return;
2924 }
2925
Chandler Carruth87c44602011-07-01 23:49:12 +00002926 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002927 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002928 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002929 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2930 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2931 << FD->getType()
2932 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2933 "void");
2934 } else {
2935 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2936 << FD->getType();
2937 }
2938 return;
2939 }
2940
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002941 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002942 } else {
2943 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2944 }
2945}
2946
Chandler Carruth1b03c872011-07-02 00:01:44 +00002947static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002948 if (S.LangOpts.CUDA) {
2949 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002950 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002951 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002952
Peter Collingbourneced76712010-12-01 03:15:31 +00002953
Chandler Carruth87c44602011-07-01 23:49:12 +00002954 if (!isa<FunctionDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002955 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002956 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002957 return;
2958 }
2959
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002960 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002961 } else {
2962 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2963 }
2964}
2965
Chandler Carruth1b03c872011-07-02 00:01:44 +00002966static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002967 if (S.LangOpts.CUDA) {
2968 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002969 if (!checkAttributeNumArgs(S, Attr, 0))
Peter Collingbourneced76712010-12-01 03:15:31 +00002970 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00002971
Peter Collingbourneced76712010-12-01 03:15:31 +00002972
Chandler Carruth87c44602011-07-01 23:49:12 +00002973 if (!isa<VarDecl>(D)) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002974 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002975 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002976 return;
2977 }
2978
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00002979 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getRange(), S.Context));
Peter Collingbourneced76712010-12-01 03:15:31 +00002980 } else {
2981 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2982 }
2983}
2984
Chandler Carruth1b03c872011-07-02 00:01:44 +00002985static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner26e25542009-04-14 16:30:50 +00002986 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00002987 if (!checkAttributeNumArgs(S, Attr, 0))
Chris Lattner26e25542009-04-14 16:30:50 +00002988 return;
Mike Stumpbf916502009-07-24 19:02:52 +00002989
Chandler Carruth87c44602011-07-01 23:49:12 +00002990 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattnerc5197432009-04-14 17:02:11 +00002991 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002992 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002993 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002994 return;
2995 }
Mike Stumpbf916502009-07-24 19:02:52 +00002996
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002997 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002998 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002999 return;
3000 }
Mike Stumpbf916502009-07-24 19:02:52 +00003001
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003002 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getRange(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00003003}
3004
Chandler Carruth1b03c872011-07-02 00:01:44 +00003005static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003006 if (hasDeclarator(D)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003007
Chandler Carruth87c44602011-07-01 23:49:12 +00003008 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall711c52b2011-01-05 12:14:39 +00003009 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
3010 CallingConv CC;
Chandler Carruth87c44602011-07-01 23:49:12 +00003011 if (S.CheckCallingConvAttr(Attr, CC))
John McCall711c52b2011-01-05 12:14:39 +00003012 return;
3013
Chandler Carruth87c44602011-07-01 23:49:12 +00003014 if (!isa<ObjCMethodDecl>(D)) {
3015 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3016 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00003017 return;
3018 }
3019
Chandler Carruth87c44602011-07-01 23:49:12 +00003020 switch (Attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00003021 case AttributeList::AT_fastcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003022 D->addAttr(::new (S.Context) FastCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003023 return;
3024 case AttributeList::AT_stdcall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003025 D->addAttr(::new (S.Context) StdCallAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003026 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003027 case AttributeList::AT_thiscall:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003028 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getRange(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00003029 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00003030 case AttributeList::AT_cdecl:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003031 D->addAttr(::new (S.Context) CDeclAttr(Attr.getRange(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00003032 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003033 case AttributeList::AT_pascal:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003034 D->addAttr(::new (S.Context) PascalAttr(Attr.getRange(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00003035 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003036 case AttributeList::AT_pcs: {
Chandler Carruth87c44602011-07-01 23:49:12 +00003037 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003038 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003039 if (!Str || !Str->isAscii()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003040 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003041 << "pcs" << 1;
Chandler Carruth87c44602011-07-01 23:49:12 +00003042 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003043 return;
3044 }
3045
Chris Lattner5f9e2722011-07-23 10:55:15 +00003046 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003047 PcsAttr::PCSType PCS;
3048 if (StrRef == "aapcs")
3049 PCS = PcsAttr::AAPCS;
3050 else if (StrRef == "aapcs-vfp")
3051 PCS = PcsAttr::AAPCS_VFP;
3052 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003053 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
3054 Attr.setInvalid();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003055 return;
3056 }
3057
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003058 D->addAttr(::new (S.Context) PcsAttr(Attr.getRange(), S.Context, PCS));
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003059 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00003060 default:
3061 llvm_unreachable("unexpected attribute kind");
Abramo Bagnarae215f722010-04-30 13:10:51 +00003062 }
3063}
3064
Chandler Carruth1b03c872011-07-02 00:01:44 +00003065static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Chandler Carruth56aeb402011-07-11 23:33:05 +00003066 assert(!Attr.isInvalid());
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003067 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context));
Peter Collingbournef315fa82011-02-14 01:42:53 +00003068}
3069
John McCall711c52b2011-01-05 12:14:39 +00003070bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
3071 if (attr.isInvalid())
3072 return true;
3073
Ted Kremenek831efae2011-04-15 05:49:29 +00003074 if ((attr.getNumArgs() != 0 &&
3075 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
3076 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00003077 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
3078 attr.setInvalid();
3079 return true;
3080 }
3081
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003082 // TODO: diagnose uses of these conventions on the wrong target. Or, better
3083 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00003084 switch (attr.getKind()) {
3085 case AttributeList::AT_cdecl: CC = CC_C; break;
3086 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
3087 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
3088 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
3089 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003090 case AttributeList::AT_pcs: {
3091 Expr *Arg = attr.getArg(0);
3092 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003093 if (!Str || !Str->isAscii()) {
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003094 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
3095 << "pcs" << 1;
3096 attr.setInvalid();
3097 return true;
3098 }
3099
Chris Lattner5f9e2722011-07-23 10:55:15 +00003100 StringRef StrRef = Str->getString();
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003101 if (StrRef == "aapcs") {
3102 CC = CC_AAPCS;
3103 break;
3104 } else if (StrRef == "aapcs-vfp") {
3105 CC = CC_AAPCS_VFP;
3106 break;
3107 }
3108 // FALLS THROUGH
3109 }
David Blaikie7530c032012-01-17 06:56:22 +00003110 default: llvm_unreachable("unexpected attribute kind");
John McCall711c52b2011-01-05 12:14:39 +00003111 }
3112
3113 return false;
3114}
3115
Chandler Carruth1b03c872011-07-02 00:01:44 +00003116static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003117 if (hasDeclarator(D)) return;
John McCall711c52b2011-01-05 12:14:39 +00003118
3119 unsigned numParams;
Chandler Carruth87c44602011-07-01 23:49:12 +00003120 if (S.CheckRegparmAttr(Attr, numParams))
John McCall711c52b2011-01-05 12:14:39 +00003121 return;
3122
Chandler Carruth87c44602011-07-01 23:49:12 +00003123 if (!isa<ObjCMethodDecl>(D)) {
3124 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
3125 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003126 return;
3127 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003128
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003129 D->addAttr(::new (S.Context) RegparmAttr(Attr.getRange(), S.Context, numParams));
John McCall711c52b2011-01-05 12:14:39 +00003130}
3131
3132/// Checks a regparm attribute, returning true if it is ill-formed and
3133/// otherwise setting numParams to the appropriate value.
Chandler Carruth87c44602011-07-01 23:49:12 +00003134bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
3135 if (Attr.isInvalid())
John McCall711c52b2011-01-05 12:14:39 +00003136 return true;
3137
Chandler Carruth87c44602011-07-01 23:49:12 +00003138 if (Attr.getNumArgs() != 1) {
3139 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
3140 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003141 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003142 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003143
Chandler Carruth87c44602011-07-01 23:49:12 +00003144 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003145 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00003146 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00003147 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003148 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003149 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003150 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003151 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003152 }
3153
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003154 if (Context.getTargetInfo().getRegParmMax() == 0) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003155 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003156 << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003157 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003158 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003159 }
3160
John McCall711c52b2011-01-05 12:14:39 +00003161 numParams = NumParams.getZExtValue();
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003162 if (numParams > Context.getTargetInfo().getRegParmMax()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003163 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
Douglas Gregorbcfd1f52011-09-02 00:18:52 +00003164 << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruth87c44602011-07-01 23:49:12 +00003165 Attr.setInvalid();
John McCall711c52b2011-01-05 12:14:39 +00003166 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00003167 }
3168
John McCall711c52b2011-01-05 12:14:39 +00003169 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00003170}
3171
Chandler Carruth1b03c872011-07-02 00:01:44 +00003172static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7b381982010-12-12 23:03:07 +00003173 if (S.LangOpts.CUDA) {
3174 // check the attribute arguments.
3175 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00003176 // FIXME: 0 is not okay.
3177 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003178 return;
3179 }
3180
Chandler Carruth87c44602011-07-01 23:49:12 +00003181 if (!isFunctionOrMethod(D)) {
Peter Collingbourne7b381982010-12-12 23:03:07 +00003182 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00003183 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003184 return;
3185 }
3186
3187 Expr *MaxThreadsExpr = Attr.getArg(0);
3188 llvm::APSInt MaxThreads(32);
3189 if (MaxThreadsExpr->isTypeDependent() ||
3190 MaxThreadsExpr->isValueDependent() ||
3191 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
3192 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3193 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
3194 return;
3195 }
3196
3197 llvm::APSInt MinBlocks(32);
3198 if (Attr.getNumArgs() > 1) {
3199 Expr *MinBlocksExpr = Attr.getArg(1);
3200 if (MinBlocksExpr->isTypeDependent() ||
3201 MinBlocksExpr->isValueDependent() ||
3202 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
3203 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
3204 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
3205 return;
3206 }
3207 }
3208
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003209 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getRange(), S.Context,
Peter Collingbourne7b381982010-12-12 23:03:07 +00003210 MaxThreads.getZExtValue(),
3211 MinBlocks.getZExtValue()));
3212 } else {
3213 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
3214 }
3215}
3216
Chris Lattner0744e5f2008-06-29 00:23:49 +00003217//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00003218// Checker-specific attribute handlers.
3219//===----------------------------------------------------------------------===//
3220
John McCallc7ad3812011-01-25 03:31:58 +00003221static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003222 return type->isDependentType() ||
3223 type->isObjCObjectPointerType() ||
3224 S.Context.isObjCNSObjectType(type);
John McCallc7ad3812011-01-25 03:31:58 +00003225}
3226static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
Douglas Gregor6c73a292011-10-09 22:26:49 +00003227 return type->isDependentType() ||
3228 type->isPointerType() ||
3229 isValidSubjectOfNSAttribute(S, type);
John McCallc7ad3812011-01-25 03:31:58 +00003230}
3231
Chandler Carruth1b03c872011-07-02 00:01:44 +00003232static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003233 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCallc7ad3812011-01-25 03:31:58 +00003234 if (!param) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003235 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003236 << Attr.getRange() << Attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00003237 return;
3238 }
3239
3240 bool typeOK, cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003241 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCallc7ad3812011-01-25 03:31:58 +00003242 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
3243 cf = false;
3244 } else {
3245 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
3246 cf = true;
3247 }
3248
3249 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003250 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003251 << Attr.getRange() << Attr.getName() << cf;
John McCallc7ad3812011-01-25 03:31:58 +00003252 return;
3253 }
3254
3255 if (cf)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003256 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003257 else
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003258 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003259}
3260
Chandler Carruth1b03c872011-07-02 00:01:44 +00003261static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
3262 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003263 if (!isa<ObjCMethodDecl>(D)) {
3264 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003265 << Attr.getRange() << Attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00003266 return;
3267 }
3268
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003269 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getRange(), S.Context));
John McCallc7ad3812011-01-25 03:31:58 +00003270}
3271
Chandler Carruth1b03c872011-07-02 00:01:44 +00003272static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
3273 const AttributeList &Attr) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003274
John McCallc7ad3812011-01-25 03:31:58 +00003275 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00003276
Chandler Carruth87c44602011-07-01 23:49:12 +00003277 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003278 returnType = MD->getResultType();
Chandler Carruth87c44602011-07-01 23:49:12 +00003279 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian831fb962011-06-25 00:17:46 +00003280 returnType = PD->getType();
David Blaikie4e4d0842012-03-11 07:00:24 +00003281 else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) &&
Chandler Carruth87c44602011-07-01 23:49:12 +00003282 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCallf85e1932011-06-15 23:02:42 +00003283 return; // ignore: was handled as a type attribute
Chandler Carruth87c44602011-07-01 23:49:12 +00003284 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCallc7ad3812011-01-25 03:31:58 +00003285 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003286 else {
Chandler Carruth87c44602011-07-01 23:49:12 +00003287 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003288 << Attr.getRange() << Attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00003289 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003290 return;
3291 }
Mike Stumpbf916502009-07-24 19:02:52 +00003292
John McCallc7ad3812011-01-25 03:31:58 +00003293 bool typeOK;
3294 bool cf;
Chandler Carruth87c44602011-07-01 23:49:12 +00003295 switch (Attr.getKind()) {
David Blaikie7530c032012-01-17 06:56:22 +00003296 default: llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003297 case AttributeList::AT_ns_returns_autoreleased:
3298 case AttributeList::AT_ns_returns_retained:
3299 case AttributeList::AT_ns_returns_not_retained:
3300 typeOK = isValidSubjectOfNSAttribute(S, returnType);
3301 cf = false;
3302 break;
3303
3304 case AttributeList::AT_cf_returns_retained:
3305 case AttributeList::AT_cf_returns_not_retained:
3306 typeOK = isValidSubjectOfCFAttribute(S, returnType);
3307 cf = true;
3308 break;
3309 }
3310
3311 if (!typeOK) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003312 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003313 << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00003314 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00003315 }
Mike Stumpbf916502009-07-24 19:02:52 +00003316
Chandler Carruth87c44602011-07-01 23:49:12 +00003317 switch (Attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00003318 default:
David Blaikieb219cfc2011-09-23 05:06:16 +00003319 llvm_unreachable("invalid ownership attribute");
John McCallc7ad3812011-01-25 03:31:58 +00003320 case AttributeList::AT_ns_returns_autoreleased:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003321 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getRange(),
John McCallc7ad3812011-01-25 03:31:58 +00003322 S.Context));
3323 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00003324 case AttributeList::AT_cf_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003325 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003326 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003327 return;
3328 case AttributeList::AT_ns_returns_not_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003329 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003330 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00003331 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003332 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003333 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003334 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003335 return;
3336 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003337 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getRange(),
Eric Christopherf48f3672010-12-01 22:13:54 +00003338 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00003339 return;
3340 };
3341}
3342
John McCalldc7c5ad2011-07-22 08:53:00 +00003343static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D,
3344 const AttributeList &attr) {
3345 SourceLocation loc = attr.getLoc();
3346
3347 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D);
3348
Fariborz Jahanian94d55d72012-04-21 17:51:44 +00003349 if (!method) {
Fariborz Jahanian0e78afb2012-04-20 22:00:46 +00003350 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003351 << SourceRange(loc, loc) << attr.getName() << ExpectedMethod;
John McCalldc7c5ad2011-07-22 08:53:00 +00003352 return;
3353 }
3354
3355 // Check that the method returns a normal pointer.
3356 QualType resultType = method->getResultType();
Fariborz Jahanianf2e59452011-09-30 20:50:23 +00003357
3358 if (!resultType->isReferenceType() &&
3359 (!resultType->isPointerType() || resultType->isObjCRetainableType())) {
John McCalldc7c5ad2011-07-22 08:53:00 +00003360 S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
3361 << SourceRange(loc)
3362 << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2;
3363
3364 // Drop the attribute.
3365 return;
3366 }
3367
3368 method->addAttr(
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003369 ::new (S.Context) ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context));
John McCalldc7c5ad2011-07-22 08:53:00 +00003370}
3371
John McCall8dfac0b2011-09-30 05:12:12 +00003372/// Handle cf_audited_transfer and cf_unknown_transfer.
3373static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) {
3374 if (!isa<FunctionDecl>(D)) {
3375 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003376 << A.getRange() << A.getName() << ExpectedFunction;
John McCall8dfac0b2011-09-30 05:12:12 +00003377 return;
3378 }
3379
3380 bool IsAudited = (A.getKind() == AttributeList::AT_cf_audited_transfer);
3381
3382 // Check whether there's a conflicting attribute already present.
3383 Attr *Existing;
3384 if (IsAudited) {
3385 Existing = D->getAttr<CFUnknownTransferAttr>();
3386 } else {
3387 Existing = D->getAttr<CFAuditedTransferAttr>();
3388 }
3389 if (Existing) {
3390 S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible)
3391 << A.getName()
3392 << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer")
3393 << A.getRange() << Existing->getRange();
3394 return;
3395 }
3396
3397 // All clear; add the attribute.
3398 if (IsAudited) {
3399 D->addAttr(
3400 ::new (S.Context) CFAuditedTransferAttr(A.getRange(), S.Context));
3401 } else {
3402 D->addAttr(
3403 ::new (S.Context) CFUnknownTransferAttr(A.getRange(), S.Context));
3404 }
3405}
3406
John McCallfe98da02011-09-29 07:17:38 +00003407static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D,
3408 const AttributeList &Attr) {
3409 RecordDecl *RD = dyn_cast<RecordDecl>(D);
3410 if (!RD || RD->isUnion()) {
3411 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003412 << Attr.getRange() << Attr.getName() << ExpectedStruct;
John McCallfe98da02011-09-29 07:17:38 +00003413 }
3414
3415 IdentifierInfo *ParmName = Attr.getParameterName();
3416
3417 // In Objective-C, verify that the type names an Objective-C type.
3418 // We don't want to check this outside of ObjC because people sometimes
3419 // do crazy C declarations of Objective-C types.
David Blaikie4e4d0842012-03-11 07:00:24 +00003420 if (ParmName && S.getLangOpts().ObjC1) {
John McCallfe98da02011-09-29 07:17:38 +00003421 // Check for an existing type with this name.
3422 LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(),
3423 Sema::LookupOrdinaryName);
3424 if (S.LookupName(R, Sc)) {
3425 NamedDecl *Target = R.getFoundDecl();
3426 if (Target && !isa<ObjCInterfaceDecl>(Target)) {
3427 S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface);
3428 S.Diag(Target->getLocStart(), diag::note_declared_at);
3429 }
3430 }
3431 }
3432
3433 D->addAttr(::new (S.Context) NSBridgedAttr(Attr.getRange(), S.Context,
3434 ParmName));
3435}
3436
Chandler Carruth1b03c872011-07-02 00:01:44 +00003437static void handleObjCOwnershipAttr(Sema &S, Decl *D,
3438 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003439 if (hasDeclarator(D)) return;
John McCallf85e1932011-06-15 23:02:42 +00003440
Chandler Carruth87c44602011-07-01 23:49:12 +00003441 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003442 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003443}
3444
Chandler Carruth1b03c872011-07-02 00:01:44 +00003445static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
3446 const AttributeList &Attr) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003447 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003448 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003449 << Attr.getRange() << Attr.getName() << ExpectedVariable;
John McCallf85e1932011-06-15 23:02:42 +00003450 return;
3451 }
3452
Chandler Carruth87c44602011-07-01 23:49:12 +00003453 ValueDecl *vd = cast<ValueDecl>(D);
John McCallf85e1932011-06-15 23:02:42 +00003454 QualType type = vd->getType();
3455
3456 if (!type->isDependentType() &&
3457 !type->isObjCLifetimeType()) {
Chandler Carruth87c44602011-07-01 23:49:12 +00003458 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCallf85e1932011-06-15 23:02:42 +00003459 << type;
3460 return;
3461 }
3462
3463 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
3464
3465 // If we have no lifetime yet, check the lifetime we're presumably
3466 // going to infer.
3467 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
3468 lifetime = type->getObjCARCImplicitLifetime();
3469
3470 switch (lifetime) {
3471 case Qualifiers::OCL_None:
3472 assert(type->isDependentType() &&
3473 "didn't infer lifetime for non-dependent type?");
3474 break;
3475
3476 case Qualifiers::OCL_Weak: // meaningful
3477 case Qualifiers::OCL_Strong: // meaningful
3478 break;
3479
3480 case Qualifiers::OCL_ExplicitNone:
3481 case Qualifiers::OCL_Autoreleasing:
Chandler Carruth87c44602011-07-01 23:49:12 +00003482 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCallf85e1932011-06-15 23:02:42 +00003483 << (lifetime == Qualifiers::OCL_Autoreleasing);
3484 break;
3485 }
3486
Chandler Carruth87c44602011-07-01 23:49:12 +00003487 D->addAttr(::new (S.Context)
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003488 ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context));
John McCallf85e1932011-06-15 23:02:42 +00003489}
3490
Charles Davisf0122fe2010-02-16 18:27:26 +00003491static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
Aaron Ballman94287722012-02-23 22:46:33 +00003492 switch (Attr.getKind()) {
3493 default:
3494 return false;
3495 case AttributeList::AT_dllimport:
3496 case AttributeList::AT_dllexport:
3497 case AttributeList::AT_uuid:
3498 case AttributeList::AT_deprecated:
3499 case AttributeList::AT_noreturn:
3500 case AttributeList::AT_nothrow:
3501 case AttributeList::AT_naked:
3502 case AttributeList::AT_noinline:
3503 return true;
3504 }
Francois Pichet11542142010-12-19 06:50:37 +00003505}
3506
3507//===----------------------------------------------------------------------===//
3508// Microsoft specific attribute handlers.
3509//===----------------------------------------------------------------------===//
3510
Chandler Carruth1b03c872011-07-02 00:01:44 +00003511static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Pichet62ec1f22011-09-17 17:15:52 +00003512 if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) {
Francois Pichet11542142010-12-19 06:50:37 +00003513 // check the attribute arguments.
Chandler Carruth1731e202011-07-11 23:30:35 +00003514 if (!checkAttributeNumArgs(S, Attr, 1))
Francois Pichet11542142010-12-19 06:50:37 +00003515 return;
Chandler Carruth1731e202011-07-11 23:30:35 +00003516
Francois Pichet11542142010-12-19 06:50:37 +00003517 Expr *Arg = Attr.getArg(0);
3518 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Douglas Gregor5cee1192011-07-27 05:40:30 +00003519 if (!Str || !Str->isAscii()) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003520 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
3521 << "uuid" << 1;
3522 return;
3523 }
3524
Chris Lattner5f9e2722011-07-23 10:55:15 +00003525 StringRef StrRef = Str->getString();
Francois Pichetd3d3be92010-12-20 01:41:49 +00003526
3527 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
3528 StrRef.back() == '}';
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003529
Francois Pichetd3d3be92010-12-20 01:41:49 +00003530 // Validate GUID length.
3531 if (IsCurly && StrRef.size() != 38) {
3532 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3533 return;
3534 }
3535 if (!IsCurly && StrRef.size() != 36) {
3536 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3537 return;
3538 }
3539
Douglas Gregorf6b8b582012-03-14 16:55:17 +00003540 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
Francois Pichetd3d3be92010-12-20 01:41:49 +00003541 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Chris Lattner5f9e2722011-07-23 10:55:15 +00003542 StringRef::iterator I = StrRef.begin();
Anders Carlssonf89e0422011-01-23 21:07:30 +00003543 if (IsCurly) // Skip the optional '{'
3544 ++I;
3545
3546 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00003547 if (i == 8 || i == 13 || i == 18 || i == 23) {
3548 if (*I != '-') {
3549 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3550 return;
3551 }
3552 } else if (!isxdigit(*I)) {
3553 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
3554 return;
3555 }
3556 I++;
3557 }
Francois Pichet11542142010-12-19 06:50:37 +00003558
Argyrios Kyrtzidis768d6ca2011-09-13 16:05:58 +00003559 D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context,
Francois Pichet11542142010-12-19 06:50:37 +00003560 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00003561 } else
Francois Pichet11542142010-12-19 06:50:37 +00003562 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00003563}
3564
Ted Kremenekb71368d2009-05-09 02:44:38 +00003565//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00003566// Top Level Sema Entry Points
3567//===----------------------------------------------------------------------===//
3568
Chandler Carruth1b03c872011-07-02 00:01:44 +00003569static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3570 const AttributeList &Attr) {
Peter Collingbourne60700392011-01-21 02:08:45 +00003571 switch (Attr.getKind()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003572 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
3573 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
3574 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003575 default:
3576 break;
3577 }
3578}
Abramo Bagnarae215f722010-04-30 13:10:51 +00003579
Chandler Carruth1b03c872011-07-02 00:01:44 +00003580static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
3581 const AttributeList &Attr) {
Chris Lattner803d0802008-06-29 00:43:07 +00003582 switch (Attr.getKind()) {
Michael Hane53ac8a2012-03-07 00:12:16 +00003583 case AttributeList::AT_ibaction: handleIBAction(S, D, Attr); break;
3584 case AttributeList::AT_iboutlet: handleIBOutlet(S, D, Attr); break;
3585 case AttributeList::AT_iboutletcollection:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003586 handleIBOutletCollection(S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003587 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003588 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00003589 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00003590 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00003591 case AttributeList::AT_neon_vector_type:
3592 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00003593 // Ignore these, these are type attributes, handled by
3594 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00003595 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00003596 case AttributeList::AT_device:
3597 case AttributeList::AT_host:
3598 case AttributeList::AT_overloadable:
3599 // Ignore, this is a non-inheritable attribute, handled
3600 // by ProcessNonInheritableDeclAttr.
3601 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003602 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
3603 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003604 case AttributeList::AT_always_inline:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003605 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00003606 case AttributeList::AT_analyzer_noreturn:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003607 handleAnalyzerNoReturnAttr (S, D, Attr); break;
3608 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
3609 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00003610 case AttributeList::AT_carries_dependency:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003611 handleDependencyAttr (S, D, Attr); break;
3612 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
3613 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
3614 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
3615 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
3616 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003617 case AttributeList::AT_ext_vector_type:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003618 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003619 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003620 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
3621 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
3622 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
3623 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00003624 case AttributeList::AT_launch_bounds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003625 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne7b381982010-12-12 23:03:07 +00003626 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003627 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
3628 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
3629 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
3630 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
3631 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003632 case AttributeList::AT_ownership_returns:
3633 case AttributeList::AT_ownership_takes:
3634 case AttributeList::AT_ownership_holds:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003635 handleOwnershipAttr (S, D, Attr); break;
3636 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
3637 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
3638 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
3639 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
3640 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003641
Argyrios Kyrtzidisb8b03132011-06-24 00:08:59 +00003642 case AttributeList::AT_objc_ownership:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003643 handleObjCOwnershipAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003644 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003645 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCallf85e1932011-06-15 23:02:42 +00003646
John McCalldc7c5ad2011-07-22 08:53:00 +00003647 case AttributeList::AT_objc_returns_inner_pointer:
3648 handleObjCReturnsInnerPointerAttr(S, D, Attr); break;
3649
John McCallfe98da02011-09-29 07:17:38 +00003650 case AttributeList::AT_ns_bridged:
3651 handleNSBridgedAttr(S, scope, D, Attr); break;
3652
John McCall8dfac0b2011-09-30 05:12:12 +00003653 case AttributeList::AT_cf_audited_transfer:
3654 case AttributeList::AT_cf_unknown_transfer:
3655 handleCFTransferAttr(S, D, Attr); break;
3656
Ted Kremenekb71368d2009-05-09 02:44:38 +00003657 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00003658 case AttributeList::AT_cf_consumed:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003659 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003660 case AttributeList::AT_ns_consumes_self:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003661 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCallc7ad3812011-01-25 03:31:58 +00003662
3663 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00003664 case AttributeList::AT_ns_returns_not_retained:
3665 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00003666 case AttributeList::AT_ns_returns_retained:
3667 case AttributeList::AT_cf_returns_retained:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003668 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00003669
Michael Hane53ac8a2012-03-07 00:12:16 +00003670 case AttributeList::AT_reqd_work_group_size:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003671 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begeman6f3d8382009-06-26 06:32:41 +00003672
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003673 case AttributeList::AT_init_priority:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003674 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00003675
Chandler Carruth1b03c872011-07-02 00:01:44 +00003676 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003677 case AttributeList::AT_ms_struct: handleMsStructAttr (S, D, Attr); break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003678 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3679 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003680 case AttributeList::AT_objc_arc_weak_reference_unavailable:
Fariborz Jahanian742352a2011-07-06 19:24:05 +00003681 handleArcWeakrefUnavailableAttr (S, D, Attr);
3682 break;
Patrick Beardb2f68202012-04-06 18:12:22 +00003683 case AttributeList::AT_objc_root_class:
3684 handleObjCRootClassAttr(S, D, Attr);
3685 break;
Ted Kremenek71207fc2012-01-05 22:47:47 +00003686 case AttributeList::AT_objc_requires_property_definitions:
3687 handleObjCRequiresPropertyDefsAttr (S, D, Attr);
Fariborz Jahaniane23dcf32012-01-03 18:45:41 +00003688 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003689 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
Rafael Espindolaf87cced2011-10-03 14:59:42 +00003690 case AttributeList::AT_returns_twice:
3691 handleReturnsTwiceAttr(S, D, Attr);
3692 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003693 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3694 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3695 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner026dc962009-02-14 07:37:35 +00003696 break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003697 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3698 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3699 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattner803d0802008-06-29 00:43:07 +00003700 case AttributeList::AT_transparent_union:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003701 handleTransparentUnionAttr(S, D, Attr);
Chris Lattner803d0802008-06-29 00:43:07 +00003702 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00003703 case AttributeList::AT_objc_exception:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003704 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner0db29ec2009-02-14 08:09:34 +00003705 break;
John McCalld5313b02011-03-02 11:33:24 +00003706 case AttributeList::AT_objc_method_family:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003707 handleObjCMethodFamilyAttr(S, D, Attr);
John McCalld5313b02011-03-02 11:33:24 +00003708 break;
Michael Hane53ac8a2012-03-07 00:12:16 +00003709 case AttributeList::AT_NSObject: handleObjCNSObject (S, D, Attr); break;
Chandler Carruth1b03c872011-07-02 00:01:44 +00003710 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3711 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3712 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3713 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3714 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3715 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3716 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3717 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpbf916502009-07-24 19:02:52 +00003718 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00003719 // Just ignore
3720 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00003721 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruth1b03c872011-07-02 00:01:44 +00003722 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner7255a2d2010-06-22 00:03:40 +00003723 break;
John McCall04a67a62010-02-05 21:31:56 +00003724 case AttributeList::AT_stdcall:
3725 case AttributeList::AT_cdecl:
3726 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003727 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003728 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00003729 case AttributeList::AT_pcs:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003730 handleCallConvAttr(S, D, Attr);
John McCall04a67a62010-02-05 21:31:56 +00003731 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00003732 case AttributeList::AT_opencl_kernel_function:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003733 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbournef315fa82011-02-14 01:42:53 +00003734 break;
Francois Pichet11542142010-12-19 06:50:37 +00003735 case AttributeList::AT_uuid:
Chandler Carruth1b03c872011-07-02 00:01:44 +00003736 handleUuidAttr(S, D, Attr);
Francois Pichet11542142010-12-19 06:50:37 +00003737 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003738
3739 // Thread safety attributes:
3740 case AttributeList::AT_guarded_var:
3741 handleGuardedVarAttr(S, D, Attr);
3742 break;
3743 case AttributeList::AT_pt_guarded_var:
3744 handleGuardedVarAttr(S, D, Attr, /*pointer = */true);
3745 break;
3746 case AttributeList::AT_scoped_lockable:
3747 handleLockableAttr(S, D, Attr, /*scoped = */true);
3748 break;
Kostya Serebryany71efba02012-01-24 19:25:38 +00003749 case AttributeList::AT_no_address_safety_analysis:
3750 handleNoAddressSafetyAttr(S, D, Attr);
3751 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003752 case AttributeList::AT_no_thread_safety_analysis:
3753 handleNoThreadSafetyAttr(S, D, Attr);
3754 break;
3755 case AttributeList::AT_lockable:
3756 handleLockableAttr(S, D, Attr);
3757 break;
Caitlin Sadowskidb33e142011-07-28 20:12:35 +00003758 case AttributeList::AT_guarded_by:
3759 handleGuardedByAttr(S, D, Attr);
3760 break;
3761 case AttributeList::AT_pt_guarded_by:
3762 handleGuardedByAttr(S, D, Attr, /*pointer = */true);
3763 break;
3764 case AttributeList::AT_exclusive_lock_function:
3765 handleLockFunAttr(S, D, Attr, /*exclusive = */true);
3766 break;
3767 case AttributeList::AT_exclusive_locks_required:
3768 handleLocksRequiredAttr(S, D, Attr, /*exclusive = */true);
3769 break;
3770 case AttributeList::AT_exclusive_trylock_function:
3771 handleTrylockFunAttr(S, D, Attr, /*exclusive = */true);
3772 break;
3773 case AttributeList::AT_lock_returned:
3774 handleLockReturnedAttr(S, D, Attr);
3775 break;
3776 case AttributeList::AT_locks_excluded:
3777 handleLocksExcludedAttr(S, D, Attr);
3778 break;
3779 case AttributeList::AT_shared_lock_function:
3780 handleLockFunAttr(S, D, Attr);
3781 break;
3782 case AttributeList::AT_shared_locks_required:
3783 handleLocksRequiredAttr(S, D, Attr);
3784 break;
3785 case AttributeList::AT_shared_trylock_function:
3786 handleTrylockFunAttr(S, D, Attr);
3787 break;
3788 case AttributeList::AT_unlock_function:
3789 handleUnlockFunAttr(S, D, Attr);
3790 break;
3791 case AttributeList::AT_acquired_before:
3792 handleAcquireOrderAttr(S, D, Attr, /*before = */true);
3793 break;
3794 case AttributeList::AT_acquired_after:
3795 handleAcquireOrderAttr(S, D, Attr, /*before = */false);
3796 break;
Caitlin Sadowskifdde9e72011-07-28 17:21:07 +00003797
Chris Lattner803d0802008-06-29 00:43:07 +00003798 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00003799 // Ask target about the attribute.
3800 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3801 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00003802 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3803 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00003804 break;
3805 }
3806}
3807
Peter Collingbourne60700392011-01-21 02:08:45 +00003808/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3809/// the attribute applies to decls. If the attribute is a type attribute, just
3810/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3811/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruth1b03c872011-07-02 00:01:44 +00003812static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3813 const AttributeList &Attr,
Peter Collingbourne60700392011-01-21 02:08:45 +00003814 bool NonInheritable, bool Inheritable) {
3815 if (Attr.isInvalid())
3816 return;
3817
3818 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3819 // FIXME: Try to deal with other __declspec attributes!
3820 return;
3821
3822 if (NonInheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003823 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003824
3825 if (Inheritable)
Chandler Carruth1b03c872011-07-02 00:01:44 +00003826 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourne60700392011-01-21 02:08:45 +00003827}
3828
Chris Lattner803d0802008-06-29 00:43:07 +00003829/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3830/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00003831void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00003832 const AttributeList *AttrList,
3833 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003834 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruth1b03c872011-07-02 00:01:44 +00003835 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003836 }
3837
3838 // GCC accepts
3839 // static int a9 __attribute__((weakref));
3840 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00003841 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003842 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00003843 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00003844 return;
Chris Lattner803d0802008-06-29 00:43:07 +00003845 }
3846}
3847
Erik Verbruggen5f1c8222011-10-13 09:41:32 +00003848// Annotation attributes are the only attributes allowed after an access
3849// specifier.
3850bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl,
3851 const AttributeList *AttrList) {
3852 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
3853 if (l->getKind() == AttributeList::AT_annotate) {
3854 handleAnnotateAttr(*this, ASDecl, *l);
3855 } else {
3856 Diag(l->getLoc(), diag::err_only_annotate_after_access_spec);
3857 return true;
3858 }
3859 }
3860
3861 return false;
3862}
3863
John McCalle82247a2011-10-01 05:17:03 +00003864/// checkUnusedDeclAttributes - Check a list of attributes to see if it
3865/// contains any decl attributes that we should warn about.
3866static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) {
3867 for ( ; A; A = A->getNext()) {
3868 // Only warn if the attribute is an unignored, non-type attribute.
3869 if (A->isUsedAsTypeAttr()) continue;
3870 if (A->getKind() == AttributeList::IgnoredAttribute) continue;
3871
3872 if (A->getKind() == AttributeList::UnknownAttribute) {
3873 S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored)
3874 << A->getName() << A->getRange();
3875 } else {
3876 S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl)
3877 << A->getName() << A->getRange();
3878 }
3879 }
3880}
3881
3882/// checkUnusedDeclAttributes - Given a declarator which is not being
3883/// used to build a declaration, complain about any decl attributes
3884/// which might be lying around on it.
3885void Sema::checkUnusedDeclAttributes(Declarator &D) {
3886 ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList());
3887 ::checkUnusedDeclAttributes(*this, D.getAttributes());
3888 for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i)
3889 ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs());
3890}
3891
Ryan Flynne25ff832009-07-30 03:15:39 +00003892/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3893/// #pragma weak needs a non-definition decl and source may not have one
Eli Friedman900693b2011-09-07 04:05:06 +00003894NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II,
3895 SourceLocation Loc) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003896 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003897 NamedDecl *NewD = 0;
3898 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
Eli Friedman900693b2011-09-07 04:05:06 +00003899 FunctionDecl *NewFD;
3900 // FIXME: Missing call to CheckFunctionDeclaration().
3901 // FIXME: Mangling?
3902 // FIXME: Is the qualifier info correct?
3903 // FIXME: Is the DeclContext correct?
3904 NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
3905 Loc, Loc, DeclarationName(II),
3906 FD->getType(), FD->getTypeSourceInfo(),
3907 SC_None, SC_None,
3908 false/*isInlineSpecified*/,
3909 FD->hasPrototype(),
3910 false/*isConstexprSpecified*/);
3911 NewD = NewFD;
3912
3913 if (FD->getQualifier())
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003914 NewFD->setQualifierInfo(FD->getQualifierLoc());
Eli Friedman900693b2011-09-07 04:05:06 +00003915
3916 // Fake up parameter variables; they are declared as if this were
3917 // a typedef.
3918 QualType FDTy = FD->getType();
3919 if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) {
3920 SmallVector<ParmVarDecl*, 16> Params;
3921 for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(),
3922 AE = FT->arg_type_end(); AI != AE; ++AI) {
3923 ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI);
3924 Param->setScopeInfo(0, Params.size());
3925 Params.push_back(Param);
3926 }
David Blaikie4278c652011-09-21 18:16:56 +00003927 NewFD->setParams(Params);
John McCallb6217662010-03-15 10:12:16 +00003928 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003929 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3930 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003931 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003932 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003933 VD->getStorageClass(),
3934 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003935 if (VD->getQualifier()) {
3936 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003937 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003938 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003939 }
3940 return NewD;
3941}
3942
3943/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3944/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003945void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003946 if (W.getUsed()) return; // only do this once
3947 W.setUsed(true);
3948 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3949 IdentifierInfo *NDId = ND->getIdentifier();
Eli Friedman900693b2011-09-07 04:05:06 +00003950 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation());
Sean Huntcf807c42010-08-18 23:23:40 +00003951 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3952 NDId->getName()));
3953 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003954 WeakTopLevelDecl.push_back(NewD);
3955 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3956 // to insert Decl at TU scope, sorry.
3957 DeclContext *SavedContext = CurContext;
3958 CurContext = Context.getTranslationUnitDecl();
3959 PushOnScopeChains(NewD, S);
3960 CurContext = SavedContext;
3961 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003962 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003963 }
3964}
3965
Chris Lattner0744e5f2008-06-29 00:23:49 +00003966/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3967/// it, apply them to D. This is a bit tricky because PD can have attributes
3968/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003969void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3970 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003971 // It's valid to "forward-declare" #pragma weak, in which case we
3972 // have to do this.
Douglas Gregor31e37b22011-07-28 18:09:57 +00003973 if (Inheritable) {
3974 LoadExternalWeakUndeclaredIdentifiers();
3975 if (!WeakUndeclaredIdentifiers.empty()) {
3976 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3977 if (IdentifierInfo *Id = ND->getIdentifier()) {
3978 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3979 = WeakUndeclaredIdentifiers.find(Id);
3980 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3981 WeakInfo W = I->second;
3982 DeclApplyPragmaWeak(S, ND, W);
3983 WeakUndeclaredIdentifiers[Id] = W;
3984 }
John McCalld4aff0e2010-10-27 00:59:00 +00003985 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003986 }
3987 }
3988 }
3989
Chris Lattner0744e5f2008-06-29 00:23:49 +00003990 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003991 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003992 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003993
Chris Lattner0744e5f2008-06-29 00:23:49 +00003994 // Walk the declarator structure, applying decl attributes that were in a type
3995 // position to the decl itself. This handles cases like:
3996 // int *__attr__(x)** D;
3997 // when X is a decl attribute.
3998 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3999 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00004000 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00004001
Chris Lattner0744e5f2008-06-29 00:23:49 +00004002 // Finally, apply any attributes on the decl itself.
4003 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00004004 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00004005}
John McCall54abf7d2009-11-04 02:18:39 +00004006
John McCallf85e1932011-06-15 23:02:42 +00004007/// Is the given declaration allowed to use a forbidden type?
4008static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
4009 // Private ivars are always okay. Unfortunately, people don't
4010 // always properly make their ivars private, even in system headers.
4011 // Plus we need to make fields okay, too.
Fariborz Jahaniana6b33802011-09-26 21:23:35 +00004012 // Function declarations in sys headers will be marked unavailable.
4013 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) &&
4014 !isa<FunctionDecl>(decl))
John McCallf85e1932011-06-15 23:02:42 +00004015 return false;
4016
4017 // Require it to be declared in a system header.
4018 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
4019}
4020
4021/// Handle a delayed forbidden-type diagnostic.
4022static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
4023 Decl *decl) {
4024 if (decl && isForbiddenTypeAllowed(S, decl)) {
4025 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
4026 "this system declaration uses an unsupported type"));
4027 return;
4028 }
David Blaikie4e4d0842012-03-11 07:00:24 +00004029 if (S.getLangOpts().ObjCAutoRefCount)
Fariborz Jahanian175fb102011-10-03 22:11:57 +00004030 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) {
4031 // FIXME. we may want to supress diagnostics for all
4032 // kind of forbidden type messages on unavailable functions.
4033 if (FD->hasAttr<UnavailableAttr>() &&
4034 diag.getForbiddenTypeDiagnostic() ==
4035 diag::err_arc_array_param_no_ownership) {
4036 diag.Triggered = true;
4037 return;
4038 }
4039 }
John McCallf85e1932011-06-15 23:02:42 +00004040
4041 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
4042 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
4043 diag.Triggered = true;
4044}
4045
John McCalleee1d542011-02-14 07:13:47 +00004046// This duplicates a vector push_back but hides the need to know the
4047// size of the type.
4048void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
4049 assert(StackSize <= StackCapacity);
4050
4051 // Grow the stack if necessary.
4052 if (StackSize == StackCapacity) {
4053 unsigned newCapacity = 2 * StackCapacity + 2;
4054 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
4055 const char *oldBuffer = (const char*) Stack;
4056
4057 if (StackCapacity)
4058 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
4059
4060 delete[] oldBuffer;
4061 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
4062 StackCapacity = newCapacity;
4063 }
4064
4065 assert(StackSize < StackCapacity);
4066 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00004067}
4068
John McCalleee1d542011-02-14 07:13:47 +00004069void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
4070 Decl *decl) {
4071 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00004072
John McCalleee1d542011-02-14 07:13:47 +00004073 // Check the invariants.
4074 assert(DD.StackSize >= state.SavedStackSize);
4075 assert(state.SavedStackSize >= DD.ActiveStackBase);
4076 assert(DD.ParsingDepth > 0);
4077
4078 // Drop the parsing depth.
4079 DD.ParsingDepth--;
4080
4081 // If there are no active diagnostics, we're done.
4082 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00004083 return;
4084
John McCall2f514482010-01-27 03:50:35 +00004085 // We only want to actually emit delayed diagnostics when we
4086 // successfully parsed a decl.
John McCalle8c904f2012-01-26 20:04:03 +00004087 if (decl) {
John McCalleee1d542011-02-14 07:13:47 +00004088 // We emit all the active diagnostics, not just those starting
4089 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00004090 // decl spec and another for each declarator; in a decl group like:
4091 // deprecated_typedef foo, *bar, baz();
4092 // only the declarator pops will be passed decls. This is correct;
4093 // we really do need to consider delayed diagnostics from the decl spec
4094 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00004095 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
4096 DelayedDiagnostic &diag = DD.Stack[i];
4097 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00004098 continue;
4099
John McCalleee1d542011-02-14 07:13:47 +00004100 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00004101 case DelayedDiagnostic::Deprecation:
John McCalle8c904f2012-01-26 20:04:03 +00004102 // Don't bother giving deprecation diagnostics if the decl is invalid.
4103 if (!decl->isInvalidDecl())
4104 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004105 break;
4106
4107 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00004108 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00004109 break;
John McCallf85e1932011-06-15 23:02:42 +00004110
4111 case DelayedDiagnostic::ForbiddenType:
4112 handleDelayedForbiddenType(S, diag, decl);
4113 break;
John McCall2f514482010-01-27 03:50:35 +00004114 }
4115 }
4116 }
4117
John McCall58e6f342010-03-16 05:22:47 +00004118 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00004119 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00004120 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00004121
John McCalleee1d542011-02-14 07:13:47 +00004122 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00004123}
4124
4125static bool isDeclDeprecated(Decl *D) {
4126 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00004127 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00004128 return true;
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +00004129 // A category implicitly has the availability of the interface.
4130 if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D))
4131 return CatD->getClassInterface()->isDeprecated();
John McCall54abf7d2009-11-04 02:18:39 +00004132 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
4133 return false;
4134}
4135
John McCall9c3087b2010-08-26 02:13:20 +00004136void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00004137 Decl *Ctx) {
4138 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00004139 return;
4140
John McCall2f514482010-01-27 03:50:35 +00004141 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004142 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004143 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004144 << DD.getDeprecationDecl()->getDeclName()
4145 << DD.getDeprecationMessage();
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004146 else if (DD.getUnknownObjCClass()) {
4147 Diag(DD.Loc, diag::warn_deprecated_fwdclass_message)
4148 << DD.getDeprecationDecl()->getDeclName();
4149 Diag(DD.getUnknownObjCClass()->getLocation(), diag::note_forward_class);
4150 }
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004151 else
4152 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004153 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00004154}
4155
Chris Lattner5f9e2722011-07-23 10:55:15 +00004156void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004157 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004158 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00004159 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00004160 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
Fariborz Jahanianb0a66152012-03-02 21:50:02 +00004161 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D,
4162 UnknownObjCClass,
4163 Message));
John McCall54abf7d2009-11-04 02:18:39 +00004164 return;
4165 }
4166
4167 // Otherwise, don't warn if our current context is deprecated.
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00004168 if (isDeclDeprecated(cast<Decl>(getCurLexicalContext())))
John McCall54abf7d2009-11-04 02:18:39 +00004169 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00004170 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00004171 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
4172 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004173 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00004174 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004175 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004176 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004177 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00004178 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
4179 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00004180 }
John McCall54abf7d2009-11-04 02:18:39 +00004181}