blob: 6d1a4c86f282e61276368c4cbe67cc40fa2c4fee [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();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001094 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001095
1096 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1097 // of these steps are needed).
1098 if (Introduced.isValid() && Deprecated.isValid() &&
1099 !(Introduced.Version < Deprecated.Version)) {
1100 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1101 << 1 << PlatformName << Deprecated.Version.getAsString()
1102 << 0 << Introduced.Version.getAsString();
1103 return;
1104 }
1105
1106 if (Introduced.isValid() && Obsoleted.isValid() &&
1107 !(Introduced.Version < Obsoleted.Version)) {
1108 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1109 << 2 << PlatformName << Obsoleted.Version.getAsString()
1110 << 0 << Introduced.Version.getAsString();
1111 return;
1112 }
1113
1114 if (Deprecated.isValid() && Obsoleted.isValid() &&
1115 !(Deprecated.Version < Obsoleted.Version)) {
1116 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1117 << 2 << PlatformName << Obsoleted.Version.getAsString()
1118 << 1 << Deprecated.Version.getAsString();
1119 return;
1120 }
1121
1122 d->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
1123 Platform,
1124 Introduced.Version,
1125 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001126 Obsoleted.Version,
1127 IsUnavailable));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001128}
1129
Chris Lattner803d0802008-06-29 00:43:07 +00001130static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001131 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001132 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001133 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001134 return;
1135 }
Mike Stumpbf916502009-07-24 19:02:52 +00001136
Peter Collingbourne7a730022010-11-23 20:45:58 +00001137 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001138 Arg = Arg->IgnoreParenCasts();
1139 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001140
Chris Lattner6b6b5372008-06-26 18:38:35 +00001141 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001142 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001143 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001144 return;
1145 }
Mike Stumpbf916502009-07-24 19:02:52 +00001146
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001147 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001148 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001149
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001150 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001151 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001152 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001153 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001154 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001155 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001156 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001157 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001158 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001159 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001160 return;
1161 }
Mike Stumpbf916502009-07-24 19:02:52 +00001162
Sean Huntcf807c42010-08-18 23:23:40 +00001163 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001164}
1165
John McCalld5313b02011-03-02 11:33:24 +00001166static void HandleObjCMethodFamilyAttr(Decl *decl, const AttributeList &attr,
1167 Sema &S) {
1168 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1169 if (!method) {
1170 S.Diag(attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001171 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001172 return;
1173 }
1174
1175 if (attr.getNumArgs() != 0 || !attr.getParameterName()) {
1176 if (!attr.getParameterName() && attr.getNumArgs() == 1) {
1177 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
1178 << "objc_method_family" << 1;
1179 } else {
1180 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1181 }
1182 attr.setInvalid();
1183 return;
1184 }
1185
1186 llvm::StringRef param = attr.getParameterName()->getName();
1187 ObjCMethodFamilyAttr::FamilyKind family;
1188 if (param == "none")
1189 family = ObjCMethodFamilyAttr::OMF_None;
1190 else if (param == "alloc")
1191 family = ObjCMethodFamilyAttr::OMF_alloc;
1192 else if (param == "copy")
1193 family = ObjCMethodFamilyAttr::OMF_copy;
1194 else if (param == "init")
1195 family = ObjCMethodFamilyAttr::OMF_init;
1196 else if (param == "mutableCopy")
1197 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1198 else if (param == "new")
1199 family = ObjCMethodFamilyAttr::OMF_new;
1200 else {
1201 // Just warn and ignore it. This is future-proof against new
1202 // families being used in system headers.
1203 S.Diag(attr.getParameterLoc(), diag::warn_unknown_method_family);
1204 return;
1205 }
1206
1207 decl->addAttr(new (S.Context) ObjCMethodFamilyAttr(attr.getLoc(),
1208 S.Context, family));
1209}
1210
Chris Lattner0db29ec2009-02-14 08:09:34 +00001211static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1212 Sema &S) {
1213 if (Attr.getNumArgs() != 0) {
1214 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1215 return;
1216 }
Mike Stumpbf916502009-07-24 19:02:52 +00001217
Chris Lattner0db29ec2009-02-14 08:09:34 +00001218 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1219 if (OCI == 0) {
1220 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1221 return;
1222 }
Mike Stumpbf916502009-07-24 19:02:52 +00001223
Sean Huntcf807c42010-08-18 23:23:40 +00001224 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001225}
1226
1227static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001228 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001229 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001230 return;
1231 }
Chris Lattner0db29ec2009-02-14 08:09:34 +00001232 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001233 QualType T = TD->getUnderlyingType();
1234 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001235 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001236 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1237 return;
1238 }
1239 }
Sean Huntcf807c42010-08-18 23:23:40 +00001240 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001241}
1242
Mike Stumpbf916502009-07-24 19:02:52 +00001243static void
Douglas Gregorf9201e02009-02-11 23:02:49 +00001244HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1245 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001246 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001247 return;
1248 }
1249
1250 if (!isa<FunctionDecl>(D)) {
1251 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1252 return;
1253 }
1254
Sean Huntcf807c42010-08-18 23:23:40 +00001255 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001256}
1257
Steve Naroff9eae5762008-09-18 16:44:58 +00001258static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001259 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001260 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001261 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001262 return;
1263 }
Mike Stumpbf916502009-07-24 19:02:52 +00001264
Steve Naroff9eae5762008-09-18 16:44:58 +00001265 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001266 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001267 return;
1268 }
Mike Stumpbf916502009-07-24 19:02:52 +00001269
Sean Huntcf807c42010-08-18 23:23:40 +00001270 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001271 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001272 type = BlocksAttr::ByRef;
1273 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001274 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001275 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001276 return;
1277 }
Mike Stumpbf916502009-07-24 19:02:52 +00001278
Sean Huntcf807c42010-08-18 23:23:40 +00001279 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001280}
1281
Anders Carlsson77091822008-10-05 18:05:59 +00001282static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1283 // check the attribute arguments.
1284 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001285 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001286 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001287 }
1288
Anders Carlsson77091822008-10-05 18:05:59 +00001289 int sentinel = 0;
1290 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001291 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001292 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001293 if (E->isTypeDependent() || E->isValueDependent() ||
1294 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001295 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001296 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001297 return;
1298 }
1299 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001300
Anders Carlsson77091822008-10-05 18:05:59 +00001301 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001302 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1303 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001304 return;
1305 }
1306 }
1307
1308 int nullPos = 0;
1309 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001310 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001311 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001312 if (E->isTypeDependent() || E->isValueDependent() ||
1313 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001314 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001315 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001316 return;
1317 }
1318 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001319
Anders Carlsson77091822008-10-05 18:05:59 +00001320 if (nullPos > 1 || nullPos < 0) {
1321 // FIXME: This error message could be improved, it would be nice
1322 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001323 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1324 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001325 return;
1326 }
1327 }
1328
1329 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001330 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001331 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001332
Chris Lattner897cd902009-03-17 23:03:47 +00001333 if (isa<FunctionNoProtoType>(FT)) {
1334 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1335 return;
1336 }
Mike Stumpbf916502009-07-24 19:02:52 +00001337
Chris Lattner897cd902009-03-17 23:03:47 +00001338 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001339 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001340 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001341 }
Anders Carlsson77091822008-10-05 18:05:59 +00001342 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1343 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001344 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001345 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001346 }
1347 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001348 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1349 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001350 ;
1351 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001352 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001353 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001354 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherf48f3672010-12-01 22:13:54 +00001355 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001356 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001357 int m = Ty->isFunctionPointerType() ? 0 : 1;
1358 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001359 return;
1360 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001361 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001362 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001363 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001364 return;
1365 }
Anders Carlsson77091822008-10-05 18:05:59 +00001366 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001367 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001368 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001369 return;
1370 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001371 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1372 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001373}
1374
Chris Lattner026dc962009-02-14 07:37:35 +00001375static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1376 // check the attribute arguments.
1377 if (Attr.getNumArgs() != 0) {
1378 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1379 return;
1380 }
1381
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001382 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001383 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001384 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001385 return;
1386 }
Mike Stumpbf916502009-07-24 19:02:52 +00001387
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001388 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1389 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1390 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001391 return;
1392 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001393 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1394 if (MD->getResultType()->isVoidType()) {
1395 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1396 << Attr.getName() << 1;
1397 return;
1398 }
1399
Sean Huntcf807c42010-08-18 23:23:40 +00001400 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001401}
1402
John McCall332bb2a2011-02-08 22:35:49 +00001403static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001404 // check the attribute arguments.
John McCall332bb2a2011-02-08 22:35:49 +00001405 if (attr.getNumArgs() != 0) {
1406 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001407 return;
1408 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001409
John McCall332bb2a2011-02-08 22:35:49 +00001410 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
1411 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001412 << attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001413 return;
1414 }
1415
John McCall332bb2a2011-02-08 22:35:49 +00001416 NamedDecl *nd = cast<NamedDecl>(d);
1417
1418 // 'weak' only applies to declarations with external linkage.
1419 if (hasEffectivelyInternalLinkage(nd)) {
1420 S.Diag(attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001421 return;
1422 }
Mike Stumpbf916502009-07-24 19:02:52 +00001423
John McCall332bb2a2011-02-08 22:35:49 +00001424 nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001425}
1426
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001427static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1428 // check the attribute arguments.
1429 if (Attr.getNumArgs() != 0) {
1430 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1431 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001432 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001433
1434 // weak_import only applies to variable & function declarations.
1435 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001436 if (!D->canBeWeakImported(isDef)) {
1437 if (isDef)
1438 S.Diag(Attr.getLoc(),
1439 diag::warn_attribute_weak_import_invalid_on_definition)
1440 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00001441 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
1442 (S.Context.Target.getTriple().getOS() == llvm::Triple::Darwin &&
1443 isa<ObjCInterfaceDecl>(D))) {
1444 // Nothing to warn about here.
1445 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001446 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001447 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001448
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001449 return;
1450 }
1451
Sean Huntcf807c42010-08-18 23:23:40 +00001452 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001453}
1454
Nate Begeman6f3d8382009-06-26 06:32:41 +00001455static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1456 Sema &S) {
1457 // Attribute has 3 arguments.
1458 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001459 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001460 return;
1461 }
1462
1463 unsigned WGSize[3];
1464 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001465 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001466 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001467 if (E->isTypeDependent() || E->isValueDependent() ||
1468 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001469 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1470 << "reqd_work_group_size" << E->getSourceRange();
1471 return;
1472 }
1473 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1474 }
Sean Huntcf807c42010-08-18 23:23:40 +00001475 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1476 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001477 WGSize[2]));
1478}
1479
Chris Lattner026dc962009-02-14 07:37:35 +00001480static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001481 // Attribute has no arguments.
1482 if (Attr.getNumArgs() != 1) {
1483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1484 return;
1485 }
1486
1487 // Make sure that there is a string literal as the sections's single
1488 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001489 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001490 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001491 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001492 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001493 return;
1494 }
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Chris Lattner797c3c42009-08-10 19:03:04 +00001496 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001497 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001498 if (!Error.empty()) {
1499 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1500 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001501 return;
1502 }
Mike Stump1eb44332009-09-09 15:08:12 +00001503
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001504 // This attribute cannot be applied to local variables.
1505 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1506 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1507 return;
1508 }
1509
Eric Christopherf48f3672010-12-01 22:13:54 +00001510 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1511 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001512}
1513
Chris Lattner6b6b5372008-06-26 18:38:35 +00001514
Chris Lattner803d0802008-06-29 00:43:07 +00001515static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001516 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001517 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001518 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001519 return;
1520 }
Mike Stumpbf916502009-07-24 19:02:52 +00001521
Sean Huntcf807c42010-08-18 23:23:40 +00001522 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001523}
1524
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001525static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1526 // check the attribute arguments.
1527 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001528 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001529 return;
1530 }
Mike Stumpbf916502009-07-24 19:02:52 +00001531
Sean Huntcf807c42010-08-18 23:23:40 +00001532 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001533}
1534
1535static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1536 // check the attribute arguments.
1537 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001538 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001539 return;
1540 }
Mike Stumpbf916502009-07-24 19:02:52 +00001541
Sean Huntcf807c42010-08-18 23:23:40 +00001542 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001543}
1544
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001545static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001546 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001547 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1548 return;
1549 }
Mike Stumpbf916502009-07-24 19:02:52 +00001550
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001551 if (Attr.getNumArgs() != 0) {
1552 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1553 return;
1554 }
Mike Stumpbf916502009-07-24 19:02:52 +00001555
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001556 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001557
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001558 if (!VD || !VD->hasLocalStorage()) {
1559 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1560 return;
1561 }
Mike Stumpbf916502009-07-24 19:02:52 +00001562
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001563 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001564 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00001565 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001566 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1567 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001568 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001569 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001570 Attr.getParameterName();
1571 return;
1572 }
Mike Stumpbf916502009-07-24 19:02:52 +00001573
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001574 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1575 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001576 S.Diag(Attr.getParameterLoc(),
1577 diag::err_attribute_cleanup_arg_not_function)
1578 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001579 return;
1580 }
1581
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001582 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001583 S.Diag(Attr.getParameterLoc(),
1584 diag::err_attribute_cleanup_func_must_take_one_arg)
1585 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001586 return;
1587 }
Mike Stumpbf916502009-07-24 19:02:52 +00001588
Anders Carlsson89941c12009-02-07 23:16:50 +00001589 // We're currently more strict than GCC about what function types we accept.
1590 // If this ever proves to be a problem it should be easy to fix.
1591 QualType Ty = S.Context.getPointerType(VD->getType());
1592 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00001593 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1594 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001595 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001596 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1597 Attr.getParameterName() << ParamTy << Ty;
1598 return;
1599 }
Mike Stumpbf916502009-07-24 19:02:52 +00001600
Sean Huntcf807c42010-08-18 23:23:40 +00001601 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00001602 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001603}
1604
Mike Stumpbf916502009-07-24 19:02:52 +00001605/// Handle __attribute__((format_arg((idx)))) attribute based on
1606/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1607static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001608 if (Attr.getNumArgs() != 1) {
1609 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1610 return;
1611 }
1612 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1613 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001614 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001615 return;
1616 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001617
1618 // In C++ the implicit 'this' function parameter also counts, and they are
1619 // counted from one.
1620 bool HasImplicitThisParam = isInstanceMethod(d);
1621 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001622 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001623
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001624 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001625 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001626 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001627 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1628 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001629 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1630 << "format" << 2 << IdxExpr->getSourceRange();
1631 return;
1632 }
Mike Stumpbf916502009-07-24 19:02:52 +00001633
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001634 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1635 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1636 << "format" << 2 << IdxExpr->getSourceRange();
1637 return;
1638 }
Mike Stumpbf916502009-07-24 19:02:52 +00001639
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001640 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001641
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001642 if (HasImplicitThisParam) {
1643 if (ArgIdx == 0) {
1644 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1645 << "format_arg" << IdxExpr->getSourceRange();
1646 return;
1647 }
1648 ArgIdx--;
1649 }
1650
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001651 // make sure the format string is really a string
1652 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001653
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001654 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1655 if (not_nsstring_type &&
1656 !isCFStringType(Ty, S.Context) &&
1657 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001658 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001659 // FIXME: Should highlight the actual expression that has the wrong type.
1660 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001661 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001662 << IdxExpr->getSourceRange();
1663 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001664 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001665 Ty = getFunctionOrMethodResultType(d);
1666 if (!isNSStringType(Ty, S.Context) &&
1667 !isCFStringType(Ty, S.Context) &&
1668 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001669 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001670 // FIXME: Should highlight the actual expression that has the wrong type.
1671 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001672 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001673 << IdxExpr->getSourceRange();
1674 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001675 }
1676
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001677 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1678 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001679}
1680
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001681enum FormatAttrKind {
1682 CFStringFormat,
1683 NSStringFormat,
1684 StrftimeFormat,
1685 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001686 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001687 InvalidFormat
1688};
1689
1690/// getFormatAttrKind - Map from format attribute names to supported format
1691/// types.
1692static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1693 // Check for formats that get handled specially.
1694 if (Format == "NSString")
1695 return NSStringFormat;
1696 if (Format == "CFString")
1697 return CFStringFormat;
1698 if (Format == "strftime")
1699 return StrftimeFormat;
1700
1701 // Otherwise, check for supported formats.
1702 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1703 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1704 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00001705 Format == "zcmn_err" ||
1706 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001707 return SupportedFormat;
1708
Duncan Sandsbc525952010-03-23 14:44:19 +00001709 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1710 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001711 return IgnoredFormat;
1712
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001713 return InvalidFormat;
1714}
1715
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001716/// Handle __attribute__((init_priority(priority))) attributes based on
1717/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1718static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1719 Sema &S) {
1720 if (!S.getLangOptions().CPlusPlus) {
1721 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1722 return;
1723 }
1724
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001725 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1726 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1727 Attr.setInvalid();
1728 return;
1729 }
1730 QualType T = dyn_cast<VarDecl>(d)->getType();
1731 if (S.Context.getAsArrayType(T))
1732 T = S.Context.getBaseElementType(T);
1733 if (!T->getAs<RecordType>()) {
1734 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1735 Attr.setInvalid();
1736 return;
1737 }
1738
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001739 if (Attr.getNumArgs() != 1) {
1740 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1741 Attr.setInvalid();
1742 return;
1743 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001744 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001745
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001746 llvm::APSInt priority(32);
1747 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1748 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1749 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1750 << "init_priority" << priorityExpr->getSourceRange();
1751 Attr.setInvalid();
1752 return;
1753 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001754 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001755 if (prioritynum < 101 || prioritynum > 65535) {
1756 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1757 << priorityExpr->getSourceRange();
1758 Attr.setInvalid();
1759 return;
1760 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001761 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1762 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001763}
1764
Mike Stumpbf916502009-07-24 19:02:52 +00001765/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1766/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001767static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001768
Chris Lattner545dd342008-06-28 23:36:30 +00001769 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001770 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001771 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001772 return;
1773 }
1774
Chris Lattner545dd342008-06-28 23:36:30 +00001775 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001776 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001777 return;
1778 }
1779
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001780 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001781 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001782 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001783 return;
1784 }
1785
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001786 // In C++ the implicit 'this' function parameter also counts, and they are
1787 // counted from one.
1788 bool HasImplicitThisParam = isInstanceMethod(d);
1789 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001790 unsigned FirstIdx = 1;
1791
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001792 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001793
1794 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001795 if (Format.startswith("__") && Format.endswith("__"))
1796 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001797
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001798 // Check for supported formats.
1799 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001800
1801 if (Kind == IgnoredFormat)
1802 return;
1803
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001804 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001805 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001806 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001807 return;
1808 }
1809
1810 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001811 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00001812 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001813 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1814 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001815 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001816 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001817 return;
1818 }
1819
1820 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001821 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001822 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001823 return;
1824 }
1825
1826 // FIXME: Do we need to bounds check?
1827 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001828
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001829 if (HasImplicitThisParam) {
1830 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001831 S.Diag(Attr.getLoc(),
1832 diag::err_format_attribute_implicit_this_format_string)
1833 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001834 return;
1835 }
1836 ArgIdx--;
1837 }
Mike Stump1eb44332009-09-09 15:08:12 +00001838
Chris Lattner6b6b5372008-06-26 18:38:35 +00001839 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001840 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001841
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001842 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001843 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001844 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1845 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001846 return;
1847 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001848 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001849 // FIXME: do we need to check if the type is NSString*? What are the
1850 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001851 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001852 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001853 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1854 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001855 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001856 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001857 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001858 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001859 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001860 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1861 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001862 return;
1863 }
1864
1865 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001866 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00001867 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001868 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1869 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001870 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001871 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001872 return;
1873 }
1874
1875 // check if the function is variadic if the 3rd argument non-zero
1876 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001877 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001878 ++NumArgs; // +1 for ...
1879 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001880 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001881 return;
1882 }
1883 }
1884
Chris Lattner3c73c412008-11-19 08:23:25 +00001885 // strftime requires FirstArg to be 0 because it doesn't read from any
1886 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001887 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001888 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001889 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1890 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001891 return;
1892 }
1893 // if 0 it disables parameter checking (to use with e.g. va_list)
1894 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001895 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001896 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001897 return;
1898 }
1899
Sean Huntcf807c42010-08-18 23:23:40 +00001900 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1901 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001902 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001903}
1904
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001905static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1906 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001907 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001908 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001909 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001910 return;
1911 }
1912
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001913 // Try to find the underlying union declaration.
1914 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001915 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001916 if (TD && TD->getUnderlyingType()->isUnionType())
1917 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1918 else
1919 RD = dyn_cast<RecordDecl>(d);
1920
1921 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001922 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001923 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001924 return;
1925 }
1926
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001927 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001928 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001929 diag::warn_transparent_union_attribute_not_definition);
1930 return;
1931 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001932
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001933 RecordDecl::field_iterator Field = RD->field_begin(),
1934 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001935 if (Field == FieldEnd) {
1936 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1937 return;
1938 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001939
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001940 FieldDecl *FirstField = *Field;
1941 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001942 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001943 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001944 diag::warn_transparent_union_attribute_floating)
1945 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001946 return;
1947 }
1948
1949 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1950 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1951 for (; Field != FieldEnd; ++Field) {
1952 QualType FieldType = Field->getType();
1953 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1954 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1955 // Warn if we drop the attribute.
1956 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001957 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001958 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001959 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001960 diag::warn_transparent_union_attribute_field_size_align)
1961 << isSize << Field->getDeclName() << FieldBits;
1962 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001963 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001964 diag::note_transparent_union_first_field_size_align)
1965 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001966 return;
1967 }
1968 }
1969
Sean Huntcf807c42010-08-18 23:23:40 +00001970 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001971}
1972
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001973static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001974 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001975 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001976 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001977 return;
1978 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001979 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001980 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001981
Chris Lattner6b6b5372008-06-26 18:38:35 +00001982 // Make sure that there is a string literal as the annotation's single
1983 // argument.
1984 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001985 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001986 return;
1987 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001988 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1989 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001990}
1991
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001992static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001993 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001994 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001995 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001996 return;
1997 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001998
1999 //FIXME: The C++0x version of this attribute has more limited applicabilty
2000 // than GNU's, and should error out when it is used to specify a
2001 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002002
Chris Lattner545dd342008-06-28 23:36:30 +00002003 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00002004 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002005 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002006 }
Mike Stumpbf916502009-07-24 19:02:52 +00002007
Peter Collingbourne7a730022010-11-23 20:45:58 +00002008 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002009}
2010
2011void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2012 if (E->isTypeDependent() || E->isValueDependent()) {
2013 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00002014 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002015 return;
2016 }
2017
Sean Huntcf807c42010-08-18 23:23:40 +00002018 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002019 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002020 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2021 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2022 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00002023 return;
2024 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002025 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002026 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2027 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002028 return;
2029 }
2030
Sean Huntcf807c42010-08-18 23:23:40 +00002031 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2032}
2033
2034void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2035 // FIXME: Cache the number on the Attr object if non-dependent?
2036 // FIXME: Perform checking of type validity
2037 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2038 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002039}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002040
Mike Stumpbf916502009-07-24 19:02:52 +00002041/// HandleModeAttr - This attribute modifies the width of a decl with primitive
2042/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002043///
Mike Stumpbf916502009-07-24 19:02:52 +00002044/// Despite what would be logical, the mode attribute is a decl attribute, not a
2045/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2046/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002047static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002048 // This attribute isn't documented, but glibc uses it. It changes
2049 // the width of an int or unsigned int to the specified size.
2050
2051 // Check that there aren't any arguments
2052 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002053 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002054 return;
2055 }
2056
2057 IdentifierInfo *Name = Attr.getParameterName();
2058 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002059 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002060 return;
2061 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002062
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002063 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002064
2065 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002066 if (Str.startswith("__") && Str.endswith("__"))
2067 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002068
2069 unsigned DestWidth = 0;
2070 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002071 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002072 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002073 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002074 switch (Str[0]) {
2075 case 'Q': DestWidth = 8; break;
2076 case 'H': DestWidth = 16; break;
2077 case 'S': DestWidth = 32; break;
2078 case 'D': DestWidth = 64; break;
2079 case 'X': DestWidth = 96; break;
2080 case 'T': DestWidth = 128; break;
2081 }
2082 if (Str[1] == 'F') {
2083 IntegerMode = false;
2084 } else if (Str[1] == 'C') {
2085 IntegerMode = false;
2086 ComplexMode = true;
2087 } else if (Str[1] != 'I') {
2088 DestWidth = 0;
2089 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002090 break;
2091 case 4:
2092 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2093 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002094 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002095 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002096 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002097 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002098 break;
2099 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002100 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002101 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002102 break;
2103 }
2104
2105 QualType OldTy;
2106 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
2107 OldTy = TD->getUnderlyingType();
2108 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2109 OldTy = VD->getType();
2110 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002111 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2112 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00002113 return;
2114 }
Eli Friedman73397492009-03-03 06:41:03 +00002115
John McCall183700f2009-09-21 23:43:11 +00002116 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002117 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2118 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002119 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002120 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2121 } else if (ComplexMode) {
2122 if (!OldTy->isComplexType())
2123 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2124 } else {
2125 if (!OldTy->isFloatingType())
2126 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2127 }
2128
Mike Stump390b4cc2009-05-16 07:39:55 +00002129 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2130 // and friends, at least with glibc.
2131 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2132 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002133 // FIXME: Make sure floating-point mappings are accurate
2134 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002135 QualType NewTy;
2136 switch (DestWidth) {
2137 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002138 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002139 return;
2140 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002141 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002142 return;
2143 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002144 if (!IntegerMode) {
2145 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2146 return;
2147 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002148 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002149 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002150 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002151 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002152 break;
2153 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002154 if (!IntegerMode) {
2155 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2156 return;
2157 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002158 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002159 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002160 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002161 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002162 break;
2163 case 32:
2164 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002165 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002166 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002167 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002168 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002169 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002170 break;
2171 case 64:
2172 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002173 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002174 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002175 if (S.Context.Target.getLongWidth() == 64)
2176 NewTy = S.Context.LongTy;
2177 else
2178 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002179 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002180 if (S.Context.Target.getLongWidth() == 64)
2181 NewTy = S.Context.UnsignedLongTy;
2182 else
2183 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002184 break;
Eli Friedman73397492009-03-03 06:41:03 +00002185 case 96:
2186 NewTy = S.Context.LongDoubleTy;
2187 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002188 case 128:
2189 if (!IntegerMode) {
2190 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2191 return;
2192 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002193 if (OldTy->isSignedIntegerType())
2194 NewTy = S.Context.Int128Ty;
2195 else
2196 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002197 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002198 }
2199
Eli Friedman73397492009-03-03 06:41:03 +00002200 if (ComplexMode) {
2201 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002202 }
2203
2204 // Install the new type.
John McCallba6a9bd2009-10-24 08:00:42 +00002205 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
2206 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002207 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002208 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002209 cast<ValueDecl>(D)->setType(NewTy);
2210}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002211
Mike Stump1feade82009-08-26 22:31:08 +00002212static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00002213 // check the attribute arguments.
2214 if (Attr.getNumArgs() > 0) {
2215 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2216 return;
2217 }
Anders Carlssone896d982009-02-13 08:11:52 +00002218
Anders Carlsson5bab7882009-02-19 19:16:48 +00002219 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002220 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002221 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002222 return;
2223 }
Mike Stumpbf916502009-07-24 19:02:52 +00002224
Sean Huntcf807c42010-08-18 23:23:40 +00002225 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002226}
2227
Mike Stump1feade82009-08-26 22:31:08 +00002228static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002229 // check the attribute arguments.
2230 if (Attr.getNumArgs() != 0) {
2231 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2232 return;
2233 }
Mike Stumpbf916502009-07-24 19:02:52 +00002234
Chris Lattnerc5197432009-04-14 17:02:11 +00002235 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002236 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002237 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002238 return;
2239 }
Mike Stumpbf916502009-07-24 19:02:52 +00002240
Sean Huntcf807c42010-08-18 23:23:40 +00002241 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002242}
2243
Chris Lattner7255a2d2010-06-22 00:03:40 +00002244static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2245 Sema &S) {
2246 // check the attribute arguments.
2247 if (Attr.getNumArgs() != 0) {
2248 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2249 return;
2250 }
2251
2252 if (!isa<FunctionDecl>(d)) {
2253 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002254 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002255 return;
2256 }
2257
Eric Christopherf48f3672010-12-01 22:13:54 +00002258 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2259 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002260}
2261
Peter Collingbourneced76712010-12-01 03:15:31 +00002262static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2263 if (S.LangOpts.CUDA) {
2264 // check the attribute arguments.
2265 if (Attr.getNumArgs() != 0) {
2266 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2267 return;
2268 }
2269
2270 if (!isa<VarDecl>(d)) {
2271 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002272 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002273 return;
2274 }
2275
2276 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2277 } else {
2278 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2279 }
2280}
2281
2282static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2283 if (S.LangOpts.CUDA) {
2284 // check the attribute arguments.
2285 if (Attr.getNumArgs() != 0) {
2286 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2287 return;
2288 }
2289
2290 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2291 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002292 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002293 return;
2294 }
2295
2296 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2297 } else {
2298 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2299 }
2300}
2301
2302static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2303 if (S.LangOpts.CUDA) {
2304 // check the attribute arguments.
2305 if (Attr.getNumArgs() != 0) {
2306 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2307 return;
2308 }
2309
2310 if (!isa<FunctionDecl>(d)) {
2311 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002312 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002313 return;
2314 }
2315
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002316 FunctionDecl *FD = cast<FunctionDecl>(d);
2317 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002318 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002319 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2320 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2321 << FD->getType()
2322 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2323 "void");
2324 } else {
2325 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2326 << FD->getType();
2327 }
2328 return;
2329 }
2330
Peter Collingbourneced76712010-12-01 03:15:31 +00002331 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2332 } else {
2333 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2334 }
2335}
2336
2337static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2338 if (S.LangOpts.CUDA) {
2339 // check the attribute arguments.
2340 if (Attr.getNumArgs() != 0) {
2341 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2342 return;
2343 }
2344
2345 if (!isa<FunctionDecl>(d)) {
2346 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002347 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002348 return;
2349 }
2350
2351 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2352 } else {
2353 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2354 }
2355}
2356
2357static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2358 if (S.LangOpts.CUDA) {
2359 // check the attribute arguments.
2360 if (Attr.getNumArgs() != 0) {
2361 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2362 return;
2363 }
2364
2365 if (!isa<VarDecl>(d)) {
2366 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002367 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002368 return;
2369 }
2370
2371 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2372 } else {
2373 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2374 }
2375}
2376
Chris Lattnercf2a7212009-04-20 19:12:28 +00002377static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002378 // check the attribute arguments.
2379 if (Attr.getNumArgs() != 0) {
2380 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2381 return;
2382 }
Mike Stumpbf916502009-07-24 19:02:52 +00002383
Chris Lattnerc5197432009-04-14 17:02:11 +00002384 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2385 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002386 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002387 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002388 return;
2389 }
Mike Stumpbf916502009-07-24 19:02:52 +00002390
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002391 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002392 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002393 return;
2394 }
Mike Stumpbf916502009-07-24 19:02:52 +00002395
Sean Huntcf807c42010-08-18 23:23:40 +00002396 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002397}
2398
John McCall711c52b2011-01-05 12:14:39 +00002399static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) {
2400 if (hasDeclarator(d)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002401
John McCall711c52b2011-01-05 12:14:39 +00002402 // Diagnostic is emitted elsewhere: here we store the (valid) attr
2403 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2404 CallingConv CC;
2405 if (S.CheckCallingConvAttr(attr, CC))
2406 return;
2407
2408 if (!isa<ObjCMethodDecl>(d)) {
2409 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002410 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00002411 return;
2412 }
2413
2414 switch (attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002415 case AttributeList::AT_fastcall:
John McCall711c52b2011-01-05 12:14:39 +00002416 d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002417 return;
2418 case AttributeList::AT_stdcall:
John McCall711c52b2011-01-05 12:14:39 +00002419 d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002420 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002421 case AttributeList::AT_thiscall:
John McCall711c52b2011-01-05 12:14:39 +00002422 d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002423 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002424 case AttributeList::AT_cdecl:
John McCall711c52b2011-01-05 12:14:39 +00002425 d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002426 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002427 case AttributeList::AT_pascal:
John McCall711c52b2011-01-05 12:14:39 +00002428 d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002429 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002430 default:
2431 llvm_unreachable("unexpected attribute kind");
2432 return;
2433 }
2434}
2435
Peter Collingbournef315fa82011-02-14 01:42:53 +00002436static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){
2437 assert(Attr.isInvalid() == false);
2438 d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
2439}
2440
John McCall711c52b2011-01-05 12:14:39 +00002441bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2442 if (attr.isInvalid())
2443 return true;
2444
2445 if (attr.getNumArgs() != 0) {
2446 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2447 attr.setInvalid();
2448 return true;
2449 }
2450
2451 // TODO: diagnose uses of these conventions on the wrong target.
2452 switch (attr.getKind()) {
2453 case AttributeList::AT_cdecl: CC = CC_C; break;
2454 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2455 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2456 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2457 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
2458 default: llvm_unreachable("unexpected attribute kind"); return true;
2459 }
2460
2461 return false;
2462}
2463
2464static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) {
2465 if (hasDeclarator(d)) return;
2466
2467 unsigned numParams;
2468 if (S.CheckRegparmAttr(attr, numParams))
2469 return;
2470
2471 if (!isa<ObjCMethodDecl>(d)) {
2472 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002473 << attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002474 return;
2475 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002476
John McCall711c52b2011-01-05 12:14:39 +00002477 d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams));
2478}
2479
2480/// Checks a regparm attribute, returning true if it is ill-formed and
2481/// otherwise setting numParams to the appropriate value.
2482bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) {
2483 if (attr.isInvalid())
2484 return true;
2485
2486 if (attr.getNumArgs() != 1) {
2487 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2488 attr.setInvalid();
2489 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002490 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002491
John McCall711c52b2011-01-05 12:14:39 +00002492 Expr *NumParamsExpr = attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002493 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002494 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00002495 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
2496 Diag(attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002497 << "regparm" << NumParamsExpr->getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +00002498 attr.setInvalid();
2499 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002500 }
2501
John McCall711c52b2011-01-05 12:14:39 +00002502 if (Context.Target.getRegParmMax() == 0) {
2503 Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002504 << NumParamsExpr->getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +00002505 attr.setInvalid();
2506 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002507 }
2508
John McCall711c52b2011-01-05 12:14:39 +00002509 numParams = NumParams.getZExtValue();
2510 if (numParams > Context.Target.getRegParmMax()) {
2511 Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2512 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
2513 attr.setInvalid();
2514 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002515 }
2516
John McCall711c52b2011-01-05 12:14:39 +00002517 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002518}
2519
Peter Collingbourne7b381982010-12-12 23:03:07 +00002520static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){
2521 if (S.LangOpts.CUDA) {
2522 // check the attribute arguments.
2523 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002524 // FIXME: 0 is not okay.
2525 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002526 return;
2527 }
2528
2529 if (!isFunctionOrMethod(d)) {
2530 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002531 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002532 return;
2533 }
2534
2535 Expr *MaxThreadsExpr = Attr.getArg(0);
2536 llvm::APSInt MaxThreads(32);
2537 if (MaxThreadsExpr->isTypeDependent() ||
2538 MaxThreadsExpr->isValueDependent() ||
2539 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2540 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2541 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2542 return;
2543 }
2544
2545 llvm::APSInt MinBlocks(32);
2546 if (Attr.getNumArgs() > 1) {
2547 Expr *MinBlocksExpr = Attr.getArg(1);
2548 if (MinBlocksExpr->isTypeDependent() ||
2549 MinBlocksExpr->isValueDependent() ||
2550 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2551 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2552 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2553 return;
2554 }
2555 }
2556
2557 d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
2558 MaxThreads.getZExtValue(),
2559 MinBlocks.getZExtValue()));
2560 } else {
2561 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2562 }
2563}
2564
Chris Lattner0744e5f2008-06-29 00:23:49 +00002565//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002566// Checker-specific attribute handlers.
2567//===----------------------------------------------------------------------===//
2568
John McCallc7ad3812011-01-25 03:31:58 +00002569static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2570 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2571}
2572static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2573 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2574}
2575
2576static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) {
2577 ParmVarDecl *param = dyn_cast<ParmVarDecl>(d);
2578 if (!param) {
2579 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002580 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00002581 return;
2582 }
2583
2584 bool typeOK, cf;
2585 if (attr.getKind() == AttributeList::AT_ns_consumed) {
2586 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2587 cf = false;
2588 } else {
2589 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2590 cf = true;
2591 }
2592
2593 if (!typeOK) {
2594 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2595 << SourceRange(attr.getLoc()) << attr.getName() << cf;
2596 return;
2597 }
2598
2599 if (cf)
2600 param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context));
2601 else
2602 param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context));
2603}
2604
2605static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr,
2606 Sema &S) {
2607 if (!isa<ObjCMethodDecl>(d)) {
2608 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002609 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00002610 return;
2611 }
2612
2613 d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context));
2614}
2615
2616static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr,
Ted Kremenekb71368d2009-05-09 02:44:38 +00002617 Sema &S) {
2618
John McCallc7ad3812011-01-25 03:31:58 +00002619 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00002620
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002621 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
John McCallc7ad3812011-01-25 03:31:58 +00002622 returnType = MD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002623 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
John McCallc7ad3812011-01-25 03:31:58 +00002624 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002625 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002626 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCallc7ad3812011-01-25 03:31:58 +00002627 << SourceRange(attr.getLoc()) << attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00002628 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002629 return;
2630 }
Mike Stumpbf916502009-07-24 19:02:52 +00002631
John McCallc7ad3812011-01-25 03:31:58 +00002632 bool typeOK;
2633 bool cf;
2634 switch (attr.getKind()) {
2635 default: llvm_unreachable("invalid ownership attribute"); return;
2636 case AttributeList::AT_ns_returns_autoreleased:
2637 case AttributeList::AT_ns_returns_retained:
2638 case AttributeList::AT_ns_returns_not_retained:
2639 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2640 cf = false;
2641 break;
2642
2643 case AttributeList::AT_cf_returns_retained:
2644 case AttributeList::AT_cf_returns_not_retained:
2645 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2646 cf = true;
2647 break;
2648 }
2649
2650 if (!typeOK) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002651 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallc7ad3812011-01-25 03:31:58 +00002652 << SourceRange(attr.getLoc())
2653 << attr.getName() << isa<ObjCMethodDecl>(d) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00002654 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002655 }
Mike Stumpbf916502009-07-24 19:02:52 +00002656
John McCallc7ad3812011-01-25 03:31:58 +00002657 switch (attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00002658 default:
2659 assert(0 && "invalid ownership attribute");
2660 return;
John McCallc7ad3812011-01-25 03:31:58 +00002661 case AttributeList::AT_ns_returns_autoreleased:
2662 d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(),
2663 S.Context));
2664 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002665 case AttributeList::AT_cf_returns_not_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002666 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002667 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002668 return;
2669 case AttributeList::AT_ns_returns_not_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002670 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002671 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002672 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002673 case AttributeList::AT_cf_returns_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002674 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002675 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002676 return;
2677 case AttributeList::AT_ns_returns_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002678 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002679 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002680 return;
2681 };
2682}
2683
Charles Davisf0122fe2010-02-16 18:27:26 +00002684static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2685 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00002686 Attr.getKind() == AttributeList::AT_dllexport ||
2687 Attr.getKind() == AttributeList::AT_uuid;
2688}
2689
2690//===----------------------------------------------------------------------===//
2691// Microsoft specific attribute handlers.
2692//===----------------------------------------------------------------------===//
2693
2694static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2695 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2696 // check the attribute arguments.
2697 if (Attr.getNumArgs() != 1) {
2698 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2699 return;
2700 }
2701 Expr *Arg = Attr.getArg(0);
2702 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichetd3d3be92010-12-20 01:41:49 +00002703 if (Str == 0 || Str->isWide()) {
2704 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2705 << "uuid" << 1;
2706 return;
2707 }
2708
2709 llvm::StringRef StrRef = Str->getString();
2710
2711 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2712 StrRef.back() == '}';
2713
2714 // Validate GUID length.
2715 if (IsCurly && StrRef.size() != 38) {
2716 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2717 return;
2718 }
2719 if (!IsCurly && StrRef.size() != 36) {
2720 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2721 return;
2722 }
2723
2724 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2725 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Anders Carlssonf89e0422011-01-23 21:07:30 +00002726 llvm::StringRef::iterator I = StrRef.begin();
2727 if (IsCurly) // Skip the optional '{'
2728 ++I;
2729
2730 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00002731 if (i == 8 || i == 13 || i == 18 || i == 23) {
2732 if (*I != '-') {
2733 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2734 return;
2735 }
2736 } else if (!isxdigit(*I)) {
2737 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2738 return;
2739 }
2740 I++;
2741 }
Francois Pichet11542142010-12-19 06:50:37 +00002742
2743 d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
2744 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00002745 } else
Francois Pichet11542142010-12-19 06:50:37 +00002746 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00002747}
2748
Ted Kremenekb71368d2009-05-09 02:44:38 +00002749//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002750// Top Level Sema Entry Points
2751//===----------------------------------------------------------------------===//
2752
Peter Collingbourne60700392011-01-21 02:08:45 +00002753static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D,
2754 const AttributeList &Attr, Sema &S) {
2755 switch (Attr.getKind()) {
2756 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
2757 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
2758 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
2759 default:
2760 break;
2761 }
2762}
Abramo Bagnarae215f722010-04-30 13:10:51 +00002763
Peter Collingbourne60700392011-01-21 02:08:45 +00002764static void ProcessInheritableDeclAttr(Scope *scope, Decl *D,
2765 const AttributeList &Attr, Sema &S) {
Chris Lattner803d0802008-06-29 00:43:07 +00002766 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002767 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002768 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2769 case AttributeList::AT_IBOutletCollection:
2770 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002771 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002772 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002773 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002774 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00002775 case AttributeList::AT_neon_vector_type:
2776 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00002777 // Ignore these, these are type attributes, handled by
2778 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002779 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00002780 case AttributeList::AT_device:
2781 case AttributeList::AT_host:
2782 case AttributeList::AT_overloadable:
2783 // Ignore, this is a non-inheritable attribute, handled
2784 // by ProcessNonInheritableDeclAttr.
2785 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002786 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2787 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002788 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002789 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002790 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002791 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002792 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002793 case AttributeList::AT_availability:HandleAvailabilityAttr(D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002794 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002795 HandleDependencyAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002796 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002797 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002798 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2799 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2800 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002801 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002802 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002803 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002804 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2805 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002806 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002807 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002808 case AttributeList::AT_launch_bounds:
2809 HandleLaunchBoundsAttr(D, Attr, S);
2810 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002811 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2812 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohman34c26302010-11-17 00:03:07 +00002813 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002814 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002815 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002816 case AttributeList::AT_ownership_returns:
2817 case AttributeList::AT_ownership_takes:
2818 case AttributeList::AT_ownership_holds:
2819 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002820 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002821 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2822 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002823 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002824 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002825
2826 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00002827 case AttributeList::AT_cf_consumed:
2828 case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break;
2829 case AttributeList::AT_ns_consumes_self:
2830 HandleNSConsumesSelfAttr(D, Attr, S); break;
2831
2832 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00002833 case AttributeList::AT_ns_returns_not_retained:
2834 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002835 case AttributeList::AT_ns_returns_retained:
2836 case AttributeList::AT_cf_returns_retained:
2837 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2838
Nate Begeman6f3d8382009-06-26 06:32:41 +00002839 case AttributeList::AT_reqd_wg_size:
2840 HandleReqdWorkGroupSize(D, Attr, S); break;
2841
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002842 case AttributeList::AT_init_priority:
2843 HandleInitPriorityAttr(D, Attr, S); break;
2844
Sean Hunt7725e672009-11-25 04:20:27 +00002845 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
2846 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002847 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2848 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2849 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002850 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002851 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2852 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002853 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002854 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002855 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002856 case AttributeList::AT_transparent_union:
2857 HandleTransparentUnionAttr(D, Attr, S);
2858 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002859 case AttributeList::AT_objc_exception:
2860 HandleObjCExceptionAttr(D, Attr, S);
2861 break;
John McCalld5313b02011-03-02 11:33:24 +00002862 case AttributeList::AT_objc_method_family:
2863 HandleObjCMethodFamilyAttr(D, Attr, S);
2864 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002865 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2866 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2867 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2868 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2869 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2870 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2871 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2872 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2873 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002874 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002875 // Just ignore
2876 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002877 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2878 HandleNoInstrumentFunctionAttr(D, Attr, S);
2879 break;
John McCall04a67a62010-02-05 21:31:56 +00002880 case AttributeList::AT_stdcall:
2881 case AttributeList::AT_cdecl:
2882 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002883 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002884 case AttributeList::AT_pascal:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002885 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002886 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00002887 case AttributeList::AT_opencl_kernel_function:
2888 HandleOpenCLKernelAttr(D, Attr, S);
2889 break;
Francois Pichet11542142010-12-19 06:50:37 +00002890 case AttributeList::AT_uuid:
2891 HandleUuidAttr(D, Attr, S);
2892 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002893 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002894 // Ask target about the attribute.
2895 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2896 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002897 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2898 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002899 break;
2900 }
2901}
2902
Peter Collingbourne60700392011-01-21 02:08:45 +00002903/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
2904/// the attribute applies to decls. If the attribute is a type attribute, just
2905/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2906/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
2907static void ProcessDeclAttribute(Scope *scope, Decl *D,
2908 const AttributeList &Attr, Sema &S,
2909 bool NonInheritable, bool Inheritable) {
2910 if (Attr.isInvalid())
2911 return;
2912
2913 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2914 // FIXME: Try to deal with other __declspec attributes!
2915 return;
2916
2917 if (NonInheritable)
2918 ProcessNonInheritableDeclAttr(scope, D, Attr, S);
2919
2920 if (Inheritable)
2921 ProcessInheritableDeclAttr(scope, D, Attr, S);
2922}
2923
Chris Lattner803d0802008-06-29 00:43:07 +00002924/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2925/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00002926void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00002927 const AttributeList *AttrList,
2928 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002929 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Peter Collingbourne60700392011-01-21 02:08:45 +00002930 ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002931 }
2932
2933 // GCC accepts
2934 // static int a9 __attribute__((weakref));
2935 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00002936 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002937 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002938 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002939 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002940 }
2941}
2942
Ryan Flynne25ff832009-07-30 03:15:39 +00002943/// DeclClonePragmaWeak - clone existing decl (maybe definition),
2944/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00002945NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002946 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00002947 NamedDecl *NewD = 0;
2948 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
2949 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002950 FD->getInnerLocStart(),
Ryan Flynne25ff832009-07-30 03:15:39 +00002951 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00002952 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00002953 if (FD->getQualifier()) {
2954 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002955 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00002956 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002957 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
2958 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002959 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00002960 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00002961 VD->getStorageClass(),
2962 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00002963 if (VD->getQualifier()) {
2964 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00002965 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00002966 }
Ryan Flynne25ff832009-07-30 03:15:39 +00002967 }
2968 return NewD;
2969}
2970
2971/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
2972/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00002973void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002974 if (W.getUsed()) return; // only do this once
2975 W.setUsed(true);
2976 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
2977 IdentifierInfo *NDId = ND->getIdentifier();
2978 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00002979 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
2980 NDId->getName()));
2981 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00002982 WeakTopLevelDecl.push_back(NewD);
2983 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2984 // to insert Decl at TU scope, sorry.
2985 DeclContext *SavedContext = CurContext;
2986 CurContext = Context.getTranslationUnitDecl();
2987 PushOnScopeChains(NewD, S);
2988 CurContext = SavedContext;
2989 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00002990 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00002991 }
2992}
2993
Chris Lattner0744e5f2008-06-29 00:23:49 +00002994/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2995/// it, apply them to D. This is a bit tricky because PD can have attributes
2996/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00002997void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
2998 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00002999 // It's valid to "forward-declare" #pragma weak, in which case we
3000 // have to do this.
Peter Collingbourne60700392011-01-21 02:08:45 +00003001 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCalld4aff0e2010-10-27 00:59:00 +00003002 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3003 if (IdentifierInfo *Id = ND->getIdentifier()) {
3004 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3005 = WeakUndeclaredIdentifiers.find(Id);
3006 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3007 WeakInfo W = I->second;
3008 DeclApplyPragmaWeak(S, ND, W);
3009 WeakUndeclaredIdentifiers[Id] = W;
3010 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003011 }
3012 }
3013 }
3014
Chris Lattner0744e5f2008-06-29 00:23:49 +00003015 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003016 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003017 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003018
Chris Lattner0744e5f2008-06-29 00:23:49 +00003019 // Walk the declarator structure, applying decl attributes that were in a type
3020 // position to the decl itself. This handles cases like:
3021 // int *__attr__(x)** D;
3022 // when X is a decl attribute.
3023 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3024 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003025 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003026
Chris Lattner0744e5f2008-06-29 00:23:49 +00003027 // Finally, apply any attributes on the decl itself.
3028 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003029 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003030}
John McCall54abf7d2009-11-04 02:18:39 +00003031
John McCalleee1d542011-02-14 07:13:47 +00003032// This duplicates a vector push_back but hides the need to know the
3033// size of the type.
3034void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3035 assert(StackSize <= StackCapacity);
3036
3037 // Grow the stack if necessary.
3038 if (StackSize == StackCapacity) {
3039 unsigned newCapacity = 2 * StackCapacity + 2;
3040 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3041 const char *oldBuffer = (const char*) Stack;
3042
3043 if (StackCapacity)
3044 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3045
3046 delete[] oldBuffer;
3047 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3048 StackCapacity = newCapacity;
3049 }
3050
3051 assert(StackSize < StackCapacity);
3052 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00003053}
3054
John McCalleee1d542011-02-14 07:13:47 +00003055void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3056 Decl *decl) {
3057 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00003058
John McCalleee1d542011-02-14 07:13:47 +00003059 // Check the invariants.
3060 assert(DD.StackSize >= state.SavedStackSize);
3061 assert(state.SavedStackSize >= DD.ActiveStackBase);
3062 assert(DD.ParsingDepth > 0);
3063
3064 // Drop the parsing depth.
3065 DD.ParsingDepth--;
3066
3067 // If there are no active diagnostics, we're done.
3068 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00003069 return;
3070
John McCall2f514482010-01-27 03:50:35 +00003071 // We only want to actually emit delayed diagnostics when we
3072 // successfully parsed a decl.
John McCalleee1d542011-02-14 07:13:47 +00003073 if (decl) {
3074 // We emit all the active diagnostics, not just those starting
3075 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00003076 // decl spec and another for each declarator; in a decl group like:
3077 // deprecated_typedef foo, *bar, baz();
3078 // only the declarator pops will be passed decls. This is correct;
3079 // we really do need to consider delayed diagnostics from the decl spec
3080 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00003081 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3082 DelayedDiagnostic &diag = DD.Stack[i];
3083 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00003084 continue;
3085
John McCalleee1d542011-02-14 07:13:47 +00003086 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00003087 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00003088 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003089 break;
3090
3091 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00003092 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003093 break;
3094 }
3095 }
3096 }
3097
John McCall58e6f342010-03-16 05:22:47 +00003098 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00003099 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00003100 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00003101
John McCalleee1d542011-02-14 07:13:47 +00003102 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00003103}
3104
3105static bool isDeclDeprecated(Decl *D) {
3106 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003107 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00003108 return true;
3109 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3110 return false;
3111}
3112
John McCall9c3087b2010-08-26 02:13:20 +00003113void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00003114 Decl *Ctx) {
3115 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00003116 return;
3117
John McCall2f514482010-01-27 03:50:35 +00003118 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003119 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003120 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003121 << DD.getDeprecationDecl()->getDeclName()
3122 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003123 else
3124 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003125 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003126}
3127
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003128void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003129 SourceLocation Loc,
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003130 bool UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003131 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00003132 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3133 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00003134 return;
3135 }
3136
3137 // Otherwise, don't warn if our current context is deprecated.
3138 if (isDeclDeprecated(cast<Decl>(CurContext)))
3139 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003140 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003141 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3142 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003143 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003144 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003145 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
3146 else
3147 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
3148 }
John McCall54abf7d2009-11-04 02:18:39 +00003149}