blob: 3f33f144e48734f9f614c43acdc604fd850780b7 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Anton Korobeynikov82d0a412010-01-10 12:58:08 +000015#include "TargetAttributesSema.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000016#include "clang/AST/ASTContext.h"
John McCall384aff82010-08-25 07:42:41 +000017#include "clang/AST/DeclCXX.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000020#include "clang/Basic/TargetInfo.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/DeclSpec.h"
John McCall9c3087b2010-08-26 02:13:20 +000022#include "clang/Sema/DelayedDiagnostic.h"
Chris Lattner797c3c42009-08-10 19:03:04 +000023#include "llvm/ADT/StringExtras.h"
Chris Lattner6b6b5372008-06-26 18:38:35 +000024using namespace clang;
John McCall9c3087b2010-08-26 02:13:20 +000025using namespace sema;
Chris Lattner6b6b5372008-06-26 18:38:35 +000026
John McCall883cc2c2011-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 Lattnere5c5ee12008-06-29 00:16:31 +000047//===----------------------------------------------------------------------===//
48// Helper functions
49//===----------------------------------------------------------------------===//
50
Ted Kremeneka18d7d82009-08-14 20:49:40 +000051static const FunctionType *getFunctionType(const Decl *d,
52 bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000053 QualType Ty;
Ted Kremeneka18d7d82009-08-14 20:49:40 +000054 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000055 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000056 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000057 Ty = decl->getType();
Ted Kremeneka18d7d82009-08-14 20:49:40 +000058 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner6b6b5372008-06-26 18:38:35 +000059 Ty = decl->getUnderlyingType();
60 else
61 return 0;
Mike Stumpbf916502009-07-24 19:02:52 +000062
Chris Lattner6b6b5372008-06-26 18:38:35 +000063 if (Ty->isFunctionPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000064 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000065 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenek6217b802009-07-29 21:53:49 +000066 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000067
John McCall183700f2009-09-21 23:43:11 +000068 return Ty->getAs<FunctionType>();
Chris Lattner6b6b5372008-06-26 18:38:35 +000069}
70
Daniel Dunbar35682492008-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 Lopesd20254f2009-12-20 23:11:08 +000074/// isFunction - Return true if the given decl has function
Ted Kremeneka18d7d82009-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 Dunbard3f2c102008-10-19 02:04:16 +000081/// type (function or function-typed variable) or an Objective-C
82/// method.
Ted Kremeneka18d7d82009-08-14 20:49:40 +000083static bool isFunctionOrMethod(const Decl *d) {
84 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000085}
86
Fariborz Jahanian620d89c2009-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 Kremeneka18d7d82009-08-14 20:49:40 +000090static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian620d89c2009-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 Jahaniand66f22d2009-05-19 17:08:59 +000098 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000099}
100
John McCall711c52b2011-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 Dunbard3f2c102008-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 Jahanian620d89c2009-05-15 23:15:03 +0000110/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000111static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000112 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000113 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +0000114 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000115 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-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 Kremeneka18d7d82009-08-14 20:49:40 +0000123static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +0000124 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000125 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000126 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
127 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +0000128 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +0000129}
130
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000131static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +0000132 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +0000133 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000134 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
135 return BD->getParamDecl(Idx)->getType();
Mike Stumpbf916502009-07-24 19:02:52 +0000136
Chris Lattner89951a82009-02-20 18:43:26 +0000137 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000138}
139
Ted Kremeneka18d7d82009-08-14 20:49:40 +0000140static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanian5b160922009-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 Kremeneka18d7d82009-08-14 20:49:40 +0000146static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000147 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000148 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000149 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000150 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
Ted Kremenekdb9a0ae2010-04-29 16:48:58 +0000151 return BD->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000152 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000153 return cast<ObjCMethodDecl>(d)->isVariadic();
154 }
155}
156
Chandler Carruth07d7e7a2010-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 Lattner6b6b5372008-06-26 18:38:35 +0000163static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall183700f2009-09-21 23:43:11 +0000164 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattnerb77792e2008-07-26 22:17:49 +0000165 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000166 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000167
John McCall506b57e2010-05-17 21:00:27 +0000168 ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface();
169 if (!Cls)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000170 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000171
John McCall506b57e2010-05-17 21:00:27 +0000172 IdentifierInfo* ClsName = Cls->getIdentifier();
Mike Stumpbf916502009-07-24 19:02:52 +0000173
Chris Lattner6b6b5372008-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 Dunbar085e8f72008-09-26 03:32:58 +0000179static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenek6217b802009-07-29 21:53:49 +0000180 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000181 if (!PT)
182 return false;
183
Ted Kremenek6217b802009-07-29 21:53:49 +0000184 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000185 if (!RT)
186 return false;
Mike Stumpbf916502009-07-24 19:02:52 +0000187
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000188 const RecordDecl *RD = RT->getDecl();
Abramo Bagnara465d41b2010-05-11 21:36:43 +0000189 if (RD->getTagKind() != TTK_Struct)
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000190 return false;
191
192 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
193}
194
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000195//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000196// Attribute Implementations
197//===----------------------------------------------------------------------===//
198
Daniel Dunbar3068ae02008-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 Stumpbf916502009-07-24 19:02:52 +0000203static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000204 const AttributeList &Attr, Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000205 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
206 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000207 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000208 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000209 }
Mike Stumpbf916502009-07-24 19:02:52 +0000210
Chris Lattner6b6b5372008-06-26 18:38:35 +0000211 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor9cdda0c2009-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 McCallf7a1a742009-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 Gregor9cdda0c2009-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 Collingbourne7a730022010-11-23 20:45:58 +0000227 sizeExpr = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000228 }
Douglas Gregor9cdda0c2009-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 McCall9ae2f072010-08-23 23:25:46 +0000232 QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc());
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000233 if (!T.isNull()) {
John McCallba6a9bd2009-10-24 08:00:42 +0000234 // FIXME: preserve the old source info.
John McCalla93c9342009-12-07 02:54:59 +0000235 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpbf916502009-07-24 19:02:52 +0000236
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000237 // Remember this typedef decl, we will need it later for diagnostics.
238 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000239 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000240}
241
Chris Lattner803d0802008-06-29 00:43:07 +0000242static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000243 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000244 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000245 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000246 return;
247 }
Mike Stumpbf916502009-07-24 19:02:52 +0000248
Chris Lattner6b6b5372008-06-26 18:38:35 +0000249 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Sean Huntcf807c42010-08-18 23:23:40 +0000250 TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-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 Lattner803d0802008-06-29 00:43:07 +0000255 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000256 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000257 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000258 else
Sean Huntcf807c42010-08-18 23:23:40 +0000259 FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000260 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000261 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000262}
263
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000264static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000265 // check the attribute arguments.
266 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000267 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000268 return;
269 }
Mike Stumpbf916502009-07-24 19:02:52 +0000270
Ted Kremenek63e5d7c2010-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()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000274 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000275 return;
276 }
277
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000278 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-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 Kremenekefbddd22010-02-17 02:37:45 +0000289 // Objective-C classes.
290 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000291 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000292 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000293 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000294
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000295 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000296}
297
Ted Kremenek857e9182010-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 Jahaniana8fb24f2010-08-17 20:23:12 +0000302 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-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 Kremenek4ee2bb12011-02-04 06:54:16 +0000310 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek857e9182010-05-19 17:38:06 +0000311 return;
312 }
Fariborz Jahanian3a3400b2010-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 Jahaniana8fb24f2010-08-17 20:23:12 +0000326 IdentifierInfo *II = Attr.getParameterName();
327 if (!II)
328 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000329
John McCallb3d87482010-08-24 05:47:05 +0000330 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Fariborz Jahaniana8fb24f2010-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 McCallb3d87482010-08-24 05:47:05 +0000336 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-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 }
Sean Huntcf807c42010-08-18 23:23:40 +0000346 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
347 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000348}
349
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000350static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-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 Dunbard3f2c102008-10-19 02:04:16 +0000353 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000354 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000355 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000356 return;
357 }
Mike Stumpbf916502009-07-24 19:02:52 +0000358
Chandler Carruth07d7e7a2010-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 Kremenekeb2b2a32008-07-21 21:53:04 +0000363
364 // The nonnull attribute only applies to pointers.
365 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000366
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000367 for (AttributeList::arg_iterator I=Attr.arg_begin(),
368 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000369
370
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000371 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000372 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000373 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000374 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
375 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000376 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
377 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000378 return;
379 }
Mike Stumpbf916502009-07-24 19:02:52 +0000380
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000381 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000382
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000383 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000384 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000385 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000386 return;
387 }
Mike Stumpbf916502009-07-24 19:02:52 +0000388
Ted Kremenek465172f2008-07-21 22:09:15 +0000389 --x;
Chandler Carruth07d7e7a2010-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 Kremenekeb2b2a32008-07-21 21:53:04 +0000399
400 // Is the function argument a pointer type?
Douglas Gregorde436322010-12-15 15:41:46 +0000401 QualType T = getFunctionOrMethodArgType(d, x).getNonReferenceType();
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000402 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000403 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000404 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000405 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000406 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000407 }
Mike Stumpbf916502009-07-24 19:02:52 +0000408
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000409 NonNullArgs.push_back(x);
410 }
Mike Stumpbf916502009-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 Kremenek7fb43c12008-09-01 19:57:52 +0000414 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000415 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
Douglas Gregorde436322010-12-15 15:41:46 +0000416 QualType T = getFunctionOrMethodArgType(d, I).getNonReferenceType();
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000417 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000418 NonNullArgs.push_back(I);
Fariborz Jahanianff3a0782010-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 Kremenek46bbaca2008-11-18 06:52:58 +0000432 }
Mike Stumpbf916502009-07-24 19:02:52 +0000433
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000434 // No pointer arguments?
Fariborz Jahanian60acea42010-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 Kremenek7fb43c12008-09-01 19:57:52 +0000440 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000441 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000442 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000443
444 unsigned* start = &NonNullArgs[0];
445 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000446 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000447 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
448 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000449}
450
Ted Kremenekdd0e4902010-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 Rose2a479922010-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 Kremenekdd0e4902010-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.
Sean Huntcf807c42010-08-18 23:23:40 +0000467 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000468 switch (AL.getKind()) {
469 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000470 K = OwnershipAttr::Takes;
Ted Kremenekdd0e4902010-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 Rose2a479922010-08-12 08:54:03 +0000475 break;
476 case AttributeList::AT_ownership_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000477 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-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 Rose2a479922010-08-12 08:54:03 +0000482 break;
483 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000484 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-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 Rose2a479922010-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 Kremenekdd0e4902010-07-31 01:52:11 +0000494 }
495
496 if (!isFunction(d) || !hasFunctionProto(d)) {
John McCall883cc2c2011-03-02 12:29:23 +0000497 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
498 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000499 return;
500 }
501
Chandler Carruth07d7e7a2010-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 Kremenekdd0e4902010-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 Rose2a479922010-08-12 08:54:03 +0000515 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
516 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000517
Peter Collingbourne7a730022010-11-23 20:45:58 +0000518 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-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 Carruth07d7e7a2010-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 Kremenekdd0e4902010-07-31 01:52:11 +0000544 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000545 case OwnershipAttr::Takes:
546 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-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)
Sean Huntcf807c42010-08-18 23:23:40 +0000552 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000553 << "pointer"
554 << IdxExpr->getSourceRange();
555 continue;
556 }
557 break;
558 }
Sean Huntcf807c42010-08-18 23:23:40 +0000559 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000560 if (AL.getNumArgs() > 1) {
561 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +0000562 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-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 Rose2a479922010-08-12 08:54:03 +0000574 default:
575 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000576 } // switch
577
578 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-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 Kremenekdd0e4902010-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);
Sean Huntcf807c42010-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 Kremenekdd0e4902010-07-31 01:52:11 +0000603 }
Sean Huntcf807c42010-08-18 23:23:40 +0000604
605 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
606 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000607}
608
John McCall332bb2a2011-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 Espindola11e8ce72010-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 McCall332bb2a2011-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 McCall883cc2c2011-03-02 12:29:23 +0000638 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +0000639 return;
640 }
641
642 NamedDecl *nd = cast<NamedDecl>(d);
643
Rafael Espindola11e8ce72010-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 Redl7a126a42010-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 McCall332bb2a2011-02-08 22:35:49 +0000657 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +0000658 return;
Rafael Espindola11e8ce72010-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 McCall332bb2a2011-02-08 22:35:49 +0000679 if (!hasEffectivelyInternalLinkage(nd)) {
680 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-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 Collingbourne7a730022010-11-23 20:45:58 +0000689 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-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 Christopherf48f3672010-12-01 22:13:54 +0000700 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
701 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000702 }
703
Sean Huntcf807c42010-08-18 23:23:40 +0000704 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000705}
706
Chris Lattner803d0802008-06-29 00:43:07 +0000707static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000708 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000709 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000710 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000711 return;
712 }
Mike Stumpbf916502009-07-24 19:02:52 +0000713
Peter Collingbourne7a730022010-11-23 20:45:58 +0000714 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000715 Arg = Arg->IgnoreParenCasts();
716 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000717
Chris Lattner6b6b5372008-06-26 18:38:35 +0000718 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000719 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000720 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000721 return;
722 }
Mike Stumpbf916502009-07-24 19:02:52 +0000723
Rafael Espindolaf5fe2922010-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 Lattner6b6b5372008-06-26 18:38:35 +0000729 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000730
Eric Christopherf48f3672010-12-01 22:13:54 +0000731 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
732 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000733}
734
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000735static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000736 Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000737 // Check the attribute arguments.
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000738 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000739 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000740 return;
741 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000742
Chris Lattnerc5197432009-04-14 17:02:11 +0000743 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000744 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000745 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-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 McCall883cc2c2011-03-02 12:29:23 +0000762 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000763 return;
764 }
Mike Stumpbf916502009-07-24 19:02:52 +0000765
Sean Huntcf807c42010-08-18 23:23:40 +0000766 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000767}
768
Ryan Flynn76168e22009-08-09 20:07:29 +0000769static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000770 // Check the attribute arguments.
Ryan Flynn76168e22009-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 Stump1eb44332009-09-09 15:08:12 +0000775
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000776 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000777 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000778 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000779 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000780 return;
781 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000782 }
783
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000784 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000785}
786
Dan Gohman34c26302010-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 Gohman34c26302010-11-17 00:03:07 +0000794 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
795}
796
Eric Christophera6cf1e72010-12-02 02:45:55 +0000797static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
798 assert(Attr.isInvalid() == false);
Eric Christopher722109c2010-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 McCall883cc2c2011-03-02 12:29:23 +0000803 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000804}
805
806static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
807 assert(Attr.isInvalid() == false);
Eric Christopher722109c2010-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 McCall883cc2c2011-03-02 12:29:23 +0000812 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000813}
814
John McCall711c52b2011-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 McCall883cc2c2011-03-02 12:29:23 +0000822 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-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 Kremenekb7252322009-04-10 00:01:14 +0000837}
838
839static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
840 Sema &S) {
Ted Kremenekb56c1cc2010-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 McCall883cc2c2011-03-02 12:29:23 +0000857 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000858 return;
859 }
860 }
861
862 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000863}
864
John Thompson35cc9622010-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 Christopherf48f3672010-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 Thompson35cc9622010-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 Thompson01add592010-09-18 01:12:07 +0000891 if (!isa<RecordDecl>(d)) {
John Thompson35cc9622010-08-09 21:53:52 +0000892 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000893 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-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 Thompson01add592010-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 Christopherf48f3672010-12-01 22:13:54 +0000915 for (RecordDecl::field_iterator iter = record->field_begin();
916 iter != record->field_end(); iter++) {
John Thompson01add592010-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
Sean Huntcf807c42010-08-18 23:23:40 +0000924 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000925}
926
Sean Huntbbd37c62009-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 McCall883cc2c2011-03-02 12:29:23 +0000930 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +0000931 return;
932 }
933 // FIXME: Actually store the attribute on the declaration
934}
935
Ted Kremenek73798892008-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 Lattner3c73c412008-11-19 08:23:25 +0000939 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000940 return;
941 }
Mike Stumpbf916502009-07-24 19:02:52 +0000942
John McCallaec58602010-03-31 02:47:45 +0000943 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
Chris Lattner57ad3782011-02-17 20:34:02 +0000944 !isa<TypeDecl>(d) && !isa<LabelDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000945 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000946 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +0000947 return;
948 }
Mike Stumpbf916502009-07-24 19:02:52 +0000949
Sean Huntcf807c42010-08-18 23:23:40 +0000950 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000951}
952
Daniel Dunbarb805dad2009-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 Stumpbf916502009-07-24 19:02:52 +0000959
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000960 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000961 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-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 McCall883cc2c2011-03-02 12:29:23 +0000967 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000968 return;
969 }
Mike Stumpbf916502009-07-24 19:02:52 +0000970
Sean Huntcf807c42010-08-18 23:23:40 +0000971 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000972}
973
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000974static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
975 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +0000976 if (Attr.getNumArgs() > 1) {
977 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000978 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000979 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000980
981 int priority = 65535; // FIXME: Do not hardcode such constants.
982 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000983 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000984 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000985 if (E->isTypeDependent() || E->isValueDependent() ||
986 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000987 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000988 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000989 return;
990 }
991 priority = Idx.getZExtValue();
992 }
Mike Stumpbf916502009-07-24 19:02:52 +0000993
Chris Lattnerc5197432009-04-14 17:02:11 +0000994 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000995 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000996 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000997 return;
998 }
999
Eric Christopherf48f3672010-12-01 22:13:54 +00001000 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
1001 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001002}
1003
1004static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1005 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001006 if (Attr.getNumArgs() > 1) {
1007 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001008 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001009 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001010
1011 int priority = 65535; // FIXME: Do not hardcode such constants.
1012 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001013 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001014 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001015 if (E->isTypeDependent() || E->isValueDependent() ||
1016 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001017 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001018 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001019 return;
1020 }
1021 priority = Idx.getZExtValue();
1022 }
Mike Stumpbf916502009-07-24 19:02:52 +00001023
Anders Carlsson6782fc62008-08-22 22:10:48 +00001024 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001025 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001026 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001027 return;
1028 }
1029
Eric Christopherf48f3672010-12-01 22:13:54 +00001030 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
1031 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001032}
1033
Chris Lattner803d0802008-06-29 00:43:07 +00001034static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001035 unsigned NumArgs = Attr.getNumArgs();
1036 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001037 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001038 return;
1039 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001040
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001041 // Handle the case where deprecated attribute has a text message.
Chris Lattner951bbb22011-02-24 05:42:24 +00001042 llvm::StringRef Str;
1043 if (NumArgs == 1) {
1044 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001045 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001046 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1047 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001048 return;
1049 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001050 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001051 }
Mike Stumpbf916502009-07-24 19:02:52 +00001052
Chris Lattner951bbb22011-02-24 05:42:24 +00001053 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001054}
1055
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001056static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001057 unsigned NumArgs = Attr.getNumArgs();
1058 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001059 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001060 return;
1061 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001062
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001063 // Handle the case where unavailable attribute has a text message.
Chris Lattner951bbb22011-02-24 05:42:24 +00001064 llvm::StringRef Str;
1065 if (NumArgs == 1) {
1066 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001067 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001068 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001069 diag::err_attribute_not_string) << "unavailable";
1070 return;
1071 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001072 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001073 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001074 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001075}
1076
Douglas Gregor0a0d2b12011-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 Lattner803d0802008-06-29 00:43:07 +00001128static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001129 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001130 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001131 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001132 return;
1133 }
Mike Stumpbf916502009-07-24 19:02:52 +00001134
Peter Collingbourne7a730022010-11-23 20:45:58 +00001135 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001136 Arg = Arg->IgnoreParenCasts();
1137 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001138
Chris Lattner6b6b5372008-06-26 18:38:35 +00001139 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001141 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001142 return;
1143 }
Mike Stumpbf916502009-07-24 19:02:52 +00001144
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001145 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001146 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001147
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001148 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001149 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001150 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001151 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001152 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001153 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001154 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001155 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001156 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001157 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001158 return;
1159 }
Mike Stumpbf916502009-07-24 19:02:52 +00001160
Sean Huntcf807c42010-08-18 23:23:40 +00001161 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001162}
1163
John McCalld5313b02011-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 McCall883cc2c2011-03-02 12:29:23 +00001169 << ExpectedMethod;
John McCalld5313b02011-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 Lattner0db29ec2009-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 Stumpbf916502009-07-24 19:02:52 +00001215
Chris Lattner0db29ec2009-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 Stumpbf916502009-07-24 19:02:52 +00001221
Sean Huntcf807c42010-08-18 23:23:40 +00001222 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001223}
1224
1225static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001226 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001227 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001228 return;
1229 }
Chris Lattner0db29ec2009-02-14 08:09:34 +00001230 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001231 QualType T = TD->getUnderlyingType();
1232 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001233 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001234 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1235 return;
1236 }
1237 }
Sean Huntcf807c42010-08-18 23:23:40 +00001238 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001239}
1240
Mike Stumpbf916502009-07-24 19:02:52 +00001241static void
Douglas Gregorf9201e02009-02-11 23:02:49 +00001242HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1243 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001244 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-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
Sean Huntcf807c42010-08-18 23:23:40 +00001253 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001254}
1255
Steve Naroff9eae5762008-09-18 16:44:58 +00001256static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001257 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001258 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001259 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001260 return;
1261 }
Mike Stumpbf916502009-07-24 19:02:52 +00001262
Steve Naroff9eae5762008-09-18 16:44:58 +00001263 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001264 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001265 return;
1266 }
Mike Stumpbf916502009-07-24 19:02:52 +00001267
Sean Huntcf807c42010-08-18 23:23:40 +00001268 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001269 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001270 type = BlocksAttr::ByRef;
1271 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001272 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001273 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001274 return;
1275 }
Mike Stumpbf916502009-07-24 19:02:52 +00001276
Sean Huntcf807c42010-08-18 23:23:40 +00001277 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001278}
1279
Anders Carlsson77091822008-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 McCallbdc49d32011-03-02 12:15:05 +00001283 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001284 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001285 }
1286
Anders Carlsson77091822008-10-05 18:05:59 +00001287 int sentinel = 0;
1288 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001289 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001290 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001291 if (E->isTypeDependent() || E->isValueDependent() ||
1292 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001293 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001294 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001295 return;
1296 }
1297 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001298
Anders Carlsson77091822008-10-05 18:05:59 +00001299 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001300 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1301 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001302 return;
1303 }
1304 }
1305
1306 int nullPos = 0;
1307 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001308 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001309 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001310 if (E->isTypeDependent() || E->isValueDependent() ||
1311 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001312 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001313 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001314 return;
1315 }
1316 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001317
Anders Carlsson77091822008-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 Lattnerfa25bbb2008-11-19 05:08:23 +00001321 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1322 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001323 return;
1324 }
1325 }
1326
1327 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001328 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001329 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001330
Chris Lattner897cd902009-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 Stumpbf916502009-07-24 19:02:52 +00001335
Chris Lattner897cd902009-03-17 23:03:47 +00001336 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001337 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001338 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001339 }
Anders Carlsson77091822008-10-05 18:05:59 +00001340 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1341 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001342 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001343 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001344 }
1345 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001346 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1347 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001348 ;
1349 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001350 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001351 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001352 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherf48f3672010-12-01 22:13:54 +00001353 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001354 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-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 Jahanian2f7c3922009-05-14 20:53:39 +00001357 return;
1358 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001359 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001360 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001361 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001362 return;
1363 }
Anders Carlsson77091822008-10-05 18:05:59 +00001364 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001365 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001366 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001367 return;
1368 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001369 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1370 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001371}
1372
Chris Lattner026dc962009-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 Jahanianf0317742010-03-30 18:22:15 +00001380 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001381 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001382 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001383 return;
1384 }
Mike Stumpbf916502009-07-24 19:02:52 +00001385
Fariborz Jahanianf0317742010-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 Lopesf8577982009-12-22 23:59:52 +00001389 return;
1390 }
Fariborz Jahanianf0317742010-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
Sean Huntcf807c42010-08-18 23:23:40 +00001398 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001399}
1400
John McCall332bb2a2011-02-08 22:35:49 +00001401static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001402 // check the attribute arguments.
John McCall332bb2a2011-02-08 22:35:49 +00001403 if (attr.getNumArgs() != 0) {
1404 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001405 return;
1406 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001407
John McCall332bb2a2011-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 McCall883cc2c2011-03-02 12:29:23 +00001410 << attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001411 return;
1412 }
1413
John McCall332bb2a2011-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 Dunbar6e775db2009-03-06 06:39:57 +00001419 return;
1420 }
Mike Stumpbf916502009-07-24 19:02:52 +00001421
John McCall332bb2a2011-02-08 22:35:49 +00001422 nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001423}
1424
Daniel Dunbar6e775db2009-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 Stumpbf916502009-07-24 19:02:52 +00001430 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001431
1432 // weak_import only applies to variable & function declarations.
1433 bool isDef = false;
Douglas Gregor0a0d2b12011-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*/;
1439 else if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin ||
1440 (!isa<ObjCInterfaceDecl>(D) &&
1441 !isa<ObjCPropertyDecl>(D) &&
1442 !isa<ObjCMethodDecl>(D)))
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001443 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001444 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001445
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001446 return;
1447 }
1448
Sean Huntcf807c42010-08-18 23:23:40 +00001449 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001450}
1451
Nate Begeman6f3d8382009-06-26 06:32:41 +00001452static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1453 Sema &S) {
1454 // Attribute has 3 arguments.
1455 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001456 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001457 return;
1458 }
1459
1460 unsigned WGSize[3];
1461 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001462 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001463 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001464 if (E->isTypeDependent() || E->isValueDependent() ||
1465 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001466 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1467 << "reqd_work_group_size" << E->getSourceRange();
1468 return;
1469 }
1470 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1471 }
Sean Huntcf807c42010-08-18 23:23:40 +00001472 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1473 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001474 WGSize[2]));
1475}
1476
Chris Lattner026dc962009-02-14 07:37:35 +00001477static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001478 // Attribute has no arguments.
1479 if (Attr.getNumArgs() != 1) {
1480 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1481 return;
1482 }
1483
1484 // Make sure that there is a string literal as the sections's single
1485 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001486 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001487 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001488 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001489 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001490 return;
1491 }
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Chris Lattner797c3c42009-08-10 19:03:04 +00001493 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001494 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001495 if (!Error.empty()) {
1496 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1497 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001498 return;
1499 }
Mike Stump1eb44332009-09-09 15:08:12 +00001500
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001501 // This attribute cannot be applied to local variables.
1502 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1503 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1504 return;
1505 }
1506
Eric Christopherf48f3672010-12-01 22:13:54 +00001507 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1508 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001509}
1510
Chris Lattner6b6b5372008-06-26 18:38:35 +00001511
Chris Lattner803d0802008-06-29 00:43:07 +00001512static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001513 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001514 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001515 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001516 return;
1517 }
Mike Stumpbf916502009-07-24 19:02:52 +00001518
Sean Huntcf807c42010-08-18 23:23:40 +00001519 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001520}
1521
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001522static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1523 // check the attribute arguments.
1524 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001525 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001526 return;
1527 }
Mike Stumpbf916502009-07-24 19:02:52 +00001528
Sean Huntcf807c42010-08-18 23:23:40 +00001529 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001530}
1531
1532static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1533 // check the attribute arguments.
1534 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001535 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001536 return;
1537 }
Mike Stumpbf916502009-07-24 19:02:52 +00001538
Sean Huntcf807c42010-08-18 23:23:40 +00001539 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001540}
1541
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001542static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001543 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001544 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1545 return;
1546 }
Mike Stumpbf916502009-07-24 19:02:52 +00001547
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001548 if (Attr.getNumArgs() != 0) {
1549 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1550 return;
1551 }
Mike Stumpbf916502009-07-24 19:02:52 +00001552
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001553 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001554
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001555 if (!VD || !VD->hasLocalStorage()) {
1556 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1557 return;
1558 }
Mike Stumpbf916502009-07-24 19:02:52 +00001559
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001560 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001561 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00001562 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001563 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1564 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001565 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001566 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001567 Attr.getParameterName();
1568 return;
1569 }
Mike Stumpbf916502009-07-24 19:02:52 +00001570
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001571 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1572 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001573 S.Diag(Attr.getParameterLoc(),
1574 diag::err_attribute_cleanup_arg_not_function)
1575 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001576 return;
1577 }
1578
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001579 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001580 S.Diag(Attr.getParameterLoc(),
1581 diag::err_attribute_cleanup_func_must_take_one_arg)
1582 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001583 return;
1584 }
Mike Stumpbf916502009-07-24 19:02:52 +00001585
Anders Carlsson89941c12009-02-07 23:16:50 +00001586 // We're currently more strict than GCC about what function types we accept.
1587 // If this ever proves to be a problem it should be easy to fix.
1588 QualType Ty = S.Context.getPointerType(VD->getType());
1589 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00001590 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1591 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001592 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001593 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1594 Attr.getParameterName() << ParamTy << Ty;
1595 return;
1596 }
Mike Stumpbf916502009-07-24 19:02:52 +00001597
Sean Huntcf807c42010-08-18 23:23:40 +00001598 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00001599 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001600}
1601
Mike Stumpbf916502009-07-24 19:02:52 +00001602/// Handle __attribute__((format_arg((idx)))) attribute based on
1603/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1604static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001605 if (Attr.getNumArgs() != 1) {
1606 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1607 return;
1608 }
1609 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1610 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001611 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001612 return;
1613 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001614
1615 // In C++ the implicit 'this' function parameter also counts, and they are
1616 // counted from one.
1617 bool HasImplicitThisParam = isInstanceMethod(d);
1618 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001619 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001620
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001621 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001622 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001623 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001624 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1625 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001626 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1627 << "format" << 2 << IdxExpr->getSourceRange();
1628 return;
1629 }
Mike Stumpbf916502009-07-24 19:02:52 +00001630
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001631 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1632 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1633 << "format" << 2 << IdxExpr->getSourceRange();
1634 return;
1635 }
Mike Stumpbf916502009-07-24 19:02:52 +00001636
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001637 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001638
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001639 if (HasImplicitThisParam) {
1640 if (ArgIdx == 0) {
1641 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1642 << "format_arg" << IdxExpr->getSourceRange();
1643 return;
1644 }
1645 ArgIdx--;
1646 }
1647
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001648 // make sure the format string is really a string
1649 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001650
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001651 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1652 if (not_nsstring_type &&
1653 !isCFStringType(Ty, S.Context) &&
1654 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001655 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001656 // FIXME: Should highlight the actual expression that has the wrong type.
1657 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001658 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001659 << IdxExpr->getSourceRange();
1660 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001661 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001662 Ty = getFunctionOrMethodResultType(d);
1663 if (!isNSStringType(Ty, S.Context) &&
1664 !isCFStringType(Ty, S.Context) &&
1665 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001666 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001667 // FIXME: Should highlight the actual expression that has the wrong type.
1668 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001669 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001670 << IdxExpr->getSourceRange();
1671 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001672 }
1673
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001674 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1675 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001676}
1677
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001678enum FormatAttrKind {
1679 CFStringFormat,
1680 NSStringFormat,
1681 StrftimeFormat,
1682 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001683 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001684 InvalidFormat
1685};
1686
1687/// getFormatAttrKind - Map from format attribute names to supported format
1688/// types.
1689static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1690 // Check for formats that get handled specially.
1691 if (Format == "NSString")
1692 return NSStringFormat;
1693 if (Format == "CFString")
1694 return CFStringFormat;
1695 if (Format == "strftime")
1696 return StrftimeFormat;
1697
1698 // Otherwise, check for supported formats.
1699 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1700 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1701 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00001702 Format == "zcmn_err" ||
1703 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001704 return SupportedFormat;
1705
Duncan Sandsbc525952010-03-23 14:44:19 +00001706 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1707 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001708 return IgnoredFormat;
1709
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001710 return InvalidFormat;
1711}
1712
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001713/// Handle __attribute__((init_priority(priority))) attributes based on
1714/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1715static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1716 Sema &S) {
1717 if (!S.getLangOptions().CPlusPlus) {
1718 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1719 return;
1720 }
1721
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001722 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1723 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1724 Attr.setInvalid();
1725 return;
1726 }
1727 QualType T = dyn_cast<VarDecl>(d)->getType();
1728 if (S.Context.getAsArrayType(T))
1729 T = S.Context.getBaseElementType(T);
1730 if (!T->getAs<RecordType>()) {
1731 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1732 Attr.setInvalid();
1733 return;
1734 }
1735
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001736 if (Attr.getNumArgs() != 1) {
1737 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1738 Attr.setInvalid();
1739 return;
1740 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001741 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001742
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001743 llvm::APSInt priority(32);
1744 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1745 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1746 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1747 << "init_priority" << priorityExpr->getSourceRange();
1748 Attr.setInvalid();
1749 return;
1750 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001751 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001752 if (prioritynum < 101 || prioritynum > 65535) {
1753 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1754 << priorityExpr->getSourceRange();
1755 Attr.setInvalid();
1756 return;
1757 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001758 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1759 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001760}
1761
Mike Stumpbf916502009-07-24 19:02:52 +00001762/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1763/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001764static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001765
Chris Lattner545dd342008-06-28 23:36:30 +00001766 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001767 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001768 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001769 return;
1770 }
1771
Chris Lattner545dd342008-06-28 23:36:30 +00001772 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001773 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001774 return;
1775 }
1776
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001777 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001778 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001779 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001780 return;
1781 }
1782
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001783 // In C++ the implicit 'this' function parameter also counts, and they are
1784 // counted from one.
1785 bool HasImplicitThisParam = isInstanceMethod(d);
1786 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001787 unsigned FirstIdx = 1;
1788
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001789 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001790
1791 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001792 if (Format.startswith("__") && Format.endswith("__"))
1793 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001794
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001795 // Check for supported formats.
1796 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001797
1798 if (Kind == IgnoredFormat)
1799 return;
1800
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001801 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001802 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001803 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001804 return;
1805 }
1806
1807 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001808 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00001809 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001810 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1811 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001812 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001813 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001814 return;
1815 }
1816
1817 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001818 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001819 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001820 return;
1821 }
1822
1823 // FIXME: Do we need to bounds check?
1824 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001825
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001826 if (HasImplicitThisParam) {
1827 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001828 S.Diag(Attr.getLoc(),
1829 diag::err_format_attribute_implicit_this_format_string)
1830 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001831 return;
1832 }
1833 ArgIdx--;
1834 }
Mike Stump1eb44332009-09-09 15:08:12 +00001835
Chris Lattner6b6b5372008-06-26 18:38:35 +00001836 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001837 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001838
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001839 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001840 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001841 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1842 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001843 return;
1844 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001845 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001846 // FIXME: do we need to check if the type is NSString*? What are the
1847 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001848 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001849 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001850 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1851 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001852 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001853 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001854 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001855 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001856 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001857 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1858 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001859 return;
1860 }
1861
1862 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001863 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00001864 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001865 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1866 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001867 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001868 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001869 return;
1870 }
1871
1872 // check if the function is variadic if the 3rd argument non-zero
1873 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001874 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001875 ++NumArgs; // +1 for ...
1876 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001877 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001878 return;
1879 }
1880 }
1881
Chris Lattner3c73c412008-11-19 08:23:25 +00001882 // strftime requires FirstArg to be 0 because it doesn't read from any
1883 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001884 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001885 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001886 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1887 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001888 return;
1889 }
1890 // if 0 it disables parameter checking (to use with e.g. va_list)
1891 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001892 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001893 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001894 return;
1895 }
1896
Sean Huntcf807c42010-08-18 23:23:40 +00001897 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1898 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001899 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001900}
1901
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001902static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1903 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001904 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001905 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001906 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001907 return;
1908 }
1909
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001910 // Try to find the underlying union declaration.
1911 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001912 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001913 if (TD && TD->getUnderlyingType()->isUnionType())
1914 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1915 else
1916 RD = dyn_cast<RecordDecl>(d);
1917
1918 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001919 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001920 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001921 return;
1922 }
1923
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001924 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001925 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001926 diag::warn_transparent_union_attribute_not_definition);
1927 return;
1928 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001929
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001930 RecordDecl::field_iterator Field = RD->field_begin(),
1931 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001932 if (Field == FieldEnd) {
1933 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1934 return;
1935 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001936
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001937 FieldDecl *FirstField = *Field;
1938 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001939 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001940 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001941 diag::warn_transparent_union_attribute_floating)
1942 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001943 return;
1944 }
1945
1946 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1947 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1948 for (; Field != FieldEnd; ++Field) {
1949 QualType FieldType = Field->getType();
1950 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1951 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1952 // Warn if we drop the attribute.
1953 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001954 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001955 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001956 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001957 diag::warn_transparent_union_attribute_field_size_align)
1958 << isSize << Field->getDeclName() << FieldBits;
1959 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001960 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001961 diag::note_transparent_union_first_field_size_align)
1962 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001963 return;
1964 }
1965 }
1966
Sean Huntcf807c42010-08-18 23:23:40 +00001967 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001968}
1969
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001970static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001971 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001972 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001973 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001974 return;
1975 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001976 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001977 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001978
Chris Lattner6b6b5372008-06-26 18:38:35 +00001979 // Make sure that there is a string literal as the annotation's single
1980 // argument.
1981 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001982 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001983 return;
1984 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001985 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1986 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001987}
1988
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001989static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001990 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001991 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001992 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001993 return;
1994 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001995
1996 //FIXME: The C++0x version of this attribute has more limited applicabilty
1997 // than GNU's, and should error out when it is used to specify a
1998 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001999
Chris Lattner545dd342008-06-28 23:36:30 +00002000 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00002001 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002002 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002003 }
Mike Stumpbf916502009-07-24 19:02:52 +00002004
Peter Collingbourne7a730022010-11-23 20:45:58 +00002005 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002006}
2007
2008void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2009 if (E->isTypeDependent() || E->isValueDependent()) {
2010 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00002011 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002012 return;
2013 }
2014
Sean Huntcf807c42010-08-18 23:23:40 +00002015 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002016 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002017 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2018 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2019 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00002020 return;
2021 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002022 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002023 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2024 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002025 return;
2026 }
2027
Sean Huntcf807c42010-08-18 23:23:40 +00002028 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2029}
2030
2031void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2032 // FIXME: Cache the number on the Attr object if non-dependent?
2033 // FIXME: Perform checking of type validity
2034 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2035 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002036}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002037
Mike Stumpbf916502009-07-24 19:02:52 +00002038/// HandleModeAttr - This attribute modifies the width of a decl with primitive
2039/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002040///
Mike Stumpbf916502009-07-24 19:02:52 +00002041/// Despite what would be logical, the mode attribute is a decl attribute, not a
2042/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2043/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002044static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002045 // This attribute isn't documented, but glibc uses it. It changes
2046 // the width of an int or unsigned int to the specified size.
2047
2048 // Check that there aren't any arguments
2049 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002050 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002051 return;
2052 }
2053
2054 IdentifierInfo *Name = Attr.getParameterName();
2055 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002056 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002057 return;
2058 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002059
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002060 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002061
2062 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002063 if (Str.startswith("__") && Str.endswith("__"))
2064 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002065
2066 unsigned DestWidth = 0;
2067 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002068 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002069 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002070 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002071 switch (Str[0]) {
2072 case 'Q': DestWidth = 8; break;
2073 case 'H': DestWidth = 16; break;
2074 case 'S': DestWidth = 32; break;
2075 case 'D': DestWidth = 64; break;
2076 case 'X': DestWidth = 96; break;
2077 case 'T': DestWidth = 128; break;
2078 }
2079 if (Str[1] == 'F') {
2080 IntegerMode = false;
2081 } else if (Str[1] == 'C') {
2082 IntegerMode = false;
2083 ComplexMode = true;
2084 } else if (Str[1] != 'I') {
2085 DestWidth = 0;
2086 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002087 break;
2088 case 4:
2089 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2090 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002091 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002092 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002093 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002094 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002095 break;
2096 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002097 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002098 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002099 break;
2100 }
2101
2102 QualType OldTy;
2103 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
2104 OldTy = TD->getUnderlyingType();
2105 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2106 OldTy = VD->getType();
2107 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002108 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2109 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00002110 return;
2111 }
Eli Friedman73397492009-03-03 06:41:03 +00002112
John McCall183700f2009-09-21 23:43:11 +00002113 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002114 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2115 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002116 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002117 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2118 } else if (ComplexMode) {
2119 if (!OldTy->isComplexType())
2120 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2121 } else {
2122 if (!OldTy->isFloatingType())
2123 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2124 }
2125
Mike Stump390b4cc2009-05-16 07:39:55 +00002126 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2127 // and friends, at least with glibc.
2128 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2129 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002130 // FIXME: Make sure floating-point mappings are accurate
2131 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002132 QualType NewTy;
2133 switch (DestWidth) {
2134 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002135 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002136 return;
2137 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002138 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002139 return;
2140 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002141 if (!IntegerMode) {
2142 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2143 return;
2144 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002145 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002146 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002147 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002148 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002149 break;
2150 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002151 if (!IntegerMode) {
2152 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2153 return;
2154 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002155 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002156 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002157 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002158 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002159 break;
2160 case 32:
2161 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002162 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002163 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002164 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002165 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002166 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002167 break;
2168 case 64:
2169 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002170 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002171 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002172 if (S.Context.Target.getLongWidth() == 64)
2173 NewTy = S.Context.LongTy;
2174 else
2175 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002176 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002177 if (S.Context.Target.getLongWidth() == 64)
2178 NewTy = S.Context.UnsignedLongTy;
2179 else
2180 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002181 break;
Eli Friedman73397492009-03-03 06:41:03 +00002182 case 96:
2183 NewTy = S.Context.LongDoubleTy;
2184 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002185 case 128:
2186 if (!IntegerMode) {
2187 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2188 return;
2189 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002190 if (OldTy->isSignedIntegerType())
2191 NewTy = S.Context.Int128Ty;
2192 else
2193 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002194 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002195 }
2196
Eli Friedman73397492009-03-03 06:41:03 +00002197 if (ComplexMode) {
2198 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002199 }
2200
2201 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00002202 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2203 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002204 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002205 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002206 cast<ValueDecl>(D)->setType(NewTy);
2207}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002208
Mike Stump1feade82009-08-26 22:31:08 +00002209static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00002210 // check the attribute arguments.
2211 if (Attr.getNumArgs() > 0) {
2212 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2213 return;
2214 }
Anders Carlssone896d982009-02-13 08:11:52 +00002215
Anders Carlsson5bab7882009-02-19 19:16:48 +00002216 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002217 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002218 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002219 return;
2220 }
Mike Stumpbf916502009-07-24 19:02:52 +00002221
Sean Huntcf807c42010-08-18 23:23:40 +00002222 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002223}
2224
Mike Stump1feade82009-08-26 22:31:08 +00002225static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002226 // check the attribute arguments.
2227 if (Attr.getNumArgs() != 0) {
2228 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2229 return;
2230 }
Mike Stumpbf916502009-07-24 19:02:52 +00002231
Chris Lattnerc5197432009-04-14 17:02:11 +00002232 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002233 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002234 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002235 return;
2236 }
Mike Stumpbf916502009-07-24 19:02:52 +00002237
Sean Huntcf807c42010-08-18 23:23:40 +00002238 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002239}
2240
Chris Lattner7255a2d2010-06-22 00:03:40 +00002241static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2242 Sema &S) {
2243 // check the attribute arguments.
2244 if (Attr.getNumArgs() != 0) {
2245 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2246 return;
2247 }
2248
2249 if (!isa<FunctionDecl>(d)) {
2250 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002251 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002252 return;
2253 }
2254
Eric Christopherf48f3672010-12-01 22:13:54 +00002255 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2256 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002257}
2258
Peter Collingbourneced76712010-12-01 03:15:31 +00002259static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2260 if (S.LangOpts.CUDA) {
2261 // check the attribute arguments.
2262 if (Attr.getNumArgs() != 0) {
2263 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2264 return;
2265 }
2266
2267 if (!isa<VarDecl>(d)) {
2268 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002269 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002270 return;
2271 }
2272
2273 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2274 } else {
2275 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2276 }
2277}
2278
2279static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2280 if (S.LangOpts.CUDA) {
2281 // check the attribute arguments.
2282 if (Attr.getNumArgs() != 0) {
2283 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2284 return;
2285 }
2286
2287 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2288 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002289 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002290 return;
2291 }
2292
2293 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2294 } else {
2295 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2296 }
2297}
2298
2299static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2300 if (S.LangOpts.CUDA) {
2301 // check the attribute arguments.
2302 if (Attr.getNumArgs() != 0) {
2303 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2304 return;
2305 }
2306
2307 if (!isa<FunctionDecl>(d)) {
2308 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002309 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002310 return;
2311 }
2312
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002313 FunctionDecl *FD = cast<FunctionDecl>(d);
2314 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002315 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002316 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2317 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2318 << FD->getType()
2319 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2320 "void");
2321 } else {
2322 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2323 << FD->getType();
2324 }
2325 return;
2326 }
2327
Peter Collingbourneced76712010-12-01 03:15:31 +00002328 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2329 } else {
2330 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2331 }
2332}
2333
2334static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2335 if (S.LangOpts.CUDA) {
2336 // check the attribute arguments.
2337 if (Attr.getNumArgs() != 0) {
2338 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2339 return;
2340 }
2341
2342 if (!isa<FunctionDecl>(d)) {
2343 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002344 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002345 return;
2346 }
2347
2348 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2349 } else {
2350 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2351 }
2352}
2353
2354static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2355 if (S.LangOpts.CUDA) {
2356 // check the attribute arguments.
2357 if (Attr.getNumArgs() != 0) {
2358 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2359 return;
2360 }
2361
2362 if (!isa<VarDecl>(d)) {
2363 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002364 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002365 return;
2366 }
2367
2368 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2369 } else {
2370 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2371 }
2372}
2373
Chris Lattnercf2a7212009-04-20 19:12:28 +00002374static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002375 // check the attribute arguments.
2376 if (Attr.getNumArgs() != 0) {
2377 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2378 return;
2379 }
Mike Stumpbf916502009-07-24 19:02:52 +00002380
Chris Lattnerc5197432009-04-14 17:02:11 +00002381 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2382 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002383 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002384 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002385 return;
2386 }
Mike Stumpbf916502009-07-24 19:02:52 +00002387
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002388 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002389 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002390 return;
2391 }
Mike Stumpbf916502009-07-24 19:02:52 +00002392
Sean Huntcf807c42010-08-18 23:23:40 +00002393 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002394}
2395
John McCall711c52b2011-01-05 12:14:39 +00002396static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) {
2397 if (hasDeclarator(d)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002398
John McCall711c52b2011-01-05 12:14:39 +00002399 // Diagnostic is emitted elsewhere: here we store the (valid) attr
2400 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2401 CallingConv CC;
2402 if (S.CheckCallingConvAttr(attr, CC))
2403 return;
2404
2405 if (!isa<ObjCMethodDecl>(d)) {
2406 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002407 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00002408 return;
2409 }
2410
2411 switch (attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002412 case AttributeList::AT_fastcall:
John McCall711c52b2011-01-05 12:14:39 +00002413 d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002414 return;
2415 case AttributeList::AT_stdcall:
John McCall711c52b2011-01-05 12:14:39 +00002416 d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002417 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002418 case AttributeList::AT_thiscall:
John McCall711c52b2011-01-05 12:14:39 +00002419 d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002420 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002421 case AttributeList::AT_cdecl:
John McCall711c52b2011-01-05 12:14:39 +00002422 d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002423 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002424 case AttributeList::AT_pascal:
John McCall711c52b2011-01-05 12:14:39 +00002425 d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002426 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002427 default:
2428 llvm_unreachable("unexpected attribute kind");
2429 return;
2430 }
2431}
2432
Peter Collingbournef315fa82011-02-14 01:42:53 +00002433static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){
2434 assert(Attr.isInvalid() == false);
2435 d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
2436}
2437
John McCall711c52b2011-01-05 12:14:39 +00002438bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2439 if (attr.isInvalid())
2440 return true;
2441
2442 if (attr.getNumArgs() != 0) {
2443 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2444 attr.setInvalid();
2445 return true;
2446 }
2447
2448 // TODO: diagnose uses of these conventions on the wrong target.
2449 switch (attr.getKind()) {
2450 case AttributeList::AT_cdecl: CC = CC_C; break;
2451 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2452 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2453 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2454 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
2455 default: llvm_unreachable("unexpected attribute kind"); return true;
2456 }
2457
2458 return false;
2459}
2460
2461static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) {
2462 if (hasDeclarator(d)) return;
2463
2464 unsigned numParams;
2465 if (S.CheckRegparmAttr(attr, numParams))
2466 return;
2467
2468 if (!isa<ObjCMethodDecl>(d)) {
2469 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002470 << attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002471 return;
2472 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002473
John McCall711c52b2011-01-05 12:14:39 +00002474 d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams));
2475}
2476
2477/// Checks a regparm attribute, returning true if it is ill-formed and
2478/// otherwise setting numParams to the appropriate value.
2479bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) {
2480 if (attr.isInvalid())
2481 return true;
2482
2483 if (attr.getNumArgs() != 1) {
2484 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2485 attr.setInvalid();
2486 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002487 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002488
John McCall711c52b2011-01-05 12:14:39 +00002489 Expr *NumParamsExpr = attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002490 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002491 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00002492 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
2493 Diag(attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002494 << "regparm" << NumParamsExpr->getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +00002495 attr.setInvalid();
2496 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002497 }
2498
John McCall711c52b2011-01-05 12:14:39 +00002499 if (Context.Target.getRegParmMax() == 0) {
2500 Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002501 << NumParamsExpr->getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +00002502 attr.setInvalid();
2503 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002504 }
2505
John McCall711c52b2011-01-05 12:14:39 +00002506 numParams = NumParams.getZExtValue();
2507 if (numParams > Context.Target.getRegParmMax()) {
2508 Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2509 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
2510 attr.setInvalid();
2511 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002512 }
2513
John McCall711c52b2011-01-05 12:14:39 +00002514 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002515}
2516
Peter Collingbourne7b381982010-12-12 23:03:07 +00002517static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){
2518 if (S.LangOpts.CUDA) {
2519 // check the attribute arguments.
2520 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002521 // FIXME: 0 is not okay.
2522 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002523 return;
2524 }
2525
2526 if (!isFunctionOrMethod(d)) {
2527 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002528 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002529 return;
2530 }
2531
2532 Expr *MaxThreadsExpr = Attr.getArg(0);
2533 llvm::APSInt MaxThreads(32);
2534 if (MaxThreadsExpr->isTypeDependent() ||
2535 MaxThreadsExpr->isValueDependent() ||
2536 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2537 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2538 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2539 return;
2540 }
2541
2542 llvm::APSInt MinBlocks(32);
2543 if (Attr.getNumArgs() > 1) {
2544 Expr *MinBlocksExpr = Attr.getArg(1);
2545 if (MinBlocksExpr->isTypeDependent() ||
2546 MinBlocksExpr->isValueDependent() ||
2547 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2548 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2549 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2550 return;
2551 }
2552 }
2553
2554 d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
2555 MaxThreads.getZExtValue(),
2556 MinBlocks.getZExtValue()));
2557 } else {
2558 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2559 }
2560}
2561
Chris Lattner0744e5f2008-06-29 00:23:49 +00002562//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002563// Checker-specific attribute handlers.
2564//===----------------------------------------------------------------------===//
2565
John McCallc7ad3812011-01-25 03:31:58 +00002566static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2567 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2568}
2569static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2570 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2571}
2572
2573static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) {
2574 ParmVarDecl *param = dyn_cast<ParmVarDecl>(d);
2575 if (!param) {
2576 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002577 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00002578 return;
2579 }
2580
2581 bool typeOK, cf;
2582 if (attr.getKind() == AttributeList::AT_ns_consumed) {
2583 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2584 cf = false;
2585 } else {
2586 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2587 cf = true;
2588 }
2589
2590 if (!typeOK) {
2591 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2592 << SourceRange(attr.getLoc()) << attr.getName() << cf;
2593 return;
2594 }
2595
2596 if (cf)
2597 param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context));
2598 else
2599 param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context));
2600}
2601
2602static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr,
2603 Sema &S) {
2604 if (!isa<ObjCMethodDecl>(d)) {
2605 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002606 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00002607 return;
2608 }
2609
2610 d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context));
2611}
2612
2613static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr,
Ted Kremenekb71368d2009-05-09 02:44:38 +00002614 Sema &S) {
2615
John McCallc7ad3812011-01-25 03:31:58 +00002616 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00002617
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002618 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
John McCallc7ad3812011-01-25 03:31:58 +00002619 returnType = MD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002620 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
John McCallc7ad3812011-01-25 03:31:58 +00002621 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002622 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002623 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCallc7ad3812011-01-25 03:31:58 +00002624 << SourceRange(attr.getLoc()) << attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00002625 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002626 return;
2627 }
Mike Stumpbf916502009-07-24 19:02:52 +00002628
John McCallc7ad3812011-01-25 03:31:58 +00002629 bool typeOK;
2630 bool cf;
2631 switch (attr.getKind()) {
2632 default: llvm_unreachable("invalid ownership attribute"); return;
2633 case AttributeList::AT_ns_returns_autoreleased:
2634 case AttributeList::AT_ns_returns_retained:
2635 case AttributeList::AT_ns_returns_not_retained:
2636 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2637 cf = false;
2638 break;
2639
2640 case AttributeList::AT_cf_returns_retained:
2641 case AttributeList::AT_cf_returns_not_retained:
2642 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2643 cf = true;
2644 break;
2645 }
2646
2647 if (!typeOK) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002648 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallc7ad3812011-01-25 03:31:58 +00002649 << SourceRange(attr.getLoc())
2650 << attr.getName() << isa<ObjCMethodDecl>(d) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00002651 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002652 }
Mike Stumpbf916502009-07-24 19:02:52 +00002653
John McCallc7ad3812011-01-25 03:31:58 +00002654 switch (attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00002655 default:
2656 assert(0 && "invalid ownership attribute");
2657 return;
John McCallc7ad3812011-01-25 03:31:58 +00002658 case AttributeList::AT_ns_returns_autoreleased:
2659 d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(),
2660 S.Context));
2661 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002662 case AttributeList::AT_cf_returns_not_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002663 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002664 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002665 return;
2666 case AttributeList::AT_ns_returns_not_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002667 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002668 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002669 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002670 case AttributeList::AT_cf_returns_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002671 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002672 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002673 return;
2674 case AttributeList::AT_ns_returns_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002675 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002676 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002677 return;
2678 };
2679}
2680
Charles Davisf0122fe2010-02-16 18:27:26 +00002681static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2682 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00002683 Attr.getKind() == AttributeList::AT_dllexport ||
2684 Attr.getKind() == AttributeList::AT_uuid;
2685}
2686
2687//===----------------------------------------------------------------------===//
2688// Microsoft specific attribute handlers.
2689//===----------------------------------------------------------------------===//
2690
2691static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2692 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2693 // check the attribute arguments.
2694 if (Attr.getNumArgs() != 1) {
2695 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2696 return;
2697 }
2698 Expr *Arg = Attr.getArg(0);
2699 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichetd3d3be92010-12-20 01:41:49 +00002700 if (Str == 0 || Str->isWide()) {
2701 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2702 << "uuid" << 1;
2703 return;
2704 }
2705
2706 llvm::StringRef StrRef = Str->getString();
2707
2708 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2709 StrRef.back() == '}';
2710
2711 // Validate GUID length.
2712 if (IsCurly && StrRef.size() != 38) {
2713 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2714 return;
2715 }
2716 if (!IsCurly && StrRef.size() != 36) {
2717 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2718 return;
2719 }
2720
2721 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2722 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Anders Carlssonf89e0422011-01-23 21:07:30 +00002723 llvm::StringRef::iterator I = StrRef.begin();
2724 if (IsCurly) // Skip the optional '{'
2725 ++I;
2726
2727 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00002728 if (i == 8 || i == 13 || i == 18 || i == 23) {
2729 if (*I != '-') {
2730 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2731 return;
2732 }
2733 } else if (!isxdigit(*I)) {
2734 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2735 return;
2736 }
2737 I++;
2738 }
Francois Pichet11542142010-12-19 06:50:37 +00002739
2740 d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
2741 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00002742 } else
Francois Pichet11542142010-12-19 06:50:37 +00002743 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00002744}
2745
Ted Kremenekb71368d2009-05-09 02:44:38 +00002746//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002747// Top Level Sema Entry Points
2748//===----------------------------------------------------------------------===//
2749
Peter Collingbourne60700392011-01-21 02:08:45 +00002750static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D,
2751 const AttributeList &Attr, Sema &S) {
2752 switch (Attr.getKind()) {
2753 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
2754 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
2755 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
2756 default:
2757 break;
2758 }
2759}
Abramo Bagnarae215f722010-04-30 13:10:51 +00002760
Peter Collingbourne60700392011-01-21 02:08:45 +00002761static void ProcessInheritableDeclAttr(Scope *scope, Decl *D,
2762 const AttributeList &Attr, Sema &S) {
Chris Lattner803d0802008-06-29 00:43:07 +00002763 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002764 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002765 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2766 case AttributeList::AT_IBOutletCollection:
2767 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002768 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002769 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002770 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002771 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00002772 case AttributeList::AT_neon_vector_type:
2773 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00002774 // Ignore these, these are type attributes, handled by
2775 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002776 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00002777 case AttributeList::AT_device:
2778 case AttributeList::AT_host:
2779 case AttributeList::AT_overloadable:
2780 // Ignore, this is a non-inheritable attribute, handled
2781 // by ProcessNonInheritableDeclAttr.
2782 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002783 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2784 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002785 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002786 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002787 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002788 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002789 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002790 case AttributeList::AT_availability:HandleAvailabilityAttr(D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002791 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002792 HandleDependencyAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002793 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002794 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002795 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2796 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2797 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002798 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002799 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002800 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002801 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2802 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002803 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002804 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002805 case AttributeList::AT_launch_bounds:
2806 HandleLaunchBoundsAttr(D, Attr, S);
2807 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002808 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2809 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohman34c26302010-11-17 00:03:07 +00002810 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002811 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002812 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002813 case AttributeList::AT_ownership_returns:
2814 case AttributeList::AT_ownership_takes:
2815 case AttributeList::AT_ownership_holds:
2816 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002817 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002818 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2819 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002820 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002821 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002822
2823 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00002824 case AttributeList::AT_cf_consumed:
2825 case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break;
2826 case AttributeList::AT_ns_consumes_self:
2827 HandleNSConsumesSelfAttr(D, Attr, S); break;
2828
2829 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00002830 case AttributeList::AT_ns_returns_not_retained:
2831 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002832 case AttributeList::AT_ns_returns_retained:
2833 case AttributeList::AT_cf_returns_retained:
2834 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2835
Nate Begeman6f3d8382009-06-26 06:32:41 +00002836 case AttributeList::AT_reqd_wg_size:
2837 HandleReqdWorkGroupSize(D, Attr, S); break;
2838
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002839 case AttributeList::AT_init_priority:
2840 HandleInitPriorityAttr(D, Attr, S); break;
2841
Sean Hunt7725e672009-11-25 04:20:27 +00002842 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2843 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002844 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2845 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2846 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002847 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002848 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2849 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002850 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002851 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002852 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002853 case AttributeList::AT_transparent_union:
2854 HandleTransparentUnionAttr(D, Attr, S);
2855 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002856 case AttributeList::AT_objc_exception:
2857 HandleObjCExceptionAttr(D, Attr, S);
2858 break;
John McCalld5313b02011-03-02 11:33:24 +00002859 case AttributeList::AT_objc_method_family:
2860 HandleObjCMethodFamilyAttr(D, Attr, S);
2861 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002862 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2863 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2864 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2865 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2866 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2867 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2868 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2869 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2870 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002871 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002872 // Just ignore
2873 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002874 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2875 HandleNoInstrumentFunctionAttr(D, Attr, S);
2876 break;
John McCall04a67a62010-02-05 21:31:56 +00002877 case AttributeList::AT_stdcall:
2878 case AttributeList::AT_cdecl:
2879 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002880 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002881 case AttributeList::AT_pascal:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002882 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002883 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00002884 case AttributeList::AT_opencl_kernel_function:
2885 HandleOpenCLKernelAttr(D, Attr, S);
2886 break;
Francois Pichet11542142010-12-19 06:50:37 +00002887 case AttributeList::AT_uuid:
2888 HandleUuidAttr(D, Attr, S);
2889 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002890 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002891 // Ask target about the attribute.
2892 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2893 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002894 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2895 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002896 break;
2897 }
2898}
2899
Peter Collingbourne60700392011-01-21 02:08:45 +00002900/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
2901/// the attribute applies to decls. If the attribute is a type attribute, just
2902/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2903/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
2904static void ProcessDeclAttribute(Scope *scope, Decl *D,
2905 const AttributeList &Attr, Sema &S,
2906 bool NonInheritable, bool Inheritable) {
2907 if (Attr.isInvalid())
2908 return;
2909
2910 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2911 // FIXME: Try to deal with other __declspec attributes!
2912 return;
2913
2914 if (NonInheritable)
2915 ProcessNonInheritableDeclAttr(scope, D, Attr, S);
2916
2917 if (Inheritable)
2918 ProcessInheritableDeclAttr(scope, D, Attr, S);
2919}
2920
Chris Lattner803d0802008-06-29 00:43:07 +00002921/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2922/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00002923void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00002924 const AttributeList *AttrList,
2925 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002926 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Peter Collingbourne60700392011-01-21 02:08:45 +00002927 ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002928 }
2929
2930 // GCC accepts
2931 // static int a9 __attribute__((weakref));
2932 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00002933 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002934 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002935 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002936 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002937 }
2938}
2939
Ryan Flynne25ff832009-07-30 03:15:39 +00002940/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2941/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002942NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002943 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002944 NamedDecl *NewD = 0;
2945 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2946 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002947 FD->getInnerLocStart(),
Ryan Flynne25ff832009-07-30 03:15:39 +00002948 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002949 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002950 if (FD->getQualifier()) {
2951 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002952 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00002953 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002954 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2955 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002956 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002957 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002958 VD->getStorageClass(),
2959 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002960 if (VD->getQualifier()) {
2961 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002962 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00002963 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002964 }
2965 return NewD;
2966}
2967
2968/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2969/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002970void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002971 if (W.getUsed()) return; // only do this once
2972 W.setUsed(true);
2973 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2974 IdentifierInfo *NDId = ND->getIdentifier();
2975 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002976 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2977 NDId->getName()));
2978 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002979 WeakTopLevelDecl.push_back(NewD);
2980 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2981 // to insert Decl at TU scope, sorry.
2982 DeclContext *SavedContext = CurContext;
2983 CurContext = Context.getTranslationUnitDecl();
2984 PushOnScopeChains(NewD, S);
2985 CurContext = SavedContext;
2986 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002987 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002988 }
2989}
2990
Chris Lattner0744e5f2008-06-29 00:23:49 +00002991/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2992/// it, apply them to D. This is a bit tricky because PD can have attributes
2993/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00002994void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
2995 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00002996 // It's valid to "forward-declare" #pragma weak, in which case we
2997 // have to do this.
Peter Collingbourne60700392011-01-21 02:08:45 +00002998 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCalld4aff0e2010-10-27 00:59:00 +00002999 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3000 if (IdentifierInfo *Id = ND->getIdentifier()) {
3001 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3002 = WeakUndeclaredIdentifiers.find(Id);
3003 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3004 WeakInfo W = I->second;
3005 DeclApplyPragmaWeak(S, ND, W);
3006 WeakUndeclaredIdentifiers[Id] = W;
3007 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003008 }
3009 }
3010 }
3011
Chris Lattner0744e5f2008-06-29 00:23:49 +00003012 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003013 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003014 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003015
Chris Lattner0744e5f2008-06-29 00:23:49 +00003016 // Walk the declarator structure, applying decl attributes that were in a type
3017 // position to the decl itself. This handles cases like:
3018 // int *__attr__(x)** D;
3019 // when X is a decl attribute.
3020 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3021 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003022 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003023
Chris Lattner0744e5f2008-06-29 00:23:49 +00003024 // Finally, apply any attributes on the decl itself.
3025 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003026 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003027}
John McCall54abf7d2009-11-04 02:18:39 +00003028
John McCalleee1d542011-02-14 07:13:47 +00003029// This duplicates a vector push_back but hides the need to know the
3030// size of the type.
3031void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3032 assert(StackSize <= StackCapacity);
3033
3034 // Grow the stack if necessary.
3035 if (StackSize == StackCapacity) {
3036 unsigned newCapacity = 2 * StackCapacity + 2;
3037 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3038 const char *oldBuffer = (const char*) Stack;
3039
3040 if (StackCapacity)
3041 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3042
3043 delete[] oldBuffer;
3044 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3045 StackCapacity = newCapacity;
3046 }
3047
3048 assert(StackSize < StackCapacity);
3049 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00003050}
3051
John McCalleee1d542011-02-14 07:13:47 +00003052void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3053 Decl *decl) {
3054 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00003055
John McCalleee1d542011-02-14 07:13:47 +00003056 // Check the invariants.
3057 assert(DD.StackSize >= state.SavedStackSize);
3058 assert(state.SavedStackSize >= DD.ActiveStackBase);
3059 assert(DD.ParsingDepth > 0);
3060
3061 // Drop the parsing depth.
3062 DD.ParsingDepth--;
3063
3064 // If there are no active diagnostics, we're done.
3065 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00003066 return;
3067
John McCall2f514482010-01-27 03:50:35 +00003068 // We only want to actually emit delayed diagnostics when we
3069 // successfully parsed a decl.
John McCalleee1d542011-02-14 07:13:47 +00003070 if (decl) {
3071 // We emit all the active diagnostics, not just those starting
3072 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00003073 // decl spec and another for each declarator; in a decl group like:
3074 // deprecated_typedef foo, *bar, baz();
3075 // only the declarator pops will be passed decls. This is correct;
3076 // we really do need to consider delayed diagnostics from the decl spec
3077 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00003078 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3079 DelayedDiagnostic &diag = DD.Stack[i];
3080 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00003081 continue;
3082
John McCalleee1d542011-02-14 07:13:47 +00003083 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00003084 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00003085 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003086 break;
3087
3088 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00003089 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003090 break;
3091 }
3092 }
3093 }
3094
John McCall58e6f342010-03-16 05:22:47 +00003095 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00003096 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
3097 DD.Stack[i].destroy();
John McCall58e6f342010-03-16 05:22:47 +00003098
John McCalleee1d542011-02-14 07:13:47 +00003099 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00003100}
3101
3102static bool isDeclDeprecated(Decl *D) {
3103 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003104 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00003105 return true;
3106 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3107 return false;
3108}
3109
John McCall9c3087b2010-08-26 02:13:20 +00003110void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00003111 Decl *Ctx) {
3112 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00003113 return;
3114
John McCall2f514482010-01-27 03:50:35 +00003115 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003116 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003117 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003118 << DD.getDeprecationDecl()->getDeclName()
3119 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003120 else
3121 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003122 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003123}
3124
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003125void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003126 SourceLocation Loc,
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003127 bool UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003128 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00003129 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3130 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00003131 return;
3132 }
3133
3134 // Otherwise, don't warn if our current context is deprecated.
3135 if (isDeclDeprecated(cast<Decl>(CurContext)))
3136 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003137 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003138 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3139 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003140 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003141 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003142 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
3143 else
3144 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
3145 }
John McCall54abf7d2009-11-04 02:18:39 +00003146}