blob: adef528b0a47f904dae964722aae689f5d5b43f3 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov55bcea12010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall28a0cf72010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
John McCall31168b02011-06-15 23:02:42 +000020#include "clang/Basic/SourceManager.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000021#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000023#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000024#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000025using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000026using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000027
John McCall5fca7ea2011-03-02 12:29:23 +000028/// These constants match the enumerated choices of
29/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
30enum {
31 ExpectedFunction,
32 ExpectedUnion,
33 ExpectedVariableOrFunction,
34 ExpectedFunctionOrMethod,
35 ExpectedParameter,
36 ExpectedParameterOrMethod,
37 ExpectedFunctionMethodOrBlock,
38 ExpectedClassOrVirtualMethod,
39 ExpectedFunctionMethodOrParameter,
40 ExpectedClass,
41 ExpectedVirtualMethod,
42 ExpectedClassMember,
43 ExpectedVariable,
44 ExpectedMethod,
45 ExpectedVariableFunctionOrLabel
46};
47
Chris Lattner58418ff2008-06-29 00:16:31 +000048//===----------------------------------------------------------------------===//
49// Helper functions
50//===----------------------------------------------------------------------===//
51
Chandler Carruthff4c4f02011-07-01 23:49:12 +000052static const FunctionType *getFunctionType(const Decl *D,
Ted Kremenek527042b2009-08-14 20:49:40 +000053 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000054 QualType Ty;
Chandler Carruthff4c4f02011-07-01 23:49:12 +000055 if (const ValueDecl *decl = dyn_cast<ValueDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000056 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000057 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000058 Ty = decl->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +000059 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000060 Ty = decl->getUnderlyingType();
61 else
62 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000063
Chris Lattner2c6fcf52008-06-26 18:38:35 +000064 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000065 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000066 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000067 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000068
John McCall9dd450b2009-09-21 23:43:11 +000069 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000070}
71
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000072// FIXME: We should provide an abstraction around a method or function
73// to provide the following bits of information.
74
Nuno Lopes518e3702009-12-20 23:11:08 +000075/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000076/// type (function or function-typed variable).
Chandler Carruthff4c4f02011-07-01 23:49:12 +000077static bool isFunction(const Decl *D) {
78 return getFunctionType(D, false) != NULL;
Ted Kremenek527042b2009-08-14 20:49:40 +000079}
80
81/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000082/// type (function or function-typed variable) or an Objective-C
83/// method.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000084static bool isFunctionOrMethod(const Decl *D) {
85 return isFunction(D)|| isa<ObjCMethodDecl>(D);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000086}
87
Fariborz Jahanian4447e172009-05-15 23:15:03 +000088/// isFunctionOrMethodOrBlock - Return true if the given decl has function
89/// type (function or function-typed variable) or an Objective-C
90/// method or a block.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000091static bool isFunctionOrMethodOrBlock(const Decl *D) {
92 if (isFunctionOrMethod(D))
Fariborz Jahanian4447e172009-05-15 23:15:03 +000093 return true;
94 // check for block is more involved.
Chandler Carruthff4c4f02011-07-01 23:49:12 +000095 if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000096 QualType Ty = V->getType();
97 return Ty->isBlockPointerType();
98 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +000099 return isa<BlockDecl>(D);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000100}
101
John McCall3882ace2011-01-05 12:14:39 +0000102/// Return true if the given decl has a declarator that should have
103/// been processed by Sema::GetTypeForDeclarator.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000104static bool hasDeclarator(const Decl *D) {
John McCall31168b02011-06-15 23:02:42 +0000105 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000106 return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) ||
107 isa<ObjCPropertyDecl>(D);
John McCall3882ace2011-01-05 12:14:39 +0000108}
109
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000110/// hasFunctionProto - Return true if the given decl has a argument
111/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000112/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000113static bool hasFunctionProto(const Decl *D) {
114 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000115 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000116 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000117 assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000118 return true;
119 }
120}
121
122/// getFunctionOrMethodNumArgs - Return number of function or method
123/// arguments. It is an error to call this on a K&R function (use
124/// hasFunctionProto first).
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000125static unsigned getFunctionOrMethodNumArgs(const Decl *D) {
126 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000129 return BD->getNumParams();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000130 return cast<ObjCMethodDecl>(D)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000131}
132
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000133static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) {
134 if (const FunctionType *FnTy = getFunctionType(D))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000135 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000136 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000137 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000138
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000139 return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000140}
141
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000142static QualType getFunctionOrMethodResultType(const Decl *D) {
143 if (const FunctionType *FnTy = getFunctionType(D))
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000144 return cast<FunctionProtoType>(FnTy)->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000145 return cast<ObjCMethodDecl>(D)->getResultType();
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000146}
147
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000148static bool isFunctionOrMethodVariadic(const Decl *D) {
149 if (const FunctionType *FnTy = getFunctionType(D)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000150 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000151 return proto->isVariadic();
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000152 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000153 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000154 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000155 return cast<ObjCMethodDecl>(D)->isVariadic();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000156 }
157}
158
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000159static bool isInstanceMethod(const Decl *D) {
160 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D))
Chandler Carruth743682b2010-11-16 08:35:43 +0000161 return MethodDecl->isInstance();
162 return false;
163}
164
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000165static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000166 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000167 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000168 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000169
John McCall96fa4842010-05-17 21:00:27 +0000170 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
171 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000172 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000173
John McCall96fa4842010-05-17 21:00:27 +0000174 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000175
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000176 // FIXME: Should we walk the chain of classes?
177 return ClsName == &Ctx.Idents.get("NSString") ||
178 ClsName == &Ctx.Idents.get("NSMutableString");
179}
180
Daniel Dunbar980c6692008-09-26 03:32:58 +0000181static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000182 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000183 if (!PT)
184 return false;
185
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000186 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000187 if (!RT)
188 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000189
Daniel Dunbar980c6692008-09-26 03:32:58 +0000190 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000191 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000192 return false;
193
194 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
195}
196
Chris Lattner58418ff2008-06-29 00:16:31 +0000197//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000198// Attribute Implementations
199//===----------------------------------------------------------------------===//
200
Daniel Dunbar032db472008-07-31 22:40:48 +0000201// FIXME: All this manual attribute parsing code is gross. At the
202// least add some helper functions to check most argument patterns (#
203// and types of args).
204
Chandler Carruthedc2c642011-07-02 00:01:44 +0000205static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D,
206 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000207 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(D);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000208 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000209 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000210 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000211 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000212
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000213 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000214
215 Expr *sizeExpr;
216
217 // Special case where the argument is a template id.
218 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000219 CXXScopeSpec SS;
220 UnqualifiedId id;
221 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
Douglas Gregor39c02722011-06-15 16:02:29 +0000222
223 ExprResult Size = S.ActOnIdExpression(scope, SS, id, false, false);
224 if (Size.isInvalid())
225 return;
226
227 sizeExpr = Size.get();
Douglas Gregor758a8692009-06-17 21:51:59 +0000228 } else {
229 // check the attribute arguments.
230 if (Attr.getNumArgs() != 1) {
231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
232 return;
233 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000234 sizeExpr = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000235 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000236
237 // Instantiate/Install the vector type, and let Sema build the type for us.
238 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000239 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000240 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000241 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000242 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000243
Douglas Gregor758a8692009-06-17 21:51:59 +0000244 // Remember this typedef decl, we will need it later for diagnostics.
245 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000246 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000247}
248
Chandler Carruthedc2c642011-07-02 00:01:44 +0000249static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000250 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000251 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000252 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000253 return;
254 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000255
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000256 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000257 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000258 else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000259 // If the alignment is less than or equal to 8 bits, the packed attribute
260 // has no effect.
261 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000262 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000263 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000264 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000265 else
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000266 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000267 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000268 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000269}
270
Chandler Carruthedc2c642011-07-02 00:01:44 +0000271static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000272 if (TagDecl *TD = dyn_cast<TagDecl>(D))
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000273 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context));
274 else
275 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
276}
277
Chandler Carruthedc2c642011-07-02 00:01:44 +0000278static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000279 // check the attribute arguments.
280 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000281 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000282 return;
283 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000284
Ted Kremenek1f672822010-02-18 03:08:58 +0000285 // The IBAction attributes only apply to instance methods.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000286 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Ted Kremenek1f672822010-02-18 03:08:58 +0000287 if (MD->isInstanceMethod()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000288 D->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000289 return;
290 }
291
Ted Kremenekd68ec812011-02-04 06:54:16 +0000292 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000293}
294
Chandler Carruthedc2c642011-07-02 00:01:44 +0000295static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek1f672822010-02-18 03:08:58 +0000296 // check the attribute arguments.
297 if (Attr.getNumArgs() > 0) {
298 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
299 return;
300 }
301
302 // The IBOutlet attributes only apply to instance variables of
Ted Kremenek06be9682010-02-17 02:37:45 +0000303 // Objective-C classes.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000304 if (isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D)) {
305 D->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000306 return;
Ted Kremenek06be9682010-02-17 02:37:45 +0000307 }
Ted Kremenek1f672822010-02-18 03:08:58 +0000308
Ted Kremenekd68ec812011-02-04 06:54:16 +0000309 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000310}
311
Chandler Carruthedc2c642011-07-02 00:01:44 +0000312static void handleIBOutletCollection(Sema &S, Decl *D,
313 const AttributeList &Attr) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000314
315 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000316 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000317 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
318 return;
319 }
320
321 // The IBOutletCollection attributes only apply to instance variables of
322 // Objective-C classes.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000323 if (!(isa<ObjCIvarDecl>(D) || isa<ObjCPropertyDecl>(D))) {
Ted Kremenekd68ec812011-02-04 06:54:16 +0000324 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek26bde772010-05-19 17:38:06 +0000325 return;
326 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000327 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D))
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000328 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
329 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
330 << VD->getType() << 0;
331 return;
332 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000333 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000334 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
335 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
336 << PD->getType() << 1;
337 return;
338 }
339
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000340 IdentifierInfo *II = Attr.getParameterName();
341 if (!II)
342 II = &S.Context.Idents.get("id");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000343
John McCallba7bf592010-08-24 05:47:05 +0000344 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000345 S.getScopeForContext(D->getDeclContext()->getParent()));
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000346 if (!TypeRep) {
347 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
348 return;
349 }
John McCallba7bf592010-08-24 05:47:05 +0000350 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000351 // Diagnose use of non-object type in iboutletcollection attribute.
352 // FIXME. Gnu attribute extension ignores use of builtin types in
353 // attributes. So, __attribute__((iboutletcollection(char))) will be
354 // treated as __attribute__((iboutletcollection())).
355 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
356 !QT->isObjCObjectType()) {
357 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
358 return;
359 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000360 D->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000361 QT));
Ted Kremenek26bde772010-05-19 17:38:06 +0000362}
363
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000364static void possibleTransparentUnionPointerType(QualType &T) {
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000365 if (const RecordType *UT = T->getAsUnionType())
366 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
367 RecordDecl *UD = UT->getDecl();
368 for (RecordDecl::field_iterator it = UD->field_begin(),
369 itend = UD->field_end(); it != itend; ++it) {
370 QualType QT = it->getType();
371 if (QT->isAnyPointerType() || QT->isBlockPointerType()) {
372 T = QT;
373 return;
374 }
375 }
376 }
377}
378
Chandler Carruthedc2c642011-07-02 00:01:44 +0000379static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000380 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
381 // ignore it as well
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000382 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000383 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000384 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000385 return;
386 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000387
Chandler Carruth743682b2010-11-16 08:35:43 +0000388 // In C++ the implicit 'this' function parameter also counts, and they are
389 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000390 bool HasImplicitThisParam = isInstanceMethod(D);
391 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000392
393 // The nonnull attribute only applies to pointers.
394 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000395
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000396 for (AttributeList::arg_iterator I=Attr.arg_begin(),
397 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000398
399
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000400 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000401 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000402 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000403 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
404 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000405 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
406 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000407 return;
408 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000409
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000410 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000411
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000412 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000413 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000414 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000415 return;
416 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000417
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000418 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000419 if (HasImplicitThisParam) {
420 if (x == 0) {
421 S.Diag(Attr.getLoc(),
422 diag::err_attribute_invalid_implicit_this_argument)
423 << "nonnull" << Ex->getSourceRange();
424 return;
425 }
426 --x;
427 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000428
429 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000430 QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000431 possibleTransparentUnionPointerType(T);
Fariborz Jahanianf4aa2792011-06-27 21:12:03 +0000432
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000433 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000434 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000435 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000436 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000437 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000438 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000439
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000440 NonNullArgs.push_back(x);
441 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000442
443 // If no arguments were specified to __attribute__((nonnull)) then all pointer
444 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000445 if (NonNullArgs.empty()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000446 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(D); I != E; ++I) {
447 QualType T = getFunctionOrMethodArgType(D, I).getNonReferenceType();
Chandler Carruth3ed22c32011-07-01 23:49:16 +0000448 possibleTransparentUnionPointerType(T);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000449 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000450 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000451 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000452
Ted Kremenek22813f42010-10-21 18:49:36 +0000453 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000454 if (NonNullArgs.empty()) {
455 // Warn the trivial case only if attribute is not coming from a
456 // macro instantiation.
457 if (Attr.getLoc().isFileID())
458 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000459 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000460 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000461 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000462
463 unsigned* start = &NonNullArgs[0];
464 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000465 llvm::array_pod_sort(start, start + size);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000466 D->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000467 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000468}
469
Chandler Carruthedc2c642011-07-02 00:01:44 +0000470static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000471 // This attribute must be applied to a function declaration.
472 // The first argument to the attribute must be a string,
473 // the name of the resource, for example "malloc".
474 // The following arguments must be argument indexes, the arguments must be
475 // of integer type for Returns, otherwise of pointer type.
476 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000477 // after being held. free() should be __attribute((ownership_takes)), whereas
478 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000479
480 if (!AL.getParameterName()) {
481 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
482 << AL.getName()->getName() << 1;
483 return;
484 }
485 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000486 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000487 switch (AL.getKind()) {
488 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000489 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000490 if (AL.getNumArgs() < 1) {
491 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
492 return;
493 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000494 break;
495 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000496 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000497 if (AL.getNumArgs() < 1) {
498 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
499 return;
500 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000501 break;
502 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000503 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000504 if (AL.getNumArgs() > 1) {
505 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
506 << AL.getNumArgs() + 1;
507 return;
508 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000509 break;
510 default:
511 // This should never happen given how we are called.
512 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000513 }
514
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000515 if (!isFunction(D) || !hasFunctionProto(D)) {
John McCall5fca7ea2011-03-02 12:29:23 +0000516 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
517 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000518 return;
519 }
520
Chandler Carruth743682b2010-11-16 08:35:43 +0000521 // In C++ the implicit 'this' function parameter also counts, and they are
522 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000523 bool HasImplicitThisParam = isInstanceMethod(D);
524 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000525
526 llvm::StringRef Module = AL.getParameterName()->getName();
527
528 // Normalize the argument, __foo__ becomes foo.
529 if (Module.startswith("__") && Module.endswith("__"))
530 Module = Module.substr(2, Module.size() - 4);
531
532 llvm::SmallVector<unsigned, 10> OwnershipArgs;
533
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000534 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
535 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000536
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000537 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000538 llvm::APSInt ArgNum(32);
539 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
540 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
541 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
542 << AL.getName()->getName() << IdxExpr->getSourceRange();
543 continue;
544 }
545
546 unsigned x = (unsigned) ArgNum.getZExtValue();
547
548 if (x > NumArgs || x < 1) {
549 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
550 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
551 continue;
552 }
553 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000554 if (HasImplicitThisParam) {
555 if (x == 0) {
556 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
557 << "ownership" << IdxExpr->getSourceRange();
558 return;
559 }
560 --x;
561 }
562
Ted Kremenekd21139a2010-07-31 01:52:11 +0000563 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000564 case OwnershipAttr::Takes:
565 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000566 // Is the function argument a pointer type?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000567 QualType T = getFunctionOrMethodArgType(D, x);
Ted Kremenekd21139a2010-07-31 01:52:11 +0000568 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
569 // FIXME: Should also highlight argument in decl.
570 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000571 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +0000572 << "pointer"
573 << IdxExpr->getSourceRange();
574 continue;
575 }
576 break;
577 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000578 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000579 if (AL.getNumArgs() > 1) {
580 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000581 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +0000582 llvm::APSInt ArgNum(32);
583 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
584 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
585 S.Diag(AL.getLoc(), diag::err_ownership_type)
586 << "ownership_returns" << "integer"
587 << IdxExpr->getSourceRange();
588 return;
589 }
590 }
591 break;
592 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000593 default:
594 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000595 } // switch
596
597 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000598 for (specific_attr_iterator<OwnershipAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000599 i = D->specific_attr_begin<OwnershipAttr>(),
600 e = D->specific_attr_end<OwnershipAttr>();
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000601 i != e; ++i) {
602 if ((*i)->getOwnKind() != K) {
603 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
604 I!=E; ++I) {
605 if (x == *I) {
606 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
607 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +0000608 }
609 }
610 }
611 }
612 OwnershipArgs.push_back(x);
613 }
614
615 unsigned* start = OwnershipArgs.data();
616 unsigned size = OwnershipArgs.size();
617 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000618
619 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
620 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
621 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000622 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000623
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000624 D->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000625 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +0000626}
627
John McCall7a198ce2011-02-08 22:35:49 +0000628/// Whether this declaration has internal linkage for the purposes of
629/// things that want to complain about things not have internal linkage.
630static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
631 switch (D->getLinkage()) {
632 case NoLinkage:
633 case InternalLinkage:
634 return true;
635
636 // Template instantiations that go from external to unique-external
637 // shouldn't get diagnosed.
638 case UniqueExternalLinkage:
639 return true;
640
641 case ExternalLinkage:
642 return false;
643 }
644 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +0000645 return false;
646}
647
Chandler Carruthedc2c642011-07-02 00:01:44 +0000648static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Rafael Espindolac18086a2010-02-23 22:00:30 +0000649 // Check the attribute arguments.
650 if (Attr.getNumArgs() > 1) {
651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
652 return;
653 }
654
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000655 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
John McCall7a198ce2011-02-08 22:35:49 +0000656 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000657 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +0000658 return;
659 }
660
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000661 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +0000662
Rafael Espindolac18086a2010-02-23 22:00:30 +0000663 // gcc rejects
664 // class c {
665 // static int a __attribute__((weakref ("v2")));
666 // static int b() __attribute__((weakref ("f3")));
667 // };
668 // and ignores the attributes of
669 // void f(void) {
670 // static int a __attribute__((weakref ("v2")));
671 // }
672 // we reject them
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000673 const DeclContext *Ctx = D->getDeclContext()->getRedeclContext();
Sebastian Redl50c68252010-08-31 00:36:30 +0000674 if (!Ctx->isFileContext()) {
675 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +0000676 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +0000677 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000678 }
679
680 // The GCC manual says
681 //
682 // At present, a declaration to which `weakref' is attached can only
683 // be `static'.
684 //
685 // It also says
686 //
687 // Without a TARGET,
688 // given as an argument to `weakref' or to `alias', `weakref' is
689 // equivalent to `weak'.
690 //
691 // gcc 4.4.1 will accept
692 // int a7 __attribute__((weakref));
693 // as
694 // int a7 __attribute__((weak));
695 // This looks like a bug in gcc. We reject that for now. We should revisit
696 // it if this behaviour is actually used.
697
John McCall7a198ce2011-02-08 22:35:49 +0000698 if (!hasEffectivelyInternalLinkage(nd)) {
699 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000700 return;
701 }
702
703 // GCC rejects
704 // static ((alias ("y"), weakref)).
705 // Should we? How to check that weakref is before or after alias?
706
707 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000708 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000709 Arg = Arg->IgnoreParenCasts();
710 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
711
712 if (Str == 0 || Str->isWide()) {
713 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
714 << "weakref" << 1;
715 return;
716 }
717 // GCC will accept anything as the argument of weakref. Should we
718 // check for an existing decl?
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000719 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +0000720 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000721 }
722
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000723 D->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000724}
725
Chandler Carruthedc2c642011-07-02 00:01:44 +0000726static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000727 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000728 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000729 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000730 return;
731 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000732
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000733 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000734 Arg = Arg->IgnoreParenCasts();
735 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000736
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000737 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000738 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000739 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000740 return;
741 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000742
Daniel Dunbar14ad22f2011-04-19 21:43:27 +0000743 if (S.Context.Target.getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +0000744 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
745 return;
746 }
747
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000748 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000749
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000750 D->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +0000751 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000752}
753
Chandler Carruthedc2c642011-07-02 00:01:44 +0000754static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000755 // Check the attribute arguments.
Daniel Dunbar03a38442008-10-28 00:17:57 +0000756 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000757 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000758 return;
759 }
Anders Carlsson88097122009-02-19 19:16:48 +0000760
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000761 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000762 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000763 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000764 return;
765 }
766
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000767 D->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000768}
769
Chandler Carruthedc2c642011-07-02 00:01:44 +0000770static void handleAlwaysInlineAttr(Sema &S, Decl *D,
771 const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000772 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000773 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000774 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
775 return;
776 }
777
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000778 if (!isa<FunctionDecl>(D)) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000779 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000780 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +0000781 return;
782 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000783
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000784 D->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +0000785}
786
Chandler Carruthedc2c642011-07-02 00:01:44 +0000787static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000788 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000789 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000790 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
791 return;
792 }
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000794 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Mike Stump11289f42009-09-09 15:08:12 +0000795 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000796 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000797 D->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +0000798 return;
799 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000800 }
801
Ted Kremenek08479ae2009-08-15 00:51:46 +0000802 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000803}
804
Chandler Carruthedc2c642011-07-02 00:01:44 +0000805static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000806 // check the attribute arguments.
807 if (Attr.getNumArgs() != 0) {
808 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
809 return;
810 }
811
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000812 D->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000813}
814
Chandler Carruthedc2c642011-07-02 00:01:44 +0000815static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher8a2ee392010-12-02 02:45:55 +0000816 assert(Attr.isInvalid() == false);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000817 if (isa<VarDecl>(D))
818 D->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +0000819 else
820 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000821 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000822}
823
Chandler Carruthedc2c642011-07-02 00:01:44 +0000824static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Eric Christopher8a2ee392010-12-02 02:45:55 +0000825 assert(Attr.isInvalid() == false);
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000826 if (isa<VarDecl>(D))
827 D->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
Eric Christopher515d87f2010-12-03 06:58:14 +0000828 else
829 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000830 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000831}
832
Chandler Carruthedc2c642011-07-02 00:01:44 +0000833static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000834 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +0000835
836 if (S.CheckNoReturnAttr(attr)) return;
837
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000838 if (!isa<ObjCMethodDecl>(D)) {
John McCall3882ace2011-01-05 12:14:39 +0000839 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000840 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +0000841 return;
842 }
843
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000844 D->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
John McCall3882ace2011-01-05 12:14:39 +0000845}
846
847bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +0000848 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +0000849 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
850 attr.setInvalid();
851 return true;
852 }
853
854 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000855}
856
Chandler Carruthedc2c642011-07-02 00:01:44 +0000857static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D,
858 const AttributeList &Attr) {
Ted Kremenek5295ce82010-08-19 00:51:58 +0000859
860 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
861 // because 'analyzer_noreturn' does not impact the type.
862
863 if (Attr.getNumArgs() != 0) {
864 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
865 return;
866 }
867
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000868 if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) {
869 ValueDecl *VD = dyn_cast<ValueDecl>(D);
Ted Kremenek5295ce82010-08-19 00:51:58 +0000870 if (VD == 0 || (!VD->getType()->isBlockPointerType()
871 && !VD->getType()->isFunctionPointerType())) {
872 S.Diag(Attr.getLoc(),
873 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
874 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000875 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +0000876 return;
877 }
878 }
879
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000880 D->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000881}
882
John Thompsoncdb847ba2010-08-09 21:53:52 +0000883// PS3 PPU-specific.
Chandler Carruthedc2c642011-07-02 00:01:44 +0000884static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000885/*
886 Returning a Vector Class in Registers
887
Eric Christopherbc638a82010-12-01 22:13:54 +0000888 According to the PPU ABI specifications, a class with a single member of
889 vector type is returned in memory when used as the return value of a function.
890 This results in inefficient code when implementing vector classes. To return
891 the value in a single vector register, add the vecreturn attribute to the
892 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +0000893
894 Example:
895
896 struct Vector
897 {
898 __vector float xyzw;
899 } __attribute__((vecreturn));
900
901 Vector Add(Vector lhs, Vector rhs)
902 {
903 Vector result;
904 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
905 return result; // This will be returned in a register
906 }
907*/
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000908 if (!isa<RecordDecl>(D)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000909 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000910 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +0000911 return;
912 }
913
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000914 if (D->getAttr<VecReturnAttr>()) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000915 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
916 return;
917 }
918
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000919 RecordDecl *record = cast<RecordDecl>(D);
John Thompson9a587aaa2010-09-18 01:12:07 +0000920 int count = 0;
921
922 if (!isa<CXXRecordDecl>(record)) {
923 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
924 return;
925 }
926
927 if (!cast<CXXRecordDecl>(record)->isPOD()) {
928 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
929 return;
930 }
931
Eric Christopherbc638a82010-12-01 22:13:54 +0000932 for (RecordDecl::field_iterator iter = record->field_begin();
933 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +0000934 if ((count == 1) || !iter->getType()->isVectorType()) {
935 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
936 return;
937 }
938 count++;
939 }
940
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000941 D->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +0000942}
943
Chandler Carruthedc2c642011-07-02 00:01:44 +0000944static void handleDependencyAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000945 if (!isFunctionOrMethod(D) && !isa<ParmVarDecl>(D)) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000947 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000948 return;
949 }
950 // FIXME: Actually store the attribute on the declaration
951}
952
Chandler Carruthedc2c642011-07-02 00:01:44 +0000953static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Ted Kremenek39c59a82008-07-25 04:39:19 +0000954 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000955 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000956 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000957 return;
958 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000959
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000960 if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) &&
961 !isa<TypeDecl>(D) && !isa<LabelDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000962 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000963 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000964 return;
965 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000966
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000967 D->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +0000968}
969
Chandler Carruthedc2c642011-07-02 00:01:44 +0000970static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000971 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000972 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000973 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
974 return;
975 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000976
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000977 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000978 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000979 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
980 return;
981 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000982 } else if (!isFunctionOrMethod(D)) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000983 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000984 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000985 return;
986 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000987
Chandler Carruthff4c4f02011-07-01 23:49:12 +0000988 D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000989}
990
Chandler Carruthedc2c642011-07-02 00:01:44 +0000991static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +0000992 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +0000993 if (Attr.getNumArgs() > 1) {
994 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +0000995 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000996 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000997
998 int priority = 65535; // FIXME: Do not hardcode such constants.
999 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001000 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001001 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001002 if (E->isTypeDependent() || E->isValueDependent() ||
1003 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001004 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001005 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001006 return;
1007 }
1008 priority = Idx.getZExtValue();
1009 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001010
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001011 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001012 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001013 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001014 return;
1015 }
1016
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001017 D->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001018 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001019}
1020
Chandler Carruthedc2c642011-07-02 00:01:44 +00001021static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001022 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001023 if (Attr.getNumArgs() > 1) {
1024 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001025 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001026 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001027
1028 int priority = 65535; // FIXME: Do not hardcode such constants.
1029 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001030 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001031 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001032 if (E->isTypeDependent() || E->isValueDependent() ||
1033 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001034 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001035 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001036 return;
1037 }
1038 priority = Idx.getZExtValue();
1039 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001040
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001041 if (!isa<FunctionDecl>(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001042 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001043 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001044 return;
1045 }
1046
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001047 D->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001048 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001049}
1050
Chandler Carruthedc2c642011-07-02 00:01:44 +00001051static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001052 unsigned NumArgs = Attr.getNumArgs();
1053 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001054 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001055 return;
1056 }
Chris Lattner190aa102011-02-24 05:42:24 +00001057
Fariborz Jahanian551063102010-10-06 21:18:44 +00001058 // Handle the case where deprecated attribute has a text message.
Chris Lattner190aa102011-02-24 05:42:24 +00001059 llvm::StringRef Str;
1060 if (NumArgs == 1) {
1061 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001062 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001063 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1064 << "deprecated";
Fariborz Jahanian551063102010-10-06 21:18:44 +00001065 return;
1066 }
Chris Lattner190aa102011-02-24 05:42:24 +00001067 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001068 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001069
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001070 D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001071}
1072
Chandler Carruthedc2c642011-07-02 00:01:44 +00001073static void handleUnavailableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner190aa102011-02-24 05:42:24 +00001074 unsigned NumArgs = Attr.getNumArgs();
1075 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001076 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001077 return;
1078 }
Chris Lattner190aa102011-02-24 05:42:24 +00001079
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001080 // Handle the case where unavailable attribute has a text message.
Chris Lattner190aa102011-02-24 05:42:24 +00001081 llvm::StringRef Str;
1082 if (NumArgs == 1) {
1083 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001084 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001085 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001086 diag::err_attribute_not_string) << "unavailable";
1087 return;
1088 }
Chris Lattner190aa102011-02-24 05:42:24 +00001089 Str = SE->getString();
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001090 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001091 D->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001092}
1093
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00001094static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D,
1095 const AttributeList &Attr) {
1096 unsigned NumArgs = Attr.getNumArgs();
1097 if (NumArgs > 0) {
1098 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0;
1099 return;
1100 }
1101
1102 D->addAttr(::new (S.Context) ArcWeakrefUnavailableAttr(
1103 Attr.getLoc(), S.Context));
1104}
1105
Chandler Carruthedc2c642011-07-02 00:01:44 +00001106static void handleAvailabilityAttr(Sema &S, Decl *D,
1107 const AttributeList &Attr) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001108 IdentifierInfo *Platform = Attr.getParameterName();
1109 SourceLocation PlatformLoc = Attr.getParameterLoc();
1110
1111 llvm::StringRef PlatformName
1112 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1113 if (PlatformName.empty()) {
1114 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1115 << Platform;
1116
1117 PlatformName = Platform->getName();
1118 }
1119
1120 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1121 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1122 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001123 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001124
1125 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1126 // of these steps are needed).
1127 if (Introduced.isValid() && Deprecated.isValid() &&
1128 !(Introduced.Version < Deprecated.Version)) {
1129 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1130 << 1 << PlatformName << Deprecated.Version.getAsString()
1131 << 0 << Introduced.Version.getAsString();
1132 return;
1133 }
1134
1135 if (Introduced.isValid() && Obsoleted.isValid() &&
1136 !(Introduced.Version < Obsoleted.Version)) {
1137 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1138 << 2 << PlatformName << Obsoleted.Version.getAsString()
1139 << 0 << Introduced.Version.getAsString();
1140 return;
1141 }
1142
1143 if (Deprecated.isValid() && Obsoleted.isValid() &&
1144 !(Deprecated.Version < Obsoleted.Version)) {
1145 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1146 << 2 << PlatformName << Obsoleted.Version.getAsString()
1147 << 1 << Deprecated.Version.getAsString();
1148 return;
1149 }
1150
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001151 D->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001152 Platform,
1153 Introduced.Version,
1154 Deprecated.Version,
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001155 Obsoleted.Version,
1156 IsUnavailable));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001157}
1158
Chandler Carruthedc2c642011-07-02 00:01:44 +00001159static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001160 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001161 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001162 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001163 return;
1164 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001165
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001166 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001167 Arg = Arg->IgnoreParenCasts();
1168 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001169
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001170 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001171 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001172 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001173 return;
1174 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001175
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001176 llvm::StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001177 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001178
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001179 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001180 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001181 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001182 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001183 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001184 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001185 else if (TypeStr == "protected")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001186 type = VisibilityAttr::Protected;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001187 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001188 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001189 return;
1190 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001191
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001192 D->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001193}
1194
Chandler Carruthedc2c642011-07-02 00:01:44 +00001195static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl,
1196 const AttributeList &Attr) {
John McCall86bc21f2011-03-02 11:33:24 +00001197 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1198 if (!method) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001199 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001200 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00001201 return;
1202 }
1203
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001204 if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) {
1205 if (!Attr.getParameterName() && Attr.getNumArgs() == 1) {
1206 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
John McCall86bc21f2011-03-02 11:33:24 +00001207 << "objc_method_family" << 1;
1208 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001209 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
John McCall86bc21f2011-03-02 11:33:24 +00001210 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001211 Attr.setInvalid();
John McCall86bc21f2011-03-02 11:33:24 +00001212 return;
1213 }
1214
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001215 llvm::StringRef param = Attr.getParameterName()->getName();
John McCall86bc21f2011-03-02 11:33:24 +00001216 ObjCMethodFamilyAttr::FamilyKind family;
1217 if (param == "none")
1218 family = ObjCMethodFamilyAttr::OMF_None;
1219 else if (param == "alloc")
1220 family = ObjCMethodFamilyAttr::OMF_alloc;
1221 else if (param == "copy")
1222 family = ObjCMethodFamilyAttr::OMF_copy;
1223 else if (param == "init")
1224 family = ObjCMethodFamilyAttr::OMF_init;
1225 else if (param == "mutableCopy")
1226 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1227 else if (param == "new")
1228 family = ObjCMethodFamilyAttr::OMF_new;
1229 else {
1230 // Just warn and ignore it. This is future-proof against new
1231 // families being used in system headers.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001232 S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family);
John McCall86bc21f2011-03-02 11:33:24 +00001233 return;
1234 }
1235
John McCall31168b02011-06-15 23:02:42 +00001236 if (family == ObjCMethodFamilyAttr::OMF_init &&
1237 !method->getResultType()->isObjCObjectPointerType()) {
1238 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1239 << method->getResultType();
1240 // Ignore the attribute.
1241 return;
1242 }
1243
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001244 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getLoc(),
John McCall31168b02011-06-15 23:02:42 +00001245 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00001246}
1247
Chandler Carruthedc2c642011-07-02 00:01:44 +00001248static void handleObjCExceptionAttr(Sema &S, Decl *D,
1249 const AttributeList &Attr) {
Chris Lattner677a3582009-02-14 08:09:34 +00001250 if (Attr.getNumArgs() != 0) {
1251 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1252 return;
1253 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001254
Chris Lattner677a3582009-02-14 08:09:34 +00001255 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1256 if (OCI == 0) {
1257 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1258 return;
1259 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001260
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001261 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001262}
1263
Chandler Carruthedc2c642011-07-02 00:01:44 +00001264static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001265 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001266 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001267 return;
1268 }
Richard Smithdda56e42011-04-15 14:24:37 +00001269 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001270 QualType T = TD->getUnderlyingType();
1271 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001272 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001273 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1274 return;
1275 }
1276 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001277 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001278}
1279
Mike Stumpd3bb5572009-07-24 19:02:52 +00001280static void
Chandler Carruthedc2c642011-07-02 00:01:44 +00001281handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001282 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001283 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001284 return;
1285 }
1286
1287 if (!isa<FunctionDecl>(D)) {
1288 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1289 return;
1290 }
1291
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001292 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001293}
1294
Chandler Carruthedc2c642011-07-02 00:01:44 +00001295static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001296 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001297 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001298 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001299 return;
1300 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001301
Steve Naroff3405a732008-09-18 16:44:58 +00001302 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001304 return;
1305 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001306
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001307 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001308 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001309 type = BlocksAttr::ByRef;
1310 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001311 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001312 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001313 return;
1314 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001315
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001316 D->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001317}
1318
Chandler Carruthedc2c642011-07-02 00:01:44 +00001319static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001320 // check the attribute arguments.
1321 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00001322 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00001323 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001324 }
1325
Anders Carlssonc181b012008-10-05 18:05:59 +00001326 int sentinel = 0;
1327 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001328 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00001329 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001330 if (E->isTypeDependent() || E->isValueDependent() ||
1331 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001332 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001333 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001334 return;
1335 }
1336 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001337
Anders Carlssonc181b012008-10-05 18:05:59 +00001338 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001339 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1340 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001341 return;
1342 }
1343 }
1344
1345 int nullPos = 0;
1346 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001347 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00001348 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001349 if (E->isTypeDependent() || E->isValueDependent() ||
1350 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001351 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001352 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001353 return;
1354 }
1355 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001356
Anders Carlssonc181b012008-10-05 18:05:59 +00001357 if (nullPos > 1 || nullPos < 0) {
1358 // FIXME: This error message could be improved, it would be nice
1359 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001360 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1361 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001362 return;
1363 }
1364 }
1365
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001366 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
John McCall9dd450b2009-09-21 23:43:11 +00001367 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001368 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001369
Chris Lattner9363e312009-03-17 23:03:47 +00001370 if (isa<FunctionNoProtoType>(FT)) {
1371 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1372 return;
1373 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001374
Chris Lattner9363e312009-03-17 23:03:47 +00001375 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001376 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001377 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001378 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001379 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
Anders Carlssonc181b012008-10-05 18:05:59 +00001380 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001381 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001382 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001383 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001384 } else if (isa<BlockDecl>(D)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001385 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1386 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001387 ;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001388 } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001389 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001390 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001391 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D)
Eric Christopherbc638a82010-12-01 22:13:54 +00001392 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001393 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001394 int m = Ty->isFunctionPointerType() ? 0 : 1;
1395 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001396 return;
1397 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001398 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001399 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001400 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001401 return;
1402 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001403 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001404 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001405 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00001406 return;
1407 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001408 D->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
Eric Christopherbc638a82010-12-01 22:13:54 +00001409 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001410}
1411
Chandler Carruthedc2c642011-07-02 00:01:44 +00001412static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner237f2752009-02-14 07:37:35 +00001413 // check the attribute arguments.
1414 if (Attr.getNumArgs() != 0) {
1415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1416 return;
1417 }
1418
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001419 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001420 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001421 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00001422 return;
1423 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001424
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001425 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1426 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1427 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001428 return;
1429 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001430 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1431 if (MD->getResultType()->isVoidType()) {
1432 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1433 << Attr.getName() << 1;
1434 return;
1435 }
1436
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001437 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001438}
1439
Chandler Carruthedc2c642011-07-02 00:01:44 +00001440static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001441 // check the attribute arguments.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001442 if (Attr.hasParameterOrArguments()) {
1443 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001444 return;
1445 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001446
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001447 if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) {
1448 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1449 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001450 return;
1451 }
1452
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001453 NamedDecl *nd = cast<NamedDecl>(D);
John McCall7a198ce2011-02-08 22:35:49 +00001454
1455 // 'weak' only applies to declarations with external linkage.
1456 if (hasEffectivelyInternalLinkage(nd)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001457 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001458 return;
1459 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001460
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001461 nd->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001462}
1463
Chandler Carruthedc2c642011-07-02 00:01:44 +00001464static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001465 // check the attribute arguments.
1466 if (Attr.getNumArgs() != 0) {
1467 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1468 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001469 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001470
1471 // weak_import only applies to variable & function declarations.
1472 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001473 if (!D->canBeWeakImported(isDef)) {
1474 if (isDef)
1475 S.Diag(Attr.getLoc(),
1476 diag::warn_attribute_weak_import_invalid_on_definition)
1477 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00001478 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00001479 (S.Context.Target.getTriple().isOSDarwin() &&
Douglas Gregord71149a2011-03-23 13:27:51 +00001480 isa<ObjCInterfaceDecl>(D))) {
1481 // Nothing to warn about here.
1482 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001483 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001484 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001485
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001486 return;
1487 }
1488
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001489 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001490}
1491
Chandler Carruthedc2c642011-07-02 00:01:44 +00001492static void handleReqdWorkGroupSize(Sema &S, Decl *D,
1493 const AttributeList &Attr) {
Nate Begemanf2758702009-06-26 06:32:41 +00001494 // Attribute has 3 arguments.
1495 if (Attr.getNumArgs() != 3) {
Chandler Carruthc91f09d2011-06-30 08:14:54 +00001496 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Nate Begemanf2758702009-06-26 06:32:41 +00001497 return;
1498 }
1499
1500 unsigned WGSize[3];
1501 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001502 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00001503 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001504 if (E->isTypeDependent() || E->isValueDependent() ||
1505 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001506 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1507 << "reqd_work_group_size" << E->getSourceRange();
1508 return;
1509 }
1510 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1511 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001512 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1513 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001514 WGSize[2]));
1515}
1516
Chandler Carruthedc2c642011-07-02 00:01:44 +00001517static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001518 // Attribute has no arguments.
1519 if (Attr.getNumArgs() != 1) {
1520 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1521 return;
1522 }
1523
1524 // Make sure that there is a string literal as the sections's single
1525 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001526 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001527 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001528 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001529 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001530 return;
1531 }
Mike Stump11289f42009-09-09 15:08:12 +00001532
Chris Lattner30ba6742009-08-10 19:03:04 +00001533 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001534 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001535 if (!Error.empty()) {
1536 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1537 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001538 return;
1539 }
Mike Stump11289f42009-09-09 15:08:12 +00001540
Chris Lattner20aee9b2010-01-12 20:58:53 +00001541 // This attribute cannot be applied to local variables.
1542 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1543 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1544 return;
1545 }
1546
Eric Christopherbc638a82010-12-01 22:13:54 +00001547 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1548 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001549}
1550
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001551
Chandler Carruthedc2c642011-07-02 00:01:44 +00001552static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001553 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001554 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001555 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001556 return;
1557 }
Douglas Gregor88336832011-06-15 05:45:11 +00001558
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001559 if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00001560 if (Existing->getLocation().isInvalid())
1561 Existing->setLocation(Attr.getLoc());
1562 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001563 D->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00001564 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001565}
1566
Chandler Carruthedc2c642011-07-02 00:01:44 +00001567static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00001568 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001569 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001570 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001571 return;
1572 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001573
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001574 if (ConstAttr *Existing = D->getAttr<ConstAttr>()) {
Douglas Gregor88336832011-06-15 05:45:11 +00001575 if (Existing->getLocation().isInvalid())
1576 Existing->setLocation(Attr.getLoc());
1577 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001578 D->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Douglas Gregor88336832011-06-15 05:45:11 +00001579 }
Anders Carlssonb8316282008-10-05 23:32:53 +00001580}
1581
Chandler Carruthedc2c642011-07-02 00:01:44 +00001582static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlssonb8316282008-10-05 23:32:53 +00001583 // check the attribute arguments.
1584 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001585 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001586 return;
1587 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001588
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001589 D->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001590}
1591
Chandler Carruthedc2c642011-07-02 00:01:44 +00001592static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001593 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001594 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1595 return;
1596 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001597
Anders Carlssond277d792009-01-31 01:16:18 +00001598 if (Attr.getNumArgs() != 0) {
1599 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1600 return;
1601 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001602
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001603 VarDecl *VD = dyn_cast<VarDecl>(D);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001604
Anders Carlssond277d792009-01-31 01:16:18 +00001605 if (!VD || !VD->hasLocalStorage()) {
1606 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1607 return;
1608 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001609
Anders Carlssond277d792009-01-31 01:16:18 +00001610 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001611 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00001612 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001613 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1614 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001615 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001616 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001617 Attr.getParameterName();
1618 return;
1619 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001620
Anders Carlssond277d792009-01-31 01:16:18 +00001621 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1622 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001623 S.Diag(Attr.getParameterLoc(),
1624 diag::err_attribute_cleanup_arg_not_function)
1625 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001626 return;
1627 }
1628
Anders Carlssond277d792009-01-31 01:16:18 +00001629 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001630 S.Diag(Attr.getParameterLoc(),
1631 diag::err_attribute_cleanup_func_must_take_one_arg)
1632 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001633 return;
1634 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001635
Anders Carlsson723f55d2009-02-07 23:16:50 +00001636 // We're currently more strict than GCC about what function types we accept.
1637 // If this ever proves to be a problem it should be easy to fix.
1638 QualType Ty = S.Context.getPointerType(VD->getType());
1639 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00001640 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1641 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001642 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001643 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1644 Attr.getParameterName() << ParamTy << Ty;
1645 return;
1646 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001647
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001648 D->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidise88168a2010-12-06 17:51:53 +00001649 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00001650}
1651
Mike Stumpd3bb5572009-07-24 19:02:52 +00001652/// Handle __attribute__((format_arg((idx)))) attribute based on
1653/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00001654static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001655 if (Attr.getNumArgs() != 1) {
1656 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1657 return;
1658 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001659 if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001660 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001661 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001662 return;
1663 }
Chandler Carruth743682b2010-11-16 08:35:43 +00001664
1665 // In C++ the implicit 'this' function parameter also counts, and they are
1666 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001667 bool HasImplicitThisParam = isInstanceMethod(D);
1668 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001669 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00001670
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001671 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001672 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001673 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001674 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1675 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001676 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1677 << "format" << 2 << IdxExpr->getSourceRange();
1678 return;
1679 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001680
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001681 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1682 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1683 << "format" << 2 << IdxExpr->getSourceRange();
1684 return;
1685 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001686
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001687 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001688
Chandler Carruth743682b2010-11-16 08:35:43 +00001689 if (HasImplicitThisParam) {
1690 if (ArgIdx == 0) {
1691 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1692 << "format_arg" << IdxExpr->getSourceRange();
1693 return;
1694 }
1695 ArgIdx--;
1696 }
1697
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001698 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001699 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001700
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001701 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1702 if (not_nsstring_type &&
1703 !isCFStringType(Ty, S.Context) &&
1704 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001705 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001706 // FIXME: Should highlight the actual expression that has the wrong type.
1707 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001708 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001709 << IdxExpr->getSourceRange();
1710 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001711 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001712 Ty = getFunctionOrMethodResultType(D);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001713 if (!isNSStringType(Ty, S.Context) &&
1714 !isCFStringType(Ty, S.Context) &&
1715 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001716 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001717 // FIXME: Should highlight the actual expression that has the wrong type.
1718 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001719 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001720 << IdxExpr->getSourceRange();
1721 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001722 }
1723
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001724 D->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
Chandler Carruth743682b2010-11-16 08:35:43 +00001725 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001726}
1727
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001728enum FormatAttrKind {
1729 CFStringFormat,
1730 NSStringFormat,
1731 StrftimeFormat,
1732 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001733 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001734 InvalidFormat
1735};
1736
1737/// getFormatAttrKind - Map from format attribute names to supported format
1738/// types.
1739static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1740 // Check for formats that get handled specially.
1741 if (Format == "NSString")
1742 return NSStringFormat;
1743 if (Format == "CFString")
1744 return CFStringFormat;
1745 if (Format == "strftime")
1746 return StrftimeFormat;
1747
1748 // Otherwise, check for supported formats.
1749 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1750 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1751 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattner0ddd0ae2011-02-18 17:05:55 +00001752 Format == "zcmn_err" ||
1753 Format == "kprintf") // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001754 return SupportedFormat;
1755
Duncan Sandsde4fe352010-03-23 14:44:19 +00001756 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1757 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001758 return IgnoredFormat;
1759
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001760 return InvalidFormat;
1761}
1762
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001763/// Handle __attribute__((init_priority(priority))) attributes based on
1764/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00001765static void handleInitPriorityAttr(Sema &S, Decl *D,
1766 const AttributeList &Attr) {
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001767 if (!S.getLangOptions().CPlusPlus) {
1768 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1769 return;
1770 }
1771
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001772 if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) {
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001773 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1774 Attr.setInvalid();
1775 return;
1776 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001777 QualType T = dyn_cast<VarDecl>(D)->getType();
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001778 if (S.Context.getAsArrayType(T))
1779 T = S.Context.getBaseElementType(T);
1780 if (!T->getAs<RecordType>()) {
1781 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1782 Attr.setInvalid();
1783 return;
1784 }
1785
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001786 if (Attr.getNumArgs() != 1) {
1787 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1788 Attr.setInvalid();
1789 return;
1790 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001791 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001792
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001793 llvm::APSInt priority(32);
1794 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1795 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1796 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1797 << "init_priority" << priorityExpr->getSourceRange();
1798 Attr.setInvalid();
1799 return;
1800 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00001801 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001802 if (prioritynum < 101 || prioritynum > 65535) {
1803 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1804 << priorityExpr->getSourceRange();
1805 Attr.setInvalid();
1806 return;
1807 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001808 D->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00001809 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001810}
1811
Mike Stumpd3bb5572009-07-24 19:02:52 +00001812/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1813/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chandler Carruthedc2c642011-07-02 00:01:44 +00001814static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001815
Chris Lattner4a927cb2008-06-28 23:36:30 +00001816 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001817 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001818 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001819 return;
1820 }
1821
Chris Lattner4a927cb2008-06-28 23:36:30 +00001822 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001823 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001824 return;
1825 }
1826
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001827 if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001828 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001829 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001830 return;
1831 }
1832
Chandler Carruth743682b2010-11-16 08:35:43 +00001833 // In C++ the implicit 'this' function parameter also counts, and they are
1834 // counted from one.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001835 bool HasImplicitThisParam = isInstanceMethod(D);
1836 unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001837 unsigned FirstIdx = 1;
1838
Daniel Dunbar07d07852009-10-18 21:17:35 +00001839 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001840
1841 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001842 if (Format.startswith("__") && Format.endswith("__"))
1843 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001844
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001845 // Check for supported formats.
1846 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001847
1848 if (Kind == IgnoredFormat)
1849 return;
1850
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001851 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001852 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001853 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001854 return;
1855 }
1856
1857 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001858 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001859 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001860 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1861 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001862 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001863 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001864 return;
1865 }
1866
1867 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001868 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001869 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001870 return;
1871 }
1872
1873 // FIXME: Do we need to bounds check?
1874 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001875
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001876 if (HasImplicitThisParam) {
1877 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00001878 S.Diag(Attr.getLoc(),
1879 diag::err_format_attribute_implicit_this_format_string)
1880 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001881 return;
1882 }
1883 ArgIdx--;
1884 }
Mike Stump11289f42009-09-09 15:08:12 +00001885
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001886 // make sure the format string is really a string
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001887 QualType Ty = getFunctionOrMethodArgType(D, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001888
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001889 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001890 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001891 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1892 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001893 return;
1894 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001895 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001896 // FIXME: do we need to check if the type is NSString*? What are the
1897 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001898 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001899 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001900 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1901 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001902 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001903 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001904 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001905 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001906 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001907 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1908 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001909 return;
1910 }
1911
1912 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001913 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001914 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001915 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1916 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001917 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001918 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001919 return;
1920 }
1921
1922 // check if the function is variadic if the 3rd argument non-zero
1923 if (FirstArg != 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001924 if (isFunctionOrMethodVariadic(D)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001925 ++NumArgs; // +1 for ...
1926 } else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001927 S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001928 return;
1929 }
1930 }
1931
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001932 // strftime requires FirstArg to be 0 because it doesn't read from any
1933 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001934 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001935 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001936 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1937 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001938 return;
1939 }
1940 // if 0 it disables parameter checking (to use with e.g. va_list)
1941 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001942 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001943 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001944 return;
1945 }
1946
Douglas Gregor88336832011-06-15 05:45:11 +00001947 // Check whether we already have an equivalent format attribute.
1948 for (specific_attr_iterator<FormatAttr>
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001949 i = D->specific_attr_begin<FormatAttr>(),
1950 e = D->specific_attr_end<FormatAttr>();
Douglas Gregor88336832011-06-15 05:45:11 +00001951 i != e ; ++i) {
1952 FormatAttr *f = *i;
1953 if (f->getType() == Format &&
1954 f->getFormatIdx() == (int)Idx.getZExtValue() &&
1955 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
1956 // If we don't have a valid location for this attribute, adopt the
1957 // location.
1958 if (f->getLocation().isInvalid())
1959 f->setLocation(Attr.getLoc());
1960 return;
1961 }
1962 }
1963
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001964 D->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001965 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001966 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001967}
1968
Chandler Carruthedc2c642011-07-02 00:01:44 +00001969static void handleTransparentUnionAttr(Sema &S, Decl *D,
1970 const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001971 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001972 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001973 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001974 return;
1975 }
1976
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001977 // Try to find the underlying union declaration.
1978 RecordDecl *RD = 0;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001979 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001980 if (TD && TD->getUnderlyingType()->isUnionType())
1981 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1982 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00001983 RD = dyn_cast<RecordDecl>(D);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001984
1985 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001986 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001987 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001988 return;
1989 }
1990
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001991 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001992 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001993 diag::warn_transparent_union_attribute_not_definition);
1994 return;
1995 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001996
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001997 RecordDecl::field_iterator Field = RD->field_begin(),
1998 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001999 if (Field == FieldEnd) {
2000 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
2001 return;
2002 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002003
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002004 FieldDecl *FirstField = *Field;
2005 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00002006 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00002007 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00002008 diag::warn_transparent_union_attribute_floating)
2009 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002010 return;
2011 }
2012
2013 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
2014 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2015 for (; Field != FieldEnd; ++Field) {
2016 QualType FieldType = Field->getType();
2017 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2018 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2019 // Warn if we drop the attribute.
2020 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002021 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002022 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002023 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002024 diag::warn_transparent_union_attribute_field_size_align)
2025 << isSize << Field->getDeclName() << FieldBits;
2026 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002027 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002028 diag::note_transparent_union_first_field_size_align)
2029 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002030 return;
2031 }
2032 }
2033
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002034 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002035}
2036
Chandler Carruthedc2c642011-07-02 00:01:44 +00002037static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002038 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002039 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002040 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002041 return;
2042 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002043 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002044 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002045
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002046 // Make sure that there is a string literal as the annotation's single
2047 // argument.
2048 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002049 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002050 return;
2051 }
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002052 D->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
Eric Christopherbc638a82010-12-01 22:13:54 +00002053 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002054}
2055
Chandler Carruthedc2c642011-07-02 00:01:44 +00002056static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002057 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002058 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002059 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002060 return;
2061 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002062
2063 //FIXME: The C++0x version of this attribute has more limited applicabilty
2064 // than GNU's, and should error out when it is used to specify a
2065 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002066
Chris Lattner4a927cb2008-06-28 23:36:30 +00002067 if (Attr.getNumArgs() == 0) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002068 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002069 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002070 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002071
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002072 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002073}
2074
2075void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2076 if (E->isTypeDependent() || E->isValueDependent()) {
2077 // Save dependent expressions in the AST to be instantiated.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002078 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002079 return;
2080 }
2081
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002082 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002083 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002084 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2085 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2086 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00002087 return;
2088 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002089 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002090 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2091 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002092 return;
2093 }
2094
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002095 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2096}
2097
2098void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2099 // FIXME: Cache the number on the Attr object if non-dependent?
2100 // FIXME: Perform checking of type validity
2101 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2102 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002103}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002104
Chandler Carruth3ed22c32011-07-01 23:49:16 +00002105/// handleModeAttr - This attribute modifies the width of a decl with primitive
Mike Stumpd3bb5572009-07-24 19:02:52 +00002106/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002107///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002108/// Despite what would be logical, the mode attribute is a decl attribute, not a
2109/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2110/// HImode, not an intermediate pointer.
Chandler Carruthedc2c642011-07-02 00:01:44 +00002111static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002112 // This attribute isn't documented, but glibc uses it. It changes
2113 // the width of an int or unsigned int to the specified size.
2114
2115 // Check that there aren't any arguments
2116 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002117 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002118 return;
2119 }
2120
2121 IdentifierInfo *Name = Attr.getParameterName();
2122 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002123 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002124 return;
2125 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002126
Daniel Dunbar07d07852009-10-18 21:17:35 +00002127 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002128
2129 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002130 if (Str.startswith("__") && Str.endswith("__"))
2131 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002132
2133 unsigned DestWidth = 0;
2134 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002135 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002136 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002137 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002138 switch (Str[0]) {
2139 case 'Q': DestWidth = 8; break;
2140 case 'H': DestWidth = 16; break;
2141 case 'S': DestWidth = 32; break;
2142 case 'D': DestWidth = 64; break;
2143 case 'X': DestWidth = 96; break;
2144 case 'T': DestWidth = 128; break;
2145 }
2146 if (Str[1] == 'F') {
2147 IntegerMode = false;
2148 } else if (Str[1] == 'C') {
2149 IntegerMode = false;
2150 ComplexMode = true;
2151 } else if (Str[1] != 'I') {
2152 DestWidth = 0;
2153 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002154 break;
2155 case 4:
2156 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2157 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002158 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002159 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002160 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002161 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002162 break;
2163 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002164 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002165 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002166 break;
2167 }
2168
2169 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002170 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002171 OldTy = TD->getUnderlyingType();
2172 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2173 OldTy = VD->getType();
2174 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002175 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2176 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00002177 return;
2178 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002179
John McCall9dd450b2009-09-21 23:43:11 +00002180 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002181 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2182 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002183 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002184 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2185 } else if (ComplexMode) {
2186 if (!OldTy->isComplexType())
2187 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2188 } else {
2189 if (!OldTy->isFloatingType())
2190 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2191 }
2192
Mike Stump87c57ac2009-05-16 07:39:55 +00002193 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2194 // and friends, at least with glibc.
2195 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2196 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002197 // FIXME: Make sure floating-point mappings are accurate
2198 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002199 QualType NewTy;
2200 switch (DestWidth) {
2201 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002202 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002203 return;
2204 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002205 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002206 return;
2207 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002208 if (!IntegerMode) {
2209 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2210 return;
2211 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002212 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002213 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002214 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002215 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002216 break;
2217 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002218 if (!IntegerMode) {
2219 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2220 return;
2221 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002222 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002223 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002224 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002225 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002226 break;
2227 case 32:
2228 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002229 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002230 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002231 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002232 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002233 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002234 break;
2235 case 64:
2236 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002237 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002238 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00002239 if (S.Context.Target.getLongWidth() == 64)
2240 NewTy = S.Context.LongTy;
2241 else
2242 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002243 else
Chandler Carruth72343702010-01-26 06:39:24 +00002244 if (S.Context.Target.getLongWidth() == 64)
2245 NewTy = S.Context.UnsignedLongTy;
2246 else
2247 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002248 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002249 case 96:
2250 NewTy = S.Context.LongDoubleTy;
2251 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002252 case 128:
2253 if (!IntegerMode) {
2254 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2255 return;
2256 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002257 if (OldTy->isSignedIntegerType())
2258 NewTy = S.Context.Int128Ty;
2259 else
2260 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002261 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002262 }
2263
Eli Friedman4735374e2009-03-03 06:41:03 +00002264 if (ComplexMode) {
2265 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002266 }
2267
2268 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00002269 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00002270 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00002271 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00002272 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002273 cast<ValueDecl>(D)->setType(NewTy);
2274}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002275
Chandler Carruthedc2c642011-07-02 00:01:44 +00002276static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002277 // check the attribute arguments.
2278 if (Attr.getNumArgs() > 0) {
2279 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2280 return;
2281 }
Anders Carlsson63784f42009-02-13 08:11:52 +00002282
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002283 if (!isFunctionOrMethod(D)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002284 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002285 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00002286 return;
2287 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002288
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002289 D->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002290}
2291
Chandler Carruthedc2c642011-07-02 00:01:44 +00002292static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Anders Carlsson88097122009-02-19 19:16:48 +00002293 // check the attribute arguments.
2294 if (Attr.getNumArgs() != 0) {
2295 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2296 return;
2297 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002298
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002299 if (!isa<FunctionDecl>(D)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002301 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00002302 return;
2303 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002304
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002305 D->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002306}
2307
Chandler Carruthedc2c642011-07-02 00:01:44 +00002308static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D,
2309 const AttributeList &Attr) {
Chris Lattner3c77a352010-06-22 00:03:40 +00002310 // check the attribute arguments.
2311 if (Attr.getNumArgs() != 0) {
2312 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2313 return;
2314 }
2315
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002316 if (!isa<FunctionDecl>(D)) {
Chris Lattner3c77a352010-06-22 00:03:40 +00002317 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002318 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00002319 return;
2320 }
2321
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002322 D->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002323 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002324}
2325
Chandler Carruthedc2c642011-07-02 00:01:44 +00002326static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002327 if (S.LangOpts.CUDA) {
2328 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002329 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002330 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2331 return;
2332 }
2333
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002334 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002335 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002336 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002337 return;
2338 }
2339
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002340 D->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002341 } else {
2342 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2343 }
2344}
2345
Chandler Carruthedc2c642011-07-02 00:01:44 +00002346static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002347 if (S.LangOpts.CUDA) {
2348 // check the attribute arguments.
2349 if (Attr.getNumArgs() != 0) {
2350 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2351 return;
2352 }
2353
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002354 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002355 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002356 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002357 return;
2358 }
2359
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002360 D->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002361 } else {
2362 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2363 }
2364}
2365
Chandler Carruthedc2c642011-07-02 00:01:44 +00002366static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002367 if (S.LangOpts.CUDA) {
2368 // check the attribute arguments.
2369 if (Attr.getNumArgs() != 0) {
2370 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2371 return;
2372 }
2373
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002374 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002375 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002376 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002377 return;
2378 }
2379
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002380 FunctionDecl *FD = cast<FunctionDecl>(D);
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002381 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00002382 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002383 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2384 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2385 << FD->getType()
2386 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2387 "void");
2388 } else {
2389 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2390 << FD->getType();
2391 }
2392 return;
2393 }
2394
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002395 D->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002396 } else {
2397 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2398 }
2399}
2400
Chandler Carruthedc2c642011-07-02 00:01:44 +00002401static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002402 if (S.LangOpts.CUDA) {
2403 // check the attribute arguments.
2404 if (Attr.getNumArgs() != 0) {
2405 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2406 return;
2407 }
2408
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002409 if (!isa<FunctionDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002410 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002411 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002412 return;
2413 }
2414
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002415 D->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002416 } else {
2417 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2418 }
2419}
2420
Chandler Carruthedc2c642011-07-02 00:01:44 +00002421static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002422 if (S.LangOpts.CUDA) {
2423 // check the attribute arguments.
2424 if (Attr.getNumArgs() != 0) {
2425 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2426 return;
2427 }
2428
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002429 if (!isa<VarDecl>(D)) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002430 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002431 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002432 return;
2433 }
2434
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002435 D->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002436 } else {
2437 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2438 }
2439}
2440
Chandler Carruthedc2c642011-07-02 00:01:44 +00002441static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002442 // check the attribute arguments.
2443 if (Attr.getNumArgs() != 0) {
2444 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2445 return;
2446 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002447
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002448 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
Chris Lattner4225e232009-04-14 17:02:11 +00002449 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002450 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002451 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00002452 return;
2453 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002454
Douglas Gregor35b57532009-10-27 21:01:01 +00002455 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002456 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002457 return;
2458 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002459
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002460 D->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002461}
2462
Chandler Carruthedc2c642011-07-02 00:01:44 +00002463static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002464 if (hasDeclarator(D)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002465
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002466 // Diagnostic is emitted elsewhere: here we store the (valid) Attr
John McCall3882ace2011-01-05 12:14:39 +00002467 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2468 CallingConv CC;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002469 if (S.CheckCallingConvAttr(Attr, CC))
John McCall3882ace2011-01-05 12:14:39 +00002470 return;
2471
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002472 if (!isa<ObjCMethodDecl>(D)) {
2473 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2474 << Attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002475 return;
2476 }
2477
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002478 switch (Attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002479 case AttributeList::AT_fastcall:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002480 D->addAttr(::new (S.Context) FastCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002481 return;
2482 case AttributeList::AT_stdcall:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002483 D->addAttr(::new (S.Context) StdCallAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002484 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00002485 case AttributeList::AT_thiscall:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002486 D->addAttr(::new (S.Context) ThisCallAttr(Attr.getLoc(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00002487 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002488 case AttributeList::AT_cdecl:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002489 D->addAttr(::new (S.Context) CDeclAttr(Attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002490 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002491 case AttributeList::AT_pascal:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002492 D->addAttr(::new (S.Context) PascalAttr(Attr.getLoc(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00002493 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002494 case AttributeList::AT_pcs: {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002495 Expr *Arg = Attr.getArg(0);
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002496 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2497 if (Str == 0 || Str->isWide()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002498 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002499 << "pcs" << 1;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002500 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002501 return;
2502 }
2503
2504 llvm::StringRef StrRef = Str->getString();
2505 PcsAttr::PCSType PCS;
2506 if (StrRef == "aapcs")
2507 PCS = PcsAttr::AAPCS;
2508 else if (StrRef == "aapcs-vfp")
2509 PCS = PcsAttr::AAPCS_VFP;
2510 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002511 S.Diag(Attr.getLoc(), diag::err_invalid_pcs);
2512 Attr.setInvalid();
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002513 return;
2514 }
2515
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002516 D->addAttr(::new (S.Context) PcsAttr(Attr.getLoc(), S.Context, PCS));
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002517 }
Abramo Bagnara50099372010-04-30 13:10:51 +00002518 default:
2519 llvm_unreachable("unexpected attribute kind");
2520 return;
2521 }
2522}
2523
Chandler Carruthedc2c642011-07-02 00:01:44 +00002524static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002525 assert(Attr.isInvalid() == false);
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002526 D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002527}
2528
John McCall3882ace2011-01-05 12:14:39 +00002529bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2530 if (attr.isInvalid())
2531 return true;
2532
Ted Kremenek1551d552011-04-15 05:49:29 +00002533 if ((attr.getNumArgs() != 0 &&
2534 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2535 attr.getParameterName()) {
John McCall3882ace2011-01-05 12:14:39 +00002536 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2537 attr.setInvalid();
2538 return true;
2539 }
2540
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002541 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2542 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00002543 switch (attr.getKind()) {
2544 case AttributeList::AT_cdecl: CC = CC_C; break;
2545 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2546 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2547 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2548 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002549 case AttributeList::AT_pcs: {
2550 Expr *Arg = attr.getArg(0);
2551 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2552 if (Str == 0 || Str->isWide()) {
2553 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2554 << "pcs" << 1;
2555 attr.setInvalid();
2556 return true;
2557 }
2558
2559 llvm::StringRef StrRef = Str->getString();
2560 if (StrRef == "aapcs") {
2561 CC = CC_AAPCS;
2562 break;
2563 } else if (StrRef == "aapcs-vfp") {
2564 CC = CC_AAPCS_VFP;
2565 break;
2566 }
2567 // FALLS THROUGH
2568 }
John McCall3882ace2011-01-05 12:14:39 +00002569 default: llvm_unreachable("unexpected attribute kind"); return true;
2570 }
2571
2572 return false;
2573}
2574
Chandler Carruthedc2c642011-07-02 00:01:44 +00002575static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002576 if (hasDeclarator(D)) return;
John McCall3882ace2011-01-05 12:14:39 +00002577
2578 unsigned numParams;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002579 if (S.CheckRegparmAttr(Attr, numParams))
John McCall3882ace2011-01-05 12:14:39 +00002580 return;
2581
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002582 if (!isa<ObjCMethodDecl>(D)) {
2583 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
2584 << Attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002585 return;
2586 }
Eli Friedman7044b762009-03-27 21:06:47 +00002587
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002588 D->addAttr(::new (S.Context) RegparmAttr(Attr.getLoc(), S.Context, numParams));
John McCall3882ace2011-01-05 12:14:39 +00002589}
2590
2591/// Checks a regparm attribute, returning true if it is ill-formed and
2592/// otherwise setting numParams to the appropriate value.
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002593bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) {
2594 if (Attr.isInvalid())
John McCall3882ace2011-01-05 12:14:39 +00002595 return true;
2596
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002597 if (Attr.getNumArgs() != 1) {
2598 Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2599 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002600 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002601 }
Eli Friedman7044b762009-03-27 21:06:47 +00002602
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002603 Expr *NumParamsExpr = Attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00002604 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002605 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00002606 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002607 Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00002608 << "regparm" << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002609 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002610 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002611 }
2612
John McCall3882ace2011-01-05 12:14:39 +00002613 if (Context.Target.getRegParmMax() == 0) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002614 Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002615 << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002616 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002617 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002618 }
2619
John McCall3882ace2011-01-05 12:14:39 +00002620 numParams = NumParams.getZExtValue();
2621 if (numParams > Context.Target.getRegParmMax()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002622 Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
John McCall3882ace2011-01-05 12:14:39 +00002623 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002624 Attr.setInvalid();
John McCall3882ace2011-01-05 12:14:39 +00002625 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002626 }
2627
John McCall3882ace2011-01-05 12:14:39 +00002628 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002629}
2630
Chandler Carruthedc2c642011-07-02 00:01:44 +00002631static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){
Peter Collingbourne827301e2010-12-12 23:03:07 +00002632 if (S.LangOpts.CUDA) {
2633 // check the attribute arguments.
2634 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00002635 // FIXME: 0 is not okay.
2636 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002637 return;
2638 }
2639
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002640 if (!isFunctionOrMethod(D)) {
Peter Collingbourne827301e2010-12-12 23:03:07 +00002641 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002642 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002643 return;
2644 }
2645
2646 Expr *MaxThreadsExpr = Attr.getArg(0);
2647 llvm::APSInt MaxThreads(32);
2648 if (MaxThreadsExpr->isTypeDependent() ||
2649 MaxThreadsExpr->isValueDependent() ||
2650 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2651 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2652 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2653 return;
2654 }
2655
2656 llvm::APSInt MinBlocks(32);
2657 if (Attr.getNumArgs() > 1) {
2658 Expr *MinBlocksExpr = Attr.getArg(1);
2659 if (MinBlocksExpr->isTypeDependent() ||
2660 MinBlocksExpr->isValueDependent() ||
2661 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2662 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2663 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2664 return;
2665 }
2666 }
2667
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002668 D->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
Peter Collingbourne827301e2010-12-12 23:03:07 +00002669 MaxThreads.getZExtValue(),
2670 MinBlocks.getZExtValue()));
2671 } else {
2672 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2673 }
2674}
2675
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002676//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002677// Checker-specific attribute handlers.
2678//===----------------------------------------------------------------------===//
2679
John McCalled433932011-01-25 03:31:58 +00002680static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2681 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2682}
2683static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2684 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2685}
2686
Chandler Carruthedc2c642011-07-02 00:01:44 +00002687static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002688 ParmVarDecl *param = dyn_cast<ParmVarDecl>(D);
John McCalled433932011-01-25 03:31:58 +00002689 if (!param) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002690 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2691 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00002692 return;
2693 }
2694
2695 bool typeOK, cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002696 if (Attr.getKind() == AttributeList::AT_ns_consumed) {
John McCalled433932011-01-25 03:31:58 +00002697 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2698 cf = false;
2699 } else {
2700 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2701 cf = true;
2702 }
2703
2704 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002705 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2706 << SourceRange(Attr.getLoc()) << Attr.getName() << cf;
John McCalled433932011-01-25 03:31:58 +00002707 return;
2708 }
2709
2710 if (cf)
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002711 param->addAttr(::new (S.Context) CFConsumedAttr(Attr.getLoc(), S.Context));
John McCalled433932011-01-25 03:31:58 +00002712 else
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002713 param->addAttr(::new (S.Context) NSConsumedAttr(Attr.getLoc(), S.Context));
John McCalled433932011-01-25 03:31:58 +00002714}
2715
Chandler Carruthedc2c642011-07-02 00:01:44 +00002716static void handleNSConsumesSelfAttr(Sema &S, Decl *D,
2717 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002718 if (!isa<ObjCMethodDecl>(D)) {
2719 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2720 << SourceRange(Attr.getLoc()) << Attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00002721 return;
2722 }
2723
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002724 D->addAttr(::new (S.Context) NSConsumesSelfAttr(Attr.getLoc(), S.Context));
John McCalled433932011-01-25 03:31:58 +00002725}
2726
Chandler Carruthedc2c642011-07-02 00:01:44 +00002727static void handleNSReturnsRetainedAttr(Sema &S, Decl *D,
2728 const AttributeList &Attr) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002729
John McCalled433932011-01-25 03:31:58 +00002730 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002731
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002732 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
John McCalled433932011-01-25 03:31:58 +00002733 returnType = MD->getResultType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002734 else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D))
Fariborz Jahanianf4105f52011-06-25 00:17:46 +00002735 returnType = PD->getType();
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002736 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(D) &&
2737 (Attr.getKind() == AttributeList::AT_ns_returns_retained))
John McCall31168b02011-06-15 23:02:42 +00002738 return; // ignore: was handled as a type attribute
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002739 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
John McCalled433932011-01-25 03:31:58 +00002740 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00002741 else {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002742 S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type)
2743 << SourceRange(Attr.getLoc()) << Attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00002744 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002745 return;
2746 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002747
John McCalled433932011-01-25 03:31:58 +00002748 bool typeOK;
2749 bool cf;
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002750 switch (Attr.getKind()) {
John McCalled433932011-01-25 03:31:58 +00002751 default: llvm_unreachable("invalid ownership attribute"); return;
2752 case AttributeList::AT_ns_returns_autoreleased:
2753 case AttributeList::AT_ns_returns_retained:
2754 case AttributeList::AT_ns_returns_not_retained:
2755 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2756 cf = false;
2757 break;
2758
2759 case AttributeList::AT_cf_returns_retained:
2760 case AttributeList::AT_cf_returns_not_retained:
2761 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2762 cf = true;
2763 break;
2764 }
2765
2766 if (!typeOK) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002767 S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
2768 << SourceRange(Attr.getLoc())
2769 << Attr.getName() << isa<ObjCMethodDecl>(D) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002770 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00002771 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002772
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002773 switch (Attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002774 default:
2775 assert(0 && "invalid ownership attribute");
2776 return;
John McCalled433932011-01-25 03:31:58 +00002777 case AttributeList::AT_ns_returns_autoreleased:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002778 D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(Attr.getLoc(),
John McCalled433932011-01-25 03:31:58 +00002779 S.Context));
2780 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00002781 case AttributeList::AT_cf_returns_not_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002782 D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002783 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002784 return;
2785 case AttributeList::AT_ns_returns_not_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002786 D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002787 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002788 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002789 case AttributeList::AT_cf_returns_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002790 D->addAttr(::new (S.Context) CFReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002791 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002792 return;
2793 case AttributeList::AT_ns_returns_retained:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002794 D->addAttr(::new (S.Context) NSReturnsRetainedAttr(Attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002795 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002796 return;
2797 };
2798}
2799
Chandler Carruthedc2c642011-07-02 00:01:44 +00002800static void handleObjCOwnershipAttr(Sema &S, Decl *D,
2801 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002802 if (hasDeclarator(D)) return;
John McCall31168b02011-06-15 23:02:42 +00002803
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002804 SourceLocation L = Attr.getLoc();
2805 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
2806 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCall31168b02011-06-15 23:02:42 +00002807}
2808
Chandler Carruthedc2c642011-07-02 00:01:44 +00002809static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D,
2810 const AttributeList &Attr) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002811 if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) {
2812 SourceLocation L = Attr.getLoc();
2813 S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type)
2814 << SourceRange(L, L) << Attr.getName() << 12 /* variable */;
John McCall31168b02011-06-15 23:02:42 +00002815 return;
2816 }
2817
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002818 ValueDecl *vd = cast<ValueDecl>(D);
John McCall31168b02011-06-15 23:02:42 +00002819 QualType type = vd->getType();
2820
2821 if (!type->isDependentType() &&
2822 !type->isObjCLifetimeType()) {
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002823 S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
John McCall31168b02011-06-15 23:02:42 +00002824 << type;
2825 return;
2826 }
2827
2828 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
2829
2830 // If we have no lifetime yet, check the lifetime we're presumably
2831 // going to infer.
2832 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
2833 lifetime = type->getObjCARCImplicitLifetime();
2834
2835 switch (lifetime) {
2836 case Qualifiers::OCL_None:
2837 assert(type->isDependentType() &&
2838 "didn't infer lifetime for non-dependent type?");
2839 break;
2840
2841 case Qualifiers::OCL_Weak: // meaningful
2842 case Qualifiers::OCL_Strong: // meaningful
2843 break;
2844
2845 case Qualifiers::OCL_ExplicitNone:
2846 case Qualifiers::OCL_Autoreleasing:
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002847 S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
John McCall31168b02011-06-15 23:02:42 +00002848 << (lifetime == Qualifiers::OCL_Autoreleasing);
2849 break;
2850 }
2851
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002852 D->addAttr(::new (S.Context)
2853 ObjCPreciseLifetimeAttr(Attr.getLoc(), S.Context));
John McCall31168b02011-06-15 23:02:42 +00002854}
2855
Charles Davis163855f2010-02-16 18:27:26 +00002856static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2857 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Picheta83957a2010-12-19 06:50:37 +00002858 Attr.getKind() == AttributeList::AT_dllexport ||
2859 Attr.getKind() == AttributeList::AT_uuid;
2860}
2861
2862//===----------------------------------------------------------------------===//
2863// Microsoft specific attribute handlers.
2864//===----------------------------------------------------------------------===//
2865
Chandler Carruthedc2c642011-07-02 00:01:44 +00002866static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) {
Francois Picheta83957a2010-12-19 06:50:37 +00002867 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2868 // check the attribute arguments.
2869 if (Attr.getNumArgs() != 1) {
2870 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2871 return;
2872 }
2873 Expr *Arg = Attr.getArg(0);
2874 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichet7da11662010-12-20 01:41:49 +00002875 if (Str == 0 || Str->isWide()) {
2876 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2877 << "uuid" << 1;
2878 return;
2879 }
2880
2881 llvm::StringRef StrRef = Str->getString();
2882
2883 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2884 StrRef.back() == '}';
2885
2886 // Validate GUID length.
2887 if (IsCurly && StrRef.size() != 38) {
2888 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2889 return;
2890 }
2891 if (!IsCurly && StrRef.size() != 36) {
2892 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2893 return;
2894 }
2895
2896 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2897 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Anders Carlsson19588aa2011-01-23 21:07:30 +00002898 llvm::StringRef::iterator I = StrRef.begin();
2899 if (IsCurly) // Skip the optional '{'
2900 ++I;
2901
2902 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00002903 if (i == 8 || i == 13 || i == 18 || i == 23) {
2904 if (*I != '-') {
2905 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2906 return;
2907 }
2908 } else if (!isxdigit(*I)) {
2909 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2910 return;
2911 }
2912 I++;
2913 }
Francois Picheta83957a2010-12-19 06:50:37 +00002914
Chandler Carruthff4c4f02011-07-01 23:49:12 +00002915 D->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
Francois Picheta83957a2010-12-19 06:50:37 +00002916 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00002917 } else
Francois Picheta83957a2010-12-19 06:50:37 +00002918 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00002919}
2920
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002921//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002922// Top Level Sema Entry Points
2923//===----------------------------------------------------------------------===//
2924
Chandler Carruthedc2c642011-07-02 00:01:44 +00002925static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
2926 const AttributeList &Attr) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00002927 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00002928 case AttributeList::AT_device: handleDeviceAttr (S, D, Attr); break;
2929 case AttributeList::AT_host: handleHostAttr (S, D, Attr); break;
2930 case AttributeList::AT_overloadable:handleOverloadableAttr(S, D, Attr); break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00002931 default:
2932 break;
2933 }
2934}
Abramo Bagnara50099372010-04-30 13:10:51 +00002935
Chandler Carruthedc2c642011-07-02 00:01:44 +00002936static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D,
2937 const AttributeList &Attr) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002938 switch (Attr.getKind()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00002939 case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break;
2940 case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00002941 case AttributeList::AT_IBOutletCollection:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002942 handleIBOutletCollection(S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002943 case AttributeList::AT_address_space:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002944 case AttributeList::AT_opencl_image_access:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00002945 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00002946 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00002947 case AttributeList::AT_neon_vector_type:
2948 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002949 // Ignore these, these are type attributes, handled by
2950 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002951 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00002952 case AttributeList::AT_device:
2953 case AttributeList::AT_host:
2954 case AttributeList::AT_overloadable:
2955 // Ignore, this is a non-inheritable attribute, handled
2956 // by ProcessNonInheritableDeclAttr.
2957 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00002958 case AttributeList::AT_alias: handleAliasAttr (S, D, Attr); break;
2959 case AttributeList::AT_aligned: handleAlignedAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002960 case AttributeList::AT_always_inline:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002961 handleAlwaysInlineAttr (S, D, Attr); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002962 case AttributeList::AT_analyzer_noreturn:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002963 handleAnalyzerNoReturnAttr (S, D, Attr); break;
2964 case AttributeList::AT_annotate: handleAnnotateAttr (S, D, Attr); break;
2965 case AttributeList::AT_availability:handleAvailabilityAttr(S, D, Attr); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002966 case AttributeList::AT_carries_dependency:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002967 handleDependencyAttr (S, D, Attr); break;
2968 case AttributeList::AT_common: handleCommonAttr (S, D, Attr); break;
2969 case AttributeList::AT_constant: handleConstantAttr (S, D, Attr); break;
2970 case AttributeList::AT_constructor: handleConstructorAttr (S, D, Attr); break;
2971 case AttributeList::AT_deprecated: handleDeprecatedAttr (S, D, Attr); break;
2972 case AttributeList::AT_destructor: handleDestructorAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002973 case AttributeList::AT_ext_vector_type:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002974 handleExtVectorTypeAttr(S, scope, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002975 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00002976 case AttributeList::AT_format: handleFormatAttr (S, D, Attr); break;
2977 case AttributeList::AT_format_arg: handleFormatArgAttr (S, D, Attr); break;
2978 case AttributeList::AT_global: handleGlobalAttr (S, D, Attr); break;
2979 case AttributeList::AT_gnu_inline: handleGNUInlineAttr (S, D, Attr); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002980 case AttributeList::AT_launch_bounds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002981 handleLaunchBoundsAttr(S, D, Attr);
Peter Collingbourne827301e2010-12-12 23:03:07 +00002982 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00002983 case AttributeList::AT_mode: handleModeAttr (S, D, Attr); break;
2984 case AttributeList::AT_malloc: handleMallocAttr (S, D, Attr); break;
2985 case AttributeList::AT_may_alias: handleMayAliasAttr (S, D, Attr); break;
2986 case AttributeList::AT_nocommon: handleNoCommonAttr (S, D, Attr); break;
2987 case AttributeList::AT_nonnull: handleNonNullAttr (S, D, Attr); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00002988 case AttributeList::AT_ownership_returns:
2989 case AttributeList::AT_ownership_takes:
2990 case AttributeList::AT_ownership_holds:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002991 handleOwnershipAttr (S, D, Attr); break;
2992 case AttributeList::AT_naked: handleNakedAttr (S, D, Attr); break;
2993 case AttributeList::AT_noreturn: handleNoReturnAttr (S, D, Attr); break;
2994 case AttributeList::AT_nothrow: handleNothrowAttr (S, D, Attr); break;
2995 case AttributeList::AT_shared: handleSharedAttr (S, D, Attr); break;
2996 case AttributeList::AT_vecreturn: handleVecReturnAttr (S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002997
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00002998 case AttributeList::AT_objc_ownership:
Chandler Carruthedc2c642011-07-02 00:01:44 +00002999 handleObjCOwnershipAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003000 case AttributeList::AT_objc_precise_lifetime:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003001 handleObjCPreciseLifetimeAttr(S, D, Attr); break;
John McCall31168b02011-06-15 23:02:42 +00003002
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003003 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00003004 case AttributeList::AT_cf_consumed:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003005 case AttributeList::AT_ns_consumed: handleNSConsumedAttr (S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003006 case AttributeList::AT_ns_consumes_self:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003007 handleNSConsumesSelfAttr(S, D, Attr); break;
John McCalled433932011-01-25 03:31:58 +00003008
3009 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00003010 case AttributeList::AT_ns_returns_not_retained:
3011 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003012 case AttributeList::AT_ns_returns_retained:
3013 case AttributeList::AT_cf_returns_retained:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003014 handleNSReturnsRetainedAttr(S, D, Attr); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00003015
Nate Begemanf2758702009-06-26 06:32:41 +00003016 case AttributeList::AT_reqd_wg_size:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003017 handleReqdWorkGroupSize(S, D, Attr); break;
Nate Begemanf2758702009-06-26 06:32:41 +00003018
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003019 case AttributeList::AT_init_priority:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003020 handleInitPriorityAttr(S, D, Attr); break;
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003021
Chandler Carruthedc2c642011-07-02 00:01:44 +00003022 case AttributeList::AT_packed: handlePackedAttr (S, D, Attr); break;
3023 case AttributeList::AT_MsStruct: handleMsStructAttr (S, D, Attr); break;
3024 case AttributeList::AT_section: handleSectionAttr (S, D, Attr); break;
3025 case AttributeList::AT_unavailable: handleUnavailableAttr (S, D, Attr); break;
Fariborz Jahanian1f626d62011-07-06 19:24:05 +00003026 case AttributeList::AT_arc_weakref_unavailable:
3027 handleArcWeakrefUnavailableAttr (S, D, Attr);
3028 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003029 case AttributeList::AT_unused: handleUnusedAttr (S, D, Attr); break;
3030 case AttributeList::AT_used: handleUsedAttr (S, D, Attr); break;
3031 case AttributeList::AT_visibility: handleVisibilityAttr (S, D, Attr); break;
3032 case AttributeList::AT_warn_unused_result: handleWarnUnusedResult(S, D, Attr);
Chris Lattner237f2752009-02-14 07:37:35 +00003033 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003034 case AttributeList::AT_weak: handleWeakAttr (S, D, Attr); break;
3035 case AttributeList::AT_weakref: handleWeakRefAttr (S, D, Attr); break;
3036 case AttributeList::AT_weak_import: handleWeakImportAttr (S, D, Attr); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003037 case AttributeList::AT_transparent_union:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003038 handleTransparentUnionAttr(S, D, Attr);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003039 break;
Chris Lattner677a3582009-02-14 08:09:34 +00003040 case AttributeList::AT_objc_exception:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003041 handleObjCExceptionAttr(S, D, Attr);
Chris Lattner677a3582009-02-14 08:09:34 +00003042 break;
John McCall86bc21f2011-03-02 11:33:24 +00003043 case AttributeList::AT_objc_method_family:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003044 handleObjCMethodFamilyAttr(S, D, Attr);
John McCall86bc21f2011-03-02 11:33:24 +00003045 break;
Chandler Carruthedc2c642011-07-02 00:01:44 +00003046 case AttributeList::AT_nsobject: handleObjCNSObject (S, D, Attr); break;
3047 case AttributeList::AT_blocks: handleBlocksAttr (S, D, Attr); break;
3048 case AttributeList::AT_sentinel: handleSentinelAttr (S, D, Attr); break;
3049 case AttributeList::AT_const: handleConstAttr (S, D, Attr); break;
3050 case AttributeList::AT_pure: handlePureAttr (S, D, Attr); break;
3051 case AttributeList::AT_cleanup: handleCleanupAttr (S, D, Attr); break;
3052 case AttributeList::AT_nodebug: handleNoDebugAttr (S, D, Attr); break;
3053 case AttributeList::AT_noinline: handleNoInlineAttr (S, D, Attr); break;
3054 case AttributeList::AT_regparm: handleRegparmAttr (S, D, Attr); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003055 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003056 // Just ignore
3057 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00003058 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Chandler Carruthedc2c642011-07-02 00:01:44 +00003059 handleNoInstrumentFunctionAttr(S, D, Attr);
Chris Lattner3c77a352010-06-22 00:03:40 +00003060 break;
John McCallab26cfa2010-02-05 21:31:56 +00003061 case AttributeList::AT_stdcall:
3062 case AttributeList::AT_cdecl:
3063 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003064 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003065 case AttributeList::AT_pascal:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003066 case AttributeList::AT_pcs:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003067 handleCallConvAttr(S, D, Attr);
John McCallab26cfa2010-02-05 21:31:56 +00003068 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003069 case AttributeList::AT_opencl_kernel_function:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003070 handleOpenCLKernelAttr(S, D, Attr);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003071 break;
Francois Picheta83957a2010-12-19 06:50:37 +00003072 case AttributeList::AT_uuid:
Chandler Carruthedc2c642011-07-02 00:01:44 +00003073 handleUuidAttr(S, D, Attr);
Francois Picheta83957a2010-12-19 06:50:37 +00003074 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003075 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003076 // Ask target about the attribute.
3077 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3078 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00003079 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3080 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003081 break;
3082 }
3083}
3084
Peter Collingbourneb331b262011-01-21 02:08:45 +00003085/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3086/// the attribute applies to decls. If the attribute is a type attribute, just
3087/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3088/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Chandler Carruthedc2c642011-07-02 00:01:44 +00003089static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
3090 const AttributeList &Attr,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003091 bool NonInheritable, bool Inheritable) {
3092 if (Attr.isInvalid())
3093 return;
3094
3095 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3096 // FIXME: Try to deal with other __declspec attributes!
3097 return;
3098
3099 if (NonInheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00003100 ProcessNonInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00003101
3102 if (Inheritable)
Chandler Carruthedc2c642011-07-02 00:01:44 +00003103 ProcessInheritableDeclAttr(S, scope, D, Attr);
Peter Collingbourneb331b262011-01-21 02:08:45 +00003104}
3105
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003106/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3107/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00003108void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003109 const AttributeList *AttrList,
3110 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003111 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Chandler Carruthedc2c642011-07-02 00:01:44 +00003112 ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable);
Rafael Espindolac18086a2010-02-23 22:00:30 +00003113 }
3114
3115 // GCC accepts
3116 // static int a9 __attribute__((weakref));
3117 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003118 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003119 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00003120 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00003121 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003122 }
3123}
3124
Ryan Flynn7d470f32009-07-30 03:15:39 +00003125/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3126/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00003127NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00003128 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00003129 NamedDecl *NewD = 0;
3130 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3131 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003132 FD->getInnerLocStart(),
Ryan Flynn7d470f32009-07-30 03:15:39 +00003133 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00003134 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00003135 if (FD->getQualifier()) {
3136 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00003137 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00003138 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003139 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3140 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003141 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00003142 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00003143 VD->getStorageClass(),
3144 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00003145 if (VD->getQualifier()) {
3146 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00003147 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00003148 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003149 }
3150 return NewD;
3151}
3152
3153/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3154/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00003155void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00003156 if (W.getUsed()) return; // only do this once
3157 W.setUsed(true);
3158 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3159 IdentifierInfo *NDId = ND->getIdentifier();
3160 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003161 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3162 NDId->getName()));
3163 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00003164 WeakTopLevelDecl.push_back(NewD);
3165 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3166 // to insert Decl at TU scope, sorry.
3167 DeclContext *SavedContext = CurContext;
3168 CurContext = Context.getTranslationUnitDecl();
3169 PushOnScopeChains(NewD, S);
3170 CurContext = SavedContext;
3171 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003172 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00003173 }
3174}
3175
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003176/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3177/// it, apply them to D. This is a bit tricky because PD can have attributes
3178/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003179void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3180 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00003181 // It's valid to "forward-declare" #pragma weak, in which case we
3182 // have to do this.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003183 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCall6fe02402010-10-27 00:59:00 +00003184 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3185 if (IdentifierInfo *Id = ND->getIdentifier()) {
3186 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3187 = WeakUndeclaredIdentifiers.find(Id);
3188 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3189 WeakInfo W = I->second;
3190 DeclApplyPragmaWeak(S, ND, W);
3191 WeakUndeclaredIdentifiers[Id] = W;
3192 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003193 }
3194 }
3195 }
3196
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003197 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00003198 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003199 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003200
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003201 // Walk the declarator structure, applying decl attributes that were in a type
3202 // position to the decl itself. This handles cases like:
3203 // int *__attr__(x)** D;
3204 // when X is a decl attribute.
3205 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3206 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003207 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003208
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003209 // Finally, apply any attributes on the decl itself.
3210 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003211 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003212}
John McCall28a6aea2009-11-04 02:18:39 +00003213
John McCall31168b02011-06-15 23:02:42 +00003214/// Is the given declaration allowed to use a forbidden type?
3215static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3216 // Private ivars are always okay. Unfortunately, people don't
3217 // always properly make their ivars private, even in system headers.
3218 // Plus we need to make fields okay, too.
3219 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl))
3220 return false;
3221
3222 // Require it to be declared in a system header.
3223 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3224}
3225
3226/// Handle a delayed forbidden-type diagnostic.
3227static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3228 Decl *decl) {
3229 if (decl && isForbiddenTypeAllowed(S, decl)) {
3230 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3231 "this system declaration uses an unsupported type"));
3232 return;
3233 }
3234
3235 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3236 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3237 diag.Triggered = true;
3238}
3239
John McCallc1465822011-02-14 07:13:47 +00003240// This duplicates a vector push_back but hides the need to know the
3241// size of the type.
3242void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3243 assert(StackSize <= StackCapacity);
3244
3245 // Grow the stack if necessary.
3246 if (StackSize == StackCapacity) {
3247 unsigned newCapacity = 2 * StackCapacity + 2;
3248 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3249 const char *oldBuffer = (const char*) Stack;
3250
3251 if (StackCapacity)
3252 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3253
3254 delete[] oldBuffer;
3255 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3256 StackCapacity = newCapacity;
3257 }
3258
3259 assert(StackSize < StackCapacity);
3260 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall86121512010-01-27 03:50:35 +00003261}
3262
John McCallc1465822011-02-14 07:13:47 +00003263void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3264 Decl *decl) {
3265 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall86121512010-01-27 03:50:35 +00003266
John McCallc1465822011-02-14 07:13:47 +00003267 // Check the invariants.
3268 assert(DD.StackSize >= state.SavedStackSize);
3269 assert(state.SavedStackSize >= DD.ActiveStackBase);
3270 assert(DD.ParsingDepth > 0);
3271
3272 // Drop the parsing depth.
3273 DD.ParsingDepth--;
3274
3275 // If there are no active diagnostics, we're done.
3276 if (DD.StackSize == DD.ActiveStackBase)
John McCall86121512010-01-27 03:50:35 +00003277 return;
3278
John McCall86121512010-01-27 03:50:35 +00003279 // We only want to actually emit delayed diagnostics when we
3280 // successfully parsed a decl.
Argyrios Kyrtzidisd8a27712011-06-24 19:59:27 +00003281 if (decl && !decl->isInvalidDecl()) {
John McCallc1465822011-02-14 07:13:47 +00003282 // We emit all the active diagnostics, not just those starting
3283 // from the saved state. The idea is this: we get one push for a
John McCall86121512010-01-27 03:50:35 +00003284 // decl spec and another for each declarator; in a decl group like:
3285 // deprecated_typedef foo, *bar, baz();
3286 // only the declarator pops will be passed decls. This is correct;
3287 // we really do need to consider delayed diagnostics from the decl spec
3288 // for each of the different declarations.
John McCallc1465822011-02-14 07:13:47 +00003289 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3290 DelayedDiagnostic &diag = DD.Stack[i];
3291 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00003292 continue;
3293
John McCallc1465822011-02-14 07:13:47 +00003294 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00003295 case DelayedDiagnostic::Deprecation:
John McCallc1465822011-02-14 07:13:47 +00003296 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003297 break;
3298
3299 case DelayedDiagnostic::Access:
John McCallc1465822011-02-14 07:13:47 +00003300 S.HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003301 break;
John McCall31168b02011-06-15 23:02:42 +00003302
3303 case DelayedDiagnostic::ForbiddenType:
3304 handleDelayedForbiddenType(S, diag, decl);
3305 break;
John McCall86121512010-01-27 03:50:35 +00003306 }
3307 }
3308 }
3309
John McCall1064d7e2010-03-16 05:22:47 +00003310 // Destroy all the delayed diagnostics we're about to pop off.
John McCallc1465822011-02-14 07:13:47 +00003311 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor899b68f2011-03-23 15:13:44 +00003312 DD.Stack[i].Destroy();
John McCall1064d7e2010-03-16 05:22:47 +00003313
John McCallc1465822011-02-14 07:13:47 +00003314 DD.StackSize = state.SavedStackSize;
John McCall28a6aea2009-11-04 02:18:39 +00003315}
3316
3317static bool isDeclDeprecated(Decl *D) {
3318 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003319 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00003320 return true;
3321 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3322 return false;
3323}
3324
John McCallb45a1e72010-08-26 02:13:20 +00003325void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00003326 Decl *Ctx) {
3327 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00003328 return;
3329
John McCall86121512010-01-27 03:50:35 +00003330 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003331 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003332 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003333 << DD.getDeprecationDecl()->getDeclName()
3334 << DD.getDeprecationMessage();
Fariborz Jahanian551063102010-10-06 21:18:44 +00003335 else
3336 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003337 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00003338}
3339
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003340void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003341 SourceLocation Loc,
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003342 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00003343 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00003344 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3345 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall28a6aea2009-11-04 02:18:39 +00003346 return;
3347 }
3348
3349 // Otherwise, don't warn if our current context is deprecated.
3350 if (isDeclDeprecated(cast<Decl>(CurContext)))
3351 return;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003352 if (!Message.empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003353 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3354 << Message;
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003355 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00003356 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003357 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003358 else {
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003359 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003360 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3361 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003362 }
John McCall28a6aea2009-11-04 02:18:39 +00003363}