blob: 240c15a8e06428edae9fcb68d757bebef4f585f3 [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
Ted Kremenek527042b2009-08-14 20:49:40 +000052static const FunctionType *getFunctionType(const Decl *d,
53 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000054 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000055 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000056 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000057 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000058 Ty = decl->getType();
Richard Smithdda56e42011-04-15 14:24:37 +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).
77static bool isFunction(const Decl *d) {
78 return getFunctionType(d, false) != NULL;
79}
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.
Ted Kremenek527042b2009-08-14 20:49:40 +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.
Ted Kremenek527042b2009-08-14 20:49:40 +000091static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000092 if (isFunctionOrMethod(d))
93 return true;
94 // check for block is more involved.
95 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
96 QualType Ty = V->getType();
97 return Ty->isBlockPointerType();
98 }
Fariborz Jahanian960910a2009-05-19 17:08:59 +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.
104static bool hasDeclarator(const Decl *d) {
John McCall31168b02011-06-15 23:02:42 +0000105 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
106 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.
Ted Kremenek527042b2009-08-14 20:49:40 +0000113static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000114 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 {
Fariborz Jahanian960910a2009-05-19 17:08:59 +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).
Ted Kremenek527042b2009-08-14 20:49:40 +0000125static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +0000126 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000127 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000128 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
129 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +0000130 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000131}
132
Ted Kremenek527042b2009-08-14 20:49:40 +0000133static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000134 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000135 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000136 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
137 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000138
Chris Lattnera4997152009-02-20 18:43:26 +0000139 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000140}
141
Ted Kremenek527042b2009-08-14 20:49:40 +0000142static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000143 if (const FunctionType *FnTy = getFunctionType(d))
144 return cast<FunctionProtoType>(FnTy)->getResultType();
145 return cast<ObjCMethodDecl>(d)->getResultType();
146}
147
Ted Kremenek527042b2009-08-14 20:49:40 +0000148static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000149 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();
Fariborz Jahanian960910a2009-05-19 17:08:59 +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 {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000155 return cast<ObjCMethodDecl>(d)->isVariadic();
156 }
157}
158
Chandler Carruth743682b2010-11-16 08:35:43 +0000159static bool isInstanceMethod(const Decl *d) {
160 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(d))
161 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
Mike Stumpd3bb5572009-07-24 19:02:52 +0000205static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000206 const AttributeList &Attr, Sema &S) {
Richard Smithdda56e42011-04-15 14:24:37 +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
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000249static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
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
Chris Lattner2c6fcf52008-06-26 18:38:35 +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));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000258 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
259 // 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
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +0000271static void HandleMsStructAttr(Decl *d, const AttributeList &Attr, Sema &S) {
272 if (TagDecl *TD = dyn_cast<TagDecl>(d))
273 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
Ted Kremenek1f672822010-02-18 03:08:58 +0000278static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
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.
286 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
287 if (MD->isInstanceMethod()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +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
295static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
296 // 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.
304 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000305 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
Ted Kremenek26bde772010-05-19 17:38:06 +0000312static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
313 Sema &S) {
314
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.
323 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 }
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000327 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
328 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
329 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
330 << VD->getType() << 0;
331 return;
332 }
333 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
334 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(),
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000345 S.getScopeForContext(d->getDeclContext()->getParent()));
346 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 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000360 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
361 QT));
Ted Kremenek26bde772010-05-19 17:38:06 +0000362}
363
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000364static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000365 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
366 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000367 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000368 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000369 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000370 return;
371 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000372
Chandler Carruth743682b2010-11-16 08:35:43 +0000373 // In C++ the implicit 'this' function parameter also counts, and they are
374 // counted from one.
375 bool HasImplicitThisParam = isInstanceMethod(d);
376 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000377
378 // The nonnull attribute only applies to pointers.
379 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000380
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000381 for (AttributeList::arg_iterator I=Attr.arg_begin(),
382 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000383
384
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000385 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000386 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000387 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000388 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
389 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000390 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
391 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000392 return;
393 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000394
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000395 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000396
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000397 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000398 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000399 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000400 return;
401 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000402
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000403 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000404 if (HasImplicitThisParam) {
405 if (x == 0) {
406 S.Diag(Attr.getLoc(),
407 diag::err_attribute_invalid_implicit_this_argument)
408 << "nonnull" << Ex->getSourceRange();
409 return;
410 }
411 --x;
412 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000413
414 // Is the function argument a pointer type?
Douglas Gregor1e2cdf52010-12-15 15:41:46 +0000415 QualType T = getFunctionOrMethodArgType(d, x).getNonReferenceType();
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000416 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000417 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000418 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000419 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000420 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000421 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000422
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000423 NonNullArgs.push_back(x);
424 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000425
426 // If no arguments were specified to __attribute__((nonnull)) then all pointer
427 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000428 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000429 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
Douglas Gregor1e2cdf52010-12-15 15:41:46 +0000430 QualType T = getFunctionOrMethodArgType(d, I).getNonReferenceType();
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000431 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000432 NonNullArgs.push_back(I);
Fariborz Jahanian3567c422010-09-27 22:42:37 +0000433 else if (const RecordType *UT = T->getAsUnionType()) {
434 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
435 RecordDecl *UD = UT->getDecl();
436 for (RecordDecl::field_iterator it = UD->field_begin(),
437 itend = UD->field_end(); it != itend; ++it) {
438 T = it->getType();
439 if (T->isAnyPointerType() || T->isBlockPointerType()) {
440 NonNullArgs.push_back(I);
441 break;
442 }
443 }
444 }
445 }
Ted Kremenek5fa50522008-11-18 06:52:58 +0000446 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000447
Ted Kremenek22813f42010-10-21 18:49:36 +0000448 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000449 if (NonNullArgs.empty()) {
450 // Warn the trivial case only if attribute is not coming from a
451 // macro instantiation.
452 if (Attr.getLoc().isFileID())
453 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000454 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000455 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000456 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000457
458 unsigned* start = &NonNullArgs[0];
459 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000460 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000461 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
462 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000463}
464
Ted Kremenekd21139a2010-07-31 01:52:11 +0000465static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
466 // This attribute must be applied to a function declaration.
467 // The first argument to the attribute must be a string,
468 // the name of the resource, for example "malloc".
469 // The following arguments must be argument indexes, the arguments must be
470 // of integer type for Returns, otherwise of pointer type.
471 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000472 // after being held. free() should be __attribute((ownership_takes)), whereas
473 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000474
475 if (!AL.getParameterName()) {
476 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
477 << AL.getName()->getName() << 1;
478 return;
479 }
480 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000481 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000482 switch (AL.getKind()) {
483 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000484 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000485 if (AL.getNumArgs() < 1) {
486 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
487 return;
488 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000489 break;
490 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000491 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000492 if (AL.getNumArgs() < 1) {
493 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
494 return;
495 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000496 break;
497 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000498 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000499 if (AL.getNumArgs() > 1) {
500 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
501 << AL.getNumArgs() + 1;
502 return;
503 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000504 break;
505 default:
506 // This should never happen given how we are called.
507 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000508 }
509
510 if (!isFunction(d) || !hasFunctionProto(d)) {
John McCall5fca7ea2011-03-02 12:29:23 +0000511 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
512 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000513 return;
514 }
515
Chandler Carruth743682b2010-11-16 08:35:43 +0000516 // In C++ the implicit 'this' function parameter also counts, and they are
517 // counted from one.
518 bool HasImplicitThisParam = isInstanceMethod(d);
519 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000520
521 llvm::StringRef Module = AL.getParameterName()->getName();
522
523 // Normalize the argument, __foo__ becomes foo.
524 if (Module.startswith("__") && Module.endswith("__"))
525 Module = Module.substr(2, Module.size() - 4);
526
527 llvm::SmallVector<unsigned, 10> OwnershipArgs;
528
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000529 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
530 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000531
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000532 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000533 llvm::APSInt ArgNum(32);
534 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
535 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
536 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
537 << AL.getName()->getName() << IdxExpr->getSourceRange();
538 continue;
539 }
540
541 unsigned x = (unsigned) ArgNum.getZExtValue();
542
543 if (x > NumArgs || x < 1) {
544 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
545 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
546 continue;
547 }
548 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000549 if (HasImplicitThisParam) {
550 if (x == 0) {
551 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
552 << "ownership" << IdxExpr->getSourceRange();
553 return;
554 }
555 --x;
556 }
557
Ted Kremenekd21139a2010-07-31 01:52:11 +0000558 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000559 case OwnershipAttr::Takes:
560 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000561 // Is the function argument a pointer type?
562 QualType T = getFunctionOrMethodArgType(d, x);
563 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
564 // FIXME: Should also highlight argument in decl.
565 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000566 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +0000567 << "pointer"
568 << IdxExpr->getSourceRange();
569 continue;
570 }
571 break;
572 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000573 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000574 if (AL.getNumArgs() > 1) {
575 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000576 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +0000577 llvm::APSInt ArgNum(32);
578 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
579 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
580 S.Diag(AL.getLoc(), diag::err_ownership_type)
581 << "ownership_returns" << "integer"
582 << IdxExpr->getSourceRange();
583 return;
584 }
585 }
586 break;
587 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000588 default:
589 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000590 } // switch
591
592 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000593 for (specific_attr_iterator<OwnershipAttr>
594 i = d->specific_attr_begin<OwnershipAttr>(),
595 e = d->specific_attr_end<OwnershipAttr>();
596 i != e; ++i) {
597 if ((*i)->getOwnKind() != K) {
598 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
599 I!=E; ++I) {
600 if (x == *I) {
601 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
602 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +0000603 }
604 }
605 }
606 }
607 OwnershipArgs.push_back(x);
608 }
609
610 unsigned* start = OwnershipArgs.data();
611 unsigned size = OwnershipArgs.size();
612 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000613
614 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
615 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
616 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000617 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000618
619 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
620 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +0000621}
622
John McCall7a198ce2011-02-08 22:35:49 +0000623/// Whether this declaration has internal linkage for the purposes of
624/// things that want to complain about things not have internal linkage.
625static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
626 switch (D->getLinkage()) {
627 case NoLinkage:
628 case InternalLinkage:
629 return true;
630
631 // Template instantiations that go from external to unique-external
632 // shouldn't get diagnosed.
633 case UniqueExternalLinkage:
634 return true;
635
636 case ExternalLinkage:
637 return false;
638 }
639 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +0000640 return false;
641}
642
643static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
644 // Check the attribute arguments.
645 if (Attr.getNumArgs() > 1) {
646 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
647 return;
648 }
649
John McCall7a198ce2011-02-08 22:35:49 +0000650 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000652 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +0000653 return;
654 }
655
656 NamedDecl *nd = cast<NamedDecl>(d);
657
Rafael Espindolac18086a2010-02-23 22:00:30 +0000658 // gcc rejects
659 // class c {
660 // static int a __attribute__((weakref ("v2")));
661 // static int b() __attribute__((weakref ("f3")));
662 // };
663 // and ignores the attributes of
664 // void f(void) {
665 // static int a __attribute__((weakref ("v2")));
666 // }
667 // we reject them
Sebastian Redl50c68252010-08-31 00:36:30 +0000668 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
669 if (!Ctx->isFileContext()) {
670 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +0000671 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +0000672 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000673 }
674
675 // The GCC manual says
676 //
677 // At present, a declaration to which `weakref' is attached can only
678 // be `static'.
679 //
680 // It also says
681 //
682 // Without a TARGET,
683 // given as an argument to `weakref' or to `alias', `weakref' is
684 // equivalent to `weak'.
685 //
686 // gcc 4.4.1 will accept
687 // int a7 __attribute__((weakref));
688 // as
689 // int a7 __attribute__((weak));
690 // This looks like a bug in gcc. We reject that for now. We should revisit
691 // it if this behaviour is actually used.
692
John McCall7a198ce2011-02-08 22:35:49 +0000693 if (!hasEffectivelyInternalLinkage(nd)) {
694 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000695 return;
696 }
697
698 // GCC rejects
699 // static ((alias ("y"), weakref)).
700 // Should we? How to check that weakref is before or after alias?
701
702 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000703 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000704 Arg = Arg->IgnoreParenCasts();
705 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
706
707 if (Str == 0 || Str->isWide()) {
708 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
709 << "weakref" << 1;
710 return;
711 }
712 // GCC will accept anything as the argument of weakref. Should we
713 // check for an existing decl?
Eric Christopherbc638a82010-12-01 22:13:54 +0000714 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
715 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000716 }
717
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000718 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000719}
720
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000721static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000722 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000723 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000724 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000725 return;
726 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000727
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000728 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000729 Arg = Arg->IgnoreParenCasts();
730 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000731
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000732 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000733 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000734 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000735 return;
736 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000737
Daniel Dunbar14ad22f2011-04-19 21:43:27 +0000738 if (S.Context.Target.getTriple().isOSDarwin()) {
Rafael Espindola0017c5f2010-12-07 15:23:23 +0000739 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
740 return;
741 }
742
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000743 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000744
Eric Christopherbc638a82010-12-01 22:13:54 +0000745 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
746 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000747}
748
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000749static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000750 Sema &S) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000751 // Check the attribute arguments.
Daniel Dunbar03a38442008-10-28 00:17:57 +0000752 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000753 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000754 return;
755 }
Anders Carlsson88097122009-02-19 19:16:48 +0000756
Chris Lattner4225e232009-04-14 17:02:11 +0000757 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000758 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000759 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000760 return;
761 }
762
763 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
764}
765
766static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
767 Sema &S) {
768 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000769 if (Attr.hasParameterOrArguments()) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000770 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
771 return;
772 }
773
774 if (!isa<FunctionDecl>(d)) {
775 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000776 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +0000777 return;
778 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000779
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000780 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +0000781}
782
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000783static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000784 // Check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000785 if (Attr.hasParameterOrArguments()) {
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000786 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
787 return;
788 }
Mike Stump11289f42009-09-09 15:08:12 +0000789
Ted Kremenek08479ae2009-08-15 00:51:46 +0000790 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000791 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000792 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000793 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +0000794 return;
795 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000796 }
797
Ted Kremenek08479ae2009-08-15 00:51:46 +0000798 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000799}
800
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000801static void HandleMayAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
802 // check the attribute arguments.
803 if (Attr.getNumArgs() != 0) {
804 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
805 return;
806 }
807
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000808 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
809}
810
Eric Christopher8a2ee392010-12-02 02:45:55 +0000811static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
812 assert(Attr.isInvalid() == false);
Eric Christopher515d87f2010-12-03 06:58:14 +0000813 if (isa<VarDecl>(d))
814 d->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
815 else
816 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000817 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000818}
819
820static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
821 assert(Attr.isInvalid() == false);
Eric Christopher515d87f2010-12-03 06:58:14 +0000822 if (isa<VarDecl>(d))
823 d->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
824 else
825 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000826 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000827}
828
John McCall3882ace2011-01-05 12:14:39 +0000829static void HandleNoReturnAttr(Decl *d, const AttributeList &attr, Sema &S) {
830 if (hasDeclarator(d)) return;
831
832 if (S.CheckNoReturnAttr(attr)) return;
833
834 if (!isa<ObjCMethodDecl>(d)) {
835 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000836 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +0000837 return;
838 }
839
840 d->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
841}
842
843bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek1551d552011-04-15 05:49:29 +0000844 if (attr.hasParameterOrArguments()) {
John McCall3882ace2011-01-05 12:14:39 +0000845 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
846 attr.setInvalid();
847 return true;
848 }
849
850 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000851}
852
853static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
854 Sema &S) {
Ted Kremenek5295ce82010-08-19 00:51:58 +0000855
856 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
857 // because 'analyzer_noreturn' does not impact the type.
858
859 if (Attr.getNumArgs() != 0) {
860 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
861 return;
862 }
863
864 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
865 ValueDecl *VD = dyn_cast<ValueDecl>(d);
866 if (VD == 0 || (!VD->getType()->isBlockPointerType()
867 && !VD->getType()->isFunctionPointerType())) {
868 S.Diag(Attr.getLoc(),
869 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
870 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000871 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +0000872 return;
873 }
874 }
875
876 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000877}
878
John Thompsoncdb847ba2010-08-09 21:53:52 +0000879// PS3 PPU-specific.
880static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
881 Sema &S) {
882/*
883 Returning a Vector Class in Registers
884
Eric Christopherbc638a82010-12-01 22:13:54 +0000885 According to the PPU ABI specifications, a class with a single member of
886 vector type is returned in memory when used as the return value of a function.
887 This results in inefficient code when implementing vector classes. To return
888 the value in a single vector register, add the vecreturn attribute to the
889 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +0000890
891 Example:
892
893 struct Vector
894 {
895 __vector float xyzw;
896 } __attribute__((vecreturn));
897
898 Vector Add(Vector lhs, Vector rhs)
899 {
900 Vector result;
901 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
902 return result; // This will be returned in a register
903 }
904*/
John Thompson9a587aaa2010-09-18 01:12:07 +0000905 if (!isa<RecordDecl>(d)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000906 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000907 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +0000908 return;
909 }
910
911 if (d->getAttr<VecReturnAttr>()) {
912 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
913 return;
914 }
915
John Thompson9a587aaa2010-09-18 01:12:07 +0000916 RecordDecl *record = cast<RecordDecl>(d);
917 int count = 0;
918
919 if (!isa<CXXRecordDecl>(record)) {
920 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
921 return;
922 }
923
924 if (!cast<CXXRecordDecl>(record)->isPOD()) {
925 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
926 return;
927 }
928
Eric Christopherbc638a82010-12-01 22:13:54 +0000929 for (RecordDecl::field_iterator iter = record->field_begin();
930 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +0000931 if ((count == 1) || !iter->getType()->isVectorType()) {
932 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
933 return;
934 }
935 count++;
936 }
937
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000938 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +0000939}
940
Alexis Hunt96d5c762009-11-21 08:43:09 +0000941static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
942 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
943 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000944 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000945 return;
946 }
947 // FIXME: Actually store the attribute on the declaration
948}
949
Ted Kremenek39c59a82008-07-25 04:39:19 +0000950static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
951 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000952 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000953 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000954 return;
955 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000956
John McCallcef15822010-03-31 02:47:45 +0000957 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
Chris Lattnercab02a62011-02-17 20:34:02 +0000958 !isa<TypeDecl>(d) && !isa<LabelDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000959 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000960 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000961 return;
962 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000963
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000964 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +0000965}
966
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000967static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
968 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +0000969 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000970 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
971 return;
972 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000973
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000974 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000975 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000976 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
977 return;
978 }
979 } else if (!isFunctionOrMethod(d)) {
980 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000981 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000982 return;
983 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000984
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000985 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000986}
987
Daniel Dunbar032db472008-07-31 22:40:48 +0000988static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
989 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +0000990 if (Attr.getNumArgs() > 1) {
991 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +0000992 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000993 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000994
995 int priority = 65535; // FIXME: Do not hardcode such constants.
996 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000997 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +0000998 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000999 if (E->isTypeDependent() || E->isValueDependent() ||
1000 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001001 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001002 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001003 return;
1004 }
1005 priority = Idx.getZExtValue();
1006 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001007
Chris Lattner4225e232009-04-14 17:02:11 +00001008 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001009 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001010 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001011 return;
1012 }
1013
Eric Christopherbc638a82010-12-01 22:13:54 +00001014 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
1015 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001016}
1017
1018static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1019 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001020 if (Attr.getNumArgs() > 1) {
1021 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001022 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001023 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001024
1025 int priority = 65535; // FIXME: Do not hardcode such constants.
1026 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001027 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001028 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001029 if (E->isTypeDependent() || E->isValueDependent() ||
1030 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001031 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001032 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001033 return;
1034 }
1035 priority = Idx.getZExtValue();
1036 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001037
Anders Carlsson8e82b7f2008-08-22 22:10:48 +00001038 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001039 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001040 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001041 return;
1042 }
1043
Eric Christopherbc638a82010-12-01 22:13:54 +00001044 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
1045 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001046}
1047
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001048static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner190aa102011-02-24 05:42:24 +00001049 unsigned NumArgs = Attr.getNumArgs();
1050 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001051 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001052 return;
1053 }
Chris Lattner190aa102011-02-24 05:42:24 +00001054
Fariborz Jahanian551063102010-10-06 21:18:44 +00001055 // Handle the case where deprecated attribute has a text message.
Chris Lattner190aa102011-02-24 05:42:24 +00001056 llvm::StringRef Str;
1057 if (NumArgs == 1) {
1058 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001059 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001060 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1061 << "deprecated";
Fariborz Jahanian551063102010-10-06 21:18:44 +00001062 return;
1063 }
Chris Lattner190aa102011-02-24 05:42:24 +00001064 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001065 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001066
Chris Lattner190aa102011-02-24 05:42:24 +00001067 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001068}
1069
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001070static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner190aa102011-02-24 05:42:24 +00001071 unsigned NumArgs = Attr.getNumArgs();
1072 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001073 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001074 return;
1075 }
Chris Lattner190aa102011-02-24 05:42:24 +00001076
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001077 // Handle the case where unavailable attribute has a text message.
Chris Lattner190aa102011-02-24 05:42:24 +00001078 llvm::StringRef Str;
1079 if (NumArgs == 1) {
1080 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001081 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001082 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001083 diag::err_attribute_not_string) << "unavailable";
1084 return;
1085 }
Chris Lattner190aa102011-02-24 05:42:24 +00001086 Str = SE->getString();
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001087 }
Chris Lattner190aa102011-02-24 05:42:24 +00001088 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001089}
1090
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001091static void HandleAvailabilityAttr(Decl *d, const AttributeList &Attr,
1092 Sema &S) {
1093 IdentifierInfo *Platform = Attr.getParameterName();
1094 SourceLocation PlatformLoc = Attr.getParameterLoc();
1095
1096 llvm::StringRef PlatformName
1097 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1098 if (PlatformName.empty()) {
1099 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1100 << Platform;
1101
1102 PlatformName = Platform->getName();
1103 }
1104
1105 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1106 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1107 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001108 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001109
1110 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1111 // of these steps are needed).
1112 if (Introduced.isValid() && Deprecated.isValid() &&
1113 !(Introduced.Version < Deprecated.Version)) {
1114 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1115 << 1 << PlatformName << Deprecated.Version.getAsString()
1116 << 0 << Introduced.Version.getAsString();
1117 return;
1118 }
1119
1120 if (Introduced.isValid() && Obsoleted.isValid() &&
1121 !(Introduced.Version < Obsoleted.Version)) {
1122 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1123 << 2 << PlatformName << Obsoleted.Version.getAsString()
1124 << 0 << Introduced.Version.getAsString();
1125 return;
1126 }
1127
1128 if (Deprecated.isValid() && Obsoleted.isValid() &&
1129 !(Deprecated.Version < Obsoleted.Version)) {
1130 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1131 << 2 << PlatformName << Obsoleted.Version.getAsString()
1132 << 1 << Deprecated.Version.getAsString();
1133 return;
1134 }
1135
1136 d->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
1137 Platform,
1138 Introduced.Version,
1139 Deprecated.Version,
Douglas Gregor7ab142b2011-03-26 03:35:55 +00001140 Obsoleted.Version,
1141 IsUnavailable));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001142}
1143
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001144static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001145 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001146 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001147 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001148 return;
1149 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001150
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001151 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001152 Arg = Arg->IgnoreParenCasts();
1153 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001154
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001155 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001156 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001157 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001158 return;
1159 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001160
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001161 llvm::StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001162 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001163
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001164 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001165 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001166 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001167 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001168 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001169 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001170 else if (TypeStr == "protected")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001171 type = VisibilityAttr::Protected;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001172 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001173 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001174 return;
1175 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001176
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001177 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001178}
1179
John McCall86bc21f2011-03-02 11:33:24 +00001180static void HandleObjCMethodFamilyAttr(Decl *decl, const AttributeList &attr,
1181 Sema &S) {
1182 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1183 if (!method) {
1184 S.Diag(attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001185 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00001186 return;
1187 }
1188
1189 if (attr.getNumArgs() != 0 || !attr.getParameterName()) {
1190 if (!attr.getParameterName() && attr.getNumArgs() == 1) {
1191 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
1192 << "objc_method_family" << 1;
1193 } else {
1194 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1195 }
1196 attr.setInvalid();
1197 return;
1198 }
1199
1200 llvm::StringRef param = attr.getParameterName()->getName();
1201 ObjCMethodFamilyAttr::FamilyKind family;
1202 if (param == "none")
1203 family = ObjCMethodFamilyAttr::OMF_None;
1204 else if (param == "alloc")
1205 family = ObjCMethodFamilyAttr::OMF_alloc;
1206 else if (param == "copy")
1207 family = ObjCMethodFamilyAttr::OMF_copy;
1208 else if (param == "init")
1209 family = ObjCMethodFamilyAttr::OMF_init;
1210 else if (param == "mutableCopy")
1211 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1212 else if (param == "new")
1213 family = ObjCMethodFamilyAttr::OMF_new;
1214 else {
1215 // Just warn and ignore it. This is future-proof against new
1216 // families being used in system headers.
1217 S.Diag(attr.getParameterLoc(), diag::warn_unknown_method_family);
1218 return;
1219 }
1220
John McCall31168b02011-06-15 23:02:42 +00001221 if (family == ObjCMethodFamilyAttr::OMF_init &&
1222 !method->getResultType()->isObjCObjectPointerType()) {
1223 S.Diag(method->getLocation(), diag::err_init_method_bad_return_type)
1224 << method->getResultType();
1225 // Ignore the attribute.
1226 return;
1227 }
1228
1229 method->addAttr(new (S.Context) ObjCMethodFamilyAttr(attr.getLoc(),
1230 S.Context, family));
John McCall86bc21f2011-03-02 11:33:24 +00001231}
1232
Chris Lattner677a3582009-02-14 08:09:34 +00001233static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1234 Sema &S) {
1235 if (Attr.getNumArgs() != 0) {
1236 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1237 return;
1238 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001239
Chris Lattner677a3582009-02-14 08:09:34 +00001240 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1241 if (OCI == 0) {
1242 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1243 return;
1244 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001245
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001246 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001247}
1248
1249static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001250 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001251 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001252 return;
1253 }
Richard Smithdda56e42011-04-15 14:24:37 +00001254 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001255 QualType T = TD->getUnderlyingType();
1256 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001257 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001258 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1259 return;
1260 }
1261 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001262 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001263}
1264
Mike Stumpd3bb5572009-07-24 19:02:52 +00001265static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001266HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1267 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001268 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001269 return;
1270 }
1271
1272 if (!isa<FunctionDecl>(D)) {
1273 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1274 return;
1275 }
1276
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001277 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001278}
1279
Steve Naroff3405a732008-09-18 16:44:58 +00001280static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001281 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001282 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001283 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001284 return;
1285 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001286
Steve Naroff3405a732008-09-18 16:44:58 +00001287 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001288 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001289 return;
1290 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001291
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001292 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001293 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001294 type = BlocksAttr::ByRef;
1295 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001296 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001297 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001298 return;
1299 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001300
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001301 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001302}
1303
Anders Carlssonc181b012008-10-05 18:05:59 +00001304static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1305 // check the attribute arguments.
1306 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00001307 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00001308 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001309 }
1310
Anders Carlssonc181b012008-10-05 18:05:59 +00001311 int sentinel = 0;
1312 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001313 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00001314 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001315 if (E->isTypeDependent() || E->isValueDependent() ||
1316 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001317 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001318 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001319 return;
1320 }
1321 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001322
Anders Carlssonc181b012008-10-05 18:05:59 +00001323 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001324 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1325 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001326 return;
1327 }
1328 }
1329
1330 int nullPos = 0;
1331 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001332 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00001333 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001334 if (E->isTypeDependent() || E->isValueDependent() ||
1335 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001336 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001337 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001338 return;
1339 }
1340 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001341
Anders Carlssonc181b012008-10-05 18:05:59 +00001342 if (nullPos > 1 || nullPos < 0) {
1343 // FIXME: This error message could be improved, it would be nice
1344 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001345 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1346 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001347 return;
1348 }
1349 }
1350
1351 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +00001352 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001353 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001354
Chris Lattner9363e312009-03-17 23:03:47 +00001355 if (isa<FunctionNoProtoType>(FT)) {
1356 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1357 return;
1358 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001359
Chris Lattner9363e312009-03-17 23:03:47 +00001360 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001361 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001362 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001363 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001364 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1365 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001366 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001367 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001368 }
1369 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001370 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1371 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001372 ;
1373 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001374 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001375 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001376 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherbc638a82010-12-01 22:13:54 +00001377 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001378 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001379 int m = Ty->isFunctionPointerType() ? 0 : 1;
1380 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001381 return;
1382 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001383 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001384 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001385 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001386 return;
1387 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001388 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001389 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001390 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00001391 return;
1392 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001393 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1394 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001395}
1396
Chris Lattner237f2752009-02-14 07:37:35 +00001397static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1398 // check the attribute arguments.
1399 if (Attr.getNumArgs() != 0) {
1400 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1401 return;
1402 }
1403
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001404 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001405 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001406 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00001407 return;
1408 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001409
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001410 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1411 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1412 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001413 return;
1414 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001415 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1416 if (MD->getResultType()->isVoidType()) {
1417 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1418 << Attr.getName() << 1;
1419 return;
1420 }
1421
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001422 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001423}
1424
John McCall7a198ce2011-02-08 22:35:49 +00001425static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001426 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001427 if (attr.hasParameterOrArguments()) {
John McCall7a198ce2011-02-08 22:35:49 +00001428 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001429 return;
1430 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001431
John McCall7a198ce2011-02-08 22:35:49 +00001432 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
1433 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001434 << attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001435 return;
1436 }
1437
John McCall7a198ce2011-02-08 22:35:49 +00001438 NamedDecl *nd = cast<NamedDecl>(d);
1439
1440 // 'weak' only applies to declarations with external linkage.
1441 if (hasEffectivelyInternalLinkage(nd)) {
1442 S.Diag(attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001443 return;
1444 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001445
John McCall7a198ce2011-02-08 22:35:49 +00001446 nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001447}
1448
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001449static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1450 // check the attribute arguments.
1451 if (Attr.getNumArgs() != 0) {
1452 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1453 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001454 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001455
1456 // weak_import only applies to variable & function declarations.
1457 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001458 if (!D->canBeWeakImported(isDef)) {
1459 if (isDef)
1460 S.Diag(Attr.getLoc(),
1461 diag::warn_attribute_weak_import_invalid_on_definition)
1462 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00001463 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Daniel Dunbar14ad22f2011-04-19 21:43:27 +00001464 (S.Context.Target.getTriple().isOSDarwin() &&
Douglas Gregord71149a2011-03-23 13:27:51 +00001465 isa<ObjCInterfaceDecl>(D))) {
1466 // Nothing to warn about here.
1467 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001468 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001469 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001470
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001471 return;
1472 }
1473
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001474 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001475}
1476
Nate Begemanf2758702009-06-26 06:32:41 +00001477static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1478 Sema &S) {
1479 // Attribute has 3 arguments.
1480 if (Attr.getNumArgs() != 3) {
John McCall61d82582010-05-28 18:25:28 +00001481 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begemanf2758702009-06-26 06:32:41 +00001482 return;
1483 }
1484
1485 unsigned WGSize[3];
1486 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001487 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00001488 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001489 if (E->isTypeDependent() || E->isValueDependent() ||
1490 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001491 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1492 << "reqd_work_group_size" << E->getSourceRange();
1493 return;
1494 }
1495 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1496 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001497 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1498 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001499 WGSize[2]));
1500}
1501
Chris Lattner237f2752009-02-14 07:37:35 +00001502static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001503 // Attribute has no arguments.
1504 if (Attr.getNumArgs() != 1) {
1505 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1506 return;
1507 }
1508
1509 // Make sure that there is a string literal as the sections's single
1510 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001511 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001512 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001513 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001514 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001515 return;
1516 }
Mike Stump11289f42009-09-09 15:08:12 +00001517
Chris Lattner30ba6742009-08-10 19:03:04 +00001518 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001519 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001520 if (!Error.empty()) {
1521 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1522 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001523 return;
1524 }
Mike Stump11289f42009-09-09 15:08:12 +00001525
Chris Lattner20aee9b2010-01-12 20:58:53 +00001526 // This attribute cannot be applied to local variables.
1527 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1528 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1529 return;
1530 }
1531
Eric Christopherbc638a82010-12-01 22:13:54 +00001532 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1533 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001534}
1535
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001536
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001537static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001538 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00001539 if (Attr.hasParameterOrArguments()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001540 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001541 return;
1542 }
Douglas Gregor88336832011-06-15 05:45:11 +00001543
1544 if (NoThrowAttr *Existing = d->getAttr<NoThrowAttr>()) {
1545 if (Existing->getLocation().isInvalid())
1546 Existing->setLocation(Attr.getLoc());
1547 } else {
1548 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
1549 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001550}
1551
Anders Carlssonb8316282008-10-05 23:32:53 +00001552static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1553 // 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;
Anders Carlssonb8316282008-10-05 23:32:53 +00001556 return;
1557 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001558
Douglas Gregor88336832011-06-15 05:45:11 +00001559 if (ConstAttr *Existing = d->getAttr<ConstAttr>()) {
1560 if (Existing->getLocation().isInvalid())
1561 Existing->setLocation(Attr.getLoc());
1562 } else {
1563 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
1564 }
Anders Carlssonb8316282008-10-05 23:32:53 +00001565}
1566
1567static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1568 // check the attribute arguments.
1569 if (Attr.getNumArgs() != 0) {
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
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001574 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001575}
1576
Anders Carlssond277d792009-01-31 01:16:18 +00001577static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001578 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001579 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1580 return;
1581 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001582
Anders Carlssond277d792009-01-31 01:16:18 +00001583 if (Attr.getNumArgs() != 0) {
1584 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1585 return;
1586 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001587
Anders Carlssond277d792009-01-31 01:16:18 +00001588 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001589
Anders Carlssond277d792009-01-31 01:16:18 +00001590 if (!VD || !VD->hasLocalStorage()) {
1591 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1592 return;
1593 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001594
Anders Carlssond277d792009-01-31 01:16:18 +00001595 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001596 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00001597 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001598 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1599 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001600 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001601 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001602 Attr.getParameterName();
1603 return;
1604 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001605
Anders Carlssond277d792009-01-31 01:16:18 +00001606 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1607 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001608 S.Diag(Attr.getParameterLoc(),
1609 diag::err_attribute_cleanup_arg_not_function)
1610 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001611 return;
1612 }
1613
Anders Carlssond277d792009-01-31 01:16:18 +00001614 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001615 S.Diag(Attr.getParameterLoc(),
1616 diag::err_attribute_cleanup_func_must_take_one_arg)
1617 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001618 return;
1619 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001620
Anders Carlsson723f55d2009-02-07 23:16:50 +00001621 // We're currently more strict than GCC about what function types we accept.
1622 // If this ever proves to be a problem it should be easy to fix.
1623 QualType Ty = S.Context.getPointerType(VD->getType());
1624 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00001625 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1626 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001627 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001628 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1629 Attr.getParameterName() << ParamTy << Ty;
1630 return;
1631 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001632
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001633 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidise88168a2010-12-06 17:51:53 +00001634 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00001635}
1636
Mike Stumpd3bb5572009-07-24 19:02:52 +00001637/// Handle __attribute__((format_arg((idx)))) attribute based on
1638/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1639static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001640 if (Attr.getNumArgs() != 1) {
1641 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1642 return;
1643 }
1644 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1645 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001646 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001647 return;
1648 }
Chandler Carruth743682b2010-11-16 08:35:43 +00001649
1650 // In C++ the implicit 'this' function parameter also counts, and they are
1651 // counted from one.
1652 bool HasImplicitThisParam = isInstanceMethod(d);
1653 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001654 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00001655
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001656 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001657 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001658 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001659 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1660 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001661 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1662 << "format" << 2 << IdxExpr->getSourceRange();
1663 return;
1664 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001665
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001666 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1667 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1668 << "format" << 2 << IdxExpr->getSourceRange();
1669 return;
1670 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001671
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001672 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001673
Chandler Carruth743682b2010-11-16 08:35:43 +00001674 if (HasImplicitThisParam) {
1675 if (ArgIdx == 0) {
1676 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1677 << "format_arg" << IdxExpr->getSourceRange();
1678 return;
1679 }
1680 ArgIdx--;
1681 }
1682
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001683 // make sure the format string is really a string
1684 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001685
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001686 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1687 if (not_nsstring_type &&
1688 !isCFStringType(Ty, S.Context) &&
1689 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001690 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001691 // FIXME: Should highlight the actual expression that has the wrong type.
1692 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001693 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001694 << IdxExpr->getSourceRange();
1695 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001696 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001697 Ty = getFunctionOrMethodResultType(d);
1698 if (!isNSStringType(Ty, S.Context) &&
1699 !isCFStringType(Ty, S.Context) &&
1700 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001701 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001702 // FIXME: Should highlight the actual expression that has the wrong type.
1703 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001704 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001705 << IdxExpr->getSourceRange();
1706 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001707 }
1708
Chandler Carruth743682b2010-11-16 08:35:43 +00001709 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1710 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001711}
1712
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001713enum FormatAttrKind {
1714 CFStringFormat,
1715 NSStringFormat,
1716 StrftimeFormat,
1717 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001718 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001719 InvalidFormat
1720};
1721
1722/// getFormatAttrKind - Map from format attribute names to supported format
1723/// types.
1724static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1725 // Check for formats that get handled specially.
1726 if (Format == "NSString")
1727 return NSStringFormat;
1728 if (Format == "CFString")
1729 return CFStringFormat;
1730 if (Format == "strftime")
1731 return StrftimeFormat;
1732
1733 // Otherwise, check for supported formats.
1734 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1735 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1736 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattner0ddd0ae2011-02-18 17:05:55 +00001737 Format == "zcmn_err" ||
1738 Format == "kprintf") // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001739 return SupportedFormat;
1740
Duncan Sandsde4fe352010-03-23 14:44:19 +00001741 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1742 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001743 return IgnoredFormat;
1744
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001745 return InvalidFormat;
1746}
1747
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001748/// Handle __attribute__((init_priority(priority))) attributes based on
1749/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1750static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1751 Sema &S) {
1752 if (!S.getLangOptions().CPlusPlus) {
1753 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1754 return;
1755 }
1756
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001757 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1758 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1759 Attr.setInvalid();
1760 return;
1761 }
1762 QualType T = dyn_cast<VarDecl>(d)->getType();
1763 if (S.Context.getAsArrayType(T))
1764 T = S.Context.getBaseElementType(T);
1765 if (!T->getAs<RecordType>()) {
1766 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1767 Attr.setInvalid();
1768 return;
1769 }
1770
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001771 if (Attr.getNumArgs() != 1) {
1772 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1773 Attr.setInvalid();
1774 return;
1775 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001776 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001777
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001778 llvm::APSInt priority(32);
1779 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1780 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1781 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1782 << "init_priority" << priorityExpr->getSourceRange();
1783 Attr.setInvalid();
1784 return;
1785 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00001786 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001787 if (prioritynum < 101 || prioritynum > 65535) {
1788 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1789 << priorityExpr->getSourceRange();
1790 Attr.setInvalid();
1791 return;
1792 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001793 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1794 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001795}
1796
Mike Stumpd3bb5572009-07-24 19:02:52 +00001797/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1798/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001799static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001800
Chris Lattner4a927cb2008-06-28 23:36:30 +00001801 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001802 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001803 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001804 return;
1805 }
1806
Chris Lattner4a927cb2008-06-28 23:36:30 +00001807 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001808 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001809 return;
1810 }
1811
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001812 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001813 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001814 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001815 return;
1816 }
1817
Chandler Carruth743682b2010-11-16 08:35:43 +00001818 // In C++ the implicit 'this' function parameter also counts, and they are
1819 // counted from one.
1820 bool HasImplicitThisParam = isInstanceMethod(d);
1821 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001822 unsigned FirstIdx = 1;
1823
Daniel Dunbar07d07852009-10-18 21:17:35 +00001824 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001825
1826 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001827 if (Format.startswith("__") && Format.endswith("__"))
1828 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001829
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001830 // Check for supported formats.
1831 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001832
1833 if (Kind == IgnoredFormat)
1834 return;
1835
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001836 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001837 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001838 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001839 return;
1840 }
1841
1842 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001843 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001844 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001845 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1846 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001847 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001848 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001849 return;
1850 }
1851
1852 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001853 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001854 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001855 return;
1856 }
1857
1858 // FIXME: Do we need to bounds check?
1859 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001860
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001861 if (HasImplicitThisParam) {
1862 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00001863 S.Diag(Attr.getLoc(),
1864 diag::err_format_attribute_implicit_this_format_string)
1865 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001866 return;
1867 }
1868 ArgIdx--;
1869 }
Mike Stump11289f42009-09-09 15:08:12 +00001870
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001871 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001872 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001873
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001874 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001875 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001876 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1877 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001878 return;
1879 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001880 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001881 // FIXME: do we need to check if the type is NSString*? What are the
1882 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001883 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001884 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001885 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1886 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001887 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001888 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001889 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001890 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001891 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001892 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1893 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001894 return;
1895 }
1896
1897 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001898 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001899 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001900 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1901 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001902 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001903 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001904 return;
1905 }
1906
1907 // check if the function is variadic if the 3rd argument non-zero
1908 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001909 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001910 ++NumArgs; // +1 for ...
1911 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001912 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001913 return;
1914 }
1915 }
1916
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001917 // strftime requires FirstArg to be 0 because it doesn't read from any
1918 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001919 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001920 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001921 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1922 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001923 return;
1924 }
1925 // if 0 it disables parameter checking (to use with e.g. va_list)
1926 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001927 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001928 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001929 return;
1930 }
1931
Douglas Gregor88336832011-06-15 05:45:11 +00001932 // Check whether we already have an equivalent format attribute.
1933 for (specific_attr_iterator<FormatAttr>
1934 i = d->specific_attr_begin<FormatAttr>(),
1935 e = d->specific_attr_end<FormatAttr>();
1936 i != e ; ++i) {
1937 FormatAttr *f = *i;
1938 if (f->getType() == Format &&
1939 f->getFormatIdx() == (int)Idx.getZExtValue() &&
1940 f->getFirstArg() == (int)FirstArg.getZExtValue()) {
1941 // If we don't have a valid location for this attribute, adopt the
1942 // location.
1943 if (f->getLocation().isInvalid())
1944 f->setLocation(Attr.getLoc());
1945 return;
1946 }
1947 }
1948
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001949 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1950 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001951 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001952}
1953
Chris Lattnera663a0a2008-06-29 00:28:59 +00001954static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1955 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001956 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001957 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001958 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001959 return;
1960 }
1961
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001962 // Try to find the underlying union declaration.
1963 RecordDecl *RD = 0;
Richard Smithdda56e42011-04-15 14:24:37 +00001964 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001965 if (TD && TD->getUnderlyingType()->isUnionType())
1966 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1967 else
1968 RD = dyn_cast<RecordDecl>(d);
1969
1970 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001971 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001972 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001973 return;
1974 }
1975
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001976 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001977 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001978 diag::warn_transparent_union_attribute_not_definition);
1979 return;
1980 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001981
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001982 RecordDecl::field_iterator Field = RD->field_begin(),
1983 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001984 if (Field == FieldEnd) {
1985 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1986 return;
1987 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001988
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001989 FieldDecl *FirstField = *Field;
1990 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00001991 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001992 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00001993 diag::warn_transparent_union_attribute_floating)
1994 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001995 return;
1996 }
1997
1998 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1999 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
2000 for (; Field != FieldEnd; ++Field) {
2001 QualType FieldType = Field->getType();
2002 if (S.Context.getTypeSize(FieldType) != FirstSize ||
2003 S.Context.getTypeAlign(FieldType) != FirstAlign) {
2004 // Warn if we drop the attribute.
2005 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002006 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002007 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002008 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002009 diag::warn_transparent_union_attribute_field_size_align)
2010 << isSize << Field->getDeclName() << FieldBits;
2011 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002012 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00002013 diag::note_transparent_union_first_field_size_align)
2014 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00002015 return;
2016 }
2017 }
2018
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002019 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002020}
2021
Chris Lattnera663a0a2008-06-29 00:28:59 +00002022static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002023 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002024 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002025 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002026 return;
2027 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002028 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00002029 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002030
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002031 // Make sure that there is a string literal as the annotation's single
2032 // argument.
2033 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00002034 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002035 return;
2036 }
Eric Christopherbc638a82010-12-01 22:13:54 +00002037 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
2038 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002039}
2040
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002041static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002042 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00002043 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002044 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002045 return;
2046 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00002047
2048 //FIXME: The C++0x version of this attribute has more limited applicabilty
2049 // than GNU's, and should error out when it is used to specify a
2050 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002051
Chris Lattner4a927cb2008-06-28 23:36:30 +00002052 if (Attr.getNumArgs() == 0) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002053 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002054 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002055 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002056
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002057 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002058}
2059
2060void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2061 if (E->isTypeDependent() || E->isValueDependent()) {
2062 // Save dependent expressions in the AST to be instantiated.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002063 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002064 return;
2065 }
2066
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002067 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002068 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002069 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2070 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2071 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00002072 return;
2073 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002074 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002075 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2076 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002077 return;
2078 }
2079
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002080 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2081}
2082
2083void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2084 // FIXME: Cache the number on the Attr object if non-dependent?
2085 // FIXME: Perform checking of type validity
2086 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2087 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002088}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002089
Mike Stumpd3bb5572009-07-24 19:02:52 +00002090/// HandleModeAttr - This attribute modifies the width of a decl with primitive
2091/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002092///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002093/// Despite what would be logical, the mode attribute is a decl attribute, not a
2094/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2095/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00002096static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002097 // This attribute isn't documented, but glibc uses it. It changes
2098 // the width of an int or unsigned int to the specified size.
2099
2100 // Check that there aren't any arguments
2101 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002102 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002103 return;
2104 }
2105
2106 IdentifierInfo *Name = Attr.getParameterName();
2107 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002108 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002109 return;
2110 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002111
Daniel Dunbar07d07852009-10-18 21:17:35 +00002112 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002113
2114 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002115 if (Str.startswith("__") && Str.endswith("__"))
2116 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002117
2118 unsigned DestWidth = 0;
2119 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002120 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002121 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002122 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002123 switch (Str[0]) {
2124 case 'Q': DestWidth = 8; break;
2125 case 'H': DestWidth = 16; break;
2126 case 'S': DestWidth = 32; break;
2127 case 'D': DestWidth = 64; break;
2128 case 'X': DestWidth = 96; break;
2129 case 'T': DestWidth = 128; break;
2130 }
2131 if (Str[1] == 'F') {
2132 IntegerMode = false;
2133 } else if (Str[1] == 'C') {
2134 IntegerMode = false;
2135 ComplexMode = true;
2136 } else if (Str[1] != 'I') {
2137 DestWidth = 0;
2138 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002139 break;
2140 case 4:
2141 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2142 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002143 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002144 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002145 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002146 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002147 break;
2148 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002149 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002150 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002151 break;
2152 }
2153
2154 QualType OldTy;
Richard Smithdda56e42011-04-15 14:24:37 +00002155 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattneracbc2d22008-06-27 22:18:37 +00002156 OldTy = TD->getUnderlyingType();
2157 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2158 OldTy = VD->getType();
2159 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002160 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2161 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00002162 return;
2163 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002164
John McCall9dd450b2009-09-21 23:43:11 +00002165 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002166 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2167 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002168 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002169 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2170 } else if (ComplexMode) {
2171 if (!OldTy->isComplexType())
2172 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2173 } else {
2174 if (!OldTy->isFloatingType())
2175 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2176 }
2177
Mike Stump87c57ac2009-05-16 07:39:55 +00002178 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2179 // and friends, at least with glibc.
2180 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2181 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002182 // FIXME: Make sure floating-point mappings are accurate
2183 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002184 QualType NewTy;
2185 switch (DestWidth) {
2186 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002187 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002188 return;
2189 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002190 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002191 return;
2192 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002193 if (!IntegerMode) {
2194 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2195 return;
2196 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002197 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002198 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002199 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002200 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002201 break;
2202 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002203 if (!IntegerMode) {
2204 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2205 return;
2206 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002207 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002208 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002209 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002210 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002211 break;
2212 case 32:
2213 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002214 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002215 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002216 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002217 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002218 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002219 break;
2220 case 64:
2221 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002222 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002223 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00002224 if (S.Context.Target.getLongWidth() == 64)
2225 NewTy = S.Context.LongTy;
2226 else
2227 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002228 else
Chandler Carruth72343702010-01-26 06:39:24 +00002229 if (S.Context.Target.getLongWidth() == 64)
2230 NewTy = S.Context.UnsignedLongTy;
2231 else
2232 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002233 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002234 case 96:
2235 NewTy = S.Context.LongDoubleTy;
2236 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002237 case 128:
2238 if (!IntegerMode) {
2239 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2240 return;
2241 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002242 if (OldTy->isSignedIntegerType())
2243 NewTy = S.Context.Int128Ty;
2244 else
2245 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002246 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002247 }
2248
Eli Friedman4735374e2009-03-03 06:41:03 +00002249 if (ComplexMode) {
2250 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002251 }
2252
2253 // Install the new type.
Richard Smithdda56e42011-04-15 14:24:37 +00002254 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCall703a3f82009-10-24 08:00:42 +00002255 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00002256 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00002257 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002258 cast<ValueDecl>(D)->setType(NewTy);
2259}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002260
Mike Stump3722f582009-08-26 22:31:08 +00002261static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002262 // check the attribute arguments.
2263 if (Attr.getNumArgs() > 0) {
2264 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2265 return;
2266 }
Anders Carlsson63784f42009-02-13 08:11:52 +00002267
Anders Carlsson88097122009-02-19 19:16:48 +00002268 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002269 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002270 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00002271 return;
2272 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002273
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002274 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002275}
2276
Mike Stump3722f582009-08-26 22:31:08 +00002277static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00002278 // check the attribute arguments.
2279 if (Attr.getNumArgs() != 0) {
2280 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2281 return;
2282 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002283
Chris Lattner4225e232009-04-14 17:02:11 +00002284 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002285 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002286 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00002287 return;
2288 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002289
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002290 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002291}
2292
Chris Lattner3c77a352010-06-22 00:03:40 +00002293static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2294 Sema &S) {
2295 // check the attribute arguments.
2296 if (Attr.getNumArgs() != 0) {
2297 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2298 return;
2299 }
2300
2301 if (!isa<FunctionDecl>(d)) {
2302 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002303 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00002304 return;
2305 }
2306
Eric Christopherbc638a82010-12-01 22:13:54 +00002307 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2308 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002309}
2310
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002311static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2312 if (S.LangOpts.CUDA) {
2313 // check the attribute arguments.
Ted Kremenek1551d552011-04-15 05:49:29 +00002314 if (Attr.hasParameterOrArguments()) {
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002315 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2316 return;
2317 }
2318
2319 if (!isa<VarDecl>(d)) {
2320 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002321 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002322 return;
2323 }
2324
2325 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2326 } else {
2327 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2328 }
2329}
2330
2331static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2332 if (S.LangOpts.CUDA) {
2333 // check the attribute arguments.
2334 if (Attr.getNumArgs() != 0) {
2335 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2336 return;
2337 }
2338
2339 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2340 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002341 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002342 return;
2343 }
2344
2345 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2346 } else {
2347 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2348 }
2349}
2350
2351static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2352 if (S.LangOpts.CUDA) {
2353 // check the attribute arguments.
2354 if (Attr.getNumArgs() != 0) {
2355 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2356 return;
2357 }
2358
2359 if (!isa<FunctionDecl>(d)) {
2360 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002361 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002362 return;
2363 }
2364
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002365 FunctionDecl *FD = cast<FunctionDecl>(d);
2366 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00002367 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002368 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2369 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2370 << FD->getType()
2371 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2372 "void");
2373 } else {
2374 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2375 << FD->getType();
2376 }
2377 return;
2378 }
2379
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002380 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2381 } else {
2382 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2383 }
2384}
2385
2386static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2387 if (S.LangOpts.CUDA) {
2388 // check the attribute arguments.
2389 if (Attr.getNumArgs() != 0) {
2390 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2391 return;
2392 }
2393
2394 if (!isa<FunctionDecl>(d)) {
2395 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002396 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002397 return;
2398 }
2399
2400 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2401 } else {
2402 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2403 }
2404}
2405
2406static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2407 if (S.LangOpts.CUDA) {
2408 // check the attribute arguments.
2409 if (Attr.getNumArgs() != 0) {
2410 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2411 return;
2412 }
2413
2414 if (!isa<VarDecl>(d)) {
2415 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002416 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002417 return;
2418 }
2419
2420 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2421 } else {
2422 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2423 }
2424}
2425
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002426static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002427 // check the attribute arguments.
2428 if (Attr.getNumArgs() != 0) {
2429 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2430 return;
2431 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002432
Chris Lattner4225e232009-04-14 17:02:11 +00002433 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2434 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002435 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002436 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00002437 return;
2438 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002439
Douglas Gregor35b57532009-10-27 21:01:01 +00002440 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002441 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002442 return;
2443 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002444
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002445 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002446}
2447
John McCall3882ace2011-01-05 12:14:39 +00002448static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) {
2449 if (hasDeclarator(d)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002450
John McCall3882ace2011-01-05 12:14:39 +00002451 // Diagnostic is emitted elsewhere: here we store the (valid) attr
2452 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2453 CallingConv CC;
2454 if (S.CheckCallingConvAttr(attr, CC))
2455 return;
2456
2457 if (!isa<ObjCMethodDecl>(d)) {
2458 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002459 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002460 return;
2461 }
2462
2463 switch (attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002464 case AttributeList::AT_fastcall:
John McCall3882ace2011-01-05 12:14:39 +00002465 d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002466 return;
2467 case AttributeList::AT_stdcall:
John McCall3882ace2011-01-05 12:14:39 +00002468 d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002469 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00002470 case AttributeList::AT_thiscall:
John McCall3882ace2011-01-05 12:14:39 +00002471 d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00002472 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002473 case AttributeList::AT_cdecl:
John McCall3882ace2011-01-05 12:14:39 +00002474 d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002475 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002476 case AttributeList::AT_pascal:
John McCall3882ace2011-01-05 12:14:39 +00002477 d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00002478 return;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002479 case AttributeList::AT_pcs: {
2480 Expr *Arg = attr.getArg(0);
2481 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2482 if (Str == 0 || Str->isWide()) {
2483 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2484 << "pcs" << 1;
2485 attr.setInvalid();
2486 return;
2487 }
2488
2489 llvm::StringRef StrRef = Str->getString();
2490 PcsAttr::PCSType PCS;
2491 if (StrRef == "aapcs")
2492 PCS = PcsAttr::AAPCS;
2493 else if (StrRef == "aapcs-vfp")
2494 PCS = PcsAttr::AAPCS_VFP;
2495 else {
2496 S.Diag(attr.getLoc(), diag::err_invalid_pcs);
2497 attr.setInvalid();
2498 return;
2499 }
2500
2501 d->addAttr(::new (S.Context) PcsAttr(attr.getLoc(), S.Context, PCS));
2502 }
Abramo Bagnara50099372010-04-30 13:10:51 +00002503 default:
2504 llvm_unreachable("unexpected attribute kind");
2505 return;
2506 }
2507}
2508
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002509static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){
2510 assert(Attr.isInvalid() == false);
2511 d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
2512}
2513
John McCall3882ace2011-01-05 12:14:39 +00002514bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2515 if (attr.isInvalid())
2516 return true;
2517
Ted Kremenek1551d552011-04-15 05:49:29 +00002518 if ((attr.getNumArgs() != 0 &&
2519 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2520 attr.getParameterName()) {
John McCall3882ace2011-01-05 12:14:39 +00002521 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2522 attr.setInvalid();
2523 return true;
2524 }
2525
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002526 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2527 // move to TargetAttributesSema one day.
John McCall3882ace2011-01-05 12:14:39 +00002528 switch (attr.getKind()) {
2529 case AttributeList::AT_cdecl: CC = CC_C; break;
2530 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2531 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2532 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2533 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov231e8752011-04-14 20:06:49 +00002534 case AttributeList::AT_pcs: {
2535 Expr *Arg = attr.getArg(0);
2536 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2537 if (Str == 0 || Str->isWide()) {
2538 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2539 << "pcs" << 1;
2540 attr.setInvalid();
2541 return true;
2542 }
2543
2544 llvm::StringRef StrRef = Str->getString();
2545 if (StrRef == "aapcs") {
2546 CC = CC_AAPCS;
2547 break;
2548 } else if (StrRef == "aapcs-vfp") {
2549 CC = CC_AAPCS_VFP;
2550 break;
2551 }
2552 // FALLS THROUGH
2553 }
John McCall3882ace2011-01-05 12:14:39 +00002554 default: llvm_unreachable("unexpected attribute kind"); return true;
2555 }
2556
2557 return false;
2558}
2559
2560static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) {
2561 if (hasDeclarator(d)) return;
2562
2563 unsigned numParams;
2564 if (S.CheckRegparmAttr(attr, numParams))
2565 return;
2566
2567 if (!isa<ObjCMethodDecl>(d)) {
2568 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002569 << attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002570 return;
2571 }
Eli Friedman7044b762009-03-27 21:06:47 +00002572
John McCall3882ace2011-01-05 12:14:39 +00002573 d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams));
2574}
2575
2576/// Checks a regparm attribute, returning true if it is ill-formed and
2577/// otherwise setting numParams to the appropriate value.
2578bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) {
2579 if (attr.isInvalid())
2580 return true;
2581
2582 if (attr.getNumArgs() != 1) {
2583 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2584 attr.setInvalid();
2585 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002586 }
Eli Friedman7044b762009-03-27 21:06:47 +00002587
John McCall3882ace2011-01-05 12:14:39 +00002588 Expr *NumParamsExpr = attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00002589 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002590 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00002591 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
2592 Diag(attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00002593 << "regparm" << NumParamsExpr->getSourceRange();
John McCall3882ace2011-01-05 12:14:39 +00002594 attr.setInvalid();
2595 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002596 }
2597
John McCall3882ace2011-01-05 12:14:39 +00002598 if (Context.Target.getRegParmMax() == 0) {
2599 Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002600 << NumParamsExpr->getSourceRange();
John McCall3882ace2011-01-05 12:14:39 +00002601 attr.setInvalid();
2602 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002603 }
2604
John McCall3882ace2011-01-05 12:14:39 +00002605 numParams = NumParams.getZExtValue();
2606 if (numParams > Context.Target.getRegParmMax()) {
2607 Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2608 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
2609 attr.setInvalid();
2610 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002611 }
2612
John McCall3882ace2011-01-05 12:14:39 +00002613 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002614}
2615
Peter Collingbourne827301e2010-12-12 23:03:07 +00002616static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){
2617 if (S.LangOpts.CUDA) {
2618 // check the attribute arguments.
2619 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00002620 // FIXME: 0 is not okay.
2621 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002622 return;
2623 }
2624
2625 if (!isFunctionOrMethod(d)) {
2626 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002627 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002628 return;
2629 }
2630
2631 Expr *MaxThreadsExpr = Attr.getArg(0);
2632 llvm::APSInt MaxThreads(32);
2633 if (MaxThreadsExpr->isTypeDependent() ||
2634 MaxThreadsExpr->isValueDependent() ||
2635 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2636 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2637 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2638 return;
2639 }
2640
2641 llvm::APSInt MinBlocks(32);
2642 if (Attr.getNumArgs() > 1) {
2643 Expr *MinBlocksExpr = Attr.getArg(1);
2644 if (MinBlocksExpr->isTypeDependent() ||
2645 MinBlocksExpr->isValueDependent() ||
2646 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2647 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2648 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2649 return;
2650 }
2651 }
2652
2653 d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
2654 MaxThreads.getZExtValue(),
2655 MinBlocks.getZExtValue()));
2656 } else {
2657 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2658 }
2659}
2660
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002661//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002662// Checker-specific attribute handlers.
2663//===----------------------------------------------------------------------===//
2664
John McCalled433932011-01-25 03:31:58 +00002665static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2666 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2667}
2668static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2669 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2670}
2671
2672static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) {
2673 ParmVarDecl *param = dyn_cast<ParmVarDecl>(d);
2674 if (!param) {
2675 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002676 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00002677 return;
2678 }
2679
2680 bool typeOK, cf;
2681 if (attr.getKind() == AttributeList::AT_ns_consumed) {
2682 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2683 cf = false;
2684 } else {
2685 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2686 cf = true;
2687 }
2688
2689 if (!typeOK) {
2690 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2691 << SourceRange(attr.getLoc()) << attr.getName() << cf;
2692 return;
2693 }
2694
2695 if (cf)
2696 param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context));
2697 else
2698 param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context));
2699}
2700
2701static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr,
2702 Sema &S) {
2703 if (!isa<ObjCMethodDecl>(d)) {
2704 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002705 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00002706 return;
2707 }
2708
2709 d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context));
2710}
2711
2712static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr,
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002713 Sema &S) {
2714
John McCalled433932011-01-25 03:31:58 +00002715 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002716
Ted Kremenek3b204e42009-05-13 21:07:32 +00002717 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
John McCalled433932011-01-25 03:31:58 +00002718 returnType = MD->getResultType();
John McCall31168b02011-06-15 23:02:42 +00002719 else if (S.getLangOptions().ObjCAutoRefCount && hasDeclarator(d) &&
2720 (attr.getKind() == AttributeList::AT_ns_returns_retained))
2721 return; // ignore: was handled as a type attribute
Ted Kremenek3b204e42009-05-13 21:07:32 +00002722 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
John McCalled433932011-01-25 03:31:58 +00002723 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00002724 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002725 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCalled433932011-01-25 03:31:58 +00002726 << SourceRange(attr.getLoc()) << attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00002727 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002728 return;
2729 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002730
John McCalled433932011-01-25 03:31:58 +00002731 bool typeOK;
2732 bool cf;
2733 switch (attr.getKind()) {
2734 default: llvm_unreachable("invalid ownership attribute"); return;
2735 case AttributeList::AT_ns_returns_autoreleased:
2736 case AttributeList::AT_ns_returns_retained:
2737 case AttributeList::AT_ns_returns_not_retained:
2738 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2739 cf = false;
2740 break;
2741
2742 case AttributeList::AT_cf_returns_retained:
2743 case AttributeList::AT_cf_returns_not_retained:
2744 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2745 cf = true;
2746 break;
2747 }
2748
2749 if (!typeOK) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002750 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCalled433932011-01-25 03:31:58 +00002751 << SourceRange(attr.getLoc())
2752 << attr.getName() << isa<ObjCMethodDecl>(d) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002753 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00002754 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002755
John McCalled433932011-01-25 03:31:58 +00002756 switch (attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002757 default:
2758 assert(0 && "invalid ownership attribute");
2759 return;
John McCalled433932011-01-25 03:31:58 +00002760 case AttributeList::AT_ns_returns_autoreleased:
2761 d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(),
2762 S.Context));
2763 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00002764 case AttributeList::AT_cf_returns_not_retained:
John McCalled433932011-01-25 03:31:58 +00002765 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002766 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002767 return;
2768 case AttributeList::AT_ns_returns_not_retained:
John McCalled433932011-01-25 03:31:58 +00002769 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002770 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002771 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002772 case AttributeList::AT_cf_returns_retained:
John McCalled433932011-01-25 03:31:58 +00002773 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002774 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002775 return;
2776 case AttributeList::AT_ns_returns_retained:
John McCalled433932011-01-25 03:31:58 +00002777 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002778 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002779 return;
2780 };
2781}
2782
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00002783static void HandleObjCOwnershipAttr(Decl *d, const AttributeList &attr,
John McCall31168b02011-06-15 23:02:42 +00002784 Sema &S) {
2785 if (hasDeclarator(d)) return;
2786
2787 SourceLocation L = attr.getLoc();
2788 S.Diag(d->getLocStart(), diag::err_attribute_wrong_decl_type)
2789 << SourceRange(L, L) << attr.getName() << 12 /* variable */;
2790}
2791
2792static void HandleObjCPreciseLifetimeAttr(Decl *d, const AttributeList &attr,
2793 Sema &S) {
2794 if (!isa<VarDecl>(d) && !isa<FieldDecl>(d)) {
2795 SourceLocation L = attr.getLoc();
2796 S.Diag(d->getLocStart(), diag::err_attribute_wrong_decl_type)
2797 << SourceRange(L, L) << attr.getName() << 12 /* variable */;
2798 return;
2799 }
2800
2801 ValueDecl *vd = cast<ValueDecl>(d);
2802 QualType type = vd->getType();
2803
2804 if (!type->isDependentType() &&
2805 !type->isObjCLifetimeType()) {
2806 S.Diag(attr.getLoc(), diag::err_objc_precise_lifetime_bad_type)
2807 << type;
2808 return;
2809 }
2810
2811 Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime();
2812
2813 // If we have no lifetime yet, check the lifetime we're presumably
2814 // going to infer.
2815 if (lifetime == Qualifiers::OCL_None && !type->isDependentType())
2816 lifetime = type->getObjCARCImplicitLifetime();
2817
2818 switch (lifetime) {
2819 case Qualifiers::OCL_None:
2820 assert(type->isDependentType() &&
2821 "didn't infer lifetime for non-dependent type?");
2822 break;
2823
2824 case Qualifiers::OCL_Weak: // meaningful
2825 case Qualifiers::OCL_Strong: // meaningful
2826 break;
2827
2828 case Qualifiers::OCL_ExplicitNone:
2829 case Qualifiers::OCL_Autoreleasing:
2830 S.Diag(attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless)
2831 << (lifetime == Qualifiers::OCL_Autoreleasing);
2832 break;
2833 }
2834
2835 d->addAttr(::new (S.Context)
2836 ObjCPreciseLifetimeAttr(attr.getLoc(), S.Context));
2837}
2838
Charles Davis163855f2010-02-16 18:27:26 +00002839static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2840 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Picheta83957a2010-12-19 06:50:37 +00002841 Attr.getKind() == AttributeList::AT_dllexport ||
2842 Attr.getKind() == AttributeList::AT_uuid;
2843}
2844
2845//===----------------------------------------------------------------------===//
2846// Microsoft specific attribute handlers.
2847//===----------------------------------------------------------------------===//
2848
2849static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2850 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2851 // check the attribute arguments.
2852 if (Attr.getNumArgs() != 1) {
2853 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2854 return;
2855 }
2856 Expr *Arg = Attr.getArg(0);
2857 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichet7da11662010-12-20 01:41:49 +00002858 if (Str == 0 || Str->isWide()) {
2859 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2860 << "uuid" << 1;
2861 return;
2862 }
2863
2864 llvm::StringRef StrRef = Str->getString();
2865
2866 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2867 StrRef.back() == '}';
2868
2869 // Validate GUID length.
2870 if (IsCurly && StrRef.size() != 38) {
2871 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2872 return;
2873 }
2874 if (!IsCurly && StrRef.size() != 36) {
2875 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2876 return;
2877 }
2878
2879 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2880 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Anders Carlsson19588aa2011-01-23 21:07:30 +00002881 llvm::StringRef::iterator I = StrRef.begin();
2882 if (IsCurly) // Skip the optional '{'
2883 ++I;
2884
2885 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00002886 if (i == 8 || i == 13 || i == 18 || i == 23) {
2887 if (*I != '-') {
2888 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2889 return;
2890 }
2891 } else if (!isxdigit(*I)) {
2892 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2893 return;
2894 }
2895 I++;
2896 }
Francois Picheta83957a2010-12-19 06:50:37 +00002897
2898 d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
2899 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00002900 } else
Francois Picheta83957a2010-12-19 06:50:37 +00002901 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00002902}
2903
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002904//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002905// Top Level Sema Entry Points
2906//===----------------------------------------------------------------------===//
2907
Peter Collingbourneb331b262011-01-21 02:08:45 +00002908static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D,
2909 const AttributeList &Attr, Sema &S) {
2910 switch (Attr.getKind()) {
2911 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
2912 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
2913 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
2914 default:
2915 break;
2916 }
2917}
Abramo Bagnara50099372010-04-30 13:10:51 +00002918
Peter Collingbourneb331b262011-01-21 02:08:45 +00002919static void ProcessInheritableDeclAttr(Scope *scope, Decl *D,
2920 const AttributeList &Attr, Sema &S) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002921 switch (Attr.getKind()) {
Ted Kremenek1f672822010-02-18 03:08:58 +00002922 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00002923 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2924 case AttributeList::AT_IBOutletCollection:
2925 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002926 case AttributeList::AT_address_space:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002927 case AttributeList::AT_opencl_image_access:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00002928 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00002929 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00002930 case AttributeList::AT_neon_vector_type:
2931 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002932 // Ignore these, these are type attributes, handled by
2933 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002934 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00002935 case AttributeList::AT_device:
2936 case AttributeList::AT_host:
2937 case AttributeList::AT_overloadable:
2938 // Ignore, this is a non-inheritable attribute, handled
2939 // by ProcessNonInheritableDeclAttr.
2940 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002941 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2942 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002943 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00002944 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002945 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002946 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002947 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002948 case AttributeList::AT_availability:HandleAvailabilityAttr(D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002949 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00002950 HandleDependencyAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002951 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002952 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002953 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2954 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2955 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002956 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00002957 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002958 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002959 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2960 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002961 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002962 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002963 case AttributeList::AT_launch_bounds:
2964 HandleLaunchBoundsAttr(D, Attr, S);
2965 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002966 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2967 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00002968 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002969 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002970 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00002971 case AttributeList::AT_ownership_returns:
2972 case AttributeList::AT_ownership_takes:
2973 case AttributeList::AT_ownership_holds:
2974 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00002975 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002976 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2977 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002978 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002979 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002980
Argyrios Kyrtzidiscff00d92011-06-24 00:08:59 +00002981 case AttributeList::AT_objc_ownership:
2982 HandleObjCOwnershipAttr(D, Attr, S); break;
John McCall31168b02011-06-15 23:02:42 +00002983 case AttributeList::AT_objc_precise_lifetime:
2984 HandleObjCPreciseLifetimeAttr(D, Attr, S); break;
2985
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002986 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00002987 case AttributeList::AT_cf_consumed:
2988 case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break;
2989 case AttributeList::AT_ns_consumes_self:
2990 HandleNSConsumesSelfAttr(D, Attr, S); break;
2991
2992 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00002993 case AttributeList::AT_ns_returns_not_retained:
2994 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002995 case AttributeList::AT_ns_returns_retained:
2996 case AttributeList::AT_cf_returns_retained:
2997 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2998
Nate Begemanf2758702009-06-26 06:32:41 +00002999 case AttributeList::AT_reqd_wg_size:
3000 HandleReqdWorkGroupSize(D, Attr, S); break;
3001
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00003002 case AttributeList::AT_init_priority:
3003 HandleInitPriorityAttr(D, Attr, S); break;
3004
Alexis Hunt54a02542009-11-25 04:20:27 +00003005 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Fariborz Jahanian6b4e26b2011-04-26 17:54:40 +00003006 case AttributeList::AT_MsStruct: HandleMsStructAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00003007 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00003008 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
3009 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
3010 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00003011 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00003012 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
3013 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00003014 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindolac18086a2010-02-23 22:00:30 +00003015 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00003016 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003017 case AttributeList::AT_transparent_union:
3018 HandleTransparentUnionAttr(D, Attr, S);
3019 break;
Chris Lattner677a3582009-02-14 08:09:34 +00003020 case AttributeList::AT_objc_exception:
3021 HandleObjCExceptionAttr(D, Attr, S);
3022 break;
John McCall86bc21f2011-03-02 11:33:24 +00003023 case AttributeList::AT_objc_method_family:
3024 HandleObjCMethodFamilyAttr(D, Attr, S);
3025 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00003026 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
3027 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
3028 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
3029 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
3030 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
3031 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
3032 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
3033 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
3034 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00003035 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00003036 // Just ignore
3037 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00003038 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
3039 HandleNoInstrumentFunctionAttr(D, Attr, S);
3040 break;
John McCallab26cfa2010-02-05 21:31:56 +00003041 case AttributeList::AT_stdcall:
3042 case AttributeList::AT_cdecl:
3043 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003044 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003045 case AttributeList::AT_pascal:
Anton Korobeynikov231e8752011-04-14 20:06:49 +00003046 case AttributeList::AT_pcs:
Abramo Bagnara50099372010-04-30 13:10:51 +00003047 HandleCallConvAttr(D, Attr, S);
John McCallab26cfa2010-02-05 21:31:56 +00003048 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00003049 case AttributeList::AT_opencl_kernel_function:
3050 HandleOpenCLKernelAttr(D, Attr, S);
3051 break;
Francois Picheta83957a2010-12-19 06:50:37 +00003052 case AttributeList::AT_uuid:
3053 HandleUuidAttr(D, Attr, S);
3054 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003055 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00003056 // Ask target about the attribute.
3057 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
3058 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00003059 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
3060 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003061 break;
3062 }
3063}
3064
Peter Collingbourneb331b262011-01-21 02:08:45 +00003065/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
3066/// the attribute applies to decls. If the attribute is a type attribute, just
3067/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
3068/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
3069static void ProcessDeclAttribute(Scope *scope, Decl *D,
3070 const AttributeList &Attr, Sema &S,
3071 bool NonInheritable, bool Inheritable) {
3072 if (Attr.isInvalid())
3073 return;
3074
3075 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
3076 // FIXME: Try to deal with other __declspec attributes!
3077 return;
3078
3079 if (NonInheritable)
3080 ProcessNonInheritableDeclAttr(scope, D, Attr, S);
3081
3082 if (Inheritable)
3083 ProcessInheritableDeclAttr(scope, D, Attr, S);
3084}
3085
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003086/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
3087/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00003088void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00003089 const AttributeList *AttrList,
3090 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003091 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00003092 ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable);
Rafael Espindolac18086a2010-02-23 22:00:30 +00003093 }
3094
3095 // GCC accepts
3096 // static int a9 __attribute__((weakref));
3097 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003098 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00003099 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00003100 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00003101 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00003102 }
3103}
3104
Ryan Flynn7d470f32009-07-30 03:15:39 +00003105/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3106/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00003107NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00003108 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00003109 NamedDecl *NewD = 0;
3110 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3111 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003112 FD->getInnerLocStart(),
Ryan Flynn7d470f32009-07-30 03:15:39 +00003113 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00003114 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00003115 if (FD->getQualifier()) {
3116 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00003117 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00003118 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003119 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3120 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00003121 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00003122 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00003123 VD->getStorageClass(),
3124 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00003125 if (VD->getQualifier()) {
3126 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00003127 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00003128 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003129 }
3130 return NewD;
3131}
3132
3133/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3134/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00003135void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00003136 if (W.getUsed()) return; // only do this once
3137 W.setUsed(true);
3138 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3139 IdentifierInfo *NDId = ND->getIdentifier();
3140 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003141 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3142 NDId->getName()));
3143 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00003144 WeakTopLevelDecl.push_back(NewD);
3145 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3146 // to insert Decl at TU scope, sorry.
3147 DeclContext *SavedContext = CurContext;
3148 CurContext = Context.getTranslationUnitDecl();
3149 PushOnScopeChains(NewD, S);
3150 CurContext = SavedContext;
3151 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00003152 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00003153 }
3154}
3155
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003156/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3157/// it, apply them to D. This is a bit tricky because PD can have attributes
3158/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003159void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3160 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00003161 // It's valid to "forward-declare" #pragma weak, in which case we
3162 // have to do this.
Peter Collingbourneb331b262011-01-21 02:08:45 +00003163 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCall6fe02402010-10-27 00:59:00 +00003164 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3165 if (IdentifierInfo *Id = ND->getIdentifier()) {
3166 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3167 = WeakUndeclaredIdentifiers.find(Id);
3168 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3169 WeakInfo W = I->second;
3170 DeclApplyPragmaWeak(S, ND, W);
3171 WeakUndeclaredIdentifiers[Id] = W;
3172 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003173 }
3174 }
3175 }
3176
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003177 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00003178 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003179 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003180
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003181 // Walk the declarator structure, applying decl attributes that were in a type
3182 // position to the decl itself. This handles cases like:
3183 // int *__attr__(x)** D;
3184 // when X is a decl attribute.
3185 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3186 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003187 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003188
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003189 // Finally, apply any attributes on the decl itself.
3190 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003191 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003192}
John McCall28a6aea2009-11-04 02:18:39 +00003193
John McCall31168b02011-06-15 23:02:42 +00003194/// Is the given declaration allowed to use a forbidden type?
3195static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) {
3196 // Private ivars are always okay. Unfortunately, people don't
3197 // always properly make their ivars private, even in system headers.
3198 // Plus we need to make fields okay, too.
3199 if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl))
3200 return false;
3201
3202 // Require it to be declared in a system header.
3203 return S.Context.getSourceManager().isInSystemHeader(decl->getLocation());
3204}
3205
3206/// Handle a delayed forbidden-type diagnostic.
3207static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag,
3208 Decl *decl) {
3209 if (decl && isForbiddenTypeAllowed(S, decl)) {
3210 decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context,
3211 "this system declaration uses an unsupported type"));
3212 return;
3213 }
3214
3215 S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic())
3216 << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument();
3217 diag.Triggered = true;
3218}
3219
John McCallc1465822011-02-14 07:13:47 +00003220// This duplicates a vector push_back but hides the need to know the
3221// size of the type.
3222void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3223 assert(StackSize <= StackCapacity);
3224
3225 // Grow the stack if necessary.
3226 if (StackSize == StackCapacity) {
3227 unsigned newCapacity = 2 * StackCapacity + 2;
3228 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3229 const char *oldBuffer = (const char*) Stack;
3230
3231 if (StackCapacity)
3232 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3233
3234 delete[] oldBuffer;
3235 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3236 StackCapacity = newCapacity;
3237 }
3238
3239 assert(StackSize < StackCapacity);
3240 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall86121512010-01-27 03:50:35 +00003241}
3242
John McCallc1465822011-02-14 07:13:47 +00003243void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3244 Decl *decl) {
3245 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall86121512010-01-27 03:50:35 +00003246
John McCallc1465822011-02-14 07:13:47 +00003247 // Check the invariants.
3248 assert(DD.StackSize >= state.SavedStackSize);
3249 assert(state.SavedStackSize >= DD.ActiveStackBase);
3250 assert(DD.ParsingDepth > 0);
3251
3252 // Drop the parsing depth.
3253 DD.ParsingDepth--;
3254
3255 // If there are no active diagnostics, we're done.
3256 if (DD.StackSize == DD.ActiveStackBase)
John McCall86121512010-01-27 03:50:35 +00003257 return;
3258
John McCall86121512010-01-27 03:50:35 +00003259 // We only want to actually emit delayed diagnostics when we
3260 // successfully parsed a decl.
Argyrios Kyrtzidisd8a27712011-06-24 19:59:27 +00003261 if (decl && !decl->isInvalidDecl()) {
John McCallc1465822011-02-14 07:13:47 +00003262 // We emit all the active diagnostics, not just those starting
3263 // from the saved state. The idea is this: we get one push for a
John McCall86121512010-01-27 03:50:35 +00003264 // decl spec and another for each declarator; in a decl group like:
3265 // deprecated_typedef foo, *bar, baz();
3266 // only the declarator pops will be passed decls. This is correct;
3267 // we really do need to consider delayed diagnostics from the decl spec
3268 // for each of the different declarations.
John McCallc1465822011-02-14 07:13:47 +00003269 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3270 DelayedDiagnostic &diag = DD.Stack[i];
3271 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00003272 continue;
3273
John McCallc1465822011-02-14 07:13:47 +00003274 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00003275 case DelayedDiagnostic::Deprecation:
John McCallc1465822011-02-14 07:13:47 +00003276 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003277 break;
3278
3279 case DelayedDiagnostic::Access:
John McCallc1465822011-02-14 07:13:47 +00003280 S.HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003281 break;
John McCall31168b02011-06-15 23:02:42 +00003282
3283 case DelayedDiagnostic::ForbiddenType:
3284 handleDelayedForbiddenType(S, diag, decl);
3285 break;
John McCall86121512010-01-27 03:50:35 +00003286 }
3287 }
3288 }
3289
John McCall1064d7e2010-03-16 05:22:47 +00003290 // Destroy all the delayed diagnostics we're about to pop off.
John McCallc1465822011-02-14 07:13:47 +00003291 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor899b68f2011-03-23 15:13:44 +00003292 DD.Stack[i].Destroy();
John McCall1064d7e2010-03-16 05:22:47 +00003293
John McCallc1465822011-02-14 07:13:47 +00003294 DD.StackSize = state.SavedStackSize;
John McCall28a6aea2009-11-04 02:18:39 +00003295}
3296
3297static bool isDeclDeprecated(Decl *D) {
3298 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003299 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00003300 return true;
3301 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3302 return false;
3303}
3304
John McCallb45a1e72010-08-26 02:13:20 +00003305void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00003306 Decl *Ctx) {
3307 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00003308 return;
3309
John McCall86121512010-01-27 03:50:35 +00003310 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003311 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003312 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003313 << DD.getDeprecationDecl()->getDeclName()
3314 << DD.getDeprecationMessage();
Fariborz Jahanian551063102010-10-06 21:18:44 +00003315 else
3316 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003317 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00003318}
3319
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003320void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003321 SourceLocation Loc,
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003322 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00003323 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00003324 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3325 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall28a6aea2009-11-04 02:18:39 +00003326 return;
3327 }
3328
3329 // Otherwise, don't warn if our current context is deprecated.
3330 if (isDeclDeprecated(cast<Decl>(CurContext)))
3331 return;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003332 if (!Message.empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003333 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3334 << Message;
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003335 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00003336 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003337 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003338 else {
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003339 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahaniandbbdd2f2011-04-23 17:27:19 +00003340 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3341 }
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003342 }
John McCall28a6aea2009-11-04 02:18:39 +00003343}