blob: 81bf230f94206ee7085f642efe464cb9cdb590c2 [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
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001077static void HandleAvailabilityAttr(Decl *d, const AttributeList &Attr,
1078 Sema &S) {
1079 IdentifierInfo *Platform = Attr.getParameterName();
1080 SourceLocation PlatformLoc = Attr.getParameterLoc();
1081
1082 llvm::StringRef PlatformName
1083 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1084 if (PlatformName.empty()) {
1085 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1086 << Platform;
1087
1088 PlatformName = Platform->getName();
1089 }
1090
1091 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1092 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1093 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
1094
1095 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1096 // of these steps are needed).
1097 if (Introduced.isValid() && Deprecated.isValid() &&
1098 !(Introduced.Version < Deprecated.Version)) {
1099 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1100 << 1 << PlatformName << Deprecated.Version.getAsString()
1101 << 0 << Introduced.Version.getAsString();
1102 return;
1103 }
1104
1105 if (Introduced.isValid() && Obsoleted.isValid() &&
1106 !(Introduced.Version < Obsoleted.Version)) {
1107 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1108 << 2 << PlatformName << Obsoleted.Version.getAsString()
1109 << 0 << Introduced.Version.getAsString();
1110 return;
1111 }
1112
1113 if (Deprecated.isValid() && Obsoleted.isValid() &&
1114 !(Deprecated.Version < Obsoleted.Version)) {
1115 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1116 << 2 << PlatformName << Obsoleted.Version.getAsString()
1117 << 1 << Deprecated.Version.getAsString();
1118 return;
1119 }
1120
1121 d->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
1122 Platform,
1123 Introduced.Version,
1124 Deprecated.Version,
1125 Obsoleted.Version));
1126}
1127
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001128static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001129 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001130 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001131 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001132 return;
1133 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001134
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001135 Expr *Arg = Attr.getArg(0);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001136 Arg = Arg->IgnoreParenCasts();
1137 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001138
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001139 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001141 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001142 return;
1143 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001144
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001145 llvm::StringRef TypeStr = Str->getString();
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001146 VisibilityAttr::VisibilityType type;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001147
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001148 if (TypeStr == "default")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001149 type = VisibilityAttr::Default;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001150 else if (TypeStr == "hidden")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001151 type = VisibilityAttr::Hidden;
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001152 else if (TypeStr == "internal")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001153 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramer12a6ce72010-01-23 18:16:35 +00001154 else if (TypeStr == "protected")
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001155 type = VisibilityAttr::Protected;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001156 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +00001157 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001158 return;
1159 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001160
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001161 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001162}
1163
John McCall86bc21f2011-03-02 11:33:24 +00001164static void HandleObjCMethodFamilyAttr(Decl *decl, const AttributeList &attr,
1165 Sema &S) {
1166 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1167 if (!method) {
1168 S.Diag(attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001169 << ExpectedMethod;
John McCall86bc21f2011-03-02 11:33:24 +00001170 return;
1171 }
1172
1173 if (attr.getNumArgs() != 0 || !attr.getParameterName()) {
1174 if (!attr.getParameterName() && attr.getNumArgs() == 1) {
1175 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
1176 << "objc_method_family" << 1;
1177 } else {
1178 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1179 }
1180 attr.setInvalid();
1181 return;
1182 }
1183
1184 llvm::StringRef param = attr.getParameterName()->getName();
1185 ObjCMethodFamilyAttr::FamilyKind family;
1186 if (param == "none")
1187 family = ObjCMethodFamilyAttr::OMF_None;
1188 else if (param == "alloc")
1189 family = ObjCMethodFamilyAttr::OMF_alloc;
1190 else if (param == "copy")
1191 family = ObjCMethodFamilyAttr::OMF_copy;
1192 else if (param == "init")
1193 family = ObjCMethodFamilyAttr::OMF_init;
1194 else if (param == "mutableCopy")
1195 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1196 else if (param == "new")
1197 family = ObjCMethodFamilyAttr::OMF_new;
1198 else {
1199 // Just warn and ignore it. This is future-proof against new
1200 // families being used in system headers.
1201 S.Diag(attr.getParameterLoc(), diag::warn_unknown_method_family);
1202 return;
1203 }
1204
1205 decl->addAttr(new (S.Context) ObjCMethodFamilyAttr(attr.getLoc(),
1206 S.Context, family));
1207}
1208
Chris Lattner677a3582009-02-14 08:09:34 +00001209static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1210 Sema &S) {
1211 if (Attr.getNumArgs() != 0) {
1212 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1213 return;
1214 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001215
Chris Lattner677a3582009-02-14 08:09:34 +00001216 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1217 if (OCI == 0) {
1218 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1219 return;
1220 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001221
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001222 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner677a3582009-02-14 08:09:34 +00001223}
1224
1225static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001226 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001227 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001228 return;
1229 }
Chris Lattner677a3582009-02-14 08:09:34 +00001230 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001231 QualType T = TD->getUnderlyingType();
1232 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001233 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001234 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1235 return;
1236 }
1237 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001238 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanian255c0952009-01-13 23:34:40 +00001239}
1240
Mike Stumpd3bb5572009-07-24 19:02:52 +00001241static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001242HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1243 if (Attr.getNumArgs() != 0) {
John McCall61d82582010-05-28 18:25:28 +00001244 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001245 return;
1246 }
1247
1248 if (!isa<FunctionDecl>(D)) {
1249 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1250 return;
1251 }
1252
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001253 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001254}
1255
Steve Naroff3405a732008-09-18 16:44:58 +00001256static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001257 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001258 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001259 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001260 return;
1261 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001262
Steve Naroff3405a732008-09-18 16:44:58 +00001263 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001264 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +00001265 return;
1266 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001267
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001268 BlocksAttr::BlockType type;
Chris Lattner68e48682008-11-20 04:42:34 +00001269 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +00001270 type = BlocksAttr::ByRef;
1271 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001272 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001273 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +00001274 return;
1275 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001276
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001277 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff3405a732008-09-18 16:44:58 +00001278}
1279
Anders Carlssonc181b012008-10-05 18:05:59 +00001280static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1281 // check the attribute arguments.
1282 if (Attr.getNumArgs() > 2) {
John McCall80ee5962011-03-02 12:15:05 +00001283 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlssonc181b012008-10-05 18:05:59 +00001284 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001285 }
1286
Anders Carlssonc181b012008-10-05 18:05:59 +00001287 int sentinel = 0;
1288 if (Attr.getNumArgs() > 0) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001289 Expr *E = Attr.getArg(0);
Anders Carlssonc181b012008-10-05 18:05:59 +00001290 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001291 if (E->isTypeDependent() || E->isValueDependent() ||
1292 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001293 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001294 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001295 return;
1296 }
1297 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001298
Anders Carlssonc181b012008-10-05 18:05:59 +00001299 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001300 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1301 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001302 return;
1303 }
1304 }
1305
1306 int nullPos = 0;
1307 if (Attr.getNumArgs() > 1) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001308 Expr *E = Attr.getArg(1);
Anders Carlssonc181b012008-10-05 18:05:59 +00001309 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001310 if (E->isTypeDependent() || E->isValueDependent() ||
1311 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001312 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001313 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001314 return;
1315 }
1316 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001317
Anders Carlssonc181b012008-10-05 18:05:59 +00001318 if (nullPos > 1 || nullPos < 0) {
1319 // FIXME: This error message could be improved, it would be nice
1320 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +00001321 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1322 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +00001323 return;
1324 }
1325 }
1326
1327 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +00001328 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +00001329 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +00001330
Chris Lattner9363e312009-03-17 23:03:47 +00001331 if (isa<FunctionNoProtoType>(FT)) {
1332 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1333 return;
1334 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001335
Chris Lattner9363e312009-03-17 23:03:47 +00001336 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001337 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001338 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001339 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001340 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1341 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001342 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +00001343 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001344 }
1345 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001346 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1347 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001348 ;
1349 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001350 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +00001351 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001352 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherbc638a82010-12-01 22:13:54 +00001353 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001354 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +00001355 int m = Ty->isFunctionPointerType() ? 0 : 1;
1356 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001357 return;
1358 }
Mike Stump12b8ce12009-08-04 21:02:39 +00001359 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001360 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001361 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian6607b212009-05-14 20:53:39 +00001362 return;
1363 }
Anders Carlssonc181b012008-10-05 18:05:59 +00001364 } else {
Chris Lattner3b054132008-11-19 05:08:23 +00001365 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001366 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlssonc181b012008-10-05 18:05:59 +00001367 return;
1368 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001369 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1370 nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +00001371}
1372
Chris Lattner237f2752009-02-14 07:37:35 +00001373static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1374 // check the attribute arguments.
1375 if (Attr.getNumArgs() != 0) {
1376 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1377 return;
1378 }
1379
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001380 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +00001381 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001382 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner237f2752009-02-14 07:37:35 +00001383 return;
1384 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001385
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001386 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1387 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1388 << Attr.getName() << 0;
Nuno Lopes56abcbd2009-12-22 23:59:52 +00001389 return;
1390 }
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001391 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1392 if (MD->getResultType()->isVoidType()) {
1393 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1394 << Attr.getName() << 1;
1395 return;
1396 }
1397
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001398 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner237f2752009-02-14 07:37:35 +00001399}
1400
John McCall7a198ce2011-02-08 22:35:49 +00001401static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001402 // check the attribute arguments.
John McCall7a198ce2011-02-08 22:35:49 +00001403 if (attr.getNumArgs() != 0) {
1404 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001405 return;
1406 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001407
John McCall7a198ce2011-02-08 22:35:49 +00001408 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
1409 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001410 << attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanian41136ee2009-07-16 01:12:24 +00001411 return;
1412 }
1413
John McCall7a198ce2011-02-08 22:35:49 +00001414 NamedDecl *nd = cast<NamedDecl>(d);
1415
1416 // 'weak' only applies to declarations with external linkage.
1417 if (hasEffectivelyInternalLinkage(nd)) {
1418 S.Diag(attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001419 return;
1420 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001421
John McCall7a198ce2011-02-08 22:35:49 +00001422 nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001423}
1424
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001425static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1426 // check the attribute arguments.
1427 if (Attr.getNumArgs() != 0) {
1428 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1429 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001430 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001431
1432 // weak_import only applies to variable & function declarations.
1433 bool isDef = false;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00001434 if (!D->canBeWeakImported(isDef)) {
1435 if (isDef)
1436 S.Diag(Attr.getLoc(),
1437 diag::warn_attribute_weak_import_invalid_on_definition)
1438 << "weak_import" << 2 /*variable and function*/;
Douglas Gregord71149a2011-03-23 13:27:51 +00001439 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
1440 (S.Context.Target.getTriple().getOS() == llvm::Triple::Darwin &&
1441 isa<ObjCInterfaceDecl>(D))) {
1442 // Nothing to warn about here.
1443 } else
Fariborz Jahanianea70a172010-04-13 20:22:35 +00001444 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001445 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001446
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001447 return;
1448 }
1449
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001450 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +00001451}
1452
Nate Begemanf2758702009-06-26 06:32:41 +00001453static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1454 Sema &S) {
1455 // Attribute has 3 arguments.
1456 if (Attr.getNumArgs() != 3) {
John McCall61d82582010-05-28 18:25:28 +00001457 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begemanf2758702009-06-26 06:32:41 +00001458 return;
1459 }
1460
1461 unsigned WGSize[3];
1462 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001463 Expr *E = Attr.getArg(i);
Nate Begemanf2758702009-06-26 06:32:41 +00001464 llvm::APSInt ArgNum(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001465 if (E->isTypeDependent() || E->isValueDependent() ||
1466 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begemanf2758702009-06-26 06:32:41 +00001467 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1468 << "reqd_work_group_size" << E->getSourceRange();
1469 return;
1470 }
1471 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1472 }
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001473 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1474 WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +00001475 WGSize[2]));
1476}
1477
Chris Lattner237f2752009-02-14 07:37:35 +00001478static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +00001479 // Attribute has no arguments.
1480 if (Attr.getNumArgs() != 1) {
1481 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1482 return;
1483 }
1484
1485 // Make sure that there is a string literal as the sections's single
1486 // argument.
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001487 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001488 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +00001489 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001490 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +00001491 return;
1492 }
Mike Stump11289f42009-09-09 15:08:12 +00001493
Chris Lattner30ba6742009-08-10 19:03:04 +00001494 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +00001495 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner20aee9b2010-01-12 20:58:53 +00001496 if (!Error.empty()) {
1497 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1498 << Error;
Chris Lattner30ba6742009-08-10 19:03:04 +00001499 return;
1500 }
Mike Stump11289f42009-09-09 15:08:12 +00001501
Chris Lattner20aee9b2010-01-12 20:58:53 +00001502 // This attribute cannot be applied to local variables.
1503 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1504 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1505 return;
1506 }
1507
Eric Christopherbc638a82010-12-01 22:13:54 +00001508 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1509 SE->getString()));
Daniel Dunbar648bf782009-02-12 17:28:23 +00001510}
1511
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001512
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001513static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001514 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001515 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001516 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001517 return;
1518 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001519
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001520 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001521}
1522
Anders Carlssonb8316282008-10-05 23:32:53 +00001523static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1524 // check the attribute arguments.
1525 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001526 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001527 return;
1528 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001529
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001530 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001531}
1532
1533static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1534 // check the attribute arguments.
1535 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001536 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001537 return;
1538 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001539
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001540 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlssonb8316282008-10-05 23:32:53 +00001541}
1542
Anders Carlssond277d792009-01-31 01:16:18 +00001543static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001544 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001545 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1546 return;
1547 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001548
Anders Carlssond277d792009-01-31 01:16:18 +00001549 if (Attr.getNumArgs() != 0) {
1550 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1551 return;
1552 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001553
Anders Carlssond277d792009-01-31 01:16:18 +00001554 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001555
Anders Carlssond277d792009-01-31 01:16:18 +00001556 if (!VD || !VD->hasLocalStorage()) {
1557 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1558 return;
1559 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001560
Anders Carlssond277d792009-01-31 01:16:18 +00001561 // Look up the function
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001562 // FIXME: Lookup probably isn't looking in the right place
John McCall9f3059a2009-10-09 21:13:30 +00001563 NamedDecl *CleanupDecl
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001564 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1565 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001566 if (!CleanupDecl) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001567 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001568 Attr.getParameterName();
1569 return;
1570 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001571
Anders Carlssond277d792009-01-31 01:16:18 +00001572 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1573 if (!FD) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001574 S.Diag(Attr.getParameterLoc(),
1575 diag::err_attribute_cleanup_arg_not_function)
1576 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001577 return;
1578 }
1579
Anders Carlssond277d792009-01-31 01:16:18 +00001580 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001581 S.Diag(Attr.getParameterLoc(),
1582 diag::err_attribute_cleanup_func_must_take_one_arg)
1583 << Attr.getParameterName();
Anders Carlssond277d792009-01-31 01:16:18 +00001584 return;
1585 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001586
Anders Carlsson723f55d2009-02-07 23:16:50 +00001587 // We're currently more strict than GCC about what function types we accept.
1588 // If this ever proves to be a problem it should be easy to fix.
1589 QualType Ty = S.Context.getPointerType(VD->getType());
1590 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorc03a1082011-01-28 02:26:04 +00001591 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1592 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidis7b608972010-12-06 17:51:50 +00001593 S.Diag(Attr.getParameterLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001594 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1595 Attr.getParameterName() << ParamTy << Ty;
1596 return;
1597 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001598
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001599 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidise88168a2010-12-06 17:51:53 +00001600 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssond277d792009-01-31 01:16:18 +00001601}
1602
Mike Stumpd3bb5572009-07-24 19:02:52 +00001603/// Handle __attribute__((format_arg((idx)))) attribute based on
1604/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1605static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001606 if (Attr.getNumArgs() != 1) {
1607 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1608 return;
1609 }
1610 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1611 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001612 << Attr.getName() << ExpectedFunction;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001613 return;
1614 }
Chandler Carruth743682b2010-11-16 08:35:43 +00001615
1616 // In C++ the implicit 'this' function parameter also counts, and they are
1617 // counted from one.
1618 bool HasImplicitThisParam = isInstanceMethod(d);
1619 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001620 unsigned FirstIdx = 1;
Chandler Carruth743682b2010-11-16 08:35:43 +00001621
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001622 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001623 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001624 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001625 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1626 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001627 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1628 << "format" << 2 << IdxExpr->getSourceRange();
1629 return;
1630 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001631
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001632 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1633 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1634 << "format" << 2 << IdxExpr->getSourceRange();
1635 return;
1636 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001637
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001638 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001639
Chandler Carruth743682b2010-11-16 08:35:43 +00001640 if (HasImplicitThisParam) {
1641 if (ArgIdx == 0) {
1642 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1643 << "format_arg" << IdxExpr->getSourceRange();
1644 return;
1645 }
1646 ArgIdx--;
1647 }
1648
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001649 // make sure the format string is really a string
1650 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001651
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001652 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1653 if (not_nsstring_type &&
1654 !isCFStringType(Ty, S.Context) &&
1655 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001656 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001657 // FIXME: Should highlight the actual expression that has the wrong type.
1658 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001659 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001660 << IdxExpr->getSourceRange();
1661 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001662 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001663 Ty = getFunctionOrMethodResultType(d);
1664 if (!isNSStringType(Ty, S.Context) &&
1665 !isCFStringType(Ty, S.Context) &&
1666 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001667 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001668 // FIXME: Should highlight the actual expression that has the wrong type.
1669 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001670 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001671 << IdxExpr->getSourceRange();
1672 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001673 }
1674
Chandler Carruth743682b2010-11-16 08:35:43 +00001675 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1676 Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001677}
1678
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001679enum FormatAttrKind {
1680 CFStringFormat,
1681 NSStringFormat,
1682 StrftimeFormat,
1683 SupportedFormat,
Chris Lattner12161d32010-03-22 21:08:50 +00001684 IgnoredFormat,
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001685 InvalidFormat
1686};
1687
1688/// getFormatAttrKind - Map from format attribute names to supported format
1689/// types.
1690static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1691 // Check for formats that get handled specially.
1692 if (Format == "NSString")
1693 return NSStringFormat;
1694 if (Format == "CFString")
1695 return CFStringFormat;
1696 if (Format == "strftime")
1697 return StrftimeFormat;
1698
1699 // Otherwise, check for supported formats.
1700 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1701 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1702 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattner0ddd0ae2011-02-18 17:05:55 +00001703 Format == "zcmn_err" ||
1704 Format == "kprintf") // OpenBSD.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001705 return SupportedFormat;
1706
Duncan Sandsde4fe352010-03-23 14:44:19 +00001707 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1708 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner12161d32010-03-22 21:08:50 +00001709 return IgnoredFormat;
1710
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001711 return InvalidFormat;
1712}
1713
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001714/// Handle __attribute__((init_priority(priority))) attributes based on
1715/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1716static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1717 Sema &S) {
1718 if (!S.getLangOptions().CPlusPlus) {
1719 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1720 return;
1721 }
1722
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001723 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1724 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1725 Attr.setInvalid();
1726 return;
1727 }
1728 QualType T = dyn_cast<VarDecl>(d)->getType();
1729 if (S.Context.getAsArrayType(T))
1730 T = S.Context.getBaseElementType(T);
1731 if (!T->getAs<RecordType>()) {
1732 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1733 Attr.setInvalid();
1734 return;
1735 }
1736
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001737 if (Attr.getNumArgs() != 1) {
1738 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1739 Attr.setInvalid();
1740 return;
1741 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001742 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanian0bf5ee72010-06-18 23:14:53 +00001743
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001744 llvm::APSInt priority(32);
1745 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1746 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1747 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1748 << "init_priority" << priorityExpr->getSourceRange();
1749 Attr.setInvalid();
1750 return;
1751 }
Fariborz Jahanian9f2a4ee2010-06-21 18:45:05 +00001752 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001753 if (prioritynum < 101 || prioritynum > 65535) {
1754 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1755 << priorityExpr->getSourceRange();
1756 Attr.setInvalid();
1757 return;
1758 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001759 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1760 prioritynum));
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00001761}
1762
Mike Stumpd3bb5572009-07-24 19:02:52 +00001763/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1764/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001765static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001766
Chris Lattner4a927cb2008-06-28 23:36:30 +00001767 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001768 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001769 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001770 return;
1771 }
1772
Chris Lattner4a927cb2008-06-28 23:36:30 +00001773 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001774 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001775 return;
1776 }
1777
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001778 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001779 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001780 << Attr.getName() << ExpectedFunction;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001781 return;
1782 }
1783
Chandler Carruth743682b2010-11-16 08:35:43 +00001784 // In C++ the implicit 'this' function parameter also counts, and they are
1785 // counted from one.
1786 bool HasImplicitThisParam = isInstanceMethod(d);
1787 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001788 unsigned FirstIdx = 1;
1789
Daniel Dunbar07d07852009-10-18 21:17:35 +00001790 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001791
1792 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001793 if (Format.startswith("__") && Format.endswith("__"))
1794 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001795
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001796 // Check for supported formats.
1797 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner12161d32010-03-22 21:08:50 +00001798
1799 if (Kind == IgnoredFormat)
1800 return;
1801
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001802 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001803 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001804 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001805 return;
1806 }
1807
1808 // checks for the 2nd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001809 Expr *IdxExpr = Attr.getArg(0);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001810 llvm::APSInt Idx(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001811 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1812 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001813 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001814 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001815 return;
1816 }
1817
1818 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001819 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001820 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001821 return;
1822 }
1823
1824 // FIXME: Do we need to bounds check?
1825 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001826
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001827 if (HasImplicitThisParam) {
1828 if (ArgIdx == 0) {
Chandler Carruth743682b2010-11-16 08:35:43 +00001829 S.Diag(Attr.getLoc(),
1830 diag::err_format_attribute_implicit_this_format_string)
1831 << IdxExpr->getSourceRange();
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001832 return;
1833 }
1834 ArgIdx--;
1835 }
Mike Stump11289f42009-09-09 15:08:12 +00001836
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001837 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001838 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001839
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001840 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001841 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001842 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1843 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001844 return;
1845 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001846 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001847 // FIXME: do we need to check if the type is NSString*? What are the
1848 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001849 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001850 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001851 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1852 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001853 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001854 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001855 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001856 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001857 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001858 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1859 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001860 return;
1861 }
1862
1863 // check the 3rd argument
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001864 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001865 llvm::APSInt FirstArg(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00001866 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1867 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001868 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001869 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001870 return;
1871 }
1872
1873 // check if the function is variadic if the 3rd argument non-zero
1874 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001875 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001876 ++NumArgs; // +1 for ...
1877 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001878 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001879 return;
1880 }
1881 }
1882
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001883 // strftime requires FirstArg to be 0 because it doesn't read from any
1884 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001885 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001886 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001887 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1888 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001889 return;
1890 }
1891 // if 0 it disables parameter checking (to use with e.g. va_list)
1892 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001893 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001894 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001895 return;
1896 }
1897
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001898 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1899 Idx.getZExtValue(),
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001900 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001901}
1902
Chris Lattnera663a0a2008-06-29 00:28:59 +00001903static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1904 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001905 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001906 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001907 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001908 return;
1909 }
1910
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001911 // Try to find the underlying union declaration.
1912 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001913 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001914 if (TD && TD->getUnderlyingType()->isUnionType())
1915 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1916 else
1917 RD = dyn_cast<RecordDecl>(d);
1918
1919 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001920 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00001921 << Attr.getName() << ExpectedUnion;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001922 return;
1923 }
1924
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001925 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001926 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001927 diag::warn_transparent_union_attribute_not_definition);
1928 return;
1929 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001930
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001931 RecordDecl::field_iterator Field = RD->field_begin(),
1932 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001933 if (Field == FieldEnd) {
1934 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1935 return;
1936 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001937
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001938 FieldDecl *FirstField = *Field;
1939 QualType FirstType = FirstField->getType();
Douglas Gregor21872662010-06-30 17:24:13 +00001940 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001941 S.Diag(FirstField->getLocation(),
Douglas Gregor21872662010-06-30 17:24:13 +00001942 diag::warn_transparent_union_attribute_floating)
1943 << FirstType->isVectorType() << FirstType;
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001944 return;
1945 }
1946
1947 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1948 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1949 for (; Field != FieldEnd; ++Field) {
1950 QualType FieldType = Field->getType();
1951 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1952 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1953 // Warn if we drop the attribute.
1954 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001955 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001956 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001957 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001958 diag::warn_transparent_union_attribute_field_size_align)
1959 << isSize << Field->getDeclName() << FieldBits;
1960 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001961 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001962 diag::note_transparent_union_first_field_size_align)
1963 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001964 return;
1965 }
1966 }
1967
Alexis Huntdcfba7b2010-08-18 23:23:40 +00001968 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001969}
1970
Chris Lattnera663a0a2008-06-29 00:28:59 +00001971static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001972 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001973 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001974 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001975 return;
1976 }
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00001977 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner30ba6742009-08-10 19:03:04 +00001978 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001979
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001980 // Make sure that there is a string literal as the annotation's single
1981 // argument.
1982 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001983 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001984 return;
1985 }
Eric Christopherbc638a82010-12-01 22:13:54 +00001986 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1987 SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001988}
1989
Chandler Carruthf40c42f2010-06-25 03:22:07 +00001990static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001991 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001992 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001993 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001994 return;
1995 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001996
1997 //FIXME: The C++0x version of this attribute has more limited applicabilty
1998 // than GNU's, and should error out when it is used to specify a
1999 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002000
Chris Lattner4a927cb2008-06-28 23:36:30 +00002001 if (Attr.getNumArgs() == 0) {
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002002 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002003 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002004 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002005
Peter Collingbournee57e9ef2010-11-23 20:45:58 +00002006 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002007}
2008
2009void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2010 if (E->isTypeDependent() || E->isValueDependent()) {
2011 // Save dependent expressions in the AST to be instantiated.
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002012 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002013 return;
2014 }
2015
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002016 // FIXME: Cache the number on the Attr object?
Chris Lattner4627b742008-06-28 23:50:44 +00002017 llvm::APSInt Alignment(32);
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002018 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2019 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2020 << "aligned" << E->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00002021 return;
2022 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002023 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruthf40c42f2010-06-25 03:22:07 +00002024 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2025 << E->getSourceRange();
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00002026 return;
2027 }
2028
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002029 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2030}
2031
2032void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2033 // FIXME: Cache the number on the Attr object if non-dependent?
2034 // FIXME: Perform checking of type validity
2035 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2036 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00002037}
Chris Lattneracbc2d22008-06-27 22:18:37 +00002038
Mike Stumpd3bb5572009-07-24 19:02:52 +00002039/// HandleModeAttr - This attribute modifies the width of a decl with primitive
2040/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00002041///
Mike Stumpd3bb5572009-07-24 19:02:52 +00002042/// Despite what would be logical, the mode attribute is a decl attribute, not a
2043/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2044/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00002045static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002046 // This attribute isn't documented, but glibc uses it. It changes
2047 // the width of an int or unsigned int to the specified size.
2048
2049 // Check that there aren't any arguments
2050 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002051 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002052 return;
2053 }
2054
2055 IdentifierInfo *Name = Attr.getParameterName();
2056 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00002057 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002058 return;
2059 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00002060
Daniel Dunbar07d07852009-10-18 21:17:35 +00002061 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002062
2063 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002064 if (Str.startswith("__") && Str.endswith("__"))
2065 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002066
2067 unsigned DestWidth = 0;
2068 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00002069 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00002070 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00002071 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00002072 switch (Str[0]) {
2073 case 'Q': DestWidth = 8; break;
2074 case 'H': DestWidth = 16; break;
2075 case 'S': DestWidth = 32; break;
2076 case 'D': DestWidth = 64; break;
2077 case 'X': DestWidth = 96; break;
2078 case 'T': DestWidth = 128; break;
2079 }
2080 if (Str[1] == 'F') {
2081 IntegerMode = false;
2082 } else if (Str[1] == 'C') {
2083 IntegerMode = false;
2084 ComplexMode = true;
2085 } else if (Str[1] != 'I') {
2086 DestWidth = 0;
2087 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002088 break;
2089 case 4:
2090 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2091 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00002092 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002093 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00002094 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002095 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00002096 break;
2097 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00002098 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00002099 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002100 break;
2101 }
2102
2103 QualType OldTy;
2104 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
2105 OldTy = TD->getUnderlyingType();
2106 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2107 OldTy = VD->getType();
2108 else {
Chris Lattner3b054132008-11-19 05:08:23 +00002109 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2110 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00002111 return;
2112 }
Eli Friedman4735374e2009-03-03 06:41:03 +00002113
John McCall9dd450b2009-09-21 23:43:11 +00002114 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002115 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2116 else if (IntegerMode) {
Douglas Gregorb90df602010-06-16 00:17:44 +00002117 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman4735374e2009-03-03 06:41:03 +00002118 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2119 } else if (ComplexMode) {
2120 if (!OldTy->isComplexType())
2121 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2122 } else {
2123 if (!OldTy->isFloatingType())
2124 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2125 }
2126
Mike Stump87c57ac2009-05-16 07:39:55 +00002127 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2128 // and friends, at least with glibc.
2129 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2130 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00002131 // FIXME: Make sure floating-point mappings are accurate
2132 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00002133 QualType NewTy;
2134 switch (DestWidth) {
2135 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002136 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002137 return;
2138 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002139 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002140 return;
2141 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00002142 if (!IntegerMode) {
2143 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2144 return;
2145 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002146 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002147 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002148 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002149 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002150 break;
2151 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00002152 if (!IntegerMode) {
2153 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2154 return;
2155 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00002156 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002157 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002158 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002159 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002160 break;
2161 case 32:
2162 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002163 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002164 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00002165 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002166 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00002167 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002168 break;
2169 case 64:
2170 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00002171 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002172 else if (OldTy->isSignedIntegerType())
Chandler Carruth72343702010-01-26 06:39:24 +00002173 if (S.Context.Target.getLongWidth() == 64)
2174 NewTy = S.Context.LongTy;
2175 else
2176 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002177 else
Chandler Carruth72343702010-01-26 06:39:24 +00002178 if (S.Context.Target.getLongWidth() == 64)
2179 NewTy = S.Context.UnsignedLongTy;
2180 else
2181 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002182 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00002183 case 96:
2184 NewTy = S.Context.LongDoubleTy;
2185 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00002186 case 128:
2187 if (!IntegerMode) {
2188 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2189 return;
2190 }
Anders Carlsson88ea2452009-12-29 07:07:36 +00002191 if (OldTy->isSignedIntegerType())
2192 NewTy = S.Context.Int128Ty;
2193 else
2194 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman4735374e2009-03-03 06:41:03 +00002195 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00002196 }
2197
Eli Friedman4735374e2009-03-03 06:41:03 +00002198 if (ComplexMode) {
2199 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00002200 }
2201
2202 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00002203 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2204 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00002205 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00002206 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00002207 cast<ValueDecl>(D)->setType(NewTy);
2208}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002209
Mike Stump3722f582009-08-26 22:31:08 +00002210static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002211 // check the attribute arguments.
2212 if (Attr.getNumArgs() > 0) {
2213 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2214 return;
2215 }
Anders Carlsson63784f42009-02-13 08:11:52 +00002216
Anders Carlsson88097122009-02-19 19:16:48 +00002217 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00002218 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002219 << Attr.getName() << ExpectedFunction;
Anders Carlsson76187b42009-02-13 06:46:13 +00002220 return;
2221 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002222
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002223 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlsson76187b42009-02-13 06:46:13 +00002224}
2225
Mike Stump3722f582009-08-26 22:31:08 +00002226static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00002227 // check the attribute arguments.
2228 if (Attr.getNumArgs() != 0) {
2229 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2230 return;
2231 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002232
Chris Lattner4225e232009-04-14 17:02:11 +00002233 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00002234 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002235 << Attr.getName() << ExpectedFunction;
Anders Carlsson88097122009-02-19 19:16:48 +00002236 return;
2237 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002238
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002239 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson88097122009-02-19 19:16:48 +00002240}
2241
Chris Lattner3c77a352010-06-22 00:03:40 +00002242static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2243 Sema &S) {
2244 // check the attribute arguments.
2245 if (Attr.getNumArgs() != 0) {
2246 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2247 return;
2248 }
2249
2250 if (!isa<FunctionDecl>(d)) {
2251 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002252 << Attr.getName() << ExpectedFunction;
Chris Lattner3c77a352010-06-22 00:03:40 +00002253 return;
2254 }
2255
Eric Christopherbc638a82010-12-01 22:13:54 +00002256 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2257 S.Context));
Chris Lattner3c77a352010-06-22 00:03:40 +00002258}
2259
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002260static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2261 if (S.LangOpts.CUDA) {
2262 // check the attribute arguments.
2263 if (Attr.getNumArgs() != 0) {
2264 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2265 return;
2266 }
2267
2268 if (!isa<VarDecl>(d)) {
2269 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002270 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002271 return;
2272 }
2273
2274 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2275 } else {
2276 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2277 }
2278}
2279
2280static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2281 if (S.LangOpts.CUDA) {
2282 // check the attribute arguments.
2283 if (Attr.getNumArgs() != 0) {
2284 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2285 return;
2286 }
2287
2288 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2289 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002290 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002291 return;
2292 }
2293
2294 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2295 } else {
2296 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2297 }
2298}
2299
2300static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2301 if (S.LangOpts.CUDA) {
2302 // check the attribute arguments.
2303 if (Attr.getNumArgs() != 0) {
2304 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2305 return;
2306 }
2307
2308 if (!isa<FunctionDecl>(d)) {
2309 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002310 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002311 return;
2312 }
2313
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002314 FunctionDecl *FD = cast<FunctionDecl>(d);
2315 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara6d810632010-12-14 22:11:44 +00002316 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbournee8cfaf42010-12-12 23:02:57 +00002317 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2318 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2319 << FD->getType()
2320 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2321 "void");
2322 } else {
2323 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2324 << FD->getType();
2325 }
2326 return;
2327 }
2328
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002329 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2330 } else {
2331 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2332 }
2333}
2334
2335static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2336 if (S.LangOpts.CUDA) {
2337 // check the attribute arguments.
2338 if (Attr.getNumArgs() != 0) {
2339 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2340 return;
2341 }
2342
2343 if (!isa<FunctionDecl>(d)) {
2344 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002345 << Attr.getName() << ExpectedFunction;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002346 return;
2347 }
2348
2349 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2350 } else {
2351 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2352 }
2353}
2354
2355static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2356 if (S.LangOpts.CUDA) {
2357 // check the attribute arguments.
2358 if (Attr.getNumArgs() != 0) {
2359 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2360 return;
2361 }
2362
2363 if (!isa<VarDecl>(d)) {
2364 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002365 << Attr.getName() << ExpectedVariable;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002366 return;
2367 }
2368
2369 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2370 } else {
2371 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2372 }
2373}
2374
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002375static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002376 // check the attribute arguments.
2377 if (Attr.getNumArgs() != 0) {
2378 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2379 return;
2380 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002381
Chris Lattner4225e232009-04-14 17:02:11 +00002382 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2383 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00002384 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002385 << Attr.getName() << ExpectedFunction;
Chris Lattnereaad6b72009-04-14 16:30:50 +00002386 return;
2387 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002388
Douglas Gregor35b57532009-10-27 21:01:01 +00002389 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00002390 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00002391 return;
2392 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002393
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002394 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattnereaad6b72009-04-14 16:30:50 +00002395}
2396
John McCall3882ace2011-01-05 12:14:39 +00002397static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) {
2398 if (hasDeclarator(d)) return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002399
John McCall3882ace2011-01-05 12:14:39 +00002400 // Diagnostic is emitted elsewhere: here we store the (valid) attr
2401 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2402 CallingConv CC;
2403 if (S.CheckCallingConvAttr(attr, CC))
2404 return;
2405
2406 if (!isa<ObjCMethodDecl>(d)) {
2407 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002408 << attr.getName() << ExpectedFunctionOrMethod;
John McCall3882ace2011-01-05 12:14:39 +00002409 return;
2410 }
2411
2412 switch (attr.getKind()) {
Abramo Bagnara50099372010-04-30 13:10:51 +00002413 case AttributeList::AT_fastcall:
John McCall3882ace2011-01-05 12:14:39 +00002414 d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002415 return;
2416 case AttributeList::AT_stdcall:
John McCall3882ace2011-01-05 12:14:39 +00002417 d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002418 return;
Douglas Gregora941dca2010-05-18 16:57:00 +00002419 case AttributeList::AT_thiscall:
John McCall3882ace2011-01-05 12:14:39 +00002420 d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context));
Douglas Gregor4d13d102010-08-30 23:30:49 +00002421 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002422 case AttributeList::AT_cdecl:
John McCall3882ace2011-01-05 12:14:39 +00002423 d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context));
Abramo Bagnara50099372010-04-30 13:10:51 +00002424 return;
Dawn Perchik335e16b2010-09-03 01:29:35 +00002425 case AttributeList::AT_pascal:
John McCall3882ace2011-01-05 12:14:39 +00002426 d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context));
Dawn Perchik335e16b2010-09-03 01:29:35 +00002427 return;
Abramo Bagnara50099372010-04-30 13:10:51 +00002428 default:
2429 llvm_unreachable("unexpected attribute kind");
2430 return;
2431 }
2432}
2433
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002434static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){
2435 assert(Attr.isInvalid() == false);
2436 d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
2437}
2438
John McCall3882ace2011-01-05 12:14:39 +00002439bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2440 if (attr.isInvalid())
2441 return true;
2442
2443 if (attr.getNumArgs() != 0) {
2444 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2445 attr.setInvalid();
2446 return true;
2447 }
2448
2449 // TODO: diagnose uses of these conventions on the wrong target.
2450 switch (attr.getKind()) {
2451 case AttributeList::AT_cdecl: CC = CC_C; break;
2452 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2453 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2454 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2455 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
2456 default: llvm_unreachable("unexpected attribute kind"); return true;
2457 }
2458
2459 return false;
2460}
2461
2462static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) {
2463 if (hasDeclarator(d)) return;
2464
2465 unsigned numParams;
2466 if (S.CheckRegparmAttr(attr, numParams))
2467 return;
2468
2469 if (!isa<ObjCMethodDecl>(d)) {
2470 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002471 << attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002472 return;
2473 }
Eli Friedman7044b762009-03-27 21:06:47 +00002474
John McCall3882ace2011-01-05 12:14:39 +00002475 d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams));
2476}
2477
2478/// Checks a regparm attribute, returning true if it is ill-formed and
2479/// otherwise setting numParams to the appropriate value.
2480bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) {
2481 if (attr.isInvalid())
2482 return true;
2483
2484 if (attr.getNumArgs() != 1) {
2485 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2486 attr.setInvalid();
2487 return true;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002488 }
Eli Friedman7044b762009-03-27 21:06:47 +00002489
John McCall3882ace2011-01-05 12:14:39 +00002490 Expr *NumParamsExpr = attr.getArg(0);
Eli Friedman7044b762009-03-27 21:06:47 +00002491 llvm::APSInt NumParams(32);
Douglas Gregorbdb604a2010-05-18 23:01:22 +00002492 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall3882ace2011-01-05 12:14:39 +00002493 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
2494 Diag(attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman7044b762009-03-27 21:06:47 +00002495 << "regparm" << NumParamsExpr->getSourceRange();
John McCall3882ace2011-01-05 12:14:39 +00002496 attr.setInvalid();
2497 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002498 }
2499
John McCall3882ace2011-01-05 12:14:39 +00002500 if (Context.Target.getRegParmMax() == 0) {
2501 Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00002502 << NumParamsExpr->getSourceRange();
John McCall3882ace2011-01-05 12:14:39 +00002503 attr.setInvalid();
2504 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002505 }
2506
John McCall3882ace2011-01-05 12:14:39 +00002507 numParams = NumParams.getZExtValue();
2508 if (numParams > Context.Target.getRegParmMax()) {
2509 Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2510 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
2511 attr.setInvalid();
2512 return true;
Eli Friedman7044b762009-03-27 21:06:47 +00002513 }
2514
John McCall3882ace2011-01-05 12:14:39 +00002515 return false;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00002516}
2517
Peter Collingbourne827301e2010-12-12 23:03:07 +00002518static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){
2519 if (S.LangOpts.CUDA) {
2520 // check the attribute arguments.
2521 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCall80ee5962011-03-02 12:15:05 +00002522 // FIXME: 0 is not okay.
2523 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002524 return;
2525 }
2526
2527 if (!isFunctionOrMethod(d)) {
2528 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002529 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002530 return;
2531 }
2532
2533 Expr *MaxThreadsExpr = Attr.getArg(0);
2534 llvm::APSInt MaxThreads(32);
2535 if (MaxThreadsExpr->isTypeDependent() ||
2536 MaxThreadsExpr->isValueDependent() ||
2537 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2538 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2539 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2540 return;
2541 }
2542
2543 llvm::APSInt MinBlocks(32);
2544 if (Attr.getNumArgs() > 1) {
2545 Expr *MinBlocksExpr = Attr.getArg(1);
2546 if (MinBlocksExpr->isTypeDependent() ||
2547 MinBlocksExpr->isValueDependent() ||
2548 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2549 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2550 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2551 return;
2552 }
2553 }
2554
2555 d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
2556 MaxThreads.getZExtValue(),
2557 MinBlocks.getZExtValue()));
2558 } else {
2559 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2560 }
2561}
2562
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002563//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002564// Checker-specific attribute handlers.
2565//===----------------------------------------------------------------------===//
2566
John McCalled433932011-01-25 03:31:58 +00002567static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2568 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2569}
2570static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2571 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2572}
2573
2574static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) {
2575 ParmVarDecl *param = dyn_cast<ParmVarDecl>(d);
2576 if (!param) {
2577 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002578 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedParameter;
John McCalled433932011-01-25 03:31:58 +00002579 return;
2580 }
2581
2582 bool typeOK, cf;
2583 if (attr.getKind() == AttributeList::AT_ns_consumed) {
2584 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2585 cf = false;
2586 } else {
2587 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2588 cf = true;
2589 }
2590
2591 if (!typeOK) {
2592 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2593 << SourceRange(attr.getLoc()) << attr.getName() << cf;
2594 return;
2595 }
2596
2597 if (cf)
2598 param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context));
2599 else
2600 param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context));
2601}
2602
2603static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr,
2604 Sema &S) {
2605 if (!isa<ObjCMethodDecl>(d)) {
2606 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall5fca7ea2011-03-02 12:29:23 +00002607 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedMethod;
John McCalled433932011-01-25 03:31:58 +00002608 return;
2609 }
2610
2611 d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context));
2612}
2613
2614static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr,
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002615 Sema &S) {
2616
John McCalled433932011-01-25 03:31:58 +00002617 QualType returnType;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002618
Ted Kremenek3b204e42009-05-13 21:07:32 +00002619 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
John McCalled433932011-01-25 03:31:58 +00002620 returnType = MD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00002621 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
John McCalled433932011-01-25 03:31:58 +00002622 returnType = FD->getResultType();
Ted Kremenek3b204e42009-05-13 21:07:32 +00002623 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002624 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCalled433932011-01-25 03:31:58 +00002625 << SourceRange(attr.getLoc()) << attr.getName()
John McCall5fca7ea2011-03-02 12:29:23 +00002626 << ExpectedFunctionOrMethod;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002627 return;
2628 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002629
John McCalled433932011-01-25 03:31:58 +00002630 bool typeOK;
2631 bool cf;
2632 switch (attr.getKind()) {
2633 default: llvm_unreachable("invalid ownership attribute"); return;
2634 case AttributeList::AT_ns_returns_autoreleased:
2635 case AttributeList::AT_ns_returns_retained:
2636 case AttributeList::AT_ns_returns_not_retained:
2637 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2638 cf = false;
2639 break;
2640
2641 case AttributeList::AT_cf_returns_retained:
2642 case AttributeList::AT_cf_returns_not_retained:
2643 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2644 cf = true;
2645 break;
2646 }
2647
2648 if (!typeOK) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00002649 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCalled433932011-01-25 03:31:58 +00002650 << SourceRange(attr.getLoc())
2651 << attr.getName() << isa<ObjCMethodDecl>(d) << cf;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002652 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00002653 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00002654
John McCalled433932011-01-25 03:31:58 +00002655 switch (attr.getKind()) {
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002656 default:
2657 assert(0 && "invalid ownership attribute");
2658 return;
John McCalled433932011-01-25 03:31:58 +00002659 case AttributeList::AT_ns_returns_autoreleased:
2660 d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(),
2661 S.Context));
2662 return;
Ted Kremenekd9c66632010-02-18 00:05:45 +00002663 case AttributeList::AT_cf_returns_not_retained:
John McCalled433932011-01-25 03:31:58 +00002664 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002665 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002666 return;
2667 case AttributeList::AT_ns_returns_not_retained:
John McCalled433932011-01-25 03:31:58 +00002668 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002669 S.Context));
Ted Kremenekd9c66632010-02-18 00:05:45 +00002670 return;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002671 case AttributeList::AT_cf_returns_retained:
John McCalled433932011-01-25 03:31:58 +00002672 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002673 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002674 return;
2675 case AttributeList::AT_ns_returns_retained:
John McCalled433932011-01-25 03:31:58 +00002676 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(),
Eric Christopherbc638a82010-12-01 22:13:54 +00002677 S.Context));
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002678 return;
2679 };
2680}
2681
Charles Davis163855f2010-02-16 18:27:26 +00002682static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2683 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Picheta83957a2010-12-19 06:50:37 +00002684 Attr.getKind() == AttributeList::AT_dllexport ||
2685 Attr.getKind() == AttributeList::AT_uuid;
2686}
2687
2688//===----------------------------------------------------------------------===//
2689// Microsoft specific attribute handlers.
2690//===----------------------------------------------------------------------===//
2691
2692static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2693 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2694 // check the attribute arguments.
2695 if (Attr.getNumArgs() != 1) {
2696 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2697 return;
2698 }
2699 Expr *Arg = Attr.getArg(0);
2700 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichet7da11662010-12-20 01:41:49 +00002701 if (Str == 0 || Str->isWide()) {
2702 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2703 << "uuid" << 1;
2704 return;
2705 }
2706
2707 llvm::StringRef StrRef = Str->getString();
2708
2709 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2710 StrRef.back() == '}';
2711
2712 // Validate GUID length.
2713 if (IsCurly && StrRef.size() != 38) {
2714 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2715 return;
2716 }
2717 if (!IsCurly && StrRef.size() != 36) {
2718 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2719 return;
2720 }
2721
2722 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2723 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Anders Carlsson19588aa2011-01-23 21:07:30 +00002724 llvm::StringRef::iterator I = StrRef.begin();
2725 if (IsCurly) // Skip the optional '{'
2726 ++I;
2727
2728 for (int i = 0; i < 36; ++i) {
Francois Pichet7da11662010-12-20 01:41:49 +00002729 if (i == 8 || i == 13 || i == 18 || i == 23) {
2730 if (*I != '-') {
2731 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2732 return;
2733 }
2734 } else if (!isxdigit(*I)) {
2735 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2736 return;
2737 }
2738 I++;
2739 }
Francois Picheta83957a2010-12-19 06:50:37 +00002740
2741 d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
2742 Str->getString()));
Francois Pichet7da11662010-12-20 01:41:49 +00002743 } else
Francois Picheta83957a2010-12-19 06:50:37 +00002744 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davis163855f2010-02-16 18:27:26 +00002745}
2746
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002747//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002748// Top Level Sema Entry Points
2749//===----------------------------------------------------------------------===//
2750
Peter Collingbourneb331b262011-01-21 02:08:45 +00002751static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D,
2752 const AttributeList &Attr, Sema &S) {
2753 switch (Attr.getKind()) {
2754 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
2755 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
2756 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
2757 default:
2758 break;
2759 }
2760}
Abramo Bagnara50099372010-04-30 13:10:51 +00002761
Peter Collingbourneb331b262011-01-21 02:08:45 +00002762static void ProcessInheritableDeclAttr(Scope *scope, Decl *D,
2763 const AttributeList &Attr, Sema &S) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002764 switch (Attr.getKind()) {
Ted Kremenek1f672822010-02-18 03:08:58 +00002765 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek26bde772010-05-19 17:38:06 +00002766 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2767 case AttributeList::AT_IBOutletCollection:
2768 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002769 case AttributeList::AT_address_space:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002770 case AttributeList::AT_opencl_image_access:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00002771 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00002772 case AttributeList::AT_vector_size:
Bob Wilson118baf72010-11-16 00:32:24 +00002773 case AttributeList::AT_neon_vector_type:
2774 case AttributeList::AT_neon_polyvector_type:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002775 // Ignore these, these are type attributes, handled by
2776 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002777 break;
Peter Collingbourneb331b262011-01-21 02:08:45 +00002778 case AttributeList::AT_device:
2779 case AttributeList::AT_host:
2780 case AttributeList::AT_overloadable:
2781 // Ignore, this is a non-inheritable attribute, handled
2782 // by ProcessNonInheritableDeclAttr.
2783 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002784 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2785 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002786 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00002787 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00002788 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00002789 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002790 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00002791 case AttributeList::AT_availability:HandleAvailabilityAttr(D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00002792 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00002793 HandleDependencyAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002794 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002795 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002796 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2797 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2798 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002799 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00002800 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002801 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002802 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2803 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002804 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002805 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
Peter Collingbourne827301e2010-12-12 23:03:07 +00002806 case AttributeList::AT_launch_bounds:
2807 HandleLaunchBoundsAttr(D, Attr, S);
2808 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002809 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2810 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohmanbbb7d622010-11-17 00:03:07 +00002811 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christopher8a2ee392010-12-02 02:45:55 +00002812 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002813 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekd21139a2010-07-31 01:52:11 +00002814 case AttributeList::AT_ownership_returns:
2815 case AttributeList::AT_ownership_takes:
2816 case AttributeList::AT_ownership_holds:
2817 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbar8caf6412010-09-29 18:20:25 +00002818 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002819 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2820 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Peter Collingbourne6ab610c2010-12-01 03:15:31 +00002821 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompsoncdb847ba2010-08-09 21:53:52 +00002822 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002823
2824 // Checker-specific.
John McCalled433932011-01-25 03:31:58 +00002825 case AttributeList::AT_cf_consumed:
2826 case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break;
2827 case AttributeList::AT_ns_consumes_self:
2828 HandleNSConsumesSelfAttr(D, Attr, S); break;
2829
2830 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenekd9c66632010-02-18 00:05:45 +00002831 case AttributeList::AT_ns_returns_not_retained:
2832 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00002833 case AttributeList::AT_ns_returns_retained:
2834 case AttributeList::AT_cf_returns_retained:
2835 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2836
Nate Begemanf2758702009-06-26 06:32:41 +00002837 case AttributeList::AT_reqd_wg_size:
2838 HandleReqdWorkGroupSize(D, Attr, S); break;
2839
Fariborz Jahanianef5f6212010-06-18 21:44:06 +00002840 case AttributeList::AT_init_priority:
2841 HandleInitPriorityAttr(D, Attr, S); break;
2842
Alexis Hunt54a02542009-11-25 04:20:27 +00002843 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2844 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002845 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2846 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2847 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002848 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00002849 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2850 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002851 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindolac18086a2010-02-23 22:00:30 +00002852 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002853 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002854 case AttributeList::AT_transparent_union:
2855 HandleTransparentUnionAttr(D, Attr, S);
2856 break;
Chris Lattner677a3582009-02-14 08:09:34 +00002857 case AttributeList::AT_objc_exception:
2858 HandleObjCExceptionAttr(D, Attr, S);
2859 break;
John McCall86bc21f2011-03-02 11:33:24 +00002860 case AttributeList::AT_objc_method_family:
2861 HandleObjCMethodFamilyAttr(D, Attr, S);
2862 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00002863 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2864 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2865 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2866 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2867 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2868 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2869 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2870 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2871 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00002872 case AttributeList::IgnoredAttribute:
Anders Carlssonb4f31342009-02-13 08:16:43 +00002873 // Just ignore
2874 break;
Chris Lattner3c77a352010-06-22 00:03:40 +00002875 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2876 HandleNoInstrumentFunctionAttr(D, Attr, S);
2877 break;
John McCallab26cfa2010-02-05 21:31:56 +00002878 case AttributeList::AT_stdcall:
2879 case AttributeList::AT_cdecl:
2880 case AttributeList::AT_fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002881 case AttributeList::AT_thiscall:
Dawn Perchik335e16b2010-09-03 01:29:35 +00002882 case AttributeList::AT_pascal:
Abramo Bagnara50099372010-04-30 13:10:51 +00002883 HandleCallConvAttr(D, Attr, S);
John McCallab26cfa2010-02-05 21:31:56 +00002884 break;
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002885 case AttributeList::AT_opencl_kernel_function:
2886 HandleOpenCLKernelAttr(D, Attr, S);
2887 break;
Francois Picheta83957a2010-12-19 06:50:37 +00002888 case AttributeList::AT_uuid:
2889 HandleUuidAttr(D, Attr, S);
2890 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002891 default:
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00002892 // Ask target about the attribute.
2893 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2894 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruthdd1bc0f2010-07-08 09:42:26 +00002895 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2896 << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002897 break;
2898 }
2899}
2900
Peter Collingbourneb331b262011-01-21 02:08:45 +00002901/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
2902/// the attribute applies to decls. If the attribute is a type attribute, just
2903/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2904/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
2905static void ProcessDeclAttribute(Scope *scope, Decl *D,
2906 const AttributeList &Attr, Sema &S,
2907 bool NonInheritable, bool Inheritable) {
2908 if (Attr.isInvalid())
2909 return;
2910
2911 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2912 // FIXME: Try to deal with other __declspec attributes!
2913 return;
2914
2915 if (NonInheritable)
2916 ProcessNonInheritableDeclAttr(scope, D, Attr, S);
2917
2918 if (Inheritable)
2919 ProcessInheritableDeclAttr(scope, D, Attr, S);
2920}
2921
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002922/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2923/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherbc638a82010-12-01 22:13:54 +00002924void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourneb331b262011-01-21 02:08:45 +00002925 const AttributeList *AttrList,
2926 bool NonInheritable, bool Inheritable) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00002927 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Peter Collingbourneb331b262011-01-21 02:08:45 +00002928 ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable);
Rafael Espindolac18086a2010-02-23 22:00:30 +00002929 }
2930
2931 // GCC accepts
2932 // static int a9 __attribute__((weakref));
2933 // but that looks really pointless. We reject it.
Peter Collingbourneb331b262011-01-21 02:08:45 +00002934 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindolac18086a2010-02-23 22:00:30 +00002935 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekd21139a2010-07-31 01:52:11 +00002936 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindolac18086a2010-02-23 22:00:30 +00002937 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00002938 }
2939}
2940
Ryan Flynn7d470f32009-07-30 03:15:39 +00002941/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2942/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00002943NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002944 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002945 NamedDecl *NewD = 0;
2946 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2947 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002948 FD->getInnerLocStart(),
Ryan Flynn7d470f32009-07-30 03:15:39 +00002949 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00002950 FD->getType(), FD->getTypeSourceInfo());
John McCall3e11ebe2010-03-15 10:12:16 +00002951 if (FD->getQualifier()) {
2952 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00002953 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00002954 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002955 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2956 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaradff19302011-03-08 08:55:46 +00002957 VD->getInnerLocStart(), VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00002958 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregorc4df4072010-04-19 22:54:31 +00002959 VD->getStorageClass(),
2960 VD->getStorageClassAsWritten());
John McCall3e11ebe2010-03-15 10:12:16 +00002961 if (VD->getQualifier()) {
2962 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregor14454802011-02-25 02:25:35 +00002963 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCall3e11ebe2010-03-15 10:12:16 +00002964 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00002965 }
2966 return NewD;
2967}
2968
2969/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2970/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00002971void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00002972 if (W.getUsed()) return; // only do this once
2973 W.setUsed(true);
2974 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2975 IdentifierInfo *NDId = ND->getIdentifier();
2976 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002977 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2978 NDId->getName()));
2979 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnere6eab982009-09-08 18:10:11 +00002980 WeakTopLevelDecl.push_back(NewD);
2981 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2982 // to insert Decl at TU scope, sorry.
2983 DeclContext *SavedContext = CurContext;
2984 CurContext = Context.getTranslationUnitDecl();
2985 PushOnScopeChains(NewD, S);
2986 CurContext = SavedContext;
2987 } else { // just add weak to existing
Alexis Huntdcfba7b2010-08-18 23:23:40 +00002988 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynn7d470f32009-07-30 03:15:39 +00002989 }
2990}
2991
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002992/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2993/// it, apply them to D. This is a bit tricky because PD can have attributes
2994/// specified in many different places, and we need to find and apply them all.
Peter Collingbourneb331b262011-01-21 02:08:45 +00002995void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
2996 bool NonInheritable, bool Inheritable) {
John McCall6fe02402010-10-27 00:59:00 +00002997 // It's valid to "forward-declare" #pragma weak, in which case we
2998 // have to do this.
Peter Collingbourneb331b262011-01-21 02:08:45 +00002999 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCall6fe02402010-10-27 00:59:00 +00003000 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3001 if (IdentifierInfo *Id = ND->getIdentifier()) {
3002 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3003 = WeakUndeclaredIdentifiers.find(Id);
3004 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3005 WeakInfo W = I->second;
3006 DeclApplyPragmaWeak(S, ND, W);
3007 WeakUndeclaredIdentifiers[Id] = W;
3008 }
Ryan Flynn7d470f32009-07-30 03:15:39 +00003009 }
3010 }
3011 }
3012
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003013 // Apply decl attributes from the DeclSpec if present.
John McCall53fa7142010-12-24 02:08:15 +00003014 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003015 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003016
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003017 // Walk the declarator structure, applying decl attributes that were in a type
3018 // position to the decl itself. This handles cases like:
3019 // int *__attr__(x)** D;
3020 // when X is a decl attribute.
3021 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3022 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003023 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpd3bb5572009-07-24 19:02:52 +00003024
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003025 // Finally, apply any attributes on the decl itself.
3026 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourneb331b262011-01-21 02:08:45 +00003027 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00003028}
John McCall28a6aea2009-11-04 02:18:39 +00003029
John McCallc1465822011-02-14 07:13:47 +00003030// This duplicates a vector push_back but hides the need to know the
3031// size of the type.
3032void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3033 assert(StackSize <= StackCapacity);
3034
3035 // Grow the stack if necessary.
3036 if (StackSize == StackCapacity) {
3037 unsigned newCapacity = 2 * StackCapacity + 2;
3038 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3039 const char *oldBuffer = (const char*) Stack;
3040
3041 if (StackCapacity)
3042 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3043
3044 delete[] oldBuffer;
3045 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3046 StackCapacity = newCapacity;
3047 }
3048
3049 assert(StackSize < StackCapacity);
3050 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall86121512010-01-27 03:50:35 +00003051}
3052
John McCallc1465822011-02-14 07:13:47 +00003053void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3054 Decl *decl) {
3055 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall86121512010-01-27 03:50:35 +00003056
John McCallc1465822011-02-14 07:13:47 +00003057 // Check the invariants.
3058 assert(DD.StackSize >= state.SavedStackSize);
3059 assert(state.SavedStackSize >= DD.ActiveStackBase);
3060 assert(DD.ParsingDepth > 0);
3061
3062 // Drop the parsing depth.
3063 DD.ParsingDepth--;
3064
3065 // If there are no active diagnostics, we're done.
3066 if (DD.StackSize == DD.ActiveStackBase)
John McCall86121512010-01-27 03:50:35 +00003067 return;
3068
John McCall86121512010-01-27 03:50:35 +00003069 // We only want to actually emit delayed diagnostics when we
3070 // successfully parsed a decl.
John McCallc1465822011-02-14 07:13:47 +00003071 if (decl) {
3072 // We emit all the active diagnostics, not just those starting
3073 // from the saved state. The idea is this: we get one push for a
John McCall86121512010-01-27 03:50:35 +00003074 // decl spec and another for each declarator; in a decl group like:
3075 // deprecated_typedef foo, *bar, baz();
3076 // only the declarator pops will be passed decls. This is correct;
3077 // we really do need to consider delayed diagnostics from the decl spec
3078 // for each of the different declarations.
John McCallc1465822011-02-14 07:13:47 +00003079 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3080 DelayedDiagnostic &diag = DD.Stack[i];
3081 if (diag.Triggered)
John McCall86121512010-01-27 03:50:35 +00003082 continue;
3083
John McCallc1465822011-02-14 07:13:47 +00003084 switch (diag.Kind) {
John McCall86121512010-01-27 03:50:35 +00003085 case DelayedDiagnostic::Deprecation:
John McCallc1465822011-02-14 07:13:47 +00003086 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003087 break;
3088
3089 case DelayedDiagnostic::Access:
John McCallc1465822011-02-14 07:13:47 +00003090 S.HandleDelayedAccessCheck(diag, decl);
John McCall86121512010-01-27 03:50:35 +00003091 break;
3092 }
3093 }
3094 }
3095
John McCall1064d7e2010-03-16 05:22:47 +00003096 // Destroy all the delayed diagnostics we're about to pop off.
John McCallc1465822011-02-14 07:13:47 +00003097 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor899b68f2011-03-23 15:13:44 +00003098 DD.Stack[i].Destroy();
John McCall1064d7e2010-03-16 05:22:47 +00003099
John McCallc1465822011-02-14 07:13:47 +00003100 DD.StackSize = state.SavedStackSize;
John McCall28a6aea2009-11-04 02:18:39 +00003101}
3102
3103static bool isDeclDeprecated(Decl *D) {
3104 do {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +00003105 if (D->isDeprecated())
John McCall28a6aea2009-11-04 02:18:39 +00003106 return true;
3107 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3108 return false;
3109}
3110
John McCallb45a1e72010-08-26 02:13:20 +00003111void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall86121512010-01-27 03:50:35 +00003112 Decl *Ctx) {
3113 if (isDeclDeprecated(Ctx))
John McCall28a6aea2009-11-04 02:18:39 +00003114 return;
3115
John McCall86121512010-01-27 03:50:35 +00003116 DD.Triggered = true;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003117 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003118 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003119 << DD.getDeprecationDecl()->getDeclName()
3120 << DD.getDeprecationMessage();
Fariborz Jahanian551063102010-10-06 21:18:44 +00003121 else
3122 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003123 << DD.getDeprecationDecl()->getDeclName();
John McCall28a6aea2009-11-04 02:18:39 +00003124}
3125
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003126void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003127 SourceLocation Loc,
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00003128 bool UnknownObjCClass) {
John McCall28a6aea2009-11-04 02:18:39 +00003129 // Delay if we're currently parsing a declaration.
John McCallc1465822011-02-14 07:13:47 +00003130 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3131 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall28a6aea2009-11-04 02:18:39 +00003132 return;
3133 }
3134
3135 // Otherwise, don't warn if our current context is deprecated.
3136 if (isDeclDeprecated(cast<Decl>(CurContext)))
3137 return;
Benjamin Kramerbfac7dc2010-10-09 15:49:00 +00003138 if (!Message.empty())
Fariborz Jahanian551063102010-10-06 21:18:44 +00003139 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3140 << Message;
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003141 else {
Peter Collingbourneed12ffb2011-01-02 19:53:12 +00003142 if (!UnknownObjCClass)
Fariborz Jahanian7d6e11a2010-12-21 00:44:01 +00003143 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
3144 else
3145 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
3146 }
John McCall28a6aea2009-11-04 02:18:39 +00003147}