blob: 7f93ab72d6d1c0903da82fa4e1289a8c4a964bd4 [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();
Richard Smith162e1c12011-04-15 14:24:37 +000058 else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(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) {
Richard Smith162e1c12011-04-15 14:24:37 +0000104 // In some sense, TypedefNameDecl really *ought* to be a DeclaratorDecl.
105 return isa<DeclaratorDecl>(d) || isa<BlockDecl>(d) || isa<TypedefNameDecl>(d);
John McCall711c52b2011-01-05 12:14:39 +0000106}
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) {
Richard Smith162e1c12011-04-15 14:24:37 +0000205 TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(d);
Chris Lattner545dd342008-06-28 23:36:30 +0000206 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
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +0000264static void HandleMsStructAttr(Decl *d, const AttributeList &Attr, Sema &S) {
265 if (TagDecl *TD = dyn_cast<TagDecl>(d))
266 TD->addAttr(::new (S.Context) MsStructAttr(Attr.getLoc(), S.Context));
267 else
268 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
269}
270
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000271static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek96329d42008-07-15 22:26:48 +0000272 // check the attribute arguments.
273 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000274 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000275 return;
276 }
Mike Stumpbf916502009-07-24 19:02:52 +0000277
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000278 // The IBAction attributes only apply to instance methods.
279 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
280 if (MD->isInstanceMethod()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000281 d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000282 return;
283 }
284
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000285 S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName();
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000286}
287
288static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) {
289 // check the attribute arguments.
290 if (Attr.getNumArgs() > 0) {
291 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
292 return;
293 }
294
295 // The IBOutlet attributes only apply to instance variables of
Ted Kremenekefbddd22010-02-17 02:37:45 +0000296 // Objective-C classes.
297 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) {
Sean Huntcf807c42010-08-18 23:23:40 +0000298 d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context));
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000299 return;
Ted Kremenekefbddd22010-02-17 02:37:45 +0000300 }
Ted Kremenek63e5d7c2010-02-18 03:08:58 +0000301
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000302 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek96329d42008-07-15 22:26:48 +0000303}
304
Ted Kremenek857e9182010-05-19 17:38:06 +0000305static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr,
306 Sema &S) {
307
308 // The iboutletcollection attribute can have zero or one arguments.
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000309 if (Attr.getParameterName() && Attr.getNumArgs() > 0) {
Ted Kremenek857e9182010-05-19 17:38:06 +0000310 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
311 return;
312 }
313
314 // The IBOutletCollection attributes only apply to instance variables of
315 // Objective-C classes.
316 if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) {
Ted Kremenek4ee2bb12011-02-04 06:54:16 +0000317 S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName();
Ted Kremenek857e9182010-05-19 17:38:06 +0000318 return;
319 }
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000320 if (const ValueDecl *VD = dyn_cast<ValueDecl>(d))
321 if (!VD->getType()->getAs<ObjCObjectPointerType>()) {
322 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
323 << VD->getType() << 0;
324 return;
325 }
326 if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d))
327 if (!PD->getType()->getAs<ObjCObjectPointerType>()) {
328 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type)
329 << PD->getType() << 1;
330 return;
331 }
332
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000333 IdentifierInfo *II = Attr.getParameterName();
334 if (!II)
335 II = &S.Context.Idents.get("id");
Fariborz Jahanian3a3400b2010-08-17 21:39:27 +0000336
John McCallb3d87482010-08-24 05:47:05 +0000337 ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(),
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000338 S.getScopeForContext(d->getDeclContext()->getParent()));
339 if (!TypeRep) {
340 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
341 return;
342 }
John McCallb3d87482010-08-24 05:47:05 +0000343 QualType QT = TypeRep.get();
Fariborz Jahaniana8fb24f2010-08-17 20:23:12 +0000344 // Diagnose use of non-object type in iboutletcollection attribute.
345 // FIXME. Gnu attribute extension ignores use of builtin types in
346 // attributes. So, __attribute__((iboutletcollection(char))) will be
347 // treated as __attribute__((iboutletcollection())).
348 if (!QT->isObjCIdType() && !QT->isObjCClassType() &&
349 !QT->isObjCObjectType()) {
350 S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II;
351 return;
352 }
Sean Huntcf807c42010-08-18 23:23:40 +0000353 d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context,
354 QT));
Ted Kremenek857e9182010-05-19 17:38:06 +0000355}
356
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000357static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +0000358 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
359 // ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000360 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000361 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000362 << Attr.getName() << ExpectedFunction;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000363 return;
364 }
Mike Stumpbf916502009-07-24 19:02:52 +0000365
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000366 // In C++ the implicit 'this' function parameter also counts, and they are
367 // counted from one.
368 bool HasImplicitThisParam = isInstanceMethod(d);
369 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000370
371 // The nonnull attribute only applies to pointers.
372 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpbf916502009-07-24 19:02:52 +0000373
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000374 for (AttributeList::arg_iterator I=Attr.arg_begin(),
375 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpbf916502009-07-24 19:02:52 +0000376
377
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000378 // The argument must be an integer constant expression.
Peter Collingbourne7a730022010-11-23 20:45:58 +0000379 Expr *Ex = *I;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000380 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000381 if (Ex->isTypeDependent() || Ex->isValueDependent() ||
382 !Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000383 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
384 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000385 return;
386 }
Mike Stumpbf916502009-07-24 19:02:52 +0000387
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000388 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +0000389
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000390 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000391 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000392 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000393 return;
394 }
Mike Stumpbf916502009-07-24 19:02:52 +0000395
Ted Kremenek465172f2008-07-21 22:09:15 +0000396 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000397 if (HasImplicitThisParam) {
398 if (x == 0) {
399 S.Diag(Attr.getLoc(),
400 diag::err_attribute_invalid_implicit_this_argument)
401 << "nonnull" << Ex->getSourceRange();
402 return;
403 }
404 --x;
405 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000406
407 // Is the function argument a pointer type?
Douglas Gregorde436322010-12-15 15:41:46 +0000408 QualType T = getFunctionOrMethodArgType(d, x).getNonReferenceType();
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000409 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000410 // FIXME: Should also highlight argument in decl.
Douglas Gregorc9ef4052010-08-12 18:48:43 +0000411 S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000412 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000413 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000414 }
Mike Stumpbf916502009-07-24 19:02:52 +0000415
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000416 NonNullArgs.push_back(x);
417 }
Mike Stumpbf916502009-07-24 19:02:52 +0000418
419 // If no arguments were specified to __attribute__((nonnull)) then all pointer
420 // arguments have a nonnull attribute.
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000421 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000422 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
Douglas Gregorde436322010-12-15 15:41:46 +0000423 QualType T = getFunctionOrMethodArgType(d, I).getNonReferenceType();
Ted Kremenekdbfe99e2009-07-15 23:23:54 +0000424 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000425 NonNullArgs.push_back(I);
Fariborz Jahanianff3a0782010-09-27 22:42:37 +0000426 else if (const RecordType *UT = T->getAsUnionType()) {
427 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) {
428 RecordDecl *UD = UT->getDecl();
429 for (RecordDecl::field_iterator it = UD->field_begin(),
430 itend = UD->field_end(); it != itend; ++it) {
431 T = it->getType();
432 if (T->isAnyPointerType() || T->isBlockPointerType()) {
433 NonNullArgs.push_back(I);
434 break;
435 }
436 }
437 }
438 }
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000439 }
Mike Stumpbf916502009-07-24 19:02:52 +0000440
Ted Kremenekee1c08c2010-10-21 18:49:36 +0000441 // No pointer arguments?
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000442 if (NonNullArgs.empty()) {
443 // Warn the trivial case only if attribute is not coming from a
444 // macro instantiation.
445 if (Attr.getLoc().isFileID())
446 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000447 return;
Fariborz Jahanian60acea42010-09-27 19:05:51 +0000448 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000449 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000450
451 unsigned* start = &NonNullArgs[0];
452 unsigned size = NonNullArgs.size();
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000453 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000454 d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start,
455 size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000456}
457
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000458static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) {
459 // This attribute must be applied to a function declaration.
460 // The first argument to the attribute must be a string,
461 // the name of the resource, for example "malloc".
462 // The following arguments must be argument indexes, the arguments must be
463 // of integer type for Returns, otherwise of pointer type.
464 // The difference between Holds and Takes is that a pointer may still be used
Jordy Rose2a479922010-08-12 08:54:03 +0000465 // after being held. free() should be __attribute((ownership_takes)), whereas
466 // a list append function may well be __attribute((ownership_holds)).
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000467
468 if (!AL.getParameterName()) {
469 S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string)
470 << AL.getName()->getName() << 1;
471 return;
472 }
473 // Figure out our Kind, and check arguments while we're at it.
Sean Huntcf807c42010-08-18 23:23:40 +0000474 OwnershipAttr::OwnershipKind K;
Jordy Rose2a479922010-08-12 08:54:03 +0000475 switch (AL.getKind()) {
476 case AttributeList::AT_ownership_takes:
Sean Huntcf807c42010-08-18 23:23:40 +0000477 K = OwnershipAttr::Takes;
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_holds:
Sean Huntcf807c42010-08-18 23:23:40 +0000484 K = OwnershipAttr::Holds;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000485 if (AL.getNumArgs() < 1) {
486 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
487 return;
488 }
Jordy Rose2a479922010-08-12 08:54:03 +0000489 break;
490 case AttributeList::AT_ownership_returns:
Sean Huntcf807c42010-08-18 23:23:40 +0000491 K = OwnershipAttr::Returns;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000492 if (AL.getNumArgs() > 1) {
493 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments)
494 << AL.getNumArgs() + 1;
495 return;
496 }
Jordy Rose2a479922010-08-12 08:54:03 +0000497 break;
498 default:
499 // This should never happen given how we are called.
500 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000501 }
502
503 if (!isFunction(d) || !hasFunctionProto(d)) {
John McCall883cc2c2011-03-02 12:29:23 +0000504 S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
505 << AL.getName() << ExpectedFunction;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000506 return;
507 }
508
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000509 // In C++ the implicit 'this' function parameter also counts, and they are
510 // counted from one.
511 bool HasImplicitThisParam = isInstanceMethod(d);
512 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000513
514 llvm::StringRef Module = AL.getParameterName()->getName();
515
516 // Normalize the argument, __foo__ becomes foo.
517 if (Module.startswith("__") && Module.endswith("__"))
518 Module = Module.substr(2, Module.size() - 4);
519
520 llvm::SmallVector<unsigned, 10> OwnershipArgs;
521
Jordy Rose2a479922010-08-12 08:54:03 +0000522 for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E;
523 ++I) {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000524
Peter Collingbourne7a730022010-11-23 20:45:58 +0000525 Expr *IdxExpr = *I;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000526 llvm::APSInt ArgNum(32);
527 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
528 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
529 S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int)
530 << AL.getName()->getName() << IdxExpr->getSourceRange();
531 continue;
532 }
533
534 unsigned x = (unsigned) ArgNum.getZExtValue();
535
536 if (x > NumArgs || x < 1) {
537 S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds)
538 << AL.getName()->getName() << x << IdxExpr->getSourceRange();
539 continue;
540 }
541 --x;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +0000542 if (HasImplicitThisParam) {
543 if (x == 0) {
544 S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
545 << "ownership" << IdxExpr->getSourceRange();
546 return;
547 }
548 --x;
549 }
550
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000551 switch (K) {
Sean Huntcf807c42010-08-18 23:23:40 +0000552 case OwnershipAttr::Takes:
553 case OwnershipAttr::Holds: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000554 // Is the function argument a pointer type?
555 QualType T = getFunctionOrMethodArgType(d, x);
556 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
557 // FIXME: Should also highlight argument in decl.
558 S.Diag(AL.getLoc(), diag::err_ownership_type)
Sean Huntcf807c42010-08-18 23:23:40 +0000559 << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds")
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000560 << "pointer"
561 << IdxExpr->getSourceRange();
562 continue;
563 }
564 break;
565 }
Sean Huntcf807c42010-08-18 23:23:40 +0000566 case OwnershipAttr::Returns: {
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000567 if (AL.getNumArgs() > 1) {
568 // Is the function argument an integer type?
Peter Collingbourne7a730022010-11-23 20:45:58 +0000569 Expr *IdxExpr = AL.getArg(0);
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000570 llvm::APSInt ArgNum(32);
571 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent()
572 || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) {
573 S.Diag(AL.getLoc(), diag::err_ownership_type)
574 << "ownership_returns" << "integer"
575 << IdxExpr->getSourceRange();
576 return;
577 }
578 }
579 break;
580 }
Jordy Rose2a479922010-08-12 08:54:03 +0000581 default:
582 llvm_unreachable("Unknown ownership attribute");
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000583 } // switch
584
585 // Check we don't have a conflict with another ownership attribute.
Sean Huntcf807c42010-08-18 23:23:40 +0000586 for (specific_attr_iterator<OwnershipAttr>
587 i = d->specific_attr_begin<OwnershipAttr>(),
588 e = d->specific_attr_end<OwnershipAttr>();
589 i != e; ++i) {
590 if ((*i)->getOwnKind() != K) {
591 for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end();
592 I!=E; ++I) {
593 if (x == *I) {
594 S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible)
595 << AL.getName()->getName() << "ownership_*";
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000596 }
597 }
598 }
599 }
600 OwnershipArgs.push_back(x);
601 }
602
603 unsigned* start = OwnershipArgs.data();
604 unsigned size = OwnershipArgs.size();
605 llvm::array_pod_sort(start, start + size);
Sean Huntcf807c42010-08-18 23:23:40 +0000606
607 if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) {
608 S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2;
609 return;
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000610 }
Sean Huntcf807c42010-08-18 23:23:40 +0000611
612 d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module,
613 start, size));
Ted Kremenekdd0e4902010-07-31 01:52:11 +0000614}
615
John McCall332bb2a2011-02-08 22:35:49 +0000616/// Whether this declaration has internal linkage for the purposes of
617/// things that want to complain about things not have internal linkage.
618static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
619 switch (D->getLinkage()) {
620 case NoLinkage:
621 case InternalLinkage:
622 return true;
623
624 // Template instantiations that go from external to unique-external
625 // shouldn't get diagnosed.
626 case UniqueExternalLinkage:
627 return true;
628
629 case ExternalLinkage:
630 return false;
631 }
632 llvm_unreachable("unknown linkage kind!");
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000633 return false;
634}
635
636static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) {
637 // Check the attribute arguments.
638 if (Attr.getNumArgs() > 1) {
639 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
640 return;
641 }
642
John McCall332bb2a2011-02-08 22:35:49 +0000643 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
644 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000645 << Attr.getName() << ExpectedVariableOrFunction;
John McCall332bb2a2011-02-08 22:35:49 +0000646 return;
647 }
648
649 NamedDecl *nd = cast<NamedDecl>(d);
650
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000651 // gcc rejects
652 // class c {
653 // static int a __attribute__((weakref ("v2")));
654 // static int b() __attribute__((weakref ("f3")));
655 // };
656 // and ignores the attributes of
657 // void f(void) {
658 // static int a __attribute__((weakref ("v2")));
659 // }
660 // we reject them
Sebastian Redl7a126a42010-08-31 00:36:30 +0000661 const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
662 if (!Ctx->isFileContext()) {
663 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
John McCall332bb2a2011-02-08 22:35:49 +0000664 nd->getNameAsString();
Sebastian Redl7a126a42010-08-31 00:36:30 +0000665 return;
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000666 }
667
668 // The GCC manual says
669 //
670 // At present, a declaration to which `weakref' is attached can only
671 // be `static'.
672 //
673 // It also says
674 //
675 // Without a TARGET,
676 // given as an argument to `weakref' or to `alias', `weakref' is
677 // equivalent to `weak'.
678 //
679 // gcc 4.4.1 will accept
680 // int a7 __attribute__((weakref));
681 // as
682 // int a7 __attribute__((weak));
683 // This looks like a bug in gcc. We reject that for now. We should revisit
684 // it if this behaviour is actually used.
685
John McCall332bb2a2011-02-08 22:35:49 +0000686 if (!hasEffectivelyInternalLinkage(nd)) {
687 S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000688 return;
689 }
690
691 // GCC rejects
692 // static ((alias ("y"), weakref)).
693 // Should we? How to check that weakref is before or after alias?
694
695 if (Attr.getNumArgs() == 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000696 Expr *Arg = Attr.getArg(0);
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000697 Arg = Arg->IgnoreParenCasts();
698 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
699
700 if (Str == 0 || Str->isWide()) {
701 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
702 << "weakref" << 1;
703 return;
704 }
705 // GCC will accept anything as the argument of weakref. Should we
706 // check for an existing decl?
Eric Christopherf48f3672010-12-01 22:13:54 +0000707 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
708 Str->getString()));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000709 }
710
Sean Huntcf807c42010-08-18 23:23:40 +0000711 d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context));
Rafael Espindola11e8ce72010-02-23 22:00:30 +0000712}
713
Chris Lattner803d0802008-06-29 00:43:07 +0000714static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000715 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000716 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000717 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000718 return;
719 }
Mike Stumpbf916502009-07-24 19:02:52 +0000720
Peter Collingbourne7a730022010-11-23 20:45:58 +0000721 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000722 Arg = Arg->IgnoreParenCasts();
723 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +0000724
Chris Lattner6b6b5372008-06-26 18:38:35 +0000725 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000726 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000727 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000728 return;
729 }
Mike Stumpbf916502009-07-24 19:02:52 +0000730
Daniel Dunbardb57a4c2011-04-19 21:43:27 +0000731 if (S.Context.Target.getTriple().isOSDarwin()) {
Rafael Espindolaf5fe2922010-12-07 15:23:23 +0000732 S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin);
733 return;
734 }
735
Chris Lattner6b6b5372008-06-26 18:38:35 +0000736 // FIXME: check if target symbol exists in current file
Mike Stumpbf916502009-07-24 19:02:52 +0000737
Eric Christopherf48f3672010-12-01 22:13:54 +0000738 d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context,
739 Str->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000740}
741
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000742static void HandleNakedAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000743 Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000744 // Check the attribute arguments.
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000745 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000746 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000747 return;
748 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000749
Chris Lattnerc5197432009-04-14 17:02:11 +0000750 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000751 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000752 << Attr.getName() << ExpectedFunction;
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000753 return;
754 }
755
756 d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context));
757}
758
759static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
760 Sema &S) {
761 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000762 if (Attr.hasParameterOrArguments()) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000763 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
764 return;
765 }
766
767 if (!isa<FunctionDecl>(d)) {
768 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000769 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000770 return;
771 }
Mike Stumpbf916502009-07-24 19:02:52 +0000772
Sean Huntcf807c42010-08-18 23:23:40 +0000773 d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context));
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000774}
775
Ryan Flynn76168e22009-08-09 20:07:29 +0000776static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Daniel Dunbardd0cb222010-09-29 18:20:25 +0000777 // Check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000778 if (Attr.hasParameterOrArguments()) {
Ryan Flynn76168e22009-08-09 20:07:29 +0000779 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
780 return;
781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000783 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000784 QualType RetTy = FD->getResultType();
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000785 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
Sean Huntcf807c42010-08-18 23:23:40 +0000786 d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context));
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000787 return;
788 }
Ryan Flynn76168e22009-08-09 20:07:29 +0000789 }
790
Ted Kremenek2cff7d12009-08-15 00:51:46 +0000791 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn76168e22009-08-09 20:07:29 +0000792}
793
Dan Gohman34c26302010-11-17 00:03:07 +0000794static void HandleMayAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
795 // check the attribute arguments.
796 if (Attr.getNumArgs() != 0) {
797 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
798 return;
799 }
800
Dan Gohman34c26302010-11-17 00:03:07 +0000801 d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context));
802}
803
Eric Christophera6cf1e72010-12-02 02:45:55 +0000804static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
805 assert(Attr.isInvalid() == false);
Eric Christopher722109c2010-12-03 06:58:14 +0000806 if (isa<VarDecl>(d))
807 d->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context));
808 else
809 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000810 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000811}
812
813static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) {
814 assert(Attr.isInvalid() == false);
Eric Christopher722109c2010-12-03 06:58:14 +0000815 if (isa<VarDecl>(d))
816 d->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context));
817 else
818 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000819 << Attr.getName() << ExpectedVariable;
Eric Christophera6cf1e72010-12-02 02:45:55 +0000820}
821
John McCall711c52b2011-01-05 12:14:39 +0000822static void HandleNoReturnAttr(Decl *d, const AttributeList &attr, Sema &S) {
823 if (hasDeclarator(d)) return;
824
825 if (S.CheckNoReturnAttr(attr)) return;
826
827 if (!isa<ObjCMethodDecl>(d)) {
828 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000829 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +0000830 return;
831 }
832
833 d->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context));
834}
835
836bool Sema::CheckNoReturnAttr(const AttributeList &attr) {
Ted Kremenek831efae2011-04-15 05:49:29 +0000837 if (attr.hasParameterOrArguments()) {
John McCall711c52b2011-01-05 12:14:39 +0000838 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
839 attr.setInvalid();
840 return true;
841 }
842
843 return false;
Ted Kremenekb7252322009-04-10 00:01:14 +0000844}
845
846static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
847 Sema &S) {
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000848
849 // The checking path for 'noreturn' and 'analyzer_noreturn' are different
850 // because 'analyzer_noreturn' does not impact the type.
851
852 if (Attr.getNumArgs() != 0) {
853 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
854 return;
855 }
856
857 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
858 ValueDecl *VD = dyn_cast<ValueDecl>(d);
859 if (VD == 0 || (!VD->getType()->isBlockPointerType()
860 && !VD->getType()->isFunctionPointerType())) {
861 S.Diag(Attr.getLoc(),
862 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
863 : diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000864 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Ted Kremenekb56c1cc2010-08-19 00:51:58 +0000865 return;
866 }
867 }
868
869 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000870}
871
John Thompson35cc9622010-08-09 21:53:52 +0000872// PS3 PPU-specific.
873static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr,
874 Sema &S) {
875/*
876 Returning a Vector Class in Registers
877
Eric Christopherf48f3672010-12-01 22:13:54 +0000878 According to the PPU ABI specifications, a class with a single member of
879 vector type is returned in memory when used as the return value of a function.
880 This results in inefficient code when implementing vector classes. To return
881 the value in a single vector register, add the vecreturn attribute to the
882 class definition. This attribute is also applicable to struct types.
John Thompson35cc9622010-08-09 21:53:52 +0000883
884 Example:
885
886 struct Vector
887 {
888 __vector float xyzw;
889 } __attribute__((vecreturn));
890
891 Vector Add(Vector lhs, Vector rhs)
892 {
893 Vector result;
894 result.xyzw = vec_add(lhs.xyzw, rhs.xyzw);
895 return result; // This will be returned in a register
896 }
897*/
John Thompson01add592010-09-18 01:12:07 +0000898 if (!isa<RecordDecl>(d)) {
John Thompson35cc9622010-08-09 21:53:52 +0000899 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000900 << Attr.getName() << ExpectedClass;
John Thompson35cc9622010-08-09 21:53:52 +0000901 return;
902 }
903
904 if (d->getAttr<VecReturnAttr>()) {
905 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn";
906 return;
907 }
908
John Thompson01add592010-09-18 01:12:07 +0000909 RecordDecl *record = cast<RecordDecl>(d);
910 int count = 0;
911
912 if (!isa<CXXRecordDecl>(record)) {
913 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
914 return;
915 }
916
917 if (!cast<CXXRecordDecl>(record)->isPOD()) {
918 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record);
919 return;
920 }
921
Eric Christopherf48f3672010-12-01 22:13:54 +0000922 for (RecordDecl::field_iterator iter = record->field_begin();
923 iter != record->field_end(); iter++) {
John Thompson01add592010-09-18 01:12:07 +0000924 if ((count == 1) || !iter->getType()->isVectorType()) {
925 S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member);
926 return;
927 }
928 count++;
929 }
930
Sean Huntcf807c42010-08-18 23:23:40 +0000931 d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context));
John Thompson35cc9622010-08-09 21:53:52 +0000932}
933
Sean Huntbbd37c62009-11-21 08:43:09 +0000934static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
935 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
936 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000937 << Attr.getName() << ExpectedFunctionMethodOrParameter;
Sean Huntbbd37c62009-11-21 08:43:09 +0000938 return;
939 }
940 // FIXME: Actually store the attribute on the declaration
941}
942
Ted Kremenek73798892008-07-25 04:39:19 +0000943static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
944 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000945 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000947 return;
948 }
Mike Stumpbf916502009-07-24 19:02:52 +0000949
John McCallaec58602010-03-31 02:47:45 +0000950 if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) &&
Chris Lattner57ad3782011-02-17 20:34:02 +0000951 !isa<TypeDecl>(d) && !isa<LabelDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000953 << Attr.getName() << ExpectedVariableFunctionOrLabel;
Ted Kremenek73798892008-07-25 04:39:19 +0000954 return;
955 }
Mike Stumpbf916502009-07-24 19:02:52 +0000956
Sean Huntcf807c42010-08-18 23:23:40 +0000957 d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context));
Ted Kremenek73798892008-07-25 04:39:19 +0000958}
959
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000960static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
961 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +0000962 if (Attr.hasParameterOrArguments()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000963 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
964 return;
965 }
Mike Stumpbf916502009-07-24 19:02:52 +0000966
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000967 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000968 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000969 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
970 return;
971 }
972 } else if (!isFunctionOrMethod(d)) {
973 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +0000974 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000975 return;
976 }
Mike Stumpbf916502009-07-24 19:02:52 +0000977
Sean Huntcf807c42010-08-18 23:23:40 +0000978 d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context));
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000979}
980
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000981static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
982 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +0000983 if (Attr.getNumArgs() > 1) {
984 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000985 return;
Mike Stumpbf916502009-07-24 19:02:52 +0000986 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000987
988 int priority = 65535; // FIXME: Do not hardcode such constants.
989 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +0000990 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000991 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +0000992 if (E->isTypeDependent() || E->isValueDependent() ||
993 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000994 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000995 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000996 return;
997 }
998 priority = Idx.getZExtValue();
999 }
Mike Stumpbf916502009-07-24 19:02:52 +00001000
Chris Lattnerc5197432009-04-14 17:02:11 +00001001 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001002 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001003 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001004 return;
1005 }
1006
Eric Christopherf48f3672010-12-01 22:13:54 +00001007 d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context,
1008 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001009}
1010
1011static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1012 // check the attribute arguments.
John McCallbdc49d32011-03-02 12:15:05 +00001013 if (Attr.getNumArgs() > 1) {
1014 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001015 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001016 }
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001017
1018 int priority = 65535; // FIXME: Do not hardcode such constants.
1019 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001020 Expr *E = Attr.getArg(0);
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001021 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001022 if (E->isTypeDependent() || E->isValueDependent() ||
1023 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001024 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001025 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001026 return;
1027 }
1028 priority = Idx.getZExtValue();
1029 }
Mike Stumpbf916502009-07-24 19:02:52 +00001030
Anders Carlsson6782fc62008-08-22 22:10:48 +00001031 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001032 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001033 << Attr.getName() << ExpectedFunction;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001034 return;
1035 }
1036
Eric Christopherf48f3672010-12-01 22:13:54 +00001037 d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context,
1038 priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001039}
1040
Chris Lattner803d0802008-06-29 00:43:07 +00001041static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001042 unsigned NumArgs = Attr.getNumArgs();
1043 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001044 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001045 return;
1046 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001047
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001048 // Handle the case where deprecated attribute has a text message.
Chris Lattner951bbb22011-02-24 05:42:24 +00001049 llvm::StringRef Str;
1050 if (NumArgs == 1) {
1051 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001052 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001053 S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string)
1054 << "deprecated";
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001055 return;
1056 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001057 Str = SE->getString();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00001058 }
Mike Stumpbf916502009-07-24 19:02:52 +00001059
Chris Lattner951bbb22011-02-24 05:42:24 +00001060 d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001061}
1062
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001063static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001064 unsigned NumArgs = Attr.getNumArgs();
1065 if (NumArgs > 1) {
John McCallbdc49d32011-03-02 12:15:05 +00001066 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001067 return;
1068 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001069
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001070 // Handle the case where unavailable attribute has a text message.
Chris Lattner951bbb22011-02-24 05:42:24 +00001071 llvm::StringRef Str;
1072 if (NumArgs == 1) {
1073 StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0));
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001074 if (!SE) {
Chris Lattner951bbb22011-02-24 05:42:24 +00001075 S.Diag(Attr.getArg(0)->getLocStart(),
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001076 diag::err_attribute_not_string) << "unavailable";
1077 return;
1078 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001079 Str = SE->getString();
Fariborz Jahanianc784dc12010-10-06 23:12:32 +00001080 }
Chris Lattner951bbb22011-02-24 05:42:24 +00001081 d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str));
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001082}
1083
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001084static void HandleAvailabilityAttr(Decl *d, const AttributeList &Attr,
1085 Sema &S) {
1086 IdentifierInfo *Platform = Attr.getParameterName();
1087 SourceLocation PlatformLoc = Attr.getParameterLoc();
1088
1089 llvm::StringRef PlatformName
1090 = AvailabilityAttr::getPrettyPlatformName(Platform->getName());
1091 if (PlatformName.empty()) {
1092 S.Diag(PlatformLoc, diag::warn_availability_unknown_platform)
1093 << Platform;
1094
1095 PlatformName = Platform->getName();
1096 }
1097
1098 AvailabilityChange Introduced = Attr.getAvailabilityIntroduced();
1099 AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated();
1100 AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted();
Douglas Gregorb53e4172011-03-26 03:35:55 +00001101 bool IsUnavailable = Attr.getUnavailableLoc().isValid();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001102
1103 // Ensure that Introduced < Deprecated < Obsoleted (although not all
1104 // of these steps are needed).
1105 if (Introduced.isValid() && Deprecated.isValid() &&
1106 !(Introduced.Version < Deprecated.Version)) {
1107 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1108 << 1 << PlatformName << Deprecated.Version.getAsString()
1109 << 0 << Introduced.Version.getAsString();
1110 return;
1111 }
1112
1113 if (Introduced.isValid() && Obsoleted.isValid() &&
1114 !(Introduced.Version < Obsoleted.Version)) {
1115 S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering)
1116 << 2 << PlatformName << Obsoleted.Version.getAsString()
1117 << 0 << Introduced.Version.getAsString();
1118 return;
1119 }
1120
1121 if (Deprecated.isValid() && Obsoleted.isValid() &&
1122 !(Deprecated.Version < Obsoleted.Version)) {
1123 S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering)
1124 << 2 << PlatformName << Obsoleted.Version.getAsString()
1125 << 1 << Deprecated.Version.getAsString();
1126 return;
1127 }
1128
1129 d->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context,
1130 Platform,
1131 Introduced.Version,
1132 Deprecated.Version,
Douglas Gregorb53e4172011-03-26 03:35:55 +00001133 Obsoleted.Version,
1134 IsUnavailable));
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001135}
1136
Chris Lattner803d0802008-06-29 00:43:07 +00001137static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001138 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001139 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001140 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001141 return;
1142 }
Mike Stumpbf916502009-07-24 19:02:52 +00001143
Peter Collingbourne7a730022010-11-23 20:45:58 +00001144 Expr *Arg = Attr.getArg(0);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001145 Arg = Arg->IgnoreParenCasts();
1146 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpbf916502009-07-24 19:02:52 +00001147
Chris Lattner6b6b5372008-06-26 18:38:35 +00001148 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001149 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001150 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001151 return;
1152 }
Mike Stumpbf916502009-07-24 19:02:52 +00001153
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001154 llvm::StringRef TypeStr = Str->getString();
Sean Huntcf807c42010-08-18 23:23:40 +00001155 VisibilityAttr::VisibilityType type;
Mike Stumpbf916502009-07-24 19:02:52 +00001156
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001157 if (TypeStr == "default")
Sean Huntcf807c42010-08-18 23:23:40 +00001158 type = VisibilityAttr::Default;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001159 else if (TypeStr == "hidden")
Sean Huntcf807c42010-08-18 23:23:40 +00001160 type = VisibilityAttr::Hidden;
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001161 else if (TypeStr == "internal")
Sean Huntcf807c42010-08-18 23:23:40 +00001162 type = VisibilityAttr::Hidden; // FIXME
Benjamin Kramerc96f4942010-01-23 18:16:35 +00001163 else if (TypeStr == "protected")
Sean Huntcf807c42010-08-18 23:23:40 +00001164 type = VisibilityAttr::Protected;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001165 else {
Chris Lattner08631c52008-11-23 21:45:46 +00001166 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001167 return;
1168 }
Mike Stumpbf916502009-07-24 19:02:52 +00001169
Sean Huntcf807c42010-08-18 23:23:40 +00001170 d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001171}
1172
John McCalld5313b02011-03-02 11:33:24 +00001173static void HandleObjCMethodFamilyAttr(Decl *decl, const AttributeList &attr,
1174 Sema &S) {
1175 ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl);
1176 if (!method) {
1177 S.Diag(attr.getLoc(), diag::err_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001178 << ExpectedMethod;
John McCalld5313b02011-03-02 11:33:24 +00001179 return;
1180 }
1181
1182 if (attr.getNumArgs() != 0 || !attr.getParameterName()) {
1183 if (!attr.getParameterName() && attr.getNumArgs() == 1) {
1184 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
1185 << "objc_method_family" << 1;
1186 } else {
1187 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1188 }
1189 attr.setInvalid();
1190 return;
1191 }
1192
1193 llvm::StringRef param = attr.getParameterName()->getName();
1194 ObjCMethodFamilyAttr::FamilyKind family;
1195 if (param == "none")
1196 family = ObjCMethodFamilyAttr::OMF_None;
1197 else if (param == "alloc")
1198 family = ObjCMethodFamilyAttr::OMF_alloc;
1199 else if (param == "copy")
1200 family = ObjCMethodFamilyAttr::OMF_copy;
1201 else if (param == "init")
1202 family = ObjCMethodFamilyAttr::OMF_init;
1203 else if (param == "mutableCopy")
1204 family = ObjCMethodFamilyAttr::OMF_mutableCopy;
1205 else if (param == "new")
1206 family = ObjCMethodFamilyAttr::OMF_new;
1207 else {
1208 // Just warn and ignore it. This is future-proof against new
1209 // families being used in system headers.
1210 S.Diag(attr.getParameterLoc(), diag::warn_unknown_method_family);
1211 return;
1212 }
1213
1214 decl->addAttr(new (S.Context) ObjCMethodFamilyAttr(attr.getLoc(),
1215 S.Context, family));
1216}
1217
Chris Lattner0db29ec2009-02-14 08:09:34 +00001218static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
1219 Sema &S) {
1220 if (Attr.getNumArgs() != 0) {
1221 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1222 return;
1223 }
Mike Stumpbf916502009-07-24 19:02:52 +00001224
Chris Lattner0db29ec2009-02-14 08:09:34 +00001225 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
1226 if (OCI == 0) {
1227 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
1228 return;
1229 }
Mike Stumpbf916502009-07-24 19:02:52 +00001230
Sean Huntcf807c42010-08-18 23:23:40 +00001231 D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context));
Chris Lattner0db29ec2009-02-14 08:09:34 +00001232}
1233
1234static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001235 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001236 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001237 return;
1238 }
Richard Smith162e1c12011-04-15 14:24:37 +00001239 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001240 QualType T = TD->getUnderlyingType();
1241 if (!T->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001242 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001243 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
1244 return;
1245 }
1246 }
Sean Huntcf807c42010-08-18 23:23:40 +00001247 D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context));
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001248}
1249
Mike Stumpbf916502009-07-24 19:02:52 +00001250static void
Douglas Gregorf9201e02009-02-11 23:02:49 +00001251HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1252 if (Attr.getNumArgs() != 0) {
John McCall2b7baf02010-05-28 18:25:28 +00001253 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001254 return;
1255 }
1256
1257 if (!isa<FunctionDecl>(D)) {
1258 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
1259 return;
1260 }
1261
Sean Huntcf807c42010-08-18 23:23:40 +00001262 D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context));
Douglas Gregorf9201e02009-02-11 23:02:49 +00001263}
1264
Steve Naroff9eae5762008-09-18 16:44:58 +00001265static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001266 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001267 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001268 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001269 return;
1270 }
Mike Stumpbf916502009-07-24 19:02:52 +00001271
Steve Naroff9eae5762008-09-18 16:44:58 +00001272 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001273 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +00001274 return;
1275 }
Mike Stumpbf916502009-07-24 19:02:52 +00001276
Sean Huntcf807c42010-08-18 23:23:40 +00001277 BlocksAttr::BlockType type;
Chris Lattner92e62b02008-11-20 04:42:34 +00001278 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +00001279 type = BlocksAttr::ByRef;
1280 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001281 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +00001282 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +00001283 return;
1284 }
Mike Stumpbf916502009-07-24 19:02:52 +00001285
Sean Huntcf807c42010-08-18 23:23:40 +00001286 d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type));
Steve Naroff9eae5762008-09-18 16:44:58 +00001287}
1288
Anders Carlsson77091822008-10-05 18:05:59 +00001289static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1290 // check the attribute arguments.
1291 if (Attr.getNumArgs() > 2) {
John McCallbdc49d32011-03-02 12:15:05 +00001292 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Anders Carlsson77091822008-10-05 18:05:59 +00001293 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001294 }
1295
Anders Carlsson77091822008-10-05 18:05:59 +00001296 int sentinel = 0;
1297 if (Attr.getNumArgs() > 0) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001298 Expr *E = Attr.getArg(0);
Anders Carlsson77091822008-10-05 18:05:59 +00001299 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001300 if (E->isTypeDependent() || E->isValueDependent() ||
1301 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001302 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001303 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001304 return;
1305 }
1306 sentinel = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001307
Anders Carlsson77091822008-10-05 18:05:59 +00001308 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001309 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
1310 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001311 return;
1312 }
1313 }
1314
1315 int nullPos = 0;
1316 if (Attr.getNumArgs() > 1) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001317 Expr *E = Attr.getArg(1);
Anders Carlsson77091822008-10-05 18:05:59 +00001318 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001319 if (E->isTypeDependent() || E->isValueDependent() ||
1320 !E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001321 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001322 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001323 return;
1324 }
1325 nullPos = Idx.getZExtValue();
Mike Stumpbf916502009-07-24 19:02:52 +00001326
Anders Carlsson77091822008-10-05 18:05:59 +00001327 if (nullPos > 1 || nullPos < 0) {
1328 // FIXME: This error message could be improved, it would be nice
1329 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001330 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
1331 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +00001332 return;
1333 }
1334 }
1335
1336 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall183700f2009-09-21 23:43:11 +00001337 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner897cd902009-03-17 23:03:47 +00001338 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpbf916502009-07-24 19:02:52 +00001339
Chris Lattner897cd902009-03-17 23:03:47 +00001340 if (isa<FunctionNoProtoType>(FT)) {
1341 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
1342 return;
1343 }
Mike Stumpbf916502009-07-24 19:02:52 +00001344
Chris Lattner897cd902009-03-17 23:03:47 +00001345 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001346 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001347 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001348 }
Anders Carlsson77091822008-10-05 18:05:59 +00001349 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
1350 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001351 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +00001352 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001353 }
1354 } else if (isa<BlockDecl>(d)) {
Mike Stumpbf916502009-07-24 19:02:52 +00001355 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
1356 // caller.
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001357 ;
1358 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001359 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +00001360 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001361 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
Eric Christopherf48f3672010-12-01 22:13:54 +00001362 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001363 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +00001364 int m = Ty->isFunctionPointerType() ? 0 : 1;
1365 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001366 return;
1367 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001368 } else {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001369 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001370 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +00001371 return;
1372 }
Anders Carlsson77091822008-10-05 18:05:59 +00001373 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001374 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001375 << Attr.getName() << ExpectedFunctionMethodOrBlock;
Anders Carlsson77091822008-10-05 18:05:59 +00001376 return;
1377 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001378 d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel,
1379 nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +00001380}
1381
Chris Lattner026dc962009-02-14 07:37:35 +00001382static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
1383 // check the attribute arguments.
1384 if (Attr.getNumArgs() != 0) {
1385 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1386 return;
1387 }
1388
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001389 if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) {
Chris Lattner026dc962009-02-14 07:37:35 +00001390 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001391 << Attr.getName() << ExpectedFunctionOrMethod;
Chris Lattner026dc962009-02-14 07:37:35 +00001392 return;
1393 }
Mike Stumpbf916502009-07-24 19:02:52 +00001394
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001395 if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) {
1396 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1397 << Attr.getName() << 0;
Nuno Lopesf8577982009-12-22 23:59:52 +00001398 return;
1399 }
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001400 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
1401 if (MD->getResultType()->isVoidType()) {
1402 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method)
1403 << Attr.getName() << 1;
1404 return;
1405 }
1406
Sean Huntcf807c42010-08-18 23:23:40 +00001407 D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
Chris Lattner026dc962009-02-14 07:37:35 +00001408}
1409
John McCall332bb2a2011-02-08 22:35:49 +00001410static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001411 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001412 if (attr.hasParameterOrArguments()) {
John McCall332bb2a2011-02-08 22:35:49 +00001413 S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001414 return;
1415 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001416
John McCall332bb2a2011-02-08 22:35:49 +00001417 if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
1418 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001419 << attr.getName() << ExpectedVariableOrFunction;
Fariborz Jahanianf23ecd92009-07-16 01:12:24 +00001420 return;
1421 }
1422
John McCall332bb2a2011-02-08 22:35:49 +00001423 NamedDecl *nd = cast<NamedDecl>(d);
1424
1425 // 'weak' only applies to declarations with external linkage.
1426 if (hasEffectivelyInternalLinkage(nd)) {
1427 S.Diag(attr.getLoc(), diag::err_attribute_weak_static);
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001428 return;
1429 }
Mike Stumpbf916502009-07-24 19:02:52 +00001430
John McCall332bb2a2011-02-08 22:35:49 +00001431 nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001432}
1433
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001434static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
1435 // check the attribute arguments.
1436 if (Attr.getNumArgs() != 0) {
1437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1438 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001439 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001440
1441 // weak_import only applies to variable & function declarations.
1442 bool isDef = false;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001443 if (!D->canBeWeakImported(isDef)) {
1444 if (isDef)
1445 S.Diag(Attr.getLoc(),
1446 diag::warn_attribute_weak_import_invalid_on_definition)
1447 << "weak_import" << 2 /*variable and function*/;
Douglas Gregordef86312011-03-23 13:27:51 +00001448 else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) ||
Daniel Dunbardb57a4c2011-04-19 21:43:27 +00001449 (S.Context.Target.getTriple().isOSDarwin() &&
Douglas Gregordef86312011-03-23 13:27:51 +00001450 isa<ObjCInterfaceDecl>(D))) {
1451 // Nothing to warn about here.
1452 } else
Fariborz Jahanianc0349742010-04-13 20:22:35 +00001453 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001454 << Attr.getName() << ExpectedVariableOrFunction;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001455
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001456 return;
1457 }
1458
Sean Huntcf807c42010-08-18 23:23:40 +00001459 D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context));
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001460}
1461
Nate Begeman6f3d8382009-06-26 06:32:41 +00001462static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
1463 Sema &S) {
1464 // Attribute has 3 arguments.
1465 if (Attr.getNumArgs() != 3) {
John McCall2b7baf02010-05-28 18:25:28 +00001466 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Nate Begeman6f3d8382009-06-26 06:32:41 +00001467 return;
1468 }
1469
1470 unsigned WGSize[3];
1471 for (unsigned i = 0; i < 3; ++i) {
Peter Collingbourne7a730022010-11-23 20:45:58 +00001472 Expr *E = Attr.getArg(i);
Nate Begeman6f3d8382009-06-26 06:32:41 +00001473 llvm::APSInt ArgNum(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001474 if (E->isTypeDependent() || E->isValueDependent() ||
1475 !E->isIntegerConstantExpr(ArgNum, S.Context)) {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001476 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1477 << "reqd_work_group_size" << E->getSourceRange();
1478 return;
1479 }
1480 WGSize[i] = (unsigned) ArgNum.getZExtValue();
1481 }
Sean Huntcf807c42010-08-18 23:23:40 +00001482 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context,
1483 WGSize[0], WGSize[1],
Nate Begeman6f3d8382009-06-26 06:32:41 +00001484 WGSize[2]));
1485}
1486
Chris Lattner026dc962009-02-14 07:37:35 +00001487static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001488 // Attribute has no arguments.
1489 if (Attr.getNumArgs() != 1) {
1490 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1491 return;
1492 }
1493
1494 // Make sure that there is a string literal as the sections's single
1495 // argument.
Peter Collingbourne7a730022010-11-23 20:45:58 +00001496 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001497 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001498 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001499 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001500 return;
1501 }
Mike Stump1eb44332009-09-09 15:08:12 +00001502
Chris Lattner797c3c42009-08-10 19:03:04 +00001503 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramerbb377ed2009-11-30 17:08:26 +00001504 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001505 if (!Error.empty()) {
1506 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
1507 << Error;
Chris Lattner797c3c42009-08-10 19:03:04 +00001508 return;
1509 }
Mike Stump1eb44332009-09-09 15:08:12 +00001510
Chris Lattnera1e1dc72010-01-12 20:58:53 +00001511 // This attribute cannot be applied to local variables.
1512 if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) {
1513 S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable);
1514 return;
1515 }
1516
Eric Christopherf48f3672010-12-01 22:13:54 +00001517 D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context,
1518 SE->getString()));
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001519}
1520
Chris Lattner6b6b5372008-06-26 18:38:35 +00001521
Chris Lattner803d0802008-06-29 00:43:07 +00001522static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001523 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001524 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001525 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001526 return;
1527 }
Mike Stumpbf916502009-07-24 19:02:52 +00001528
Sean Huntcf807c42010-08-18 23:23:40 +00001529 d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001530}
1531
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001532static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1533 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00001534 if (Attr.hasParameterOrArguments()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001535 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001536 return;
1537 }
Mike Stumpbf916502009-07-24 19:02:52 +00001538
Sean Huntcf807c42010-08-18 23:23:40 +00001539 d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001540}
1541
1542static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1543 // check the attribute arguments.
1544 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001545 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001546 return;
1547 }
Mike Stumpbf916502009-07-24 19:02:52 +00001548
Sean Huntcf807c42010-08-18 23:23:40 +00001549 d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context));
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001550}
1551
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001552static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpbf916502009-07-24 19:02:52 +00001553 if (!Attr.getParameterName()) {
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001554 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1555 return;
1556 }
Mike Stumpbf916502009-07-24 19:02:52 +00001557
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001558 if (Attr.getNumArgs() != 0) {
1559 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1560 return;
1561 }
Mike Stumpbf916502009-07-24 19:02:52 +00001562
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001563 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpbf916502009-07-24 19:02:52 +00001564
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001565 if (!VD || !VD->hasLocalStorage()) {
1566 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1567 return;
1568 }
Mike Stumpbf916502009-07-24 19:02:52 +00001569
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001570 // Look up the function
Douglas Gregorc83c6872010-04-15 22:33:43 +00001571 // FIXME: Lookup probably isn't looking in the right place
John McCallf36e02d2009-10-09 21:13:30 +00001572 NamedDecl *CleanupDecl
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001573 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1574 Attr.getParameterLoc(), Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001575 if (!CleanupDecl) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001576 S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001577 Attr.getParameterName();
1578 return;
1579 }
Mike Stumpbf916502009-07-24 19:02:52 +00001580
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001581 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1582 if (!FD) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001583 S.Diag(Attr.getParameterLoc(),
1584 diag::err_attribute_cleanup_arg_not_function)
1585 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001586 return;
1587 }
1588
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001589 if (FD->getNumParams() != 1) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001590 S.Diag(Attr.getParameterLoc(),
1591 diag::err_attribute_cleanup_func_must_take_one_arg)
1592 << Attr.getParameterName();
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001593 return;
1594 }
Mike Stumpbf916502009-07-24 19:02:52 +00001595
Anders Carlsson89941c12009-02-07 23:16:50 +00001596 // We're currently more strict than GCC about what function types we accept.
1597 // If this ever proves to be a problem it should be easy to fix.
1598 QualType Ty = S.Context.getPointerType(VD->getType());
1599 QualType ParamTy = FD->getParamDecl(0)->getType();
Douglas Gregorb608b982011-01-28 02:26:04 +00001600 if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(),
1601 ParamTy, Ty) != Sema::Compatible) {
Argyrios Kyrtzidisf0b0ccc2010-12-06 17:51:50 +00001602 S.Diag(Attr.getParameterLoc(),
Anders Carlsson89941c12009-02-07 23:16:50 +00001603 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1604 Attr.getParameterName() << ParamTy << Ty;
1605 return;
1606 }
Mike Stumpbf916502009-07-24 19:02:52 +00001607
Sean Huntcf807c42010-08-18 23:23:40 +00001608 d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD));
Argyrios Kyrtzidis223ae5c2010-12-06 17:51:53 +00001609 S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001610}
1611
Mike Stumpbf916502009-07-24 19:02:52 +00001612/// Handle __attribute__((format_arg((idx)))) attribute based on
1613/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1614static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001615 if (Attr.getNumArgs() != 1) {
1616 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1617 return;
1618 }
1619 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1620 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001621 << Attr.getName() << ExpectedFunction;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001622 return;
1623 }
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001624
1625 // In C++ the implicit 'this' function parameter also counts, and they are
1626 // counted from one.
1627 bool HasImplicitThisParam = isInstanceMethod(d);
1628 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001629 unsigned FirstIdx = 1;
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001630
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001631 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001632 Expr *IdxExpr = Attr.getArg(0);
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001633 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001634 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1635 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001636 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1637 << "format" << 2 << IdxExpr->getSourceRange();
1638 return;
1639 }
Mike Stumpbf916502009-07-24 19:02:52 +00001640
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001641 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1642 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1643 << "format" << 2 << IdxExpr->getSourceRange();
1644 return;
1645 }
Mike Stumpbf916502009-07-24 19:02:52 +00001646
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001647 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001648
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001649 if (HasImplicitThisParam) {
1650 if (ArgIdx == 0) {
1651 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument)
1652 << "format_arg" << IdxExpr->getSourceRange();
1653 return;
1654 }
1655 ArgIdx--;
1656 }
1657
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001658 // make sure the format string is really a string
1659 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpbf916502009-07-24 19:02:52 +00001660
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001661 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1662 if (not_nsstring_type &&
1663 !isCFStringType(Ty, S.Context) &&
1664 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001665 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001666 // FIXME: Should highlight the actual expression that has the wrong type.
1667 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001668 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001669 << IdxExpr->getSourceRange();
1670 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001671 }
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001672 Ty = getFunctionOrMethodResultType(d);
1673 if (!isNSStringType(Ty, S.Context) &&
1674 !isCFStringType(Ty, S.Context) &&
1675 (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001676 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001677 // FIXME: Should highlight the actual expression that has the wrong type.
1678 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpbf916502009-07-24 19:02:52 +00001679 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001680 << IdxExpr->getSourceRange();
1681 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001682 }
1683
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001684 d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context,
1685 Idx.getZExtValue()));
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001686}
1687
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001688enum FormatAttrKind {
1689 CFStringFormat,
1690 NSStringFormat,
1691 StrftimeFormat,
1692 SupportedFormat,
Chris Lattner3c989022010-03-22 21:08:50 +00001693 IgnoredFormat,
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001694 InvalidFormat
1695};
1696
1697/// getFormatAttrKind - Map from format attribute names to supported format
1698/// types.
1699static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1700 // Check for formats that get handled specially.
1701 if (Format == "NSString")
1702 return NSStringFormat;
1703 if (Format == "CFString")
1704 return CFStringFormat;
1705 if (Format == "strftime")
1706 return StrftimeFormat;
1707
1708 // Otherwise, check for supported formats.
1709 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1710 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1711 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
Chris Lattnercd5b3062011-02-18 17:05:55 +00001712 Format == "zcmn_err" ||
1713 Format == "kprintf") // OpenBSD.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001714 return SupportedFormat;
1715
Duncan Sandsbc525952010-03-23 14:44:19 +00001716 if (Format == "gcc_diag" || Format == "gcc_cdiag" ||
1717 Format == "gcc_cxxdiag" || Format == "gcc_tdiag")
Chris Lattner3c989022010-03-22 21:08:50 +00001718 return IgnoredFormat;
1719
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001720 return InvalidFormat;
1721}
1722
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001723/// Handle __attribute__((init_priority(priority))) attributes based on
1724/// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html
1725static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr,
1726 Sema &S) {
1727 if (!S.getLangOptions().CPlusPlus) {
1728 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
1729 return;
1730 }
1731
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001732 if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) {
1733 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1734 Attr.setInvalid();
1735 return;
1736 }
1737 QualType T = dyn_cast<VarDecl>(d)->getType();
1738 if (S.Context.getAsArrayType(T))
1739 T = S.Context.getBaseElementType(T);
1740 if (!T->getAs<RecordType>()) {
1741 S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr);
1742 Attr.setInvalid();
1743 return;
1744 }
1745
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001746 if (Attr.getNumArgs() != 1) {
1747 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1748 Attr.setInvalid();
1749 return;
1750 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001751 Expr *priorityExpr = Attr.getArg(0);
Fariborz Jahanianb9d5c222010-06-18 23:14:53 +00001752
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001753 llvm::APSInt priority(32);
1754 if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() ||
1755 !priorityExpr->isIntegerConstantExpr(priority, S.Context)) {
1756 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1757 << "init_priority" << priorityExpr->getSourceRange();
1758 Attr.setInvalid();
1759 return;
1760 }
Fariborz Jahanian9f967c52010-06-21 18:45:05 +00001761 unsigned prioritynum = priority.getZExtValue();
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001762 if (prioritynum < 101 || prioritynum > 65535) {
1763 S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range)
1764 << priorityExpr->getSourceRange();
1765 Attr.setInvalid();
1766 return;
1767 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001768 d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context,
1769 prioritynum));
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00001770}
1771
Mike Stumpbf916502009-07-24 19:02:52 +00001772/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1773/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001774static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001775
Chris Lattner545dd342008-06-28 23:36:30 +00001776 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001777 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001778 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001779 return;
1780 }
1781
Chris Lattner545dd342008-06-28 23:36:30 +00001782 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001783 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001784 return;
1785 }
1786
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001787 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001788 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001789 << Attr.getName() << ExpectedFunction;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001790 return;
1791 }
1792
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001793 // In C++ the implicit 'this' function parameter also counts, and they are
1794 // counted from one.
1795 bool HasImplicitThisParam = isInstanceMethod(d);
1796 unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001797 unsigned FirstIdx = 1;
1798
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001799 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001800
1801 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001802 if (Format.startswith("__") && Format.endswith("__"))
1803 Format = Format.substr(2, Format.size() - 4);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001804
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001805 // Check for supported formats.
1806 FormatAttrKind Kind = getFormatAttrKind(Format);
Chris Lattner3c989022010-03-22 21:08:50 +00001807
1808 if (Kind == IgnoredFormat)
1809 return;
1810
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001811 if (Kind == InvalidFormat) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001812 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00001813 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001814 return;
1815 }
1816
1817 // checks for the 2nd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001818 Expr *IdxExpr = Attr.getArg(0);
Chris Lattner803d0802008-06-29 00:43:07 +00001819 llvm::APSInt Idx(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001820 if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() ||
1821 !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001822 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001823 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001824 return;
1825 }
1826
1827 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001828 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001829 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001830 return;
1831 }
1832
1833 // FIXME: Do we need to bounds check?
1834 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpbf916502009-07-24 19:02:52 +00001835
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001836 if (HasImplicitThisParam) {
1837 if (ArgIdx == 0) {
Chandler Carruth07d7e7a2010-11-16 08:35:43 +00001838 S.Diag(Attr.getLoc(),
1839 diag::err_format_attribute_implicit_this_format_string)
1840 << IdxExpr->getSourceRange();
Sebastian Redl4a2614e2009-11-17 18:02:24 +00001841 return;
1842 }
1843 ArgIdx--;
1844 }
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Chris Lattner6b6b5372008-06-26 18:38:35 +00001846 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001847 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001848
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001849 if (Kind == CFStringFormat) {
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001850 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001851 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1852 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001853 return;
1854 }
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001855 } else if (Kind == NSStringFormat) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001856 // FIXME: do we need to check if the type is NSString*? What are the
1857 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001858 if (!isNSStringType(Ty, S.Context)) {
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 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001862 return;
Mike Stumpbf916502009-07-24 19:02:52 +00001863 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001864 } else if (!Ty->isPointerType() ||
Ted Kremenek6217b802009-07-29 21:53:49 +00001865 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001866 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001867 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1868 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001869 return;
1870 }
1871
1872 // check the 3rd argument
Peter Collingbourne7a730022010-11-23 20:45:58 +00001873 Expr *FirstArgExpr = Attr.getArg(1);
Chris Lattner803d0802008-06-29 00:43:07 +00001874 llvm::APSInt FirstArg(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00001875 if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() ||
1876 !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001877 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001878 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001879 return;
1880 }
1881
1882 // check if the function is variadic if the 3rd argument non-zero
1883 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001884 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001885 ++NumArgs; // +1 for ...
1886 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001887 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001888 return;
1889 }
1890 }
1891
Chris Lattner3c73c412008-11-19 08:23:25 +00001892 // strftime requires FirstArg to be 0 because it doesn't read from any
1893 // variable the input is just the current time + the format string.
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001894 if (Kind == StrftimeFormat) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001895 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001896 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1897 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001898 return;
1899 }
1900 // if 0 it disables parameter checking (to use with e.g. va_list)
1901 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001902 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001903 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001904 return;
1905 }
1906
Sean Huntcf807c42010-08-18 23:23:40 +00001907 d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format,
1908 Idx.getZExtValue(),
Daniel Dunbar2b0d9a22009-10-18 02:09:17 +00001909 FirstArg.getZExtValue()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001910}
1911
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001912static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1913 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001914 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001915 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001916 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001917 return;
1918 }
1919
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001920 // Try to find the underlying union declaration.
1921 RecordDecl *RD = 0;
Richard Smith162e1c12011-04-15 14:24:37 +00001922 TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001923 if (TD && TD->getUnderlyingType()->isUnionType())
1924 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1925 else
1926 RD = dyn_cast<RecordDecl>(d);
1927
1928 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001929 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00001930 << Attr.getName() << ExpectedUnion;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001931 return;
1932 }
1933
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001934 if (!RD->isDefinition()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001935 S.Diag(Attr.getLoc(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001936 diag::warn_transparent_union_attribute_not_definition);
1937 return;
1938 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001939
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001940 RecordDecl::field_iterator Field = RD->field_begin(),
1941 FieldEnd = RD->field_end();
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001942 if (Field == FieldEnd) {
1943 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1944 return;
1945 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001946
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001947 FieldDecl *FirstField = *Field;
1948 QualType FirstType = FirstField->getType();
Douglas Gregor90cd6722010-06-30 17:24:13 +00001949 if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) {
Mike Stumpbf916502009-07-24 19:02:52 +00001950 S.Diag(FirstField->getLocation(),
Douglas Gregor90cd6722010-06-30 17:24:13 +00001951 diag::warn_transparent_union_attribute_floating)
1952 << FirstType->isVectorType() << FirstType;
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001953 return;
1954 }
1955
1956 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1957 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1958 for (; Field != FieldEnd; ++Field) {
1959 QualType FieldType = Field->getType();
1960 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1961 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1962 // Warn if we drop the attribute.
1963 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpbf916502009-07-24 19:02:52 +00001964 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001965 : S.Context.getTypeAlign(FieldType);
Mike Stumpbf916502009-07-24 19:02:52 +00001966 S.Diag(Field->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001967 diag::warn_transparent_union_attribute_field_size_align)
1968 << isSize << Field->getDeclName() << FieldBits;
1969 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpbf916502009-07-24 19:02:52 +00001970 S.Diag(FirstField->getLocation(),
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001971 diag::note_transparent_union_first_field_size_align)
1972 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001973 return;
1974 }
1975 }
1976
Sean Huntcf807c42010-08-18 23:23:40 +00001977 RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001978}
1979
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001980static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001981 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001982 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001983 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001984 return;
1985 }
Peter Collingbourne7a730022010-11-23 20:45:58 +00001986 Expr *ArgExpr = Attr.getArg(0);
Chris Lattner797c3c42009-08-10 19:03:04 +00001987 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpbf916502009-07-24 19:02:52 +00001988
Chris Lattner6b6b5372008-06-26 18:38:35 +00001989 // Make sure that there is a string literal as the annotation's single
1990 // argument.
1991 if (!SE) {
Chris Lattner797c3c42009-08-10 19:03:04 +00001992 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner6b6b5372008-06-26 18:38:35 +00001993 return;
1994 }
Eric Christopherf48f3672010-12-01 22:13:54 +00001995 d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context,
1996 SE->getString()));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001997}
1998
Chandler Carruth4ced79f2010-06-25 03:22:07 +00001999static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00002000 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00002001 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002002 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002003 return;
2004 }
Sean Huntbbd37c62009-11-21 08:43:09 +00002005
2006 //FIXME: The C++0x version of this attribute has more limited applicabilty
2007 // than GNU's, and should error out when it is used to specify a
2008 // weaker alignment, rather than being silently ignored.
Chris Lattner6b6b5372008-06-26 18:38:35 +00002009
Chris Lattner545dd342008-06-28 23:36:30 +00002010 if (Attr.getNumArgs() == 0) {
Sean Huntcf807c42010-08-18 23:23:40 +00002011 D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00002012 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002013 }
Mike Stumpbf916502009-07-24 19:02:52 +00002014
Peter Collingbourne7a730022010-11-23 20:45:58 +00002015 S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002016}
2017
2018void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) {
2019 if (E->isTypeDependent() || E->isValueDependent()) {
2020 // Save dependent expressions in the AST to be instantiated.
Sean Huntcf807c42010-08-18 23:23:40 +00002021 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002022 return;
2023 }
2024
Sean Huntcf807c42010-08-18 23:23:40 +00002025 // FIXME: Cache the number on the Attr object?
Chris Lattner49e2d342008-06-28 23:50:44 +00002026 llvm::APSInt Alignment(32);
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002027 if (!E->isIntegerConstantExpr(Alignment, Context)) {
2028 Diag(AttrLoc, diag::err_attribute_argument_not_int)
2029 << "aligned" << E->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00002030 return;
2031 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002032 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Chandler Carruth4ced79f2010-06-25 03:22:07 +00002033 Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two)
2034 << E->getSourceRange();
Daniel Dunbar396b2a22009-02-16 23:37:57 +00002035 return;
2036 }
2037
Sean Huntcf807c42010-08-18 23:23:40 +00002038 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E));
2039}
2040
2041void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) {
2042 // FIXME: Cache the number on the Attr object if non-dependent?
2043 // FIXME: Perform checking of type validity
2044 D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS));
2045 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00002046}
Chris Lattnerfbf13472008-06-27 22:18:37 +00002047
Mike Stumpbf916502009-07-24 19:02:52 +00002048/// HandleModeAttr - This attribute modifies the width of a decl with primitive
2049/// type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00002050///
Mike Stumpbf916502009-07-24 19:02:52 +00002051/// Despite what would be logical, the mode attribute is a decl attribute, not a
2052/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
2053/// HImode, not an intermediate pointer.
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002054static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002055 // This attribute isn't documented, but glibc uses it. It changes
2056 // the width of an int or unsigned int to the specified size.
2057
2058 // Check that there aren't any arguments
2059 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00002060 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002061 return;
2062 }
2063
2064 IdentifierInfo *Name = Attr.getParameterName();
2065 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002066 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002067 return;
2068 }
Daniel Dunbar210ae982009-10-18 02:09:24 +00002069
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002070 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002071
2072 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002073 if (Str.startswith("__") && Str.endswith("__"))
2074 Str = Str.substr(2, Str.size() - 4);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002075
2076 unsigned DestWidth = 0;
2077 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00002078 bool ComplexMode = false;
Daniel Dunbar210ae982009-10-18 02:09:24 +00002079 switch (Str.size()) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00002080 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00002081 switch (Str[0]) {
2082 case 'Q': DestWidth = 8; break;
2083 case 'H': DestWidth = 16; break;
2084 case 'S': DestWidth = 32; break;
2085 case 'D': DestWidth = 64; break;
2086 case 'X': DestWidth = 96; break;
2087 case 'T': DestWidth = 128; break;
2088 }
2089 if (Str[1] == 'F') {
2090 IntegerMode = false;
2091 } else if (Str[1] == 'C') {
2092 IntegerMode = false;
2093 ComplexMode = true;
2094 } else if (Str[1] != 'I') {
2095 DestWidth = 0;
2096 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002097 break;
2098 case 4:
2099 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
2100 // pointer on PIC16 and other embedded platforms.
Daniel Dunbar210ae982009-10-18 02:09:24 +00002101 if (Str == "word")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002102 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbar210ae982009-10-18 02:09:24 +00002103 else if (Str == "byte")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002104 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00002105 break;
2106 case 7:
Daniel Dunbar210ae982009-10-18 02:09:24 +00002107 if (Str == "pointer")
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002108 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002109 break;
2110 }
2111
2112 QualType OldTy;
Richard Smith162e1c12011-04-15 14:24:37 +00002113 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D))
Chris Lattnerfbf13472008-06-27 22:18:37 +00002114 OldTy = TD->getUnderlyingType();
2115 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
2116 OldTy = VD->getType();
2117 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00002118 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
2119 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00002120 return;
2121 }
Eli Friedman73397492009-03-03 06:41:03 +00002122
John McCall183700f2009-09-21 23:43:11 +00002123 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman73397492009-03-03 06:41:03 +00002124 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
2125 else if (IntegerMode) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002126 if (!OldTy->isIntegralOrEnumerationType())
Eli Friedman73397492009-03-03 06:41:03 +00002127 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2128 } else if (ComplexMode) {
2129 if (!OldTy->isComplexType())
2130 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2131 } else {
2132 if (!OldTy->isFloatingType())
2133 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
2134 }
2135
Mike Stump390b4cc2009-05-16 07:39:55 +00002136 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
2137 // and friends, at least with glibc.
2138 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
2139 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002140 // FIXME: Make sure floating-point mappings are accurate
2141 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00002142 QualType NewTy;
2143 switch (DestWidth) {
2144 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00002145 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002146 return;
2147 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00002148 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002149 return;
2150 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00002151 if (!IntegerMode) {
2152 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2153 return;
2154 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002155 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002156 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002157 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002158 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002159 break;
2160 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00002161 if (!IntegerMode) {
2162 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2163 return;
2164 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00002165 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002166 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002167 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002168 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002169 break;
2170 case 32:
2171 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002172 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002173 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002174 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002175 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002176 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002177 break;
2178 case 64:
2179 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00002180 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002181 else if (OldTy->isSignedIntegerType())
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002182 if (S.Context.Target.getLongWidth() == 64)
2183 NewTy = S.Context.LongTy;
2184 else
2185 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002186 else
Chandler Carruthaec7caa2010-01-26 06:39:24 +00002187 if (S.Context.Target.getLongWidth() == 64)
2188 NewTy = S.Context.UnsignedLongTy;
2189 else
2190 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002191 break;
Eli Friedman73397492009-03-03 06:41:03 +00002192 case 96:
2193 NewTy = S.Context.LongDoubleTy;
2194 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002195 case 128:
2196 if (!IntegerMode) {
2197 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
2198 return;
2199 }
Anders Carlssonf5f7d862009-12-29 07:07:36 +00002200 if (OldTy->isSignedIntegerType())
2201 NewTy = S.Context.Int128Ty;
2202 else
2203 NewTy = S.Context.UnsignedInt128Ty;
Eli Friedman73397492009-03-03 06:41:03 +00002204 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00002205 }
2206
Eli Friedman73397492009-03-03 06:41:03 +00002207 if (ComplexMode) {
2208 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00002209 }
2210
2211 // Install the new type.
Richard Smith162e1c12011-04-15 14:24:37 +00002212 if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) {
John McCallba6a9bd2009-10-24 08:00:42 +00002213 // FIXME: preserve existing source info.
John McCalla93c9342009-12-07 02:54:59 +00002214 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCallba6a9bd2009-10-24 08:00:42 +00002215 } else
Chris Lattnerfbf13472008-06-27 22:18:37 +00002216 cast<ValueDecl>(D)->setType(NewTy);
2217}
Chris Lattner0744e5f2008-06-29 00:23:49 +00002218
Mike Stump1feade82009-08-26 22:31:08 +00002219static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlssond87df372009-02-13 06:46:13 +00002220 // check the attribute arguments.
2221 if (Attr.getNumArgs() > 0) {
2222 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2223 return;
2224 }
Anders Carlssone896d982009-02-13 08:11:52 +00002225
Anders Carlsson5bab7882009-02-19 19:16:48 +00002226 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00002227 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002228 << Attr.getName() << ExpectedFunction;
Anders Carlssond87df372009-02-13 06:46:13 +00002229 return;
2230 }
Mike Stumpbf916502009-07-24 19:02:52 +00002231
Sean Huntcf807c42010-08-18 23:23:40 +00002232 d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context));
Anders Carlssond87df372009-02-13 06:46:13 +00002233}
2234
Mike Stump1feade82009-08-26 22:31:08 +00002235static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002236 // check the attribute arguments.
2237 if (Attr.getNumArgs() != 0) {
2238 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2239 return;
2240 }
Mike Stumpbf916502009-07-24 19:02:52 +00002241
Chris Lattnerc5197432009-04-14 17:02:11 +00002242 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00002243 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002244 << Attr.getName() << ExpectedFunction;
Anders Carlsson5bab7882009-02-19 19:16:48 +00002245 return;
2246 }
Mike Stumpbf916502009-07-24 19:02:52 +00002247
Sean Huntcf807c42010-08-18 23:23:40 +00002248 d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context));
Anders Carlsson5bab7882009-02-19 19:16:48 +00002249}
2250
Chris Lattner7255a2d2010-06-22 00:03:40 +00002251static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr,
2252 Sema &S) {
2253 // check the attribute arguments.
2254 if (Attr.getNumArgs() != 0) {
2255 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2256 return;
2257 }
2258
2259 if (!isa<FunctionDecl>(d)) {
2260 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002261 << Attr.getName() << ExpectedFunction;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002262 return;
2263 }
2264
Eric Christopherf48f3672010-12-01 22:13:54 +00002265 d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(),
2266 S.Context));
Chris Lattner7255a2d2010-06-22 00:03:40 +00002267}
2268
Peter Collingbourneced76712010-12-01 03:15:31 +00002269static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2270 if (S.LangOpts.CUDA) {
2271 // check the attribute arguments.
Ted Kremenek831efae2011-04-15 05:49:29 +00002272 if (Attr.hasParameterOrArguments()) {
Peter Collingbourneced76712010-12-01 03:15:31 +00002273 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2274 return;
2275 }
2276
2277 if (!isa<VarDecl>(d)) {
2278 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002279 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002280 return;
2281 }
2282
2283 d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context));
2284 } else {
2285 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant";
2286 }
2287}
2288
2289static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2290 if (S.LangOpts.CUDA) {
2291 // check the attribute arguments.
2292 if (Attr.getNumArgs() != 0) {
2293 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2294 return;
2295 }
2296
2297 if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) {
2298 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002299 << Attr.getName() << ExpectedVariableOrFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002300 return;
2301 }
2302
2303 d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context));
2304 } else {
2305 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device";
2306 }
2307}
2308
2309static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2310 if (S.LangOpts.CUDA) {
2311 // check the attribute arguments.
2312 if (Attr.getNumArgs() != 0) {
2313 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2314 return;
2315 }
2316
2317 if (!isa<FunctionDecl>(d)) {
2318 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002319 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002320 return;
2321 }
2322
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002323 FunctionDecl *FD = cast<FunctionDecl>(d);
2324 if (!FD->getResultType()->isVoidType()) {
Abramo Bagnara723df242010-12-14 22:11:44 +00002325 TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens();
Peter Collingbourne2c2c8dd2010-12-12 23:02:57 +00002326 if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) {
2327 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2328 << FD->getType()
2329 << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(),
2330 "void");
2331 } else {
2332 S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return)
2333 << FD->getType();
2334 }
2335 return;
2336 }
2337
Peter Collingbourneced76712010-12-01 03:15:31 +00002338 d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context));
2339 } else {
2340 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global";
2341 }
2342}
2343
2344static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2345 if (S.LangOpts.CUDA) {
2346 // check the attribute arguments.
2347 if (Attr.getNumArgs() != 0) {
2348 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2349 return;
2350 }
2351
2352 if (!isa<FunctionDecl>(d)) {
2353 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002354 << Attr.getName() << ExpectedFunction;
Peter Collingbourneced76712010-12-01 03:15:31 +00002355 return;
2356 }
2357
2358 d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context));
2359 } else {
2360 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host";
2361 }
2362}
2363
2364static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2365 if (S.LangOpts.CUDA) {
2366 // check the attribute arguments.
2367 if (Attr.getNumArgs() != 0) {
2368 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2369 return;
2370 }
2371
2372 if (!isa<VarDecl>(d)) {
2373 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002374 << Attr.getName() << ExpectedVariable;
Peter Collingbourneced76712010-12-01 03:15:31 +00002375 return;
2376 }
2377
2378 d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context));
2379 } else {
2380 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared";
2381 }
2382}
2383
Chris Lattnercf2a7212009-04-20 19:12:28 +00002384static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00002385 // check the attribute arguments.
2386 if (Attr.getNumArgs() != 0) {
2387 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2388 return;
2389 }
Mike Stumpbf916502009-07-24 19:02:52 +00002390
Chris Lattnerc5197432009-04-14 17:02:11 +00002391 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
2392 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00002393 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002394 << Attr.getName() << ExpectedFunction;
Chris Lattner26e25542009-04-14 16:30:50 +00002395 return;
2396 }
Mike Stumpbf916502009-07-24 19:02:52 +00002397
Douglas Gregor0130f3c2009-10-27 21:01:01 +00002398 if (!Fn->isInlineSpecified()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00002399 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00002400 return;
2401 }
Mike Stumpbf916502009-07-24 19:02:52 +00002402
Sean Huntcf807c42010-08-18 23:23:40 +00002403 d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context));
Chris Lattner26e25542009-04-14 16:30:50 +00002404}
2405
John McCall711c52b2011-01-05 12:14:39 +00002406static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) {
2407 if (hasDeclarator(d)) return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002408
John McCall711c52b2011-01-05 12:14:39 +00002409 // Diagnostic is emitted elsewhere: here we store the (valid) attr
2410 // in the Decl node for syntactic reasoning, e.g., pretty-printing.
2411 CallingConv CC;
2412 if (S.CheckCallingConvAttr(attr, CC))
2413 return;
2414
2415 if (!isa<ObjCMethodDecl>(d)) {
2416 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002417 << attr.getName() << ExpectedFunctionOrMethod;
John McCall711c52b2011-01-05 12:14:39 +00002418 return;
2419 }
2420
2421 switch (attr.getKind()) {
Abramo Bagnarae215f722010-04-30 13:10:51 +00002422 case AttributeList::AT_fastcall:
John McCall711c52b2011-01-05 12:14:39 +00002423 d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002424 return;
2425 case AttributeList::AT_stdcall:
John McCall711c52b2011-01-05 12:14:39 +00002426 d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002427 return;
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002428 case AttributeList::AT_thiscall:
John McCall711c52b2011-01-05 12:14:39 +00002429 d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context));
Douglas Gregor04633eb2010-08-30 23:30:49 +00002430 return;
Abramo Bagnarae215f722010-04-30 13:10:51 +00002431 case AttributeList::AT_cdecl:
John McCall711c52b2011-01-05 12:14:39 +00002432 d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context));
Abramo Bagnarae215f722010-04-30 13:10:51 +00002433 return;
Dawn Perchik52fc3142010-09-03 01:29:35 +00002434 case AttributeList::AT_pascal:
John McCall711c52b2011-01-05 12:14:39 +00002435 d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context));
Dawn Perchik52fc3142010-09-03 01:29:35 +00002436 return;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002437 case AttributeList::AT_pcs: {
2438 Expr *Arg = attr.getArg(0);
2439 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2440 if (Str == 0 || Str->isWide()) {
2441 S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2442 << "pcs" << 1;
2443 attr.setInvalid();
2444 return;
2445 }
2446
2447 llvm::StringRef StrRef = Str->getString();
2448 PcsAttr::PCSType PCS;
2449 if (StrRef == "aapcs")
2450 PCS = PcsAttr::AAPCS;
2451 else if (StrRef == "aapcs-vfp")
2452 PCS = PcsAttr::AAPCS_VFP;
2453 else {
2454 S.Diag(attr.getLoc(), diag::err_invalid_pcs);
2455 attr.setInvalid();
2456 return;
2457 }
2458
2459 d->addAttr(::new (S.Context) PcsAttr(attr.getLoc(), S.Context, PCS));
2460 }
Abramo Bagnarae215f722010-04-30 13:10:51 +00002461 default:
2462 llvm_unreachable("unexpected attribute kind");
2463 return;
2464 }
2465}
2466
Peter Collingbournef315fa82011-02-14 01:42:53 +00002467static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){
2468 assert(Attr.isInvalid() == false);
2469 d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context));
2470}
2471
John McCall711c52b2011-01-05 12:14:39 +00002472bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) {
2473 if (attr.isInvalid())
2474 return true;
2475
Ted Kremenek831efae2011-04-15 05:49:29 +00002476 if ((attr.getNumArgs() != 0 &&
2477 !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) ||
2478 attr.getParameterName()) {
John McCall711c52b2011-01-05 12:14:39 +00002479 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
2480 attr.setInvalid();
2481 return true;
2482 }
2483
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002484 // TODO: diagnose uses of these conventions on the wrong target. Or, better
2485 // move to TargetAttributesSema one day.
John McCall711c52b2011-01-05 12:14:39 +00002486 switch (attr.getKind()) {
2487 case AttributeList::AT_cdecl: CC = CC_C; break;
2488 case AttributeList::AT_fastcall: CC = CC_X86FastCall; break;
2489 case AttributeList::AT_stdcall: CC = CC_X86StdCall; break;
2490 case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break;
2491 case AttributeList::AT_pascal: CC = CC_X86Pascal; break;
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002492 case AttributeList::AT_pcs: {
2493 Expr *Arg = attr.getArg(0);
2494 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
2495 if (Str == 0 || Str->isWide()) {
2496 Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string)
2497 << "pcs" << 1;
2498 attr.setInvalid();
2499 return true;
2500 }
2501
2502 llvm::StringRef StrRef = Str->getString();
2503 if (StrRef == "aapcs") {
2504 CC = CC_AAPCS;
2505 break;
2506 } else if (StrRef == "aapcs-vfp") {
2507 CC = CC_AAPCS_VFP;
2508 break;
2509 }
2510 // FALLS THROUGH
2511 }
John McCall711c52b2011-01-05 12:14:39 +00002512 default: llvm_unreachable("unexpected attribute kind"); return true;
2513 }
2514
2515 return false;
2516}
2517
2518static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) {
2519 if (hasDeclarator(d)) return;
2520
2521 unsigned numParams;
2522 if (S.CheckRegparmAttr(attr, numParams))
2523 return;
2524
2525 if (!isa<ObjCMethodDecl>(d)) {
2526 S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002527 << attr.getName() << ExpectedFunctionOrMethod;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002528 return;
2529 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002530
John McCall711c52b2011-01-05 12:14:39 +00002531 d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams));
2532}
2533
2534/// Checks a regparm attribute, returning true if it is ill-formed and
2535/// otherwise setting numParams to the appropriate value.
2536bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) {
2537 if (attr.isInvalid())
2538 return true;
2539
2540 if (attr.getNumArgs() != 1) {
2541 Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2542 attr.setInvalid();
2543 return true;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002544 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002545
John McCall711c52b2011-01-05 12:14:39 +00002546 Expr *NumParamsExpr = attr.getArg(0);
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002547 llvm::APSInt NumParams(32);
Douglas Gregorac06a0e2010-05-18 23:01:22 +00002548 if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() ||
John McCall711c52b2011-01-05 12:14:39 +00002549 !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) {
2550 Diag(attr.getLoc(), diag::err_attribute_argument_not_int)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002551 << "regparm" << NumParamsExpr->getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +00002552 attr.setInvalid();
2553 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002554 }
2555
John McCall711c52b2011-01-05 12:14:39 +00002556 if (Context.Target.getRegParmMax() == 0) {
2557 Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002558 << NumParamsExpr->getSourceRange();
John McCall711c52b2011-01-05 12:14:39 +00002559 attr.setInvalid();
2560 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002561 }
2562
John McCall711c52b2011-01-05 12:14:39 +00002563 numParams = NumParams.getZExtValue();
2564 if (numParams > Context.Target.getRegParmMax()) {
2565 Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number)
2566 << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
2567 attr.setInvalid();
2568 return true;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00002569 }
2570
John McCall711c52b2011-01-05 12:14:39 +00002571 return false;
Fariborz Jahanianee760332009-03-27 18:38:55 +00002572}
2573
Peter Collingbourne7b381982010-12-12 23:03:07 +00002574static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){
2575 if (S.LangOpts.CUDA) {
2576 // check the attribute arguments.
2577 if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) {
John McCallbdc49d32011-03-02 12:15:05 +00002578 // FIXME: 0 is not okay.
2579 S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002580 return;
2581 }
2582
2583 if (!isFunctionOrMethod(d)) {
2584 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002585 << Attr.getName() << ExpectedFunctionOrMethod;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002586 return;
2587 }
2588
2589 Expr *MaxThreadsExpr = Attr.getArg(0);
2590 llvm::APSInt MaxThreads(32);
2591 if (MaxThreadsExpr->isTypeDependent() ||
2592 MaxThreadsExpr->isValueDependent() ||
2593 !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) {
2594 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2595 << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange();
2596 return;
2597 }
2598
2599 llvm::APSInt MinBlocks(32);
2600 if (Attr.getNumArgs() > 1) {
2601 Expr *MinBlocksExpr = Attr.getArg(1);
2602 if (MinBlocksExpr->isTypeDependent() ||
2603 MinBlocksExpr->isValueDependent() ||
2604 !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) {
2605 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
2606 << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange();
2607 return;
2608 }
2609 }
2610
2611 d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context,
2612 MaxThreads.getZExtValue(),
2613 MinBlocks.getZExtValue()));
2614 } else {
2615 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds";
2616 }
2617}
2618
Chris Lattner0744e5f2008-06-29 00:23:49 +00002619//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00002620// Checker-specific attribute handlers.
2621//===----------------------------------------------------------------------===//
2622
John McCallc7ad3812011-01-25 03:31:58 +00002623static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) {
2624 return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type);
2625}
2626static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) {
2627 return type->isPointerType() || isValidSubjectOfNSAttribute(S, type);
2628}
2629
2630static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) {
2631 ParmVarDecl *param = dyn_cast<ParmVarDecl>(d);
2632 if (!param) {
2633 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002634 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedParameter;
John McCallc7ad3812011-01-25 03:31:58 +00002635 return;
2636 }
2637
2638 bool typeOK, cf;
2639 if (attr.getKind() == AttributeList::AT_ns_consumed) {
2640 typeOK = isValidSubjectOfNSAttribute(S, param->getType());
2641 cf = false;
2642 } else {
2643 typeOK = isValidSubjectOfCFAttribute(S, param->getType());
2644 cf = true;
2645 }
2646
2647 if (!typeOK) {
2648 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type)
2649 << SourceRange(attr.getLoc()) << attr.getName() << cf;
2650 return;
2651 }
2652
2653 if (cf)
2654 param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context));
2655 else
2656 param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context));
2657}
2658
2659static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr,
2660 Sema &S) {
2661 if (!isa<ObjCMethodDecl>(d)) {
2662 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCall883cc2c2011-03-02 12:29:23 +00002663 << SourceRange(attr.getLoc()) << attr.getName() << ExpectedMethod;
John McCallc7ad3812011-01-25 03:31:58 +00002664 return;
2665 }
2666
2667 d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context));
2668}
2669
2670static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr,
Ted Kremenekb71368d2009-05-09 02:44:38 +00002671 Sema &S) {
2672
John McCallc7ad3812011-01-25 03:31:58 +00002673 QualType returnType;
Mike Stumpbf916502009-07-24 19:02:52 +00002674
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002675 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
John McCallc7ad3812011-01-25 03:31:58 +00002676 returnType = MD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002677 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
John McCallc7ad3812011-01-25 03:31:58 +00002678 returnType = FD->getResultType();
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002679 else {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002680 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
John McCallc7ad3812011-01-25 03:31:58 +00002681 << SourceRange(attr.getLoc()) << attr.getName()
John McCall883cc2c2011-03-02 12:29:23 +00002682 << ExpectedFunctionOrMethod;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002683 return;
2684 }
Mike Stumpbf916502009-07-24 19:02:52 +00002685
John McCallc7ad3812011-01-25 03:31:58 +00002686 bool typeOK;
2687 bool cf;
2688 switch (attr.getKind()) {
2689 default: llvm_unreachable("invalid ownership attribute"); return;
2690 case AttributeList::AT_ns_returns_autoreleased:
2691 case AttributeList::AT_ns_returns_retained:
2692 case AttributeList::AT_ns_returns_not_retained:
2693 typeOK = isValidSubjectOfNSAttribute(S, returnType);
2694 cf = false;
2695 break;
2696
2697 case AttributeList::AT_cf_returns_retained:
2698 case AttributeList::AT_cf_returns_not_retained:
2699 typeOK = isValidSubjectOfCFAttribute(S, returnType);
2700 cf = true;
2701 break;
2702 }
2703
2704 if (!typeOK) {
Ted Kremenek21531fa2009-08-19 23:56:48 +00002705 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
John McCallc7ad3812011-01-25 03:31:58 +00002706 << SourceRange(attr.getLoc())
2707 << attr.getName() << isa<ObjCMethodDecl>(d) << cf;
Mike Stumpbf916502009-07-24 19:02:52 +00002708 return;
Ted Kremenek5dc53c92009-05-13 21:07:32 +00002709 }
Mike Stumpbf916502009-07-24 19:02:52 +00002710
John McCallc7ad3812011-01-25 03:31:58 +00002711 switch (attr.getKind()) {
Ted Kremenekb71368d2009-05-09 02:44:38 +00002712 default:
2713 assert(0 && "invalid ownership attribute");
2714 return;
John McCallc7ad3812011-01-25 03:31:58 +00002715 case AttributeList::AT_ns_returns_autoreleased:
2716 d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(),
2717 S.Context));
2718 return;
Ted Kremenek31c780d2010-02-18 00:05:45 +00002719 case AttributeList::AT_cf_returns_not_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002720 d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002721 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002722 return;
2723 case AttributeList::AT_ns_returns_not_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002724 d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002725 S.Context));
Ted Kremenek31c780d2010-02-18 00:05:45 +00002726 return;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002727 case AttributeList::AT_cf_returns_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002728 d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002729 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002730 return;
2731 case AttributeList::AT_ns_returns_retained:
John McCallc7ad3812011-01-25 03:31:58 +00002732 d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(),
Eric Christopherf48f3672010-12-01 22:13:54 +00002733 S.Context));
Ted Kremenekb71368d2009-05-09 02:44:38 +00002734 return;
2735 };
2736}
2737
Charles Davisf0122fe2010-02-16 18:27:26 +00002738static bool isKnownDeclSpecAttr(const AttributeList &Attr) {
2739 return Attr.getKind() == AttributeList::AT_dllimport ||
Francois Pichet11542142010-12-19 06:50:37 +00002740 Attr.getKind() == AttributeList::AT_dllexport ||
2741 Attr.getKind() == AttributeList::AT_uuid;
2742}
2743
2744//===----------------------------------------------------------------------===//
2745// Microsoft specific attribute handlers.
2746//===----------------------------------------------------------------------===//
2747
2748static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) {
2749 if (S.LangOpts.Microsoft || S.LangOpts.Borland) {
2750 // check the attribute arguments.
2751 if (Attr.getNumArgs() != 1) {
2752 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
2753 return;
2754 }
2755 Expr *Arg = Attr.getArg(0);
2756 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Francois Pichetd3d3be92010-12-20 01:41:49 +00002757 if (Str == 0 || Str->isWide()) {
2758 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
2759 << "uuid" << 1;
2760 return;
2761 }
2762
2763 llvm::StringRef StrRef = Str->getString();
2764
2765 bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' &&
2766 StrRef.back() == '}';
2767
2768 // Validate GUID length.
2769 if (IsCurly && StrRef.size() != 38) {
2770 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2771 return;
2772 }
2773 if (!IsCurly && StrRef.size() != 36) {
2774 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2775 return;
2776 }
2777
2778 // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or
2779 // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
Anders Carlssonf89e0422011-01-23 21:07:30 +00002780 llvm::StringRef::iterator I = StrRef.begin();
2781 if (IsCurly) // Skip the optional '{'
2782 ++I;
2783
2784 for (int i = 0; i < 36; ++i) {
Francois Pichetd3d3be92010-12-20 01:41:49 +00002785 if (i == 8 || i == 13 || i == 18 || i == 23) {
2786 if (*I != '-') {
2787 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2788 return;
2789 }
2790 } else if (!isxdigit(*I)) {
2791 S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid);
2792 return;
2793 }
2794 I++;
2795 }
Francois Pichet11542142010-12-19 06:50:37 +00002796
2797 d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context,
2798 Str->getString()));
Francois Pichetd3d3be92010-12-20 01:41:49 +00002799 } else
Francois Pichet11542142010-12-19 06:50:37 +00002800 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid";
Charles Davisf0122fe2010-02-16 18:27:26 +00002801}
2802
Ted Kremenekb71368d2009-05-09 02:44:38 +00002803//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00002804// Top Level Sema Entry Points
2805//===----------------------------------------------------------------------===//
2806
Peter Collingbourne60700392011-01-21 02:08:45 +00002807static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D,
2808 const AttributeList &Attr, Sema &S) {
2809 switch (Attr.getKind()) {
2810 case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break;
2811 case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break;
2812 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
2813 default:
2814 break;
2815 }
2816}
Abramo Bagnarae215f722010-04-30 13:10:51 +00002817
Peter Collingbourne60700392011-01-21 02:08:45 +00002818static void ProcessInheritableDeclAttr(Scope *scope, Decl *D,
2819 const AttributeList &Attr, Sema &S) {
Chris Lattner803d0802008-06-29 00:43:07 +00002820 switch (Attr.getKind()) {
Ted Kremenek63e5d7c2010-02-18 03:08:58 +00002821 case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break;
Ted Kremenek857e9182010-05-19 17:38:06 +00002822 case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break;
2823 case AttributeList::AT_IBOutletCollection:
2824 HandleIBOutletCollection(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002825 case AttributeList::AT_address_space:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002826 case AttributeList::AT_opencl_image_access:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00002827 case AttributeList::AT_objc_gc:
John Thompson6e132aa2009-12-04 21:51:28 +00002828 case AttributeList::AT_vector_size:
Bob Wilson4211bb62010-11-16 00:32:24 +00002829 case AttributeList::AT_neon_vector_type:
2830 case AttributeList::AT_neon_polyvector_type:
Mike Stumpbf916502009-07-24 19:02:52 +00002831 // Ignore these, these are type attributes, handled by
2832 // ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00002833 break;
Peter Collingbourne60700392011-01-21 02:08:45 +00002834 case AttributeList::AT_device:
2835 case AttributeList::AT_host:
2836 case AttributeList::AT_overloadable:
2837 // Ignore, this is a non-inheritable attribute, handled
2838 // by ProcessNonInheritableDeclAttr.
2839 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002840 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
2841 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002842 case AttributeList::AT_always_inline:
Daniel Dunbaraf668b02008-10-28 00:17:57 +00002843 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00002844 case AttributeList::AT_analyzer_noreturn:
Mike Stumpbf916502009-07-24 19:02:52 +00002845 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002846 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00002847 case AttributeList::AT_availability:HandleAvailabilityAttr(D, Attr, S); break;
Sean Huntbbd37c62009-11-21 08:43:09 +00002848 case AttributeList::AT_carries_dependency:
Sean Hunt7725e672009-11-25 04:20:27 +00002849 HandleDependencyAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002850 case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002851 case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002852 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
2853 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
2854 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002855 case AttributeList::AT_ext_vector_type:
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002856 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattner803d0802008-06-29 00:43:07 +00002857 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002858 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
2859 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002860 case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002861 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
Peter Collingbourne7b381982010-12-12 23:03:07 +00002862 case AttributeList::AT_launch_bounds:
2863 HandleLaunchBoundsAttr(D, Attr, S);
2864 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002865 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
2866 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
Dan Gohman34c26302010-11-17 00:03:07 +00002867 case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break;
Eric Christophera6cf1e72010-12-02 02:45:55 +00002868 case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002869 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002870 case AttributeList::AT_ownership_returns:
2871 case AttributeList::AT_ownership_takes:
2872 case AttributeList::AT_ownership_holds:
2873 HandleOwnershipAttr (D, Attr, S); break;
Daniel Dunbardd0cb222010-09-29 18:20:25 +00002874 case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002875 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
2876 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Peter Collingbourneced76712010-12-01 03:15:31 +00002877 case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break;
John Thompson35cc9622010-08-09 21:53:52 +00002878 case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00002879
2880 // Checker-specific.
John McCallc7ad3812011-01-25 03:31:58 +00002881 case AttributeList::AT_cf_consumed:
2882 case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break;
2883 case AttributeList::AT_ns_consumes_self:
2884 HandleNSConsumesSelfAttr(D, Attr, S); break;
2885
2886 case AttributeList::AT_ns_returns_autoreleased:
Ted Kremenek31c780d2010-02-18 00:05:45 +00002887 case AttributeList::AT_ns_returns_not_retained:
2888 case AttributeList::AT_cf_returns_not_retained:
Ted Kremenekb71368d2009-05-09 02:44:38 +00002889 case AttributeList::AT_ns_returns_retained:
2890 case AttributeList::AT_cf_returns_retained:
2891 HandleNSReturnsRetainedAttr(D, Attr, S); break;
2892
Nate Begeman6f3d8382009-06-26 06:32:41 +00002893 case AttributeList::AT_reqd_wg_size:
2894 HandleReqdWorkGroupSize(D, Attr, S); break;
2895
Fariborz Jahanian521f12d2010-06-18 21:44:06 +00002896 case AttributeList::AT_init_priority:
2897 HandleInitPriorityAttr(D, Attr, S); break;
2898
Sean Hunt7725e672009-11-25 04:20:27 +00002899 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Fariborz Jahanianc1a0a732011-04-26 17:54:40 +00002900 case AttributeList::AT_MsStruct: HandleMsStructAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002901 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002902 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
2903 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
2904 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002905 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00002906 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
2907 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002908 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002909 case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break;
Sean Hunt7725e672009-11-25 04:20:27 +00002910 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00002911 case AttributeList::AT_transparent_union:
2912 HandleTransparentUnionAttr(D, Attr, S);
2913 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00002914 case AttributeList::AT_objc_exception:
2915 HandleObjCExceptionAttr(D, Attr, S);
2916 break;
John McCalld5313b02011-03-02 11:33:24 +00002917 case AttributeList::AT_objc_method_family:
2918 HandleObjCMethodFamilyAttr(D, Attr, S);
2919 break;
Sean Hunt7725e672009-11-25 04:20:27 +00002920 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
2921 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
2922 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
2923 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
2924 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
2925 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
2926 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
2927 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
2928 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpbf916502009-07-24 19:02:52 +00002929 case AttributeList::IgnoredAttribute:
Anders Carlsson05f8e472009-02-13 08:16:43 +00002930 // Just ignore
2931 break;
Chris Lattner7255a2d2010-06-22 00:03:40 +00002932 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
2933 HandleNoInstrumentFunctionAttr(D, Attr, S);
2934 break;
John McCall04a67a62010-02-05 21:31:56 +00002935 case AttributeList::AT_stdcall:
2936 case AttributeList::AT_cdecl:
2937 case AttributeList::AT_fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002938 case AttributeList::AT_thiscall:
Dawn Perchik52fc3142010-09-03 01:29:35 +00002939 case AttributeList::AT_pascal:
Anton Korobeynikov414d8962011-04-14 20:06:49 +00002940 case AttributeList::AT_pcs:
Abramo Bagnarae215f722010-04-30 13:10:51 +00002941 HandleCallConvAttr(D, Attr, S);
John McCall04a67a62010-02-05 21:31:56 +00002942 break;
Peter Collingbournef315fa82011-02-14 01:42:53 +00002943 case AttributeList::AT_opencl_kernel_function:
2944 HandleOpenCLKernelAttr(D, Attr, S);
2945 break;
Francois Pichet11542142010-12-19 06:50:37 +00002946 case AttributeList::AT_uuid:
2947 HandleUuidAttr(D, Attr, S);
2948 break;
Chris Lattner803d0802008-06-29 00:43:07 +00002949 default:
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00002950 // Ask target about the attribute.
2951 const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema();
2952 if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S))
Chandler Carruth7d5c45e2010-07-08 09:42:26 +00002953 S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored)
2954 << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00002955 break;
2956 }
2957}
2958
Peter Collingbourne60700392011-01-21 02:08:45 +00002959/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
2960/// the attribute applies to decls. If the attribute is a type attribute, just
2961/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
2962/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
2963static void ProcessDeclAttribute(Scope *scope, Decl *D,
2964 const AttributeList &Attr, Sema &S,
2965 bool NonInheritable, bool Inheritable) {
2966 if (Attr.isInvalid())
2967 return;
2968
2969 if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr))
2970 // FIXME: Try to deal with other __declspec attributes!
2971 return;
2972
2973 if (NonInheritable)
2974 ProcessNonInheritableDeclAttr(scope, D, Attr, S);
2975
2976 if (Inheritable)
2977 ProcessInheritableDeclAttr(scope, D, Attr, S);
2978}
2979
Chris Lattner803d0802008-06-29 00:43:07 +00002980/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
2981/// attribute list to the specified decl, ignoring any type attributes.
Eric Christopherf48f3672010-12-01 22:13:54 +00002982void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
Peter Collingbourne60700392011-01-21 02:08:45 +00002983 const AttributeList *AttrList,
2984 bool NonInheritable, bool Inheritable) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002985 for (const AttributeList* l = AttrList; l; l = l->getNext()) {
Peter Collingbourne60700392011-01-21 02:08:45 +00002986 ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002987 }
2988
2989 // GCC accepts
2990 // static int a9 __attribute__((weakref));
2991 // but that looks really pointless. We reject it.
Peter Collingbourne60700392011-01-21 02:08:45 +00002992 if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) {
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002993 Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) <<
Ted Kremenekdd0e4902010-07-31 01:52:11 +00002994 dyn_cast<NamedDecl>(D)->getNameAsString();
Rafael Espindola11e8ce72010-02-23 22:00:30 +00002995 return;
Chris Lattner803d0802008-06-29 00:43:07 +00002996 }
2997}
2998
Ryan Flynne25ff832009-07-30 03:15:39 +00002999/// DeclClonePragmaWeak - clone existing decl (maybe definition),
3000/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump1eb44332009-09-09 15:08:12 +00003001NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003002 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynne25ff832009-07-30 03:15:39 +00003003 NamedDecl *NewD = 0;
3004 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
3005 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003006 FD->getInnerLocStart(),
Ryan Flynne25ff832009-07-30 03:15:39 +00003007 FD->getLocation(), DeclarationName(II),
John McCalla93c9342009-12-07 02:54:59 +00003008 FD->getType(), FD->getTypeSourceInfo());
John McCallb6217662010-03-15 10:12:16 +00003009 if (FD->getQualifier()) {
3010 FunctionDecl *NewFD = cast<FunctionDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003011 NewFD->setQualifierInfo(FD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003012 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003013 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
3014 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003015 VD->getInnerLocStart(), VD->getLocation(), II,
John McCalla93c9342009-12-07 02:54:59 +00003016 VD->getType(), VD->getTypeSourceInfo(),
Douglas Gregor16573fa2010-04-19 22:54:31 +00003017 VD->getStorageClass(),
3018 VD->getStorageClassAsWritten());
John McCallb6217662010-03-15 10:12:16 +00003019 if (VD->getQualifier()) {
3020 VarDecl *NewVD = cast<VarDecl>(NewD);
Douglas Gregorc22b5ff2011-02-25 02:25:35 +00003021 NewVD->setQualifierInfo(VD->getQualifierLoc());
John McCallb6217662010-03-15 10:12:16 +00003022 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003023 }
3024 return NewD;
3025}
3026
3027/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
3028/// applied to it, possibly with an alias.
Ryan Flynn7b1fdbd2009-07-31 02:52:19 +00003029void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003030 if (W.getUsed()) return; // only do this once
3031 W.setUsed(true);
3032 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
3033 IdentifierInfo *NDId = ND->getIdentifier();
3034 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
Sean Huntcf807c42010-08-18 23:23:40 +00003035 NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context,
3036 NDId->getName()));
3037 NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Chris Lattnerc4f1fb12009-09-08 18:10:11 +00003038 WeakTopLevelDecl.push_back(NewD);
3039 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
3040 // to insert Decl at TU scope, sorry.
3041 DeclContext *SavedContext = CurContext;
3042 CurContext = Context.getTranslationUnitDecl();
3043 PushOnScopeChains(NewD, S);
3044 CurContext = SavedContext;
3045 } else { // just add weak to existing
Sean Huntcf807c42010-08-18 23:23:40 +00003046 ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context));
Ryan Flynne25ff832009-07-30 03:15:39 +00003047 }
3048}
3049
Chris Lattner0744e5f2008-06-29 00:23:49 +00003050/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
3051/// it, apply them to D. This is a bit tricky because PD can have attributes
3052/// specified in many different places, and we need to find and apply them all.
Peter Collingbourne60700392011-01-21 02:08:45 +00003053void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD,
3054 bool NonInheritable, bool Inheritable) {
John McCalld4aff0e2010-10-27 00:59:00 +00003055 // It's valid to "forward-declare" #pragma weak, in which case we
3056 // have to do this.
Peter Collingbourne60700392011-01-21 02:08:45 +00003057 if (Inheritable && !WeakUndeclaredIdentifiers.empty()) {
John McCalld4aff0e2010-10-27 00:59:00 +00003058 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
3059 if (IdentifierInfo *Id = ND->getIdentifier()) {
3060 llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I
3061 = WeakUndeclaredIdentifiers.find(Id);
3062 if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) {
3063 WeakInfo W = I->second;
3064 DeclApplyPragmaWeak(S, ND, W);
3065 WeakUndeclaredIdentifiers[Id] = W;
3066 }
Ryan Flynne25ff832009-07-30 03:15:39 +00003067 }
3068 }
3069 }
3070
Chris Lattner0744e5f2008-06-29 00:23:49 +00003071 // Apply decl attributes from the DeclSpec if present.
John McCall7f040a92010-12-24 02:08:15 +00003072 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList())
Peter Collingbourne60700392011-01-21 02:08:45 +00003073 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003074
Chris Lattner0744e5f2008-06-29 00:23:49 +00003075 // Walk the declarator structure, applying decl attributes that were in a type
3076 // position to the decl itself. This handles cases like:
3077 // int *__attr__(x)** D;
3078 // when X is a decl attribute.
3079 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
3080 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Peter Collingbourne60700392011-01-21 02:08:45 +00003081 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Mike Stumpbf916502009-07-24 19:02:52 +00003082
Chris Lattner0744e5f2008-06-29 00:23:49 +00003083 // Finally, apply any attributes on the decl itself.
3084 if (const AttributeList *Attrs = PD.getAttributes())
Peter Collingbourne60700392011-01-21 02:08:45 +00003085 ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable);
Chris Lattner0744e5f2008-06-29 00:23:49 +00003086}
John McCall54abf7d2009-11-04 02:18:39 +00003087
John McCalleee1d542011-02-14 07:13:47 +00003088// This duplicates a vector push_back but hides the need to know the
3089// size of the type.
3090void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) {
3091 assert(StackSize <= StackCapacity);
3092
3093 // Grow the stack if necessary.
3094 if (StackSize == StackCapacity) {
3095 unsigned newCapacity = 2 * StackCapacity + 2;
3096 char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)];
3097 const char *oldBuffer = (const char*) Stack;
3098
3099 if (StackCapacity)
3100 memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic));
3101
3102 delete[] oldBuffer;
3103 Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer);
3104 StackCapacity = newCapacity;
3105 }
3106
3107 assert(StackSize < StackCapacity);
3108 new (&Stack[StackSize++]) DelayedDiagnostic(diag);
John McCall2f514482010-01-27 03:50:35 +00003109}
3110
John McCalleee1d542011-02-14 07:13:47 +00003111void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state,
3112 Decl *decl) {
3113 DelayedDiagnostics &DD = S.DelayedDiagnostics;
John McCall2f514482010-01-27 03:50:35 +00003114
John McCalleee1d542011-02-14 07:13:47 +00003115 // Check the invariants.
3116 assert(DD.StackSize >= state.SavedStackSize);
3117 assert(state.SavedStackSize >= DD.ActiveStackBase);
3118 assert(DD.ParsingDepth > 0);
3119
3120 // Drop the parsing depth.
3121 DD.ParsingDepth--;
3122
3123 // If there are no active diagnostics, we're done.
3124 if (DD.StackSize == DD.ActiveStackBase)
John McCall2f514482010-01-27 03:50:35 +00003125 return;
3126
John McCall2f514482010-01-27 03:50:35 +00003127 // We only want to actually emit delayed diagnostics when we
3128 // successfully parsed a decl.
John McCalleee1d542011-02-14 07:13:47 +00003129 if (decl) {
3130 // We emit all the active diagnostics, not just those starting
3131 // from the saved state. The idea is this: we get one push for a
John McCall2f514482010-01-27 03:50:35 +00003132 // decl spec and another for each declarator; in a decl group like:
3133 // deprecated_typedef foo, *bar, baz();
3134 // only the declarator pops will be passed decls. This is correct;
3135 // we really do need to consider delayed diagnostics from the decl spec
3136 // for each of the different declarations.
John McCalleee1d542011-02-14 07:13:47 +00003137 for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) {
3138 DelayedDiagnostic &diag = DD.Stack[i];
3139 if (diag.Triggered)
John McCall2f514482010-01-27 03:50:35 +00003140 continue;
3141
John McCalleee1d542011-02-14 07:13:47 +00003142 switch (diag.Kind) {
John McCall2f514482010-01-27 03:50:35 +00003143 case DelayedDiagnostic::Deprecation:
John McCalleee1d542011-02-14 07:13:47 +00003144 S.HandleDelayedDeprecationCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003145 break;
3146
3147 case DelayedDiagnostic::Access:
John McCalleee1d542011-02-14 07:13:47 +00003148 S.HandleDelayedAccessCheck(diag, decl);
John McCall2f514482010-01-27 03:50:35 +00003149 break;
3150 }
3151 }
3152 }
3153
John McCall58e6f342010-03-16 05:22:47 +00003154 // Destroy all the delayed diagnostics we're about to pop off.
John McCalleee1d542011-02-14 07:13:47 +00003155 for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i)
Douglas Gregor29233802011-03-23 15:13:44 +00003156 DD.Stack[i].Destroy();
John McCall58e6f342010-03-16 05:22:47 +00003157
John McCalleee1d542011-02-14 07:13:47 +00003158 DD.StackSize = state.SavedStackSize;
John McCall54abf7d2009-11-04 02:18:39 +00003159}
3160
3161static bool isDeclDeprecated(Decl *D) {
3162 do {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00003163 if (D->isDeprecated())
John McCall54abf7d2009-11-04 02:18:39 +00003164 return true;
3165 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
3166 return false;
3167}
3168
John McCall9c3087b2010-08-26 02:13:20 +00003169void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD,
John McCall2f514482010-01-27 03:50:35 +00003170 Decl *Ctx) {
3171 if (isDeclDeprecated(Ctx))
John McCall54abf7d2009-11-04 02:18:39 +00003172 return;
3173
John McCall2f514482010-01-27 03:50:35 +00003174 DD.Triggered = true;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003175 if (!DD.getDeprecationMessage().empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003176 Diag(DD.Loc, diag::warn_deprecated_message)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003177 << DD.getDeprecationDecl()->getDeclName()
3178 << DD.getDeprecationMessage();
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003179 else
3180 Diag(DD.Loc, diag::warn_deprecated)
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003181 << DD.getDeprecationDecl()->getDeclName();
John McCall54abf7d2009-11-04 02:18:39 +00003182}
3183
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003184void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message,
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003185 SourceLocation Loc,
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003186 const ObjCInterfaceDecl *UnknownObjCClass) {
John McCall54abf7d2009-11-04 02:18:39 +00003187 // Delay if we're currently parsing a declaration.
John McCalleee1d542011-02-14 07:13:47 +00003188 if (DelayedDiagnostics.shouldDelayDiagnostics()) {
3189 DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message));
John McCall54abf7d2009-11-04 02:18:39 +00003190 return;
3191 }
3192
3193 // Otherwise, don't warn if our current context is deprecated.
3194 if (isDeclDeprecated(cast<Decl>(CurContext)))
3195 return;
Benjamin Kramerce2d1862010-10-09 15:49:00 +00003196 if (!Message.empty())
Fariborz Jahanianc4b35cf2010-10-06 21:18:44 +00003197 Diag(Loc, diag::warn_deprecated_message) << D->getDeclName()
3198 << Message;
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003199 else {
Peter Collingbourne743b82b2011-01-02 19:53:12 +00003200 if (!UnknownObjCClass)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003201 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003202 else {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003203 Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName();
Fariborz Jahanian89ebaed2011-04-23 17:27:19 +00003204 Diag(UnknownObjCClass->getLocation(), diag::note_forward_class);
3205 }
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00003206 }
John McCall54abf7d2009-11-04 02:18:39 +00003207}