blob: 5f6de1409e24f6d48585687ff8ef177f08305fd6 [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"
Chris Lattneracbc2d22008-06-27 22:18:37 +000020#include "clang/Basic/TargetInfo.h"
John McCall8b0666c2010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
John McCallb45a1e72010-08-26 02:13:20 +000022#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000024using namespace clang;
John McCallb45a1e72010-08-26 02:13:20 +000025using namespace sema;
Chris Lattner2c6fcf52008-06-26 18:38:35 +000026
John McCall5fca7ea2011-03-02 12:29:23 +000027/// These constants match the enumerated choices of
28/// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type.
29enum {
30 ExpectedFunction,
31 ExpectedUnion,
32 ExpectedVariableOrFunction,
33 ExpectedFunctionOrMethod,
34 ExpectedParameter,
35 ExpectedParameterOrMethod,
36 ExpectedFunctionMethodOrBlock,
37 ExpectedClassOrVirtualMethod,
38 ExpectedFunctionMethodOrParameter,
39 ExpectedClass,
40 ExpectedVirtualMethod,
41 ExpectedClassMember,
42 ExpectedVariable,
43 ExpectedMethod,
44 ExpectedVariableFunctionOrLabel
45};
46
Chris Lattner58418ff2008-06-29 00:16:31 +000047//===----------------------------------------------------------------------===//
48// Helper functions
49//===----------------------------------------------------------------------===//
50
Ted Kremenek527042b2009-08-14 20:49:40 +000051static const FunctionType *getFunctionType(const Decl *d,
52 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000053 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000054 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000055 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000056 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000057 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000058 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000059 Ty = decl->getUnderlyingType();
60 else
61 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000062
Chris Lattner2c6fcf52008-06-26 18:38:35 +000063 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000064 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000065 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000066 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000067
John McCall9dd450b2009-09-21 23:43:11 +000068 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000069}
70
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000071// FIXME: We should provide an abstraction around a method or function
72// to provide the following bits of information.
73
Nuno Lopes518e3702009-12-20 23:11:08 +000074/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000075/// type (function or function-typed variable).
76static bool isFunction(const Decl *d) {
77 return getFunctionType(d, false) != NULL;
78}
79
80/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000081/// type (function or function-typed variable) or an Objective-C
82/// method.
Ted Kremenek527042b2009-08-14 20:49:40 +000083static bool isFunctionOrMethod(const Decl *d) {
84 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000085}
86
Fariborz Jahanian4447e172009-05-15 23:15:03 +000087/// isFunctionOrMethodOrBlock - Return true if the given decl has function
88/// type (function or function-typed variable) or an Objective-C
89/// method or a block.
Ted Kremenek527042b2009-08-14 20:49:40 +000090static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000091 if (isFunctionOrMethod(d))
92 return true;
93 // check for block is more involved.
94 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
95 QualType Ty = V->getType();
96 return Ty->isBlockPointerType();
97 }
Fariborz Jahanian960910a2009-05-19 17:08:59 +000098 return isa<BlockDecl>(d);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000099}
100
John McCall3882ace2011-01-05 12:14:39 +0000101/// Return true if the given decl has a declarator that should have
102/// been processed by Sema::GetTypeForDeclarator.
103static bool hasDeclarator(const Decl *d) {
104 // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl.
105 return isa<DeclaratorDecl>(d) || isa<BlockDecl>(d) || isa<TypedefDecl>(d);
106}
107
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000108/// hasFunctionProto - Return true if the given decl has a argument
109/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000110/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +0000111static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000112 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000113 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +0000114 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000115 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000116 return true;
117 }
118}
119
120/// getFunctionOrMethodNumArgs - Return number of function or method
121/// arguments. It is an error to call this on a K&R function (use
122/// hasFunctionProto first).
Ted Kremenek527042b2009-08-14 20:49:40 +0000123static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +0000124 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000125 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000126 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
127 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +0000128 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000129}
130
Ted Kremenek527042b2009-08-14 20:49:40 +0000131static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000132 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000133 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000134 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
135 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000136
Chris Lattnera4997152009-02-20 18:43:26 +0000137 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000138}
139
Ted Kremenek527042b2009-08-14 20:49:40 +0000140static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000141 if (const FunctionType *FnTy = getFunctionType(d))
142 return cast<FunctionProtoType>(FnTy)->getResultType();
143 return cast<ObjCMethodDecl>(d)->getResultType();
144}
145
Ted Kremenek527042b2009-08-14 20:49:40 +0000146static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000147 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000148 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000149 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000150 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenek8af4f402010-04-29 16:48:58 +0000151 return BD->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000152 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000153 return cast<ObjCMethodDecl>(d)->isVariadic();
154 }
155}
156
Chandler Carruth743682b2010-11-16 08:35:43 +0000157static bool isInstanceMethod(const Decl *d) {
158 if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(d))
159 return MethodDecl->isInstance();
160 return false;
161}
162
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000163static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000164 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000165 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000166 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000167
John McCall96fa4842010-05-17 21:00:27 +0000168 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
169 if (!Cls)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000170 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000171
John McCall96fa4842010-05-17 21:00:27 +0000172 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000173
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000174 // FIXME: Should we walk the chain of classes?
175 return ClsName == &Ctx.Idents.get("NSString") ||
176 ClsName == &Ctx.Idents.get("NSMutableString");
177}
178
Daniel Dunbar980c6692008-09-26 03:32:58 +0000179static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000180 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000181 if (!PT)
182 return false;
183
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000184 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000185 if (!RT)
186 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000187
Daniel Dunbar980c6692008-09-26 03:32:58 +0000188 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara6150c882010-05-11 21:36:43 +0000189 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar980c6692008-09-26 03:32:58 +0000190 return false;
191
192 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
193}
194
Chris Lattner58418ff2008-06-29 00:16:31 +0000195//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000196// Attribute Implementations
197//===----------------------------------------------------------------------===//
198
Daniel Dunbar032db472008-07-31 22:40:48 +0000199// FIXME: All this manual attribute parsing code is gross. At the
200// least add some helper functions to check most argument patterns (#
201// and types of args).
202
Mike Stumpd3bb5572009-07-24 19:02:52 +0000203static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000204 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000205 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
206 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000207 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000208 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000209 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000210
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000211 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000212
213 Expr *sizeExpr;
214
215 // Special case where the argument is a template id.
216 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000217 CXXScopeSpec SS;
218 UnqualifiedId id;
219 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
220 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor758a8692009-06-17 21:51:59 +0000221 } else {
222 // check the attribute arguments.
223 if (Attr.getNumArgs() != 1) {
224 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
225 return;
226 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000227 sizeExpr = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000228 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000229
230 // Instantiate/Install the vector type, and let Sema build the type for us.
231 // This will run the reguired checks.
John McCallb268a282010-08-23 23:25:46 +0000232 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor758a8692009-06-17 21:51:59 +0000233 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000234 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000235 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000236
Douglas Gregor758a8692009-06-17 21:51:59 +0000237 // Remember this typedef decl, we will need it later for diagnostics.
238 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000239 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000240}
241
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000242static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000243 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000244 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000245 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000246 return;
247 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000248
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000249 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000250 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000251 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
252 // If the alignment is less than or equal to 8 bits, the packed attribute
253 // has no effect.
254 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000255 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000256 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000257 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000258 else
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000259 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000260 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000261 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000262}
263
Ted Kremenek1f672822010-02-18 03:08:58 +0000264static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000265 // check the attribute arguments.
266 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000268 return;
269 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000270
Ted Kremenek1f672822010-02-18 03:08:58 +0000271 // The IBAction attributes only apply to instance methods.
272 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
273 if (MD->isInstanceMethod()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000274 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000275 return;
276 }
277
Ted Kremenekd68ec812011-02-04 06:54:16 +0000278 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek1f672822010-02-18 03:08:58 +0000279}
280
281static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
282 // check the attribute arguments.
283 if (Attr.getNumArgs() > 0) {
284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
285 return;
286 }
287
288 // The IBOutlet attributes only apply to instance variables of
Ted Kremenek06be9682010-02-17 02:37:45 +0000289 // Objective-C classes.
290 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000291 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek1f672822010-02-18 03:08:58 +0000292 return;
Ted Kremenek06be9682010-02-17 02:37:45 +0000293 }
Ted Kremenek1f672822010-02-18 03:08:58 +0000294
Ted Kremenekd68ec812011-02-04 06:54:16 +0000295 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000296}
297
Ted Kremenek26bde772010-05-19 17:38:06 +0000298static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
299 Sema &S) {
300
301 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000302 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek26bde772010-05-19 17:38:06 +0000303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
304 return;
305 }
306
307 // The IBOutletCollection attributes only apply to instance variables of
308 // Objective-C classes.
309 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
Ted Kremenekd68ec812011-02-04 06:54:16 +0000310 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek26bde772010-05-19 17:38:06 +0000311 return;
312 }
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000313 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
314 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
315 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
316 << VD->getType() << 0;
317 return;
318 }
319 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
320 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
321 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
322 << PD->getType() << 1;
323 return;
324 }
325
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000326 IdentifierInfo *II = Attr.getParameterName();
327 if (!II)
328 II = &S.Context.Idents.get("id");
Fariborz Jahanian798f8322010-08-17 21:39:27 +0000329
John McCallba7bf592010-08-24 05:47:05 +0000330 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000331 S.getScopeForContext(d->getDeclContext()->getParent()));
332 if (!TypeRep) {
333 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
334 return;
335 }
John McCallba7bf592010-08-24 05:47:05 +0000336 QualType QT = TypeRep.get();
Fariborz Jahanianb5d59b62010-08-17 20:23:12 +0000337 // Diagnose use of non-object type in iboutletcollection attribute.
338 // FIXME. Gnu attribute extension ignores use of builtin types in
339 // attributes. So, __attribute__((iboutletcollection(char))) will be
340 // treated as __attribute__((iboutletcollection())).
341 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
342 !QT->isObjCObjectType()) {
343 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
344 return;
345 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000346 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
347 QT));
Ted Kremenek26bde772010-05-19 17:38:06 +0000348}
349
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000350static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000351 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
352 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000353 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000354 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000355 << Attr.getName() << ExpectedFunction;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000356 return;
357 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000358
Chandler Carruth743682b2010-11-16 08:35:43 +0000359 // In C++ the implicit 'this' function parameter also counts, and they are
360 // counted from one.
361 bool HasImplicitThisParam = isInstanceMethod(d);
362 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000363
364 // The nonnull attribute only applies to pointers.
365 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000366
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000367 for (AttributeList::arg_iterator I=Attr.arg_begin(),
368 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000369
370
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000371 // The argument must be an integer constant expression.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000372 Expr *Ex = *I;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000373 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000374 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
375 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000376 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
377 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000378 return;
379 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000380
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000381 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000382
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000383 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000384 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000385 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000386 return;
387 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000388
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000389 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000390 if (HasImplicitThisParam) {
391 if (x == 0) {
392 S.Diag(Attr.getLoc(),
393 diag::err_attribute_invalid_implicit_this_argument)
394 << "nonnull" << Ex->getSourceRange();
395 return;
396 }
397 --x;
398 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000399
400 // Is the function argument a pointer type?
Douglas Gregor1e2cdf52010-12-15 15:41:46 +0000401 QualType T = getFunctionOrMethodArgType(d, x).getNonReferenceType();
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000402 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000403 // FIXME: Should also highlight argument in decl.
Douglas Gregor62157e52010-08-12 18:48:43 +0000404 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattner3b054132008-11-19 05:08:23 +0000405 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000406 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000407 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000408
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000409 NonNullArgs.push_back(x);
410 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000411
412 // If no arguments were specified to __attribute__((nonnull)) then all pointer
413 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000414 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000415 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
Douglas Gregor1e2cdf52010-12-15 15:41:46 +0000416 QualType T = getFunctionOrMethodArgType(d, I).getNonReferenceType();
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000417 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000418 NonNullArgs.push_back(I);
Fariborz Jahanian3567c422010-09-27 22:42:37 +0000419 else if (const RecordType *UT = T->getAsUnionType()) {
420 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
421 RecordDecl *UD = UT->getDecl();
422 for (RecordDecl::field_iterator it = UD->field_begin(),
423 itend = UD->field_end(); it != itend; ++it) {
424 T = it->getType();
425 if (T->isAnyPointerType() || T->isBlockPointerType()) {
426 NonNullArgs.push_back(I);
427 break;
428 }
429 }
430 }
431 }
Ted Kremenek5fa50522008-11-18 06:52:58 +0000432 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000433
Ted Kremenek22813f42010-10-21 18:49:36 +0000434 // No pointer arguments?
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000435 if (NonNullArgs.empty()) {
436 // Warn the trivial case only if attribute is not coming from a
437 // macro instantiation.
438 if (Attr.getLoc().isFileID())
439 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000440 return;
Fariborz Jahaniancb67d7b2010-09-27 19:05:51 +0000441 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000442 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000443
444 unsigned* start = &NonNullArgs[0];
445 unsigned size = NonNullArgs.size();
Ted Kremenekd21139a2010-07-31 01:52:11 +0000446 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000447 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
448 size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000449}
450
Ted Kremenekd21139a2010-07-31 01:52:11 +0000451static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
452 // This attribute must be applied to a function declaration.
453 // The first argument to the attribute must be a string,
454 // the name of the resource, for example "malloc".
455 // The following arguments must be argument indexes, the arguments must be
456 // of integer type for Returns, otherwise of pointer type.
457 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000458 // after being held. free() should be __attribute((ownership_takes)), whereas
459 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekd21139a2010-07-31 01:52:11 +0000460
461 if (!AL.getParameterName()) {
462 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
463 << AL.getName()->getName() << 1;
464 return;
465 }
466 // Figure out our Kind, and check arguments while we're at it.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000467 OwnershipAttr::OwnershipKind K;
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000468 switch (AL.getKind()) {
469 case AttributeList::AT_ownership_takes:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000470 K = OwnershipAttr::Takes;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000471 if (AL.getNumArgs() < 1) {
472 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
473 return;
474 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000475 break;
476 case AttributeList::AT_ownership_holds:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000477 K = OwnershipAttr::Holds;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000478 if (AL.getNumArgs() < 1) {
479 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
480 return;
481 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000482 break;
483 case AttributeList::AT_ownership_returns:
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000484 K = OwnershipAttr::Returns;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000485 if (AL.getNumArgs() > 1) {
486 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
487 << AL.getNumArgs() + 1;
488 return;
489 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000490 break;
491 default:
492 // This should never happen given how we are called.
493 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000494 }
495
496 if (!isFunction(d) || !hasFunctionProto(d)) {
John McCall5fca7ea2011-03-02 12:29:23 +0000497 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
498 << AL.getName() << ExpectedFunction;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000499 return;
500 }
501
Chandler Carruth743682b2010-11-16 08:35:43 +0000502 // In C++ the implicit 'this' function parameter also counts, and they are
503 // counted from one.
504 bool HasImplicitThisParam = isInstanceMethod(d);
505 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000506
507 llvm::StringRef Module = AL.getParameterName()->getName();
508
509 // Normalize the argument, __foo__ becomes foo.
510 if (Module.startswith("__") && Module.endswith("__"))
511 Module = Module.substr(2, Module.size() - 4);
512
513 llvm::SmallVector<unsigned, 10> OwnershipArgs;
514
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000515 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
516 ++I) {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000517
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000518 Expr *IdxExpr = *I;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000519 llvm::APSInt ArgNum(32);
520 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
521 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
522 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
523 << AL.getName()->getName() << IdxExpr->getSourceRange();
524 continue;
525 }
526
527 unsigned x = (unsigned) ArgNum.getZExtValue();
528
529 if (x > NumArgs || x < 1) {
530 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
531 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
532 continue;
533 }
534 --x;
Chandler Carruth743682b2010-11-16 08:35:43 +0000535 if (HasImplicitThisParam) {
536 if (x == 0) {
537 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
538 << "ownership" << IdxExpr->getSourceRange();
539 return;
540 }
541 --x;
542 }
543
Ted Kremenekd21139a2010-07-31 01:52:11 +0000544 switch (K) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000545 case OwnershipAttr::Takes:
546 case OwnershipAttr::Holds: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000547 // Is the function argument a pointer type?
548 QualType T = getFunctionOrMethodArgType(d, x);
549 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
550 // FIXME: Should also highlight argument in decl.
551 S.Diag(AL.getLoc(), diag::err_ownership_type)
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000552 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekd21139a2010-07-31 01:52:11 +0000553 << "pointer"
554 << IdxExpr->getSourceRange();
555 continue;
556 }
557 break;
558 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000559 case OwnershipAttr::Returns: {
Ted Kremenekd21139a2010-07-31 01:52:11 +0000560 if (AL.getNumArgs() > 1) {
561 // Is the function argument an integer type?
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000562 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekd21139a2010-07-31 01:52:11 +0000563 llvm::APSInt ArgNum(32);
564 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
565 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
566 S.Diag(AL.getLoc(), diag::err_ownership_type)
567 << "ownership_returns" << "integer"
568 << IdxExpr->getSourceRange();
569 return;
570 }
571 }
572 break;
573 }
Jordy Rose5af0e3c2010-08-12 08:54:03 +0000574 default:
575 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekd21139a2010-07-31 01:52:11 +0000576 } // switch
577
578 // Check we don't have a conflict with another ownership attribute.
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000579 for (specific_attr_iterator<OwnershipAttr>
580 i = d->specific_attr_begin<OwnershipAttr>(),
581 e = d->specific_attr_end<OwnershipAttr>();
582 i != e; ++i) {
583 if ((*i)->getOwnKind() != K) {
584 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
585 I!=E; ++I) {
586 if (x == *I) {
587 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
588 << AL.getName()->getName() << "ownership_*";
Ted Kremenekd21139a2010-07-31 01:52:11 +0000589 }
590 }
591 }
592 }
593 OwnershipArgs.push_back(x);
594 }
595
596 unsigned* start = OwnershipArgs.data();
597 unsigned size = OwnershipArgs.size();
598 llvm::array_pod_sort(start, start + size);
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000599
600 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
601 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
602 return;
Ted Kremenekd21139a2010-07-31 01:52:11 +0000603 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000604
605 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
606 start, size));
Ted Kremenekd21139a2010-07-31 01:52:11 +0000607}
608
John McCall7a198ce2011-02-08 22:35:49 +0000609/// Whether this declaration has internal linkage for the purposes of
610/// things that want to complain about things not have internal linkage.
611static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
612 switch (D->getLinkage()) {
613 case NoLinkage:
614 case InternalLinkage:
615 return true;
616
617 // Template instantiations that go from external to unique-external
618 // shouldn't get diagnosed.
619 case UniqueExternalLinkage:
620 return true;
621
622 case ExternalLinkage:
623 return false;
624 }
625 llvm_unreachable("unknown linkage kind!");
Rafael Espindolac18086a2010-02-23 22:00:30 +0000626 return false;
627}
628
629static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
630 // Check the attribute arguments.
631 if (Attr.getNumArgs() > 1) {
632 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
633 return;
634 }
635
John McCall7a198ce2011-02-08 22:35:49 +0000636 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
637 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000638 << Attr.getName() << ExpectedVariableOrFunction;
John McCall7a198ce2011-02-08 22:35:49 +0000639 return;
640 }
641
642 NamedDecl *nd = cast<NamedDecl>(d);
643
Rafael Espindolac18086a2010-02-23 22:00:30 +0000644 // gcc rejects
645 // class c {
646 // static int a __attribute__((weakref ("v2")));
647 // static int b() __attribute__((weakref ("f3")));
648 // };
649 // and ignores the attributes of
650 // void f(void) {
651 // static int a __attribute__((weakref ("v2")));
652 // }
653 // we reject them
Sebastian Redl50c68252010-08-31 00:36:30 +0000654 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
655 if (!Ctx->isFileContext()) {
656 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall7a198ce2011-02-08 22:35:49 +0000657 nd->getNameAsString();
Sebastian Redl50c68252010-08-31 00:36:30 +0000658 return;
Rafael Espindolac18086a2010-02-23 22:00:30 +0000659 }
660
661 // The GCC manual says
662 //
663 // At present, a declaration to which `weakref' is attached can only
664 // be `static'.
665 //
666 // It also says
667 //
668 // Without a TARGET,
669 // given as an argument to `weakref' or to `alias', `weakref' is
670 // equivalent to `weak'.
671 //
672 // gcc 4.4.1 will accept
673 // int a7 __attribute__((weakref));
674 // as
675 // int a7 __attribute__((weak));
676 // This looks like a bug in gcc. We reject that for now. We should revisit
677 // it if this behaviour is actually used.
678
John McCall7a198ce2011-02-08 22:35:49 +0000679 if (!hasEffectivelyInternalLinkage(nd)) {
680 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000681 return;
682 }
683
684 // GCC rejects
685 // static ((alias ("y"), weakref)).
686 // Should we? How to check that weakref is before or after alias?
687
688 if (Attr.getNumArgs() == 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000689 Expr *Arg = Attr.getArg(0);
Rafael Espindolac18086a2010-02-23 22:00:30 +0000690 Arg = Arg->IgnoreParenCasts();
691 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
692
693 if (Str == 0 || Str->isWide()) {
694 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
695 << "weakref" << 1;
696 return;
697 }
698 // GCC will accept anything as the argument of weakref. Should we
699 // check for an existing decl?
Eric Christopherbc638a82010-12-01 22:13:54 +0000700 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
701 Str->getString()));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000702 }
703
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000704 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindolac18086a2010-02-23 22:00:30 +0000705}
706
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000707static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000708 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000709 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000710 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000711 return;
712 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000713
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000714 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000715 Arg = Arg->IgnoreParenCasts();
716 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000717
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000718 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000719 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000720 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000721 return;
722 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000723
Rafael Espindola0017c5f2010-12-07 15:23:23 +0000724 if (S.Context.Target.getTriple().getOS() == llvm::Triple::Darwin) {
725 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
726 return;
727 }
728
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000729 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000730
Eric Christopherbc638a82010-12-01 22:13:54 +0000731 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
732 Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000733}
734
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000735static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000736 Sema &S) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000737 // Check the attribute arguments.
Daniel Dunbar03a38442008-10-28 00:17:57 +0000738 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000739 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000740 return;
741 }
Anders Carlsson88097122009-02-19 19:16:48 +0000742
Chris Lattner4225e232009-04-14 17:02:11 +0000743 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000744 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000745 << Attr.getName() << ExpectedFunction;
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000746 return;
747 }
748
749 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
750}
751
752static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
753 Sema &S) {
754 // Check the attribute arguments.
755 if (Attr.getNumArgs() != 0) {
756 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
757 return;
758 }
759
760 if (!isa<FunctionDecl>(d)) {
761 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000762 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +0000763 return;
764 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000765
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000766 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbar03a38442008-10-28 00:17:57 +0000767}
768
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000769static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbar8caf6412010-09-29 18:20:25 +0000770 // Check the attribute arguments.
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000771 if (Attr.getNumArgs() != 0) {
772 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
773 return;
774 }
Mike Stump11289f42009-09-09 15:08:12 +0000775
Ted Kremenek08479ae2009-08-15 00:51:46 +0000776 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000777 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000778 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000779 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek08479ae2009-08-15 00:51:46 +0000780 return;
781 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000782 }
783
Ted Kremenek08479ae2009-08-15 00:51:46 +0000784 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000785}
786
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000787static void HandleMayAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
788 // check the attribute arguments.
789 if (Attr.getNumArgs() != 0) {
790 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
791 return;
792 }
793
Dan Gohmanbbb7d622010-11-17 00:03:07 +0000794 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
795}
796
Eric Christopher8a2ee392010-12-02 02:45:55 +0000797static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
798 assert(Attr.isInvalid() == false);
Eric Christopher515d87f2010-12-03 06:58:14 +0000799 if (isa<VarDecl>(d))
800 d->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
801 else
802 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000803 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000804}
805
806static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
807 assert(Attr.isInvalid() == false);
Eric Christopher515d87f2010-12-03 06:58:14 +0000808 if (isa<VarDecl>(d))
809 d->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
810 else
811 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000812 << Attr.getName() << ExpectedVariable;
Eric Christopher8a2ee392010-12-02 02:45:55 +0000813}
814
John McCall3882ace2011-01-05 12:14:39 +0000815static void HandleNoReturnAttr(Decl *d, const AttributeList &attr, Sema &S) {
816 if (hasDeclarator(d)) return;
817
818 if (S.CheckNoReturnAttr(attr)) return;
819
820 if (!isa<ObjCMethodDecl>(d)) {
821 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000822 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +0000823 return;
824 }
825
826 d->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
827}
828
829bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
830 if (attr.getNumArgs() != 0) {
831 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
832 attr.setInvalid();
833 return true;
834 }
835
836 return false;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000837}
838
839static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
840 Sema &S) {
Ted Kremenek5295ce82010-08-19 00:51:58 +0000841
842 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
843 // because 'analyzer_noreturn' does not impact the type.
844
845 if (Attr.getNumArgs() != 0) {
846 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
847 return;
848 }
849
850 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
851 ValueDecl *VD = dyn_cast<ValueDecl>(d);
852 if (VD == 0 || (!VD->getType()->isBlockPointerType()
853 && !VD->getType()->isFunctionPointerType())) {
854 S.Diag(Attr.getLoc(),
855 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
856 : diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000857 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenek5295ce82010-08-19 00:51:58 +0000858 return;
859 }
860 }
861
862 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000863}
864
John Thompsoncdb847ba2010-08-09 21:53:52 +0000865// PS3 PPU-specific.
866static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
867 Sema &S) {
868/*
869 Returning a Vector Class in Registers
870
Eric Christopherbc638a82010-12-01 22:13:54 +0000871 According to the PPU ABI specifications, a class with a single member of
872 vector type is returned in memory when used as the return value of a function.
873 This results in inefficient code when implementing vector classes. To return
874 the value in a single vector register, add the vecreturn attribute to the
875 class definition. This attribute is also applicable to struct types.
John Thompsoncdb847ba2010-08-09 21:53:52 +0000876
877 Example:
878
879 struct Vector
880 {
881 __vector float xyzw;
882 } __attribute__((vecreturn));
883
884 Vector Add(Vector lhs, Vector rhs)
885 {
886 Vector result;
887 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
888 return result; // This will be returned in a register
889 }
890*/
John Thompson9a587aaa2010-09-18 01:12:07 +0000891 if (!isa<RecordDecl>(d)) {
John Thompsoncdb847ba2010-08-09 21:53:52 +0000892 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000893 << Attr.getName() << ExpectedClass;
John Thompsoncdb847ba2010-08-09 21:53:52 +0000894 return;
895 }
896
897 if (d->getAttr<VecReturnAttr>()) {
898 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
899 return;
900 }
901
John Thompson9a587aaa2010-09-18 01:12:07 +0000902 RecordDecl *record = cast<RecordDecl>(d);
903 int count = 0;
904
905 if (!isa<CXXRecordDecl>(record)) {
906 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
907 return;
908 }
909
910 if (!cast<CXXRecordDecl>(record)->isPOD()) {
911 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
912 return;
913 }
914
Eric Christopherbc638a82010-12-01 22:13:54 +0000915 for (RecordDecl::field_iterator iter = record->field_begin();
916 iter != record->field_end(); iter++) {
John Thompson9a587aaa2010-09-18 01:12:07 +0000917 if ((count == 1) || !iter->getType()->isVectorType()) {
918 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
919 return;
920 }
921 count++;
922 }
923
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000924 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompsoncdb847ba2010-08-09 21:53:52 +0000925}
926
Alexis Hunt96d5c762009-11-21 08:43:09 +0000927static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
928 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
929 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000930 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Alexis Hunt96d5c762009-11-21 08:43:09 +0000931 return;
932 }
933 // FIXME: Actually store the attribute on the declaration
934}
935
Ted Kremenek39c59a82008-07-25 04:39:19 +0000936static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
937 // check the attribute arguments.
938 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000939 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000940 return;
941 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000942
John McCallcef15822010-03-31 02:47:45 +0000943 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
Chris Lattnercab02a62011-02-17 20:34:02 +0000944 !isa<TypeDecl>(d) && !isa<LabelDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000945 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000946 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000947 return;
948 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000949
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000950 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek39c59a82008-07-25 04:39:19 +0000951}
952
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000953static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
954 // check the attribute arguments.
955 if (Attr.getNumArgs() != 0) {
956 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
957 return;
958 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000959
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000960 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000961 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000962 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
963 return;
964 }
965 } else if (!isFunctionOrMethod(d)) {
966 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000967 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000968 return;
969 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000970
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000971 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000972}
973
Daniel Dunbar032db472008-07-31 22:40:48 +0000974static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
975 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +0000976 if (Attr.getNumArgs() > 1) {
977 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +0000978 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000979 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000980
981 int priority = 65535; // FIXME: Do not hardcode such constants.
982 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +0000983 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +0000984 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +0000985 if (E->isTypeDependent() || E->isValueDependent() ||
986 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000987 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000988 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000989 return;
990 }
991 priority = Idx.getZExtValue();
992 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000993
Chris Lattner4225e232009-04-14 17:02:11 +0000994 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000995 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +0000996 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +0000997 return;
998 }
999
Eric Christopherbc638a82010-12-01 22:13:54 +00001000 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
1001 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001002}
1003
1004static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1005 // check the attribute arguments.
John McCall80ee5962011-03-02 12:15:05 +00001006 if (Attr.getNumArgs() > 1) {
1007 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar032db472008-07-31 22:40:48 +00001008 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001009 }
Daniel Dunbar032db472008-07-31 22:40:48 +00001010
1011 int priority = 65535; // FIXME: Do not hardcode such constants.
1012 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001013 Expr *E = Attr.getArg(0);
Daniel Dunbar032db472008-07-31 22:40:48 +00001014 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001015 if (E->isTypeDependent() || E->isValueDependent() ||
1016 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001017 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001018 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +00001019 return;
1020 }
1021 priority = Idx.getZExtValue();
1022 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001023
Anders Carlsson8e82b7f2008-08-22 22:10:48 +00001024 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001025 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001026 << Attr.getName() << ExpectedFunction;
Daniel Dunbar032db472008-07-31 22:40:48 +00001027 return;
1028 }
1029
Eric Christopherbc638a82010-12-01 22:13:54 +00001030 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
1031 priority));
Daniel Dunbar032db472008-07-31 22:40:48 +00001032}
1033
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001034static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner190aa102011-02-24 05:42:24 +00001035 unsigned NumArgs = Attr.getNumArgs();
1036 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001037 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001038 return;
1039 }
Chris Lattner190aa102011-02-24 05:42:24 +00001040
Fariborz Jahanian551063102010-10-06 21:18:44 +00001041 // Handle the case where deprecated attribute has a text message.
Chris Lattner190aa102011-02-24 05:42:24 +00001042 llvm::StringRef Str;
1043 if (NumArgs == 1) {
1044 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanian551063102010-10-06 21:18:44 +00001045 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001046 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1047 << "deprecated";
Fariborz Jahanian551063102010-10-06 21:18:44 +00001048 return;
1049 }
Chris Lattner190aa102011-02-24 05:42:24 +00001050 Str = SE->getString();
Fariborz Jahanian551063102010-10-06 21:18:44 +00001051 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001052
Chris Lattner190aa102011-02-24 05:42:24 +00001053 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001054}
1055
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001056static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner190aa102011-02-24 05:42:24 +00001057 unsigned NumArgs = Attr.getNumArgs();
1058 if (NumArgs > 1) {
John McCall80ee5962011-03-02 12:15:05 +00001059 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001060 return;
1061 }
Chris Lattner190aa102011-02-24 05:42:24 +00001062
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001063 // Handle the case where unavailable attribute has a text message.
Chris Lattner190aa102011-02-24 05:42:24 +00001064 llvm::StringRef Str;
1065 if (NumArgs == 1) {
1066 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001067 if (!SE) {
Chris Lattner190aa102011-02-24 05:42:24 +00001068 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001069 diag::err_attribute_not_string) << "unavailable";
1070 return;
1071 }
Chris Lattner190aa102011-02-24 05:42:24 +00001072 Str = SE->getString();
Fariborz Jahanianc74073c2010-10-06 23:12:32 +00001073 }
Chris Lattner190aa102011-02-24 05:42:24 +00001074 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanian1470e932008-12-17 01:07:27 +00001075}
1076
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001077static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001078 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001079 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001080 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001081 return;
1082 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001083
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001084 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001085 Arg = Arg->IgnoreParenCasts();
1086 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001087
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001088 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001089 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001090 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001091 return;
1092 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001093
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001094 llvm::StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001095 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001096
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001097 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001098 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001099 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001100 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001101 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001102 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001103 else if (TypeStr == "protected")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001104 type = VisibilityAttr::Protected;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001105 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001106 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001107 return;
1108 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001109
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001110 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001111}
1112
John McCall86bc21f2011-03-02 11:33:24 +00001113static void HandleObjCMethodFamilyAttr(Decl *decl, const AttributeList &attr,
1114 Sema &S) {
1115 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1116 if (!method) {
1117 S.Diag(attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001118 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00001119 return;
1120 }
1121
1122 if (attr.getNumArgs() != 0 || !attr.getParameterName()) {
1123 if (!attr.getParameterName() && attr.getNumArgs() == 1) {
1124 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
1125 << "objc_method_family" << 1;
1126 } else {
1127 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1128 }
1129 attr.setInvalid();
1130 return;
1131 }
1132
1133 llvm::StringRef param = attr.getParameterName()->getName();
1134 ObjCMethodFamilyAttr::FamilyKind family;
1135 if (param == "none")
1136 family = ObjCMethodFamilyAttr::OMF_None;
1137 else if (param == "alloc")
1138 family = ObjCMethodFamilyAttr::OMF_alloc;
1139 else if (param == "copy")
1140 family = ObjCMethodFamilyAttr::OMF_copy;
1141 else if (param == "init")
1142 family = ObjCMethodFamilyAttr::OMF_init;
1143 else if (param == "mutableCopy")
1144 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1145 else if (param == "new")
1146 family = ObjCMethodFamilyAttr::OMF_new;
1147 else {
1148 // Just warn and ignore it. This is future-proof against new
1149 // families being used in system headers.
1150 S.Diag(attr.getParameterLoc(), diag::warn_unknown_method_family);
1151 return;
1152 }
1153
1154 decl->addAttr(new (S.Context) ObjCMethodFamilyAttr(attr.getLoc(),
1155 S.Context, family));
1156}
1157
Chris Lattner677a3582009-02-14 08:09:34 +00001158static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1159 Sema &S) {
1160 if (Attr.getNumArgs() != 0) {
1161 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1162 return;
1163 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001164
Chris Lattner677a3582009-02-14 08:09:34 +00001165 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1166 if (OCI == 0) {
1167 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1168 return;
1169 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001170
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001171 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001172}
1173
1174static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001175 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001176 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001177 return;
1178 }
Chris Lattner677a3582009-02-14 08:09:34 +00001179 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001180 QualType T = TD->getUnderlyingType();
1181 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001182 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001183 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1184 return;
1185 }
1186 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001187 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001188}
1189
Mike Stumpd3bb5572009-07-24 19:02:52 +00001190static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001191HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1192 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001193 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001194 return;
1195 }
1196
1197 if (!isa<FunctionDecl>(D)) {
1198 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1199 return;
1200 }
1201
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001202 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001203}
1204
Steve Naroff3405a732008-09-18 16:44:58 +00001205static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001206 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001207 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001208 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001209 return;
1210 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001211
Steve Naroff3405a732008-09-18 16:44:58 +00001212 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001214 return;
1215 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001216
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001217 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001218 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001219 type = BlocksAttr::ByRef;
1220 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001221 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001222 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001223 return;
1224 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001225
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001226 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001227}
1228
Anders Carlssonc181b012008-10-05 18:05:59 +00001229static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1230 // check the attribute arguments.
1231 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00001232 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00001233 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001234 }
1235
Anders Carlssonc181b012008-10-05 18:05:59 +00001236 int sentinel = 0;
1237 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001238 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00001239 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001240 if (E->isTypeDependent() || E->isValueDependent() ||
1241 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001242 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001243 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001244 return;
1245 }
1246 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001247
Anders Carlssonc181b012008-10-05 18:05:59 +00001248 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001249 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1250 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001251 return;
1252 }
1253 }
1254
1255 int nullPos = 0;
1256 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001257 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00001258 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001259 if (E->isTypeDependent() || E->isValueDependent() ||
1260 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001261 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001262 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001263 return;
1264 }
1265 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001266
Anders Carlssonc181b012008-10-05 18:05:59 +00001267 if (nullPos > 1 || nullPos < 0) {
1268 // FIXME: This error message could be improved, it would be nice
1269 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001270 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1271 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001272 return;
1273 }
1274 }
1275
1276 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +00001277 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001278 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001279
Chris Lattner9363e312009-03-17 23:03:47 +00001280 if (isa<FunctionNoProtoType>(FT)) {
1281 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1282 return;
1283 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001284
Chris Lattner9363e312009-03-17 23:03:47 +00001285 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001286 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001287 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001288 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001289 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1290 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001291 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001292 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001293 }
1294 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001295 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1296 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001297 ;
1298 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001299 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001300 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001301 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherbc638a82010-12-01 22:13:54 +00001302 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001303 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001304 int m = Ty->isFunctionPointerType() ? 0 : 1;
1305 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001306 return;
1307 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001308 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001309 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001310 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001311 return;
1312 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001313 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001314 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001315 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00001316 return;
1317 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001318 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1319 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001320}
1321
Chris Lattner237f2752009-02-14 07:37:35 +00001322static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1323 // check the attribute arguments.
1324 if (Attr.getNumArgs() != 0) {
1325 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1326 return;
1327 }
1328
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001329 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001330 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001331 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00001332 return;
1333 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001334
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001335 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1336 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1337 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001338 return;
1339 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001340 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1341 if (MD->getResultType()->isVoidType()) {
1342 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1343 << Attr.getName() << 1;
1344 return;
1345 }
1346
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001347 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001348}
1349
John McCall7a198ce2011-02-08 22:35:49 +00001350static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001351 // check the attribute arguments.
John McCall7a198ce2011-02-08 22:35:49 +00001352 if (attr.getNumArgs() != 0) {
1353 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001354 return;
1355 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001356
John McCall7a198ce2011-02-08 22:35:49 +00001357 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
1358 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001359 << attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001360 return;
1361 }
1362
John McCall7a198ce2011-02-08 22:35:49 +00001363 NamedDecl *nd = cast<NamedDecl>(d);
1364
1365 // 'weak' only applies to declarations with external linkage.
1366 if (hasEffectivelyInternalLinkage(nd)) {
1367 S.Diag(attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001368 return;
1369 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001370
John McCall7a198ce2011-02-08 22:35:49 +00001371 nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001372}
1373
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001374static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1375 // check the attribute arguments.
1376 if (Attr.getNumArgs() != 0) {
1377 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1378 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001379 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001380
1381 // weak_import only applies to variable & function declarations.
1382 bool isDef = false;
1383 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1384 isDef = (!VD->hasExternalStorage() || VD->getInit());
1385 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidis36ea3222010-07-07 11:31:19 +00001386 isDef = FD->hasBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +00001387 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
1388 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +00001389 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +00001390 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001391 // Don't issue the warning for darwin as target; yet, ignore the attribute.
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001392 if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001393 !isa<ObjCInterfaceDecl>(D))
1394 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001395 << Attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian5ea6bdd2010-04-12 16:57:31 +00001396 return;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001397 }
1398
1399 // Merge should handle any subsequent violations.
1400 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001401 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001402 diag::warn_attribute_weak_import_invalid_on_definition)
1403 << "weak_import" << 2 /*variable and function*/;
1404 return;
1405 }
1406
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001407 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001408}
1409
Nate Begemanf2758702009-06-26 06:32:41 +00001410static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1411 Sema &S) {
1412 // Attribute has 3 arguments.
1413 if (Attr.getNumArgs() != 3) {
John McCall61d82582010-05-28 18:25:28 +00001414 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begemanf2758702009-06-26 06:32:41 +00001415 return;
1416 }
1417
1418 unsigned WGSize[3];
1419 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001420 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00001421 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001422 if (E->isTypeDependent() || E->isValueDependent() ||
1423 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001424 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1425 << "reqd_work_group_size" << E->getSourceRange();
1426 return;
1427 }
1428 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1429 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001430 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1431 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001432 WGSize[2]));
1433}
1434
Chris Lattner237f2752009-02-14 07:37:35 +00001435static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001436 // Attribute has no arguments.
1437 if (Attr.getNumArgs() != 1) {
1438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1439 return;
1440 }
1441
1442 // Make sure that there is a string literal as the sections's single
1443 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001444 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001445 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001446 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001447 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001448 return;
1449 }
Mike Stump11289f42009-09-09 15:08:12 +00001450
Chris Lattner30ba6742009-08-10 19:03:04 +00001451 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001452 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001453 if (!Error.empty()) {
1454 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1455 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001456 return;
1457 }
Mike Stump11289f42009-09-09 15:08:12 +00001458
Chris Lattner20aee9b2010-01-12 20:58:53 +00001459 // This attribute cannot be applied to local variables.
1460 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1461 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1462 return;
1463 }
1464
Eric Christopherbc638a82010-12-01 22:13:54 +00001465 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1466 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001467}
1468
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001469
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001470static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001471 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001472 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001473 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001474 return;
1475 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001476
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001477 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001478}
1479
Anders Carlssonb8316282008-10-05 23:32:53 +00001480static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1481 // check the attribute arguments.
1482 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001484 return;
1485 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001486
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001487 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001488}
1489
1490static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1491 // check the attribute arguments.
1492 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001493 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001494 return;
1495 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001496
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001497 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001498}
1499
Anders Carlssond277d792009-01-31 01:16:18 +00001500static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001501 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001502 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1503 return;
1504 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001505
Anders Carlssond277d792009-01-31 01:16:18 +00001506 if (Attr.getNumArgs() != 0) {
1507 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1508 return;
1509 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001510
Anders Carlssond277d792009-01-31 01:16:18 +00001511 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001512
Anders Carlssond277d792009-01-31 01:16:18 +00001513 if (!VD || !VD->hasLocalStorage()) {
1514 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1515 return;
1516 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001517
Anders Carlssond277d792009-01-31 01:16:18 +00001518 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001519 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00001520 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001521 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1522 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001523 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001524 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001525 Attr.getParameterName();
1526 return;
1527 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001528
Anders Carlssond277d792009-01-31 01:16:18 +00001529 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1530 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001531 S.Diag(Attr.getParameterLoc(),
1532 diag::err_attribute_cleanup_arg_not_function)
1533 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001534 return;
1535 }
1536
Anders Carlssond277d792009-01-31 01:16:18 +00001537 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001538 S.Diag(Attr.getParameterLoc(),
1539 diag::err_attribute_cleanup_func_must_take_one_arg)
1540 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001541 return;
1542 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001543
Anders Carlsson723f55d2009-02-07 23:16:50 +00001544 // We're currently more strict than GCC about what function types we accept.
1545 // If this ever proves to be a problem it should be easy to fix.
1546 QualType Ty = S.Context.getPointerType(VD->getType());
1547 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00001548 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1549 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001550 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001551 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1552 Attr.getParameterName() << ParamTy << Ty;
1553 return;
1554 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001555
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001556 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidise88168a2010-12-06 17:51:53 +00001557 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00001558}
1559
Mike Stumpd3bb5572009-07-24 19:02:52 +00001560/// Handle __attribute__((format_arg((idx)))) attribute based on
1561/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1562static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001563 if (Attr.getNumArgs() != 1) {
1564 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1565 return;
1566 }
1567 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1568 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001569 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001570 return;
1571 }
Chandler Carruth743682b2010-11-16 08:35:43 +00001572
1573 // In C++ the implicit 'this' function parameter also counts, and they are
1574 // counted from one.
1575 bool HasImplicitThisParam = isInstanceMethod(d);
1576 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001577 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00001578
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001579 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001580 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001581 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001582 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1583 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001584 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1585 << "format" << 2 << IdxExpr->getSourceRange();
1586 return;
1587 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001588
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001589 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1590 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1591 << "format" << 2 << IdxExpr->getSourceRange();
1592 return;
1593 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001594
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001595 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001596
Chandler Carruth743682b2010-11-16 08:35:43 +00001597 if (HasImplicitThisParam) {
1598 if (ArgIdx == 0) {
1599 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1600 << "format_arg" << IdxExpr->getSourceRange();
1601 return;
1602 }
1603 ArgIdx--;
1604 }
1605
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001606 // make sure the format string is really a string
1607 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001608
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001609 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1610 if (not_nsstring_type &&
1611 !isCFStringType(Ty, S.Context) &&
1612 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001613 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001614 // FIXME: Should highlight the actual expression that has the wrong type.
1615 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001616 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001617 << IdxExpr->getSourceRange();
1618 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001619 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001620 Ty = getFunctionOrMethodResultType(d);
1621 if (!isNSStringType(Ty, S.Context) &&
1622 !isCFStringType(Ty, S.Context) &&
1623 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001624 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001625 // FIXME: Should highlight the actual expression that has the wrong type.
1626 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001627 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001628 << IdxExpr->getSourceRange();
1629 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001630 }
1631
Chandler Carruth743682b2010-11-16 08:35:43 +00001632 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1633 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001634}
1635
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001636enum FormatAttrKind {
1637 CFStringFormat,
1638 NSStringFormat,
1639 StrftimeFormat,
1640 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001641 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001642 InvalidFormat
1643};
1644
1645/// getFormatAttrKind - Map from format attribute names to supported format
1646/// types.
1647static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1648 // Check for formats that get handled specially.
1649 if (Format == "NSString")
1650 return NSStringFormat;
1651 if (Format == "CFString")
1652 return CFStringFormat;
1653 if (Format == "strftime")
1654 return StrftimeFormat;
1655
1656 // Otherwise, check for supported formats.
1657 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1658 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1659 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattner0ddd0ae2011-02-18 17:05:55 +00001660 Format == "zcmn_err" ||
1661 Format == "kprintf") // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001662 return SupportedFormat;
1663
Duncan Sandsde4fe352010-03-23 14:44:19 +00001664 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1665 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001666 return IgnoredFormat;
1667
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001668 return InvalidFormat;
1669}
1670
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001671/// Handle __attribute__((init_priority(priority))) attributes based on
1672/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1673static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1674 Sema &S) {
1675 if (!S.getLangOptions().CPlusPlus) {
1676 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1677 return;
1678 }
1679
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001680 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1681 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1682 Attr.setInvalid();
1683 return;
1684 }
1685 QualType T = dyn_cast<VarDecl>(d)->getType();
1686 if (S.Context.getAsArrayType(T))
1687 T = S.Context.getBaseElementType(T);
1688 if (!T->getAs<RecordType>()) {
1689 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1690 Attr.setInvalid();
1691 return;
1692 }
1693
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001694 if (Attr.getNumArgs() != 1) {
1695 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1696 Attr.setInvalid();
1697 return;
1698 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001699 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001700
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001701 llvm::APSInt priority(32);
1702 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1703 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1704 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1705 << "init_priority" << priorityExpr->getSourceRange();
1706 Attr.setInvalid();
1707 return;
1708 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00001709 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001710 if (prioritynum < 101 || prioritynum > 65535) {
1711 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1712 << priorityExpr->getSourceRange();
1713 Attr.setInvalid();
1714 return;
1715 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001716 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1717 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001718}
1719
Mike Stumpd3bb5572009-07-24 19:02:52 +00001720/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1721/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001722static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001723
Chris Lattner4a927cb2008-06-28 23:36:30 +00001724 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001725 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001726 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001727 return;
1728 }
1729
Chris Lattner4a927cb2008-06-28 23:36:30 +00001730 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001731 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001732 return;
1733 }
1734
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001735 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001736 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001737 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001738 return;
1739 }
1740
Chandler Carruth743682b2010-11-16 08:35:43 +00001741 // In C++ the implicit 'this' function parameter also counts, and they are
1742 // counted from one.
1743 bool HasImplicitThisParam = isInstanceMethod(d);
1744 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001745 unsigned FirstIdx = 1;
1746
Daniel Dunbar07d07852009-10-18 21:17:35 +00001747 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001748
1749 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001750 if (Format.startswith("__") && Format.endswith("__"))
1751 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001752
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001753 // Check for supported formats.
1754 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001755
1756 if (Kind == IgnoredFormat)
1757 return;
1758
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001759 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001760 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001761 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001762 return;
1763 }
1764
1765 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001766 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001767 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001768 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1769 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001770 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001771 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001772 return;
1773 }
1774
1775 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001776 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001777 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001778 return;
1779 }
1780
1781 // FIXME: Do we need to bounds check?
1782 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001783
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001784 if (HasImplicitThisParam) {
1785 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00001786 S.Diag(Attr.getLoc(),
1787 diag::err_format_attribute_implicit_this_format_string)
1788 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001789 return;
1790 }
1791 ArgIdx--;
1792 }
Mike Stump11289f42009-09-09 15:08:12 +00001793
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001794 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001795 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001796
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001797 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001798 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001799 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1800 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001801 return;
1802 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001803 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001804 // FIXME: do we need to check if the type is NSString*? What are the
1805 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001806 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001807 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001808 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1809 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001810 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001811 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001812 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001813 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001814 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001815 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1816 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001817 return;
1818 }
1819
1820 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001821 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001822 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001823 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1824 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001825 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001826 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001827 return;
1828 }
1829
1830 // check if the function is variadic if the 3rd argument non-zero
1831 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001832 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001833 ++NumArgs; // +1 for ...
1834 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001835 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001836 return;
1837 }
1838 }
1839
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001840 // strftime requires FirstArg to be 0 because it doesn't read from any
1841 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001842 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001843 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001844 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1845 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001846 return;
1847 }
1848 // if 0 it disables parameter checking (to use with e.g. va_list)
1849 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001850 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001851 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001852 return;
1853 }
1854
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001855 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1856 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001857 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001858}
1859
Chris Lattnera663a0a2008-06-29 00:28:59 +00001860static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1861 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001862 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001863 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001864 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001865 return;
1866 }
1867
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001868 // Try to find the underlying union declaration.
1869 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001870 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001871 if (TD && TD->getUnderlyingType()->isUnionType())
1872 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1873 else
1874 RD = dyn_cast<RecordDecl>(d);
1875
1876 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001877 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001878 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001879 return;
1880 }
1881
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001882 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001883 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001884 diag::warn_transparent_union_attribute_not_definition);
1885 return;
1886 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001887
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001888 RecordDecl::field_iterator Field = RD->field_begin(),
1889 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001890 if (Field == FieldEnd) {
1891 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1892 return;
1893 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001894
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001895 FieldDecl *FirstField = *Field;
1896 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00001897 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001898 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00001899 diag::warn_transparent_union_attribute_floating)
1900 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001901 return;
1902 }
1903
1904 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1905 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1906 for (; Field != FieldEnd; ++Field) {
1907 QualType FieldType = Field->getType();
1908 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1909 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1910 // Warn if we drop the attribute.
1911 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001912 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001913 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001914 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001915 diag::warn_transparent_union_attribute_field_size_align)
1916 << isSize << Field->getDeclName() << FieldBits;
1917 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001918 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001919 diag::note_transparent_union_first_field_size_align)
1920 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001921 return;
1922 }
1923 }
1924
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001925 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001926}
1927
Chris Lattnera663a0a2008-06-29 00:28:59 +00001928static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001929 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001930 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001931 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001932 return;
1933 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001934 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001935 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001936
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001937 // Make sure that there is a string literal as the annotation's single
1938 // argument.
1939 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001940 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001941 return;
1942 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001943 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1944 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001945}
1946
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001947static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001948 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001949 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001950 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001951 return;
1952 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001953
1954 //FIXME: The C++0x version of this attribute has more limited applicabilty
1955 // than GNU's, and should error out when it is used to specify a
1956 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001957
Chris Lattner4a927cb2008-06-28 23:36:30 +00001958 if (Attr.getNumArgs() == 0) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001959 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001960 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001961 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001962
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001963 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001964}
1965
1966void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
1967 if (E->isTypeDependent() || E->isValueDependent()) {
1968 // Save dependent expressions in the AST to be instantiated.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001969 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001970 return;
1971 }
1972
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001973 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00001974 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001975 if (!E->isIntegerConstantExpr(Alignment, Context)) {
1976 Diag(AttrLoc, diag::err_attribute_argument_not_int)
1977 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001978 return;
1979 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001980 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001981 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
1982 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001983 return;
1984 }
1985
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001986 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
1987}
1988
1989void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
1990 // FIXME: Cache the number on the Attr object if non-dependent?
1991 // FIXME: Perform checking of type validity
1992 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
1993 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001994}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001995
Mike Stumpd3bb5572009-07-24 19:02:52 +00001996/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1997/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001998///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001999/// Despite what would be logical, the mode attribute is a decl attribute, not a
2000/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2001/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00002002static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002003 // This attribute isn't documented, but glibc uses it. It changes
2004 // the width of an int or unsigned int to the specified size.
2005
2006 // Check that there aren't any arguments
2007 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002008 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002009 return;
2010 }
2011
2012 IdentifierInfo *Name = Attr.getParameterName();
2013 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002014 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002015 return;
2016 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002017
Daniel Dunbar07d07852009-10-18 21:17:35 +00002018 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002019
2020 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002021 if (Str.startswith("__") && Str.endswith("__"))
2022 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002023
2024 unsigned DestWidth = 0;
2025 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002026 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002027 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002028 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002029 switch (Str[0]) {
2030 case 'Q': DestWidth = 8; break;
2031 case 'H': DestWidth = 16; break;
2032 case 'S': DestWidth = 32; break;
2033 case 'D': DestWidth = 64; break;
2034 case 'X': DestWidth = 96; break;
2035 case 'T': DestWidth = 128; break;
2036 }
2037 if (Str[1] == 'F') {
2038 IntegerMode = false;
2039 } else if (Str[1] == 'C') {
2040 IntegerMode = false;
2041 ComplexMode = true;
2042 } else if (Str[1] != 'I') {
2043 DestWidth = 0;
2044 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002045 break;
2046 case 4:
2047 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2048 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002049 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002050 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002051 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002052 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002053 break;
2054 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002055 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002056 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002057 break;
2058 }
2059
2060 QualType OldTy;
2061 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
2062 OldTy = TD->getUnderlyingType();
2063 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2064 OldTy = VD->getType();
2065 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002066 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2067 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00002068 return;
2069 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002070
John McCall9dd450b2009-09-21 23:43:11 +00002071 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002072 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2073 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002074 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002075 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2076 } else if (ComplexMode) {
2077 if (!OldTy->isComplexType())
2078 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2079 } else {
2080 if (!OldTy->isFloatingType())
2081 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2082 }
2083
Mike Stump87c57ac2009-05-16 07:39:55 +00002084 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2085 // and friends, at least with glibc.
2086 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2087 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002088 // FIXME: Make sure floating-point mappings are accurate
2089 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002090 QualType NewTy;
2091 switch (DestWidth) {
2092 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002093 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002094 return;
2095 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002096 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002097 return;
2098 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002099 if (!IntegerMode) {
2100 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2101 return;
2102 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002103 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002104 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002105 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002106 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002107 break;
2108 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002109 if (!IntegerMode) {
2110 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2111 return;
2112 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002113 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002114 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002115 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002116 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002117 break;
2118 case 32:
2119 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002120 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002121 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002122 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002123 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002124 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002125 break;
2126 case 64:
2127 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002128 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002129 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00002130 if (S.Context.Target.getLongWidth() == 64)
2131 NewTy = S.Context.LongTy;
2132 else
2133 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002134 else
Chandler Carruth72343702010-01-26 06:39:24 +00002135 if (S.Context.Target.getLongWidth() == 64)
2136 NewTy = S.Context.UnsignedLongTy;
2137 else
2138 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002139 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002140 case 96:
2141 NewTy = S.Context.LongDoubleTy;
2142 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002143 case 128:
2144 if (!IntegerMode) {
2145 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2146 return;
2147 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002148 if (OldTy->isSignedIntegerType())
2149 NewTy = S.Context.Int128Ty;
2150 else
2151 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002152 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002153 }
2154
Eli Friedman4735374e2009-03-03 06:41:03 +00002155 if (ComplexMode) {
2156 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002157 }
2158
2159 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00002160 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2161 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00002162 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00002163 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002164 cast<ValueDecl>(D)->setType(NewTy);
2165}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002166
Mike Stump3722f582009-08-26 22:31:08 +00002167static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002168 // check the attribute arguments.
2169 if (Attr.getNumArgs() > 0) {
2170 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2171 return;
2172 }
Anders Carlsson63784f42009-02-13 08:11:52 +00002173
Anders Carlsson88097122009-02-19 19:16:48 +00002174 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002175 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002176 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00002177 return;
2178 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002179
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002180 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002181}
2182
Mike Stump3722f582009-08-26 22:31:08 +00002183static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00002184 // check the attribute arguments.
2185 if (Attr.getNumArgs() != 0) {
2186 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2187 return;
2188 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002189
Chris Lattner4225e232009-04-14 17:02:11 +00002190 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002191 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002192 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00002193 return;
2194 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002195
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002196 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002197}
2198
Chris Lattner3c77a352010-06-22 00:03:40 +00002199static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2200 Sema &S) {
2201 // check the attribute arguments.
2202 if (Attr.getNumArgs() != 0) {
2203 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2204 return;
2205 }
2206
2207 if (!isa<FunctionDecl>(d)) {
2208 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002209 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00002210 return;
2211 }
2212
Eric Christopherbc638a82010-12-01 22:13:54 +00002213 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2214 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002215}
2216
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002217static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2218 if (S.LangOpts.CUDA) {
2219 // check the attribute arguments.
2220 if (Attr.getNumArgs() != 0) {
2221 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2222 return;
2223 }
2224
2225 if (!isa<VarDecl>(d)) {
2226 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002227 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002228 return;
2229 }
2230
2231 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2232 } else {
2233 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2234 }
2235}
2236
2237static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2238 if (S.LangOpts.CUDA) {
2239 // check the attribute arguments.
2240 if (Attr.getNumArgs() != 0) {
2241 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2242 return;
2243 }
2244
2245 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2246 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002247 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002248 return;
2249 }
2250
2251 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2252 } else {
2253 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2254 }
2255}
2256
2257static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2258 if (S.LangOpts.CUDA) {
2259 // check the attribute arguments.
2260 if (Attr.getNumArgs() != 0) {
2261 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2262 return;
2263 }
2264
2265 if (!isa<FunctionDecl>(d)) {
2266 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002267 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002268 return;
2269 }
2270
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002271 FunctionDecl *FD = cast<FunctionDecl>(d);
2272 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00002273 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002274 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2275 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2276 << FD->getType()
2277 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2278 "void");
2279 } else {
2280 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2281 << FD->getType();
2282 }
2283 return;
2284 }
2285
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002286 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2287 } else {
2288 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2289 }
2290}
2291
2292static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2293 if (S.LangOpts.CUDA) {
2294 // check the attribute arguments.
2295 if (Attr.getNumArgs() != 0) {
2296 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2297 return;
2298 }
2299
2300 if (!isa<FunctionDecl>(d)) {
2301 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002302 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002303 return;
2304 }
2305
2306 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2307 } else {
2308 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2309 }
2310}
2311
2312static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2313 if (S.LangOpts.CUDA) {
2314 // check the attribute arguments.
2315 if (Attr.getNumArgs() != 0) {
2316 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2317 return;
2318 }
2319
2320 if (!isa<VarDecl>(d)) {
2321 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002322 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002323 return;
2324 }
2325
2326 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2327 } else {
2328 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2329 }
2330}
2331
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002332static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002333 // check the attribute arguments.
2334 if (Attr.getNumArgs() != 0) {
2335 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2336 return;
2337 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002338
Chris Lattner4225e232009-04-14 17:02:11 +00002339 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2340 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002341 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002342 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00002343 return;
2344 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002345
Douglas Gregor35b57532009-10-27 21:01:01 +00002346 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002347 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002348 return;
2349 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002350
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002351 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002352}
2353
John McCall3882ace2011-01-05 12:14:39 +00002354static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) {
2355 if (hasDeclarator(d)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002356
John McCall3882ace2011-01-05 12:14:39 +00002357 // Diagnostic is emitted elsewhere: here we store the (valid) attr
2358 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2359 CallingConv CC;
2360 if (S.CheckCallingConvAttr(attr, CC))
2361 return;
2362
2363 if (!isa<ObjCMethodDecl>(d)) {
2364 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002365 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002366 return;
2367 }
2368
2369 switch (attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002370 case AttributeList::AT_fastcall:
John McCall3882ace2011-01-05 12:14:39 +00002371 d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002372 return;
2373 case AttributeList::AT_stdcall:
John McCall3882ace2011-01-05 12:14:39 +00002374 d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002375 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00002376 case AttributeList::AT_thiscall:
John McCall3882ace2011-01-05 12:14:39 +00002377 d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00002378 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002379 case AttributeList::AT_cdecl:
John McCall3882ace2011-01-05 12:14:39 +00002380 d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002381 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002382 case AttributeList::AT_pascal:
John McCall3882ace2011-01-05 12:14:39 +00002383 d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00002384 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002385 default:
2386 llvm_unreachable("unexpected attribute kind");
2387 return;
2388 }
2389}
2390
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002391static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){
2392 assert(Attr.isInvalid() == false);
2393 d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
2394}
2395
John McCall3882ace2011-01-05 12:14:39 +00002396bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2397 if (attr.isInvalid())
2398 return true;
2399
2400 if (attr.getNumArgs() != 0) {
2401 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2402 attr.setInvalid();
2403 return true;
2404 }
2405
2406 // TODO: diagnose uses of these conventions on the wrong target.
2407 switch (attr.getKind()) {
2408 case AttributeList::AT_cdecl: CC = CC_C; break;
2409 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2410 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2411 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2412 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
2413 default: llvm_unreachable("unexpected attribute kind"); return true;
2414 }
2415
2416 return false;
2417}
2418
2419static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) {
2420 if (hasDeclarator(d)) return;
2421
2422 unsigned numParams;
2423 if (S.CheckRegparmAttr(attr, numParams))
2424 return;
2425
2426 if (!isa<ObjCMethodDecl>(d)) {
2427 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002428 << attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002429 return;
2430 }
Eli Friedman7044b762009-03-27 21:06:47 +00002431
John McCall3882ace2011-01-05 12:14:39 +00002432 d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams));
2433}
2434
2435/// Checks a regparm attribute, returning true if it is ill-formed and
2436/// otherwise setting numParams to the appropriate value.
2437bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) {
2438 if (attr.isInvalid())
2439 return true;
2440
2441 if (attr.getNumArgs() != 1) {
2442 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2443 attr.setInvalid();
2444 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002445 }
Eli Friedman7044b762009-03-27 21:06:47 +00002446
John McCall3882ace2011-01-05 12:14:39 +00002447 Expr *NumParamsExpr = attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00002448 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002449 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00002450 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
2451 Diag(attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00002452 << "regparm" << NumParamsExpr->getSourceRange();
John McCall3882ace2011-01-05 12:14:39 +00002453 attr.setInvalid();
2454 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002455 }
2456
John McCall3882ace2011-01-05 12:14:39 +00002457 if (Context.Target.getRegParmMax() == 0) {
2458 Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002459 << NumParamsExpr->getSourceRange();
John McCall3882ace2011-01-05 12:14:39 +00002460 attr.setInvalid();
2461 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002462 }
2463
John McCall3882ace2011-01-05 12:14:39 +00002464 numParams = NumParams.getZExtValue();
2465 if (numParams > Context.Target.getRegParmMax()) {
2466 Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2467 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
2468 attr.setInvalid();
2469 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002470 }
2471
John McCall3882ace2011-01-05 12:14:39 +00002472 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002473}
2474
Peter Collingbourne827301e2010-12-12 23:03:07 +00002475static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){
2476 if (S.LangOpts.CUDA) {
2477 // check the attribute arguments.
2478 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00002479 // FIXME: 0 is not okay.
2480 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002481 return;
2482 }
2483
2484 if (!isFunctionOrMethod(d)) {
2485 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002486 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002487 return;
2488 }
2489
2490 Expr *MaxThreadsExpr = Attr.getArg(0);
2491 llvm::APSInt MaxThreads(32);
2492 if (MaxThreadsExpr->isTypeDependent() ||
2493 MaxThreadsExpr->isValueDependent() ||
2494 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2495 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2496 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2497 return;
2498 }
2499
2500 llvm::APSInt MinBlocks(32);
2501 if (Attr.getNumArgs() > 1) {
2502 Expr *MinBlocksExpr = Attr.getArg(1);
2503 if (MinBlocksExpr->isTypeDependent() ||
2504 MinBlocksExpr->isValueDependent() ||
2505 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2506 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2507 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2508 return;
2509 }
2510 }
2511
2512 d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
2513 MaxThreads.getZExtValue(),
2514 MinBlocks.getZExtValue()));
2515 } else {
2516 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2517 }
2518}
2519
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002520//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002521// Checker-specific attribute handlers.
2522//===----------------------------------------------------------------------===//
2523
John McCalled433932011-01-25 03:31:58 +00002524static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2525 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2526}
2527static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2528 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2529}
2530
2531static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) {
2532 ParmVarDecl *param = dyn_cast<ParmVarDecl>(d);
2533 if (!param) {
2534 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002535 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00002536 return;
2537 }
2538
2539 bool typeOK, cf;
2540 if (attr.getKind() == AttributeList::AT_ns_consumed) {
2541 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2542 cf = false;
2543 } else {
2544 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2545 cf = true;
2546 }
2547
2548 if (!typeOK) {
2549 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2550 << SourceRange(attr.getLoc()) << attr.getName() << cf;
2551 return;
2552 }
2553
2554 if (cf)
2555 param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context));
2556 else
2557 param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context));
2558}
2559
2560static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr,
2561 Sema &S) {
2562 if (!isa<ObjCMethodDecl>(d)) {
2563 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002564 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00002565 return;
2566 }
2567
2568 d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context));
2569}
2570
2571static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr,
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002572 Sema &S) {
2573
John McCalled433932011-01-25 03:31:58 +00002574 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002575
Ted Kremenek3b204e42009-05-13 21:07:32 +00002576 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
John McCalled433932011-01-25 03:31:58 +00002577 returnType = MD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00002578 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
John McCalled433932011-01-25 03:31:58 +00002579 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00002580 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002581 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCalled433932011-01-25 03:31:58 +00002582 << SourceRange(attr.getLoc()) << attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00002583 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002584 return;
2585 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002586
John McCalled433932011-01-25 03:31:58 +00002587 bool typeOK;
2588 bool cf;
2589 switch (attr.getKind()) {
2590 default: llvm_unreachable("invalid ownership attribute"); return;
2591 case AttributeList::AT_ns_returns_autoreleased:
2592 case AttributeList::AT_ns_returns_retained:
2593 case AttributeList::AT_ns_returns_not_retained:
2594 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2595 cf = false;
2596 break;
2597
2598 case AttributeList::AT_cf_returns_retained:
2599 case AttributeList::AT_cf_returns_not_retained:
2600 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2601 cf = true;
2602 break;
2603 }
2604
2605 if (!typeOK) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002606 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCalled433932011-01-25 03:31:58 +00002607 << SourceRange(attr.getLoc())
2608 << attr.getName() << isa<ObjCMethodDecl>(d) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002609 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00002610 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002611
John McCalled433932011-01-25 03:31:58 +00002612 switch (attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002613 default:
2614 assert(0 && "invalid ownership attribute");
2615 return;
John McCalled433932011-01-25 03:31:58 +00002616 case AttributeList::AT_ns_returns_autoreleased:
2617 d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(),
2618 S.Context));
2619 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00002620 case AttributeList::AT_cf_returns_not_retained:
John McCalled433932011-01-25 03:31:58 +00002621 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002622 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002623 return;
2624 case AttributeList::AT_ns_returns_not_retained:
John McCalled433932011-01-25 03:31:58 +00002625 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002626 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002627 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002628 case AttributeList::AT_cf_returns_retained:
John McCalled433932011-01-25 03:31:58 +00002629 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002630 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002631 return;
2632 case AttributeList::AT_ns_returns_retained:
John McCalled433932011-01-25 03:31:58 +00002633 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002634 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002635 return;
2636 };
2637}
2638
Charles Davis163855f2010-02-16 18:27:26 +00002639static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2640 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Picheta83957a2010-12-19 06:50:37 +00002641 Attr.getKind() == AttributeList::AT_dllexport ||
2642 Attr.getKind() == AttributeList::AT_uuid;
2643}
2644
2645//===----------------------------------------------------------------------===//
2646// Microsoft specific attribute handlers.
2647//===----------------------------------------------------------------------===//
2648
2649static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2650 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2651 // check the attribute arguments.
2652 if (Attr.getNumArgs() != 1) {
2653 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2654 return;
2655 }
2656 Expr *Arg = Attr.getArg(0);
2657 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichet7da11662010-12-20 01:41:49 +00002658 if (Str == 0 || Str->isWide()) {
2659 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2660 << "uuid" << 1;
2661 return;
2662 }
2663
2664 llvm::StringRef StrRef = Str->getString();
2665
2666 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2667 StrRef.back() == '}';
2668
2669 // Validate GUID length.
2670 if (IsCurly && StrRef.size() != 38) {
2671 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2672 return;
2673 }
2674 if (!IsCurly && StrRef.size() != 36) {
2675 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2676 return;
2677 }
2678
2679 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2680 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Anders Carlsson19588aa2011-01-23 21:07:30 +00002681 llvm::StringRef::iterator I = StrRef.begin();
2682 if (IsCurly) // Skip the optional '{'
2683 ++I;
2684
2685 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00002686 if (i == 8 || i == 13 || i == 18 || i == 23) {
2687 if (*I != '-') {
2688 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2689 return;
2690 }
2691 } else if (!isxdigit(*I)) {
2692 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2693 return;
2694 }
2695 I++;
2696 }
Francois Picheta83957a2010-12-19 06:50:37 +00002697
2698 d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
2699 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00002700 } else
Francois Picheta83957a2010-12-19 06:50:37 +00002701 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00002702}
2703
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002704//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002705// Top Level Sema Entry Points
2706//===----------------------------------------------------------------------===//
2707
Peter Collingbourneb331b262011-01-21 02:08:45 +00002708static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D,
2709 const AttributeList &Attr, Sema &S) {
2710 switch (Attr.getKind()) {
2711 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
2712 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
2713 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
2714 default:
2715 break;
2716 }
2717}
Abramo Bagnara50099372010-04-30 13:10:51 +00002718
Peter Collingbourneb331b262011-01-21 02:08:45 +00002719static void ProcessInheritableDeclAttr(Scope *scope, Decl *D,
2720 const AttributeList &Attr, Sema &S) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002721 switch (Attr.getKind()) {
Ted Kremenek1f672822010-02-18 03:08:58 +00002722 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00002723 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2724 case AttributeList::AT_IBOutletCollection:
2725 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002726 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00002727 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00002728 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00002729 case AttributeList::AT_neon_vector_type:
2730 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002731 // Ignore these, these are type attributes, handled by
2732 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002733 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00002734 case AttributeList::AT_device:
2735 case AttributeList::AT_host:
2736 case AttributeList::AT_overloadable:
2737 // Ignore, this is a non-inheritable attribute, handled
2738 // by ProcessNonInheritableDeclAttr.
2739 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002740 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2741 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002742 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00002743 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002744 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002745 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002746 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002747 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00002748 HandleDependencyAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002749 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002750 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002751 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2752 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2753 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002754 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00002755 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002756 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002757 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2758 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002759 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002760 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002761 case AttributeList::AT_launch_bounds:
2762 HandleLaunchBoundsAttr(D, Attr, S);
2763 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002764 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2765 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00002766 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002767 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002768 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00002769 case AttributeList::AT_ownership_returns:
2770 case AttributeList::AT_ownership_takes:
2771 case AttributeList::AT_ownership_holds:
2772 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00002773 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002774 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2775 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002776 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002777 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002778
2779 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00002780 case AttributeList::AT_cf_consumed:
2781 case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break;
2782 case AttributeList::AT_ns_consumes_self:
2783 HandleNSConsumesSelfAttr(D, Attr, S); break;
2784
2785 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00002786 case AttributeList::AT_ns_returns_not_retained:
2787 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002788 case AttributeList::AT_ns_returns_retained:
2789 case AttributeList::AT_cf_returns_retained:
2790 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2791
Nate Begemanf2758702009-06-26 06:32:41 +00002792 case AttributeList::AT_reqd_wg_size:
2793 HandleReqdWorkGroupSize(D, Attr, S); break;
2794
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002795 case AttributeList::AT_init_priority:
2796 HandleInitPriorityAttr(D, Attr, S); break;
2797
Alexis Hunt54a02542009-11-25 04:20:27 +00002798 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2799 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002800 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2801 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2802 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002803 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00002804 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2805 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002806 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindolac18086a2010-02-23 22:00:30 +00002807 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002808 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002809 case AttributeList::AT_transparent_union:
2810 HandleTransparentUnionAttr(D, Attr, S);
2811 break;
Chris Lattner677a3582009-02-14 08:09:34 +00002812 case AttributeList::AT_objc_exception:
2813 HandleObjCExceptionAttr(D, Attr, S);
2814 break;
John McCall86bc21f2011-03-02 11:33:24 +00002815 case AttributeList::AT_objc_method_family:
2816 HandleObjCMethodFamilyAttr(D, Attr, S);
2817 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002818 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2819 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2820 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2821 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2822 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2823 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2824 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2825 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2826 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002827 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00002828 // Just ignore
2829 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00002830 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2831 HandleNoInstrumentFunctionAttr(D, Attr, S);
2832 break;
John McCallab26cfa2010-02-05 21:31:56 +00002833 case AttributeList::AT_stdcall:
2834 case AttributeList::AT_cdecl:
2835 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002836 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002837 case AttributeList::AT_pascal:
Abramo Bagnara50099372010-04-30 13:10:51 +00002838 HandleCallConvAttr(D, Attr, S);
John McCallab26cfa2010-02-05 21:31:56 +00002839 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002840 case AttributeList::AT_opencl_kernel_function:
2841 HandleOpenCLKernelAttr(D, Attr, S);
2842 break;
Francois Picheta83957a2010-12-19 06:50:37 +00002843 case AttributeList::AT_uuid:
2844 HandleUuidAttr(D, Attr, S);
2845 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002846 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002847 // Ask target about the attribute.
2848 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2849 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00002850 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2851 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002852 break;
2853 }
2854}
2855
Peter Collingbourneb331b262011-01-21 02:08:45 +00002856/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
2857/// the attribute applies to decls. If the attribute is a type attribute, just
2858/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2859/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
2860static void ProcessDeclAttribute(Scope *scope, Decl *D,
2861 const AttributeList &Attr, Sema &S,
2862 bool NonInheritable, bool Inheritable) {
2863 if (Attr.isInvalid())
2864 return;
2865
2866 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2867 // FIXME: Try to deal with other __declspec attributes!
2868 return;
2869
2870 if (NonInheritable)
2871 ProcessNonInheritableDeclAttr(scope, D, Attr, S);
2872
2873 if (Inheritable)
2874 ProcessInheritableDeclAttr(scope, D, Attr, S);
2875}
2876
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002877/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2878/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00002879void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00002880 const AttributeList *AttrList,
2881 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00002882 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00002883 ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable);
Rafael Espindolac18086a2010-02-23 22:00:30 +00002884 }
2885
2886 // GCC accepts
2887 // static int a9 __attribute__((weakref));
2888 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00002889 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00002890 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00002891 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00002892 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002893 }
2894}
2895
Ryan Flynn7d470f32009-07-30 03:15:39 +00002896/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2897/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00002898NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002899 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002900 NamedDecl *NewD = 0;
2901 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2902 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002903 FD->getInnerLocStart(),
Ryan Flynn7d470f32009-07-30 03:15:39 +00002904 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00002905 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00002906 if (FD->getQualifier()) {
2907 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00002908 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00002909 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002910 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2911 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002912 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00002913 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002914 VD->getStorageClass(),
2915 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002916 if (VD->getQualifier()) {
2917 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00002918 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00002919 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002920 }
2921 return NewD;
2922}
2923
2924/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2925/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002926void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002927 if (W.getUsed()) return; // only do this once
2928 W.setUsed(true);
2929 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2930 IdentifierInfo *NDId = ND->getIdentifier();
2931 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002932 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2933 NDId->getName()));
2934 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00002935 WeakTopLevelDecl.push_back(NewD);
2936 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2937 // to insert Decl at TU scope, sorry.
2938 DeclContext *SavedContext = CurContext;
2939 CurContext = Context.getTranslationUnitDecl();
2940 PushOnScopeChains(NewD, S);
2941 CurContext = SavedContext;
2942 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002943 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002944 }
2945}
2946
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002947/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2948/// it, apply them to D. This is a bit tricky because PD can have attributes
2949/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00002950void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
2951 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00002952 // It's valid to "forward-declare" #pragma weak, in which case we
2953 // have to do this.
Peter Collingbourneb331b262011-01-21 02:08:45 +00002954 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCall6fe02402010-10-27 00:59:00 +00002955 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2956 if (IdentifierInfo *Id = ND->getIdentifier()) {
2957 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
2958 = WeakUndeclaredIdentifiers.find(Id);
2959 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
2960 WeakInfo W = I->second;
2961 DeclApplyPragmaWeak(S, ND, W);
2962 WeakUndeclaredIdentifiers[Id] = W;
2963 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002964 }
2965 }
2966 }
2967
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002968 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00002969 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00002970 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002971
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002972 // Walk the declarator structure, applying decl attributes that were in a type
2973 // position to the decl itself. This handles cases like:
2974 // int *__attr__(x)** D;
2975 // when X is a decl attribute.
2976 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2977 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00002978 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002979
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002980 // Finally, apply any attributes on the decl itself.
2981 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00002982 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002983}
John McCall28a6aea2009-11-04 02:18:39 +00002984
John McCallc1465822011-02-14 07:13:47 +00002985// This duplicates a vector push_back but hides the need to know the
2986// size of the type.
2987void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
2988 assert(StackSize <= StackCapacity);
2989
2990 // Grow the stack if necessary.
2991 if (StackSize == StackCapacity) {
2992 unsigned newCapacity = 2 * StackCapacity + 2;
2993 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
2994 const char *oldBuffer = (const char*) Stack;
2995
2996 if (StackCapacity)
2997 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
2998
2999 delete[] oldBuffer;
3000 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3001 StackCapacity = newCapacity;
3002 }
3003
3004 assert(StackSize < StackCapacity);
3005 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall86121512010-01-27 03:50:35 +00003006}
3007
John McCallc1465822011-02-14 07:13:47 +00003008void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3009 Decl *decl) {
3010 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall86121512010-01-27 03:50:35 +00003011
John McCallc1465822011-02-14 07:13:47 +00003012 // Check the invariants.
3013 assert(DD.StackSize >= state.SavedStackSize);
3014 assert(state.SavedStackSize >= DD.ActiveStackBase);
3015 assert(DD.ParsingDepth > 0);
3016
3017 // Drop the parsing depth.
3018 DD.ParsingDepth--;
3019
3020 // If there are no active diagnostics, we're done.
3021 if (DD.StackSize == DD.ActiveStackBase)
John McCall86121512010-01-27 03:50:35 +00003022 return;
3023
John McCall86121512010-01-27 03:50:35 +00003024 // We only want to actually emit delayed diagnostics when we
3025 // successfully parsed a decl.
John McCallc1465822011-02-14 07:13:47 +00003026 if (decl) {
3027 // We emit all the active diagnostics, not just those starting
3028 // from the saved state. The idea is this: we get one push for a
John McCall86121512010-01-27 03:50:35 +00003029 // decl spec and another for each declarator; in a decl group like:
3030 // deprecated_typedef foo, *bar, baz();
3031 // only the declarator pops will be passed decls. This is correct;
3032 // we really do need to consider delayed diagnostics from the decl spec
3033 // for each of the different declarations.
John McCallc1465822011-02-14 07:13:47 +00003034 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3035 DelayedDiagnostic &diag = DD.Stack[i];
3036 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00003037 continue;
3038
John McCallc1465822011-02-14 07:13:47 +00003039 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00003040 case DelayedDiagnostic::Deprecation:
John McCallc1465822011-02-14 07:13:47 +00003041 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003042 break;
3043
3044 case DelayedDiagnostic::Access:
John McCallc1465822011-02-14 07:13:47 +00003045 S.HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003046 break;
3047 }
3048 }
3049 }
3050
John McCall1064d7e2010-03-16 05:22:47 +00003051 // Destroy all the delayed diagnostics we're about to pop off.
John McCallc1465822011-02-14 07:13:47 +00003052 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
3053 DD.Stack[i].destroy();
John McCall1064d7e2010-03-16 05:22:47 +00003054
John McCallc1465822011-02-14 07:13:47 +00003055 DD.StackSize = state.SavedStackSize;
John McCall28a6aea2009-11-04 02:18:39 +00003056}
3057
3058static bool isDeclDeprecated(Decl *D) {
3059 do {
3060 if (D->hasAttr<DeprecatedAttr>())
3061 return true;
3062 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3063 return false;
3064}
3065
John McCallb45a1e72010-08-26 02:13:20 +00003066void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00003067 Decl *Ctx) {
3068 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00003069 return;
3070
John McCall86121512010-01-27 03:50:35 +00003071 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003072 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003073 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003074 << DD.getDeprecationDecl()->getDeclName()
3075 << DD.getDeprecationMessage();
Fariborz Jahanian551063102010-10-06 21:18:44 +00003076 else
3077 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003078 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00003079}
3080
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003081void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003082 SourceLocation Loc,
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00003083 bool UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00003084 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00003085 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3086 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall28a6aea2009-11-04 02:18:39 +00003087 return;
3088 }
3089
3090 // Otherwise, don't warn if our current context is deprecated.
3091 if (isDeclDeprecated(cast<Decl>(CurContext)))
3092 return;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003093 if (!Message.empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003094 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3095 << Message;
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003096 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00003097 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003098 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
3099 else
3100 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
3101 }
John McCall28a6aea2009-11-04 02:18:39 +00003102}