blob: e95f479bd8e84279ee91fbbcb821abc0feafb878 [file] [log] [blame]
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbar56fdb6a2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattneracbc2d22008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar34fb6722008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Chris Lattner30ba6742009-08-10 19:03:04 +000020#include "llvm/ADT/StringExtras.h"
Chris Lattner2c6fcf52008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattner58418ff2008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Ted Kremenek527042b2009-08-14 20:49:40 +000027static const FunctionType *getFunctionType(const Decl *d,
28 bool blocksToo = true) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +000029 QualType Ty;
Ted Kremenek527042b2009-08-14 20:49:40 +000030 if (const ValueDecl *decl = dyn_cast<ValueDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000031 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000032 else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000033 Ty = decl->getType();
Ted Kremenek527042b2009-08-14 20:49:40 +000034 else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
Chris Lattner2c6fcf52008-06-26 18:38:35 +000035 Ty = decl->getUnderlyingType();
36 else
37 return 0;
Mike Stumpd3bb5572009-07-24 19:02:52 +000038
Chris Lattner2c6fcf52008-06-26 18:38:35 +000039 if (Ty->isFunctionPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000040 Ty = Ty->getAs<PointerType>()->getPointeeType();
Fariborz Jahanian28c433d2009-05-18 17:39:25 +000041 else if (blocksToo && Ty->isBlockPointerType())
Ted Kremenekc23c7e62009-07-29 21:53:49 +000042 Ty = Ty->getAs<BlockPointerType>()->getPointeeType();
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000043
John McCall9dd450b2009-09-21 23:43:11 +000044 return Ty->getAs<FunctionType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +000045}
46
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000047// FIXME: We should provide an abstraction around a method or function
48// to provide the following bits of information.
49
Nuno Lopes518e3702009-12-20 23:11:08 +000050/// isFunction - Return true if the given decl has function
Ted Kremenek527042b2009-08-14 20:49:40 +000051/// type (function or function-typed variable).
52static bool isFunction(const Decl *d) {
53 return getFunctionType(d, false) != NULL;
54}
55
56/// isFunctionOrMethod - Return true if the given decl has function
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000057/// type (function or function-typed variable) or an Objective-C
58/// method.
Ted Kremenek527042b2009-08-14 20:49:40 +000059static bool isFunctionOrMethod(const Decl *d) {
60 return isFunction(d)|| isa<ObjCMethodDecl>(d);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000061}
62
Fariborz Jahanian4447e172009-05-15 23:15:03 +000063/// isFunctionOrMethodOrBlock - Return true if the given decl has function
64/// type (function or function-typed variable) or an Objective-C
65/// method or a block.
Ted Kremenek527042b2009-08-14 20:49:40 +000066static bool isFunctionOrMethodOrBlock(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000067 if (isFunctionOrMethod(d))
68 return true;
69 // check for block is more involved.
70 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
71 QualType Ty = V->getType();
72 return Ty->isBlockPointerType();
73 }
Fariborz Jahanian960910a2009-05-19 17:08:59 +000074 return isa<BlockDecl>(d);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000075}
76
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000077/// hasFunctionProto - Return true if the given decl has a argument
78/// information. This decl should have already passed
Fariborz Jahanian4447e172009-05-15 23:15:03 +000079/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Ted Kremenek527042b2009-08-14 20:49:40 +000080static bool hasFunctionProto(const Decl *d) {
Fariborz Jahanian4447e172009-05-15 23:15:03 +000081 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000082 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian4447e172009-05-15 23:15:03 +000083 else {
Fariborz Jahanian960910a2009-05-19 17:08:59 +000084 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbar70e3eba2008-10-19 02:04:16 +000085 return true;
86 }
87}
88
89/// getFunctionOrMethodNumArgs - Return number of function or method
90/// arguments. It is an error to call this on a K&R function (use
91/// hasFunctionProto first).
Ted Kremenek527042b2009-08-14 20:49:40 +000092static unsigned getFunctionOrMethodNumArgs(const Decl *d) {
Chris Lattnera4997152009-02-20 18:43:26 +000093 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +000094 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahanian960910a2009-05-19 17:08:59 +000095 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
96 return BD->getNumParams();
Chris Lattnera4997152009-02-20 18:43:26 +000097 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +000098}
99
Ted Kremenek527042b2009-08-14 20:49:40 +0000100static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) {
Chris Lattnera4997152009-02-20 18:43:26 +0000101 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000102 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000103 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
104 return BD->getParamDecl(Idx)->getType();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000105
Chris Lattnera4997152009-02-20 18:43:26 +0000106 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000107}
108
Ted Kremenek527042b2009-08-14 20:49:40 +0000109static QualType getFunctionOrMethodResultType(const Decl *d) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +0000110 if (const FunctionType *FnTy = getFunctionType(d))
111 return cast<FunctionProtoType>(FnTy)->getResultType();
112 return cast<ObjCMethodDecl>(d)->getResultType();
113}
114
Ted Kremenek527042b2009-08-14 20:49:40 +0000115static bool isFunctionOrMethodVariadic(const Decl *d) {
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000116 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000117 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000118 return proto->isVariadic();
Fariborz Jahanian960910a2009-05-19 17:08:59 +0000119 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
120 return BD->IsVariadic();
121 else {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +0000122 return cast<ObjCMethodDecl>(d)->isVariadic();
123 }
124}
125
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000126static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
John McCall9dd450b2009-09-21 23:43:11 +0000127 const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>();
Chris Lattner574dee62008-07-26 22:17:49 +0000128 if (!PT)
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000129 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000130
John McCall9dd450b2009-09-21 23:43:11 +0000131 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000132 if (!ClsT)
133 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000134
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000135 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000136
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000137 // FIXME: Should we walk the chain of classes?
138 return ClsName == &Ctx.Idents.get("NSString") ||
139 ClsName == &Ctx.Idents.get("NSMutableString");
140}
141
Daniel Dunbar980c6692008-09-26 03:32:58 +0000142static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000143 const PointerType *PT = T->getAs<PointerType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000144 if (!PT)
145 return false;
146
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000147 const RecordType *RT = PT->getPointeeType()->getAs<RecordType>();
Daniel Dunbar980c6692008-09-26 03:32:58 +0000148 if (!RT)
149 return false;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000150
Daniel Dunbar980c6692008-09-26 03:32:58 +0000151 const RecordDecl *RD = RT->getDecl();
152 if (RD->getTagKind() != TagDecl::TK_struct)
153 return false;
154
155 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
156}
157
Chris Lattner58418ff2008-06-29 00:16:31 +0000158//===----------------------------------------------------------------------===//
Chris Lattner58418ff2008-06-29 00:16:31 +0000159// Attribute Implementations
160//===----------------------------------------------------------------------===//
161
Daniel Dunbar032db472008-07-31 22:40:48 +0000162// FIXME: All this manual attribute parsing code is gross. At the
163// least add some helper functions to check most argument patterns (#
164// and types of args).
165
Mike Stumpd3bb5572009-07-24 19:02:52 +0000166static void HandleExtVectorTypeAttr(Scope *scope, Decl *d,
Douglas Gregor758a8692009-06-17 21:51:59 +0000167 const AttributeList &Attr, Sema &S) {
Chris Lattner4a927cb2008-06-28 23:36:30 +0000168 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
169 if (tDecl == 0) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000170 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner4a927cb2008-06-28 23:36:30 +0000171 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000172 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000173
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000174 QualType curType = tDecl->getUnderlyingType();
Douglas Gregor758a8692009-06-17 21:51:59 +0000175
176 Expr *sizeExpr;
177
178 // Special case where the argument is a template id.
179 if (Attr.getParameterName()) {
John McCalle66edc12009-11-24 19:00:30 +0000180 CXXScopeSpec SS;
181 UnqualifiedId id;
182 id.setIdentifier(Attr.getParameterName(), Attr.getLoc());
183 sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>();
Douglas Gregor758a8692009-06-17 21:51:59 +0000184 } else {
185 // check the attribute arguments.
186 if (Attr.getNumArgs() != 1) {
187 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
188 return;
189 }
190 sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000191 }
Douglas Gregor758a8692009-06-17 21:51:59 +0000192
193 // Instantiate/Install the vector type, and let Sema build the type for us.
194 // This will run the reguired checks.
195 QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc());
196 if (!T.isNull()) {
John McCall703a3f82009-10-24 08:00:42 +0000197 // FIXME: preserve the old source info.
John McCallbcd03502009-12-07 02:54:59 +0000198 tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T));
Mike Stumpd3bb5572009-07-24 19:02:52 +0000199
Douglas Gregor758a8692009-06-17 21:51:59 +0000200 // Remember this typedef decl, we will need it later for diagnostics.
201 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000202 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000203}
204
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000205static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000206 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000207 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000208 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000209 return;
210 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000211
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000212 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Anders Carlsson68e0b682009-08-08 18:23:56 +0000213 TD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000214 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
215 // If the alignment is less than or equal to 8 bits, the packed attribute
216 // has no effect.
217 if (!FD->getType()->isIncompleteType() &&
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000218 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattner3b054132008-11-19 05:08:23 +0000219 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +0000220 << Attr.getName() << FD->getType();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000221 else
Anders Carlsson68e0b682009-08-08 18:23:56 +0000222 FD->addAttr(::new (S.Context) PackedAttr);
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000223 } else
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000224 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000225}
226
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000227static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
228 // check the attribute arguments.
229 if (Attr.getNumArgs() > 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000230 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000231 return;
232 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000233
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000234 // The IBOutlet attribute only applies to instance variables of Objective-C
235 // classes.
Ted Kremenek2620a062009-02-17 22:20:20 +0000236 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000237 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000238 else
Ted Kremenek2620a062009-02-17 22:20:20 +0000239 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek8e3704d2008-07-15 22:26:48 +0000240}
241
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000242static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000243 // GCC ignores the nonnull attribute on K&R style function prototypes, so we
244 // ignore it as well
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000245 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000246 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000247 << Attr.getName() << 0 /*function*/;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000248 return;
249 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000250
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000251 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000252
253 // The nonnull attribute only applies to pointers.
254 llvm::SmallVector<unsigned, 10> NonNullArgs;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000255
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000256 for (AttributeList::arg_iterator I=Attr.arg_begin(),
257 E=Attr.arg_end(); I!=E; ++I) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000258
259
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000260 // The argument must be an integer constant expression.
Ted Kremenek7d71db72008-12-04 19:38:33 +0000261 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000262 llvm::APSInt ArgNum(32);
263 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000264 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
265 << "nonnull" << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000266 return;
267 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000268
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000269 unsigned x = (unsigned) ArgNum.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000270
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000271 if (x < 1 || x > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +0000272 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner91aea712008-11-19 07:22:31 +0000273 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000274 return;
275 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000276
Ted Kremenek5224e6a2008-07-21 22:09:15 +0000277 --x;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000278
279 // Is the function argument a pointer type?
Mike Stumpd3bb5572009-07-24 19:02:52 +0000280 QualType T = getFunctionOrMethodArgType(d, x);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000281 if (!T->isAnyPointerType() && !T->isBlockPointerType()) {
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000282 // FIXME: Should also highlight argument in decl.
Chris Lattner3b054132008-11-19 05:08:23 +0000283 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
284 << "nonnull" << Ex->getSourceRange();
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000285 continue;
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000286 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000287
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000288 NonNullArgs.push_back(x);
289 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000290
291 // If no arguments were specified to __attribute__((nonnull)) then all pointer
292 // arguments have a nonnull attribute.
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000293 if (NonNullArgs.empty()) {
Ted Kremenek5fa50522008-11-18 06:52:58 +0000294 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
295 QualType T = getFunctionOrMethodArgType(d, I);
Ted Kremenekd4adebb2009-07-15 23:23:54 +0000296 if (T->isAnyPointerType() || T->isBlockPointerType())
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000297 NonNullArgs.push_back(I);
Ted Kremenek5fa50522008-11-18 06:52:58 +0000298 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000299
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000300 if (NonNullArgs.empty()) {
301 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
302 return;
303 }
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000304 }
Ted Kremenekc4f6d902008-09-01 19:57:52 +0000305
306 unsigned* start = &NonNullArgs[0];
307 unsigned size = NonNullArgs.size();
308 std::sort(start, start + size);
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000309 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenek2d63bc12008-07-21 21:53:04 +0000310}
311
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000312static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000313 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000314 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000315 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000316 return;
317 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000318
Chris Lattner4a927cb2008-06-28 23:36:30 +0000319 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000320 Arg = Arg->IgnoreParenCasts();
321 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000322
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000323 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000324 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000325 << "alias" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000326 return;
327 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000328
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000329 // FIXME: check if target symbol exists in current file
Mike Stumpd3bb5572009-07-24 19:02:52 +0000330
Benjamin Kramer5f089122009-11-30 17:08:26 +0000331 d->addAttr(::new (S.Context) AliasAttr(Str->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000332}
333
Mike Stumpd3bb5572009-07-24 19:02:52 +0000334static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
Daniel Dunbar03a38442008-10-28 00:17:57 +0000335 Sema &S) {
336 // check the attribute arguments.
337 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000338 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbar03a38442008-10-28 00:17:57 +0000339 return;
340 }
Anders Carlsson88097122009-02-19 19:16:48 +0000341
Chris Lattner4225e232009-04-14 17:02:11 +0000342 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +0000343 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000344 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +0000345 return;
346 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000347
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000348 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbar03a38442008-10-28 00:17:57 +0000349}
350
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000351static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) {
352 // check the attribute arguments.
353 if (Attr.getNumArgs() != 0) {
354 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
355 return;
356 }
Mike Stump11289f42009-09-09 15:08:12 +0000357
Ted Kremenek08479ae2009-08-15 00:51:46 +0000358 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Mike Stump11289f42009-09-09 15:08:12 +0000359 QualType RetTy = FD->getResultType();
Ted Kremenek08479ae2009-08-15 00:51:46 +0000360 if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) {
361 d->addAttr(::new (S.Context) MallocAttr());
362 return;
363 }
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000364 }
365
Ted Kremenek08479ae2009-08-15 00:51:46 +0000366 S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only);
Ryan Flynn1f1fdc02009-08-09 20:07:29 +0000367}
368
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000369static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek3b204e42009-05-13 21:07:32 +0000370 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000371 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000372 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000373 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000374 return false;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000375 }
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000376
Mike Stump88788fe2009-04-29 19:03:13 +0000377 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
378 ValueDecl *VD = dyn_cast<ValueDecl>(d);
Mike Stumpfeb19452009-12-15 03:11:10 +0000379 if (VD == 0 || (!VD->getType()->isBlockPointerType()
380 && !VD->getType()->isFunctionPointerType())) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000381 S.Diag(Attr.getLoc(),
382 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
383 : diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000384 << Attr.getName() << 0 /*function*/;
Mike Stump88788fe2009-04-29 19:03:13 +0000385 return false;
386 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000387 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000388
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000389 return true;
390}
391
392static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000393 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000394 d->addAttr(::new (S.Context) NoReturnAttr());
Ted Kremenek40f4ee72009-04-10 00:01:14 +0000395}
396
397static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
398 Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000399 if (HandleCommonNoReturnAttr(d, Attr, S))
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000400 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000401}
402
Alexis Hunt96d5c762009-11-21 08:43:09 +0000403static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) {
404 if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) {
405 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
406 << Attr.getName() << 8; /*function, method, or parameter*/
407 return;
408 }
409 // FIXME: Actually store the attribute on the declaration
410}
411
Ted Kremenek39c59a82008-07-25 04:39:19 +0000412static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
413 // check the attribute arguments.
414 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000415 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000416 return;
417 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000418
Daniel Dunbar70e3eba2008-10-19 02:04:16 +0000419 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000420 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000421 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek39c59a82008-07-25 04:39:19 +0000422 return;
423 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000424
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000425 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek39c59a82008-07-25 04:39:19 +0000426}
427
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000428static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
429 // check the attribute arguments.
430 if (Attr.getNumArgs() != 0) {
431 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
432 return;
433 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000434
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000435 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar311bf292009-02-13 22:48:56 +0000436 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000437 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
438 return;
439 }
440 } else if (!isFunctionOrMethod(d)) {
441 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000442 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000443 return;
444 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000445
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000446 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarfee07a02009-02-13 19:23:53 +0000447}
448
Daniel Dunbar032db472008-07-31 22:40:48 +0000449static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
450 // check the attribute arguments.
451 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000452 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
453 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000454 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000455 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000456
457 int priority = 65535; // FIXME: Do not hardcode such constants.
458 if (Attr.getNumArgs() > 0) {
459 Expr *E = static_cast<Expr *>(Attr.getArg(0));
460 llvm::APSInt Idx(32);
461 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000462 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000463 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000464 return;
465 }
466 priority = Idx.getZExtValue();
467 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000468
Chris Lattner4225e232009-04-14 17:02:11 +0000469 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000470 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000471 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000472 return;
473 }
474
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000475 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000476}
477
478static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
479 // check the attribute arguments.
480 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattner3b054132008-11-19 05:08:23 +0000481 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
482 << "0 or 1";
Daniel Dunbar032db472008-07-31 22:40:48 +0000483 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000484 }
Daniel Dunbar032db472008-07-31 22:40:48 +0000485
486 int priority = 65535; // FIXME: Do not hardcode such constants.
487 if (Attr.getNumArgs() > 0) {
488 Expr *E = static_cast<Expr *>(Attr.getArg(0));
489 llvm::APSInt Idx(32);
490 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000491 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000492 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar032db472008-07-31 22:40:48 +0000493 return;
494 }
495 priority = Idx.getZExtValue();
496 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000497
Anders Carlsson8e82b7f2008-08-22 22:10:48 +0000498 if (!isa<FunctionDecl>(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000499 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000500 << Attr.getName() << 0 /*function*/;
Daniel Dunbar032db472008-07-31 22:40:48 +0000501 return;
502 }
503
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000504 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar032db472008-07-31 22:40:48 +0000505}
506
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000507static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000508 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000509 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000510 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000511 return;
512 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000513
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000514 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000515}
516
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000517static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
518 // check the attribute arguments.
519 if (Attr.getNumArgs() != 0) {
520 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
521 return;
522 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000523
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000524 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000525}
526
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000527static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000528 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000529 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000530 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000531 return;
532 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000533
Chris Lattner4a927cb2008-06-28 23:36:30 +0000534 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000535 Arg = Arg->IgnoreParenCasts();
536 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
Mike Stumpd3bb5572009-07-24 19:02:52 +0000537
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000538 if (Str == 0 || Str->isWide()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000539 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000540 << "visibility" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000541 return;
542 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000543
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000544 const char *TypeStr = Str->getStrData();
545 unsigned TypeLen = Str->getByteLength();
546 VisibilityAttr::VisibilityTypes type;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000547
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000548 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
549 type = VisibilityAttr::DefaultVisibility;
550 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
551 type = VisibilityAttr::HiddenVisibility;
552 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
553 type = VisibilityAttr::HiddenVisibility; // FIXME
554 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
555 type = VisibilityAttr::ProtectedVisibility;
556 else {
Chris Lattnere3d20d92008-11-23 21:45:46 +0000557 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000558 return;
559 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000560
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000561 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000562}
563
Chris Lattner677a3582009-02-14 08:09:34 +0000564static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
565 Sema &S) {
566 if (Attr.getNumArgs() != 0) {
567 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
568 return;
569 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000570
Chris Lattner677a3582009-02-14 08:09:34 +0000571 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
572 if (OCI == 0) {
573 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
574 return;
575 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000576
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000577 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner677a3582009-02-14 08:09:34 +0000578}
579
580static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000581 if (Attr.getNumArgs() != 0) {
582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
583 return;
584 }
Chris Lattner677a3582009-02-14 08:09:34 +0000585 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000586 QualType T = TD->getUnderlyingType();
587 if (!T->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000588 !T->getAs<PointerType>()->getPointeeType()->isRecordType()) {
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000589 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
590 return;
591 }
592 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000593 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanian255c0952009-01-13 23:34:40 +0000594}
595
Mike Stumpd3bb5572009-07-24 19:02:52 +0000596static void
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000597HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
598 if (Attr.getNumArgs() != 0) {
599 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
600 return;
601 }
602
603 if (!isa<FunctionDecl>(D)) {
604 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
605 return;
606 }
607
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000608 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +0000609}
610
Steve Naroff3405a732008-09-18 16:44:58 +0000611static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000612 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +0000613 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000614 << "blocks" << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000615 return;
616 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000617
Steve Naroff3405a732008-09-18 16:44:58 +0000618 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff3405a732008-09-18 16:44:58 +0000620 return;
621 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000622
Steve Naroff3405a732008-09-18 16:44:58 +0000623 BlocksAttr::BlocksAttrTypes type;
Chris Lattner68e48682008-11-20 04:42:34 +0000624 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff3405a732008-09-18 16:44:58 +0000625 type = BlocksAttr::ByRef;
626 else {
Chris Lattner3b054132008-11-19 05:08:23 +0000627 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000628 << "blocks" << Attr.getParameterName();
Steve Naroff3405a732008-09-18 16:44:58 +0000629 return;
630 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000631
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000632 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff3405a732008-09-18 16:44:58 +0000633}
634
Anders Carlssonc181b012008-10-05 18:05:59 +0000635static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
636 // check the attribute arguments.
637 if (Attr.getNumArgs() > 2) {
Chris Lattner3b054132008-11-19 05:08:23 +0000638 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
639 << "0, 1 or 2";
Anders Carlssonc181b012008-10-05 18:05:59 +0000640 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000641 }
642
Anders Carlssonc181b012008-10-05 18:05:59 +0000643 int sentinel = 0;
644 if (Attr.getNumArgs() > 0) {
645 Expr *E = static_cast<Expr *>(Attr.getArg(0));
646 llvm::APSInt Idx(32);
647 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000648 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000649 << "sentinel" << 1 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000650 return;
651 }
652 sentinel = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000653
Anders Carlssonc181b012008-10-05 18:05:59 +0000654 if (sentinel < 0) {
Chris Lattner3b054132008-11-19 05:08:23 +0000655 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
656 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000657 return;
658 }
659 }
660
661 int nullPos = 0;
662 if (Attr.getNumArgs() > 1) {
663 Expr *E = static_cast<Expr *>(Attr.getArg(1));
664 llvm::APSInt Idx(32);
665 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000666 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000667 << "sentinel" << 2 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000668 return;
669 }
670 nullPos = Idx.getZExtValue();
Mike Stumpd3bb5572009-07-24 19:02:52 +0000671
Anders Carlssonc181b012008-10-05 18:05:59 +0000672 if (nullPos > 1 || nullPos < 0) {
673 // FIXME: This error message could be improved, it would be nice
674 // to say what the bounds actually are.
Chris Lattner3b054132008-11-19 05:08:23 +0000675 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
676 << E->getSourceRange();
Anders Carlssonc181b012008-10-05 18:05:59 +0000677 return;
678 }
679 }
680
681 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
John McCall9dd450b2009-09-21 23:43:11 +0000682 const FunctionType *FT = FD->getType()->getAs<FunctionType>();
Chris Lattner9363e312009-03-17 23:03:47 +0000683 assert(FT && "FunctionDecl has non-function type?");
Mike Stumpd3bb5572009-07-24 19:02:52 +0000684
Chris Lattner9363e312009-03-17 23:03:47 +0000685 if (isa<FunctionNoProtoType>(FT)) {
686 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
687 return;
688 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000689
Chris Lattner9363e312009-03-17 23:03:47 +0000690 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000691 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000692 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000693 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000694 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
695 if (!MD->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000696 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlssonc181b012008-10-05 18:05:59 +0000697 return;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000698 }
699 } else if (isa<BlockDecl>(d)) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000700 // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the
701 // caller.
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000702 ;
703 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000704 QualType Ty = V->getType();
Fariborz Jahanian0aa5c452009-05-15 20:33:25 +0000705 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000706 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
John McCall9dd450b2009-09-21 23:43:11 +0000707 : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>();
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000708 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian6802ed92009-05-15 21:18:04 +0000709 int m = Ty->isFunctionPointerType() ? 0 : 1;
710 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000711 return;
712 }
Mike Stump12b8ce12009-08-04 21:02:39 +0000713 } else {
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000714 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000715 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian6607b212009-05-14 20:53:39 +0000716 return;
717 }
Anders Carlssonc181b012008-10-05 18:05:59 +0000718 } else {
Chris Lattner3b054132008-11-19 05:08:23 +0000719 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanian3133a2f2009-05-14 20:57:28 +0000720 << Attr.getName() << 6 /*function, method or block */;
Anders Carlssonc181b012008-10-05 18:05:59 +0000721 return;
722 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000723 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlssonc181b012008-10-05 18:05:59 +0000724}
725
Chris Lattner237f2752009-02-14 07:37:35 +0000726static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
727 // check the attribute arguments.
728 if (Attr.getNumArgs() != 0) {
729 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
730 return;
731 }
732
Nuno Lopes518e3702009-12-20 23:11:08 +0000733 if (!isFunctionOrMethod(D)) {
Chris Lattner237f2752009-02-14 07:37:35 +0000734 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000735 << Attr.getName() << 0 /*function*/;
Chris Lattner237f2752009-02-14 07:37:35 +0000736 return;
737 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000738
Nuno Lopes56abcbd2009-12-22 23:59:52 +0000739 if (getFunctionType(D)->getResultType()->isVoidType()) {
740 S.Diag(Attr.getLoc(), diag::warn_attribute_void_function)
741 << Attr.getName();
742 return;
743 }
744
Nuno Lopes518e3702009-12-20 23:11:08 +0000745 D->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner237f2752009-02-14 07:37:35 +0000746}
747
748static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000749 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000750 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000751 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000752 return;
753 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000754
Fariborz Jahanian41136ee2009-07-16 01:12:24 +0000755 /* weak only applies to non-static declarations */
756 bool isStatic = false;
757 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
758 isStatic = VD->getStorageClass() == VarDecl::Static;
759 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
760 isStatic = FD->getStorageClass() == FunctionDecl::Static;
761 }
762 if (isStatic) {
763 S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
764 dyn_cast<NamedDecl>(D)->getNameAsString();
765 return;
766 }
767
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000768 // TODO: could also be applied to methods?
769 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
770 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000771 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000772 return;
773 }
Mike Stumpd3bb5572009-07-24 19:02:52 +0000774
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000775 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000776}
777
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000778static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
779 // check the attribute arguments.
780 if (Attr.getNumArgs() != 0) {
781 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
782 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +0000783 }
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000784
785 // weak_import only applies to variable & function declarations.
786 bool isDef = false;
787 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
788 isDef = (!VD->hasExternalStorage() || VD->getInit());
789 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000790 isDef = FD->getBody();
Fariborz Jahanian60637982009-05-04 19:35:12 +0000791 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
792 // We ignore weak import on properties and methods
Mike Stump367fee62009-03-18 17:39:31 +0000793 return;
Fariborz Jahaniand3612392009-11-17 19:08:08 +0000794 } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) {
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000795 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000796 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000797 return;
798 }
799
800 // Merge should handle any subsequent violations.
801 if (isDef) {
Mike Stumpd3bb5572009-07-24 19:02:52 +0000802 S.Diag(Attr.getLoc(),
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000803 diag::warn_attribute_weak_import_invalid_on_definition)
804 << "weak_import" << 2 /*variable and function*/;
805 return;
806 }
807
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000808 D->addAttr(::new (S.Context) WeakImportAttr());
Daniel Dunbar5cb85eb2009-03-06 06:39:57 +0000809}
810
Chris Lattner237f2752009-02-14 07:37:35 +0000811static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000812 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000813 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000814 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000815 return;
816 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000817
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000818 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000819 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000820 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000821 return;
822 }
823
Chris Lattner237f2752009-02-14 07:37:35 +0000824 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000825 if (!FD) {
826 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000827 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000828 return;
829 }
830
831 // Currently, the dllimport attribute is ignored for inlined functions.
832 // Warning is emitted.
Douglas Gregor35b57532009-10-27 21:01:01 +0000833 if (FD->isInlineSpecified()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000834 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
835 return;
836 }
837
838 // The attribute is also overridden by a subsequent declaration as dllexport.
839 // Warning is emitted.
840 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
841 nextAttr = nextAttr->getNext()) {
842 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
843 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
844 return;
845 }
846 }
847
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000848 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000849 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
850 return;
851 }
852
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000853 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000854}
855
Chris Lattner237f2752009-02-14 07:37:35 +0000856static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000857 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000858 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000859 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000860 return;
861 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000862
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000863 // Attribute can be applied only to functions or variables.
Chris Lattner237f2752009-02-14 07:37:35 +0000864 if (isa<VarDecl>(D)) {
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000865 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000866 return;
867 }
868
Chris Lattner237f2752009-02-14 07:37:35 +0000869 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000870 if (!FD) {
871 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000872 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000873 return;
874 }
875
Mike Stumpd3bb5572009-07-24 19:02:52 +0000876 // Currently, the dllexport attribute is ignored for inlined functions, unless
877 // the -fkeep-inline-functions flag has been used. Warning is emitted;
Douglas Gregor35b57532009-10-27 21:01:01 +0000878 if (FD->isInlineSpecified()) {
Anton Korobeynikovd72f47a2008-12-26 00:52:02 +0000879 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
880 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
881 return;
882 }
883
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000884 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000885}
886
Nate Begemanf2758702009-06-26 06:32:41 +0000887static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr,
888 Sema &S) {
889 // Attribute has 3 arguments.
890 if (Attr.getNumArgs() != 3) {
891 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
892 return;
893 }
894
895 unsigned WGSize[3];
896 for (unsigned i = 0; i < 3; ++i) {
897 Expr *E = static_cast<Expr *>(Attr.getArg(i));
898 llvm::APSInt ArgNum(32);
899 if (!E->isIntegerConstantExpr(ArgNum, S.Context)) {
900 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
901 << "reqd_work_group_size" << E->getSourceRange();
902 return;
903 }
904 WGSize[i] = (unsigned) ArgNum.getZExtValue();
905 }
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000906 D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1],
Nate Begemanf2758702009-06-26 06:32:41 +0000907 WGSize[2]));
908}
909
Chris Lattner237f2752009-02-14 07:37:35 +0000910static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar648bf782009-02-12 17:28:23 +0000911 // Attribute has no arguments.
912 if (Attr.getNumArgs() != 1) {
913 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
914 return;
915 }
916
917 // Make sure that there is a string literal as the sections's single
918 // argument.
Chris Lattner30ba6742009-08-10 19:03:04 +0000919 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
920 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Daniel Dunbar648bf782009-02-12 17:28:23 +0000921 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +0000922 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section";
Daniel Dunbar648bf782009-02-12 17:28:23 +0000923 return;
924 }
Mike Stump11289f42009-09-09 15:08:12 +0000925
Chris Lattner30ba6742009-08-10 19:03:04 +0000926 // If the target wants to validate the section specifier, make it happen.
Benjamin Kramer5f089122009-11-30 17:08:26 +0000927 std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString());
Chris Lattner30ba6742009-08-10 19:03:04 +0000928 if (Error.empty()) {
Benjamin Kramer5f089122009-11-30 17:08:26 +0000929 D->addAttr(::new (S.Context) SectionAttr(SE->getString()));
Chris Lattner30ba6742009-08-10 19:03:04 +0000930 return;
931 }
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattner30ba6742009-08-10 19:03:04 +0000933 S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target)
934 << Error;
Mike Stump11289f42009-09-09 15:08:12 +0000935
Daniel Dunbar648bf782009-02-12 17:28:23 +0000936}
937
Eli Friedmane4310c82009-11-09 18:38:53 +0000938static void HandleCDeclAttr(Decl *d, const AttributeList &Attr, Sema &S) {
939 // Attribute has no arguments.
940 if (Attr.getNumArgs() != 0) {
941 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
942 return;
943 }
944
945 // Attribute can be applied only to functions.
946 if (!isa<FunctionDecl>(d)) {
947 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
948 << Attr.getName() << 0 /*function*/;
949 return;
950 }
951
952 // cdecl and fastcall attributes are mutually incompatible.
953 if (d->getAttr<FastCallAttr>()) {
954 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
955 << "cdecl" << "fastcall";
956 return;
957 }
958
959 // cdecl and stdcall attributes are mutually incompatible.
960 if (d->getAttr<StdCallAttr>()) {
961 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
962 << "cdecl" << "stdcall";
963 return;
964 }
965
966 d->addAttr(::new (S.Context) CDeclAttr());
967}
968
969
Chris Lattnerb632a6e2008-06-29 00:43:07 +0000970static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000971 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +0000972 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000973 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000974 return;
975 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000976
977 // Attribute can be applied only to functions.
978 if (!isa<FunctionDecl>(d)) {
979 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +0000980 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000981 return;
982 }
983
984 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000985 if (d->getAttr<FastCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +0000986 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
987 << "stdcall" << "fastcall";
988 return;
989 }
990
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +0000991 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +0000992}
993
John McCall4976fd42009-11-04 03:36:09 +0000994/// Diagnose the use of a non-standard calling convention on the given
995/// function.
996static void DiagnoseCConv(FunctionDecl *D, const char *CConv,
997 SourceLocation Loc, Sema &S) {
998 if (!D->hasPrototype()) {
999 S.Diag(Loc, diag::err_cconv_knr) << CConv;
1000 return;
1001 }
1002
1003 const FunctionProtoType *T = D->getType()->getAs<FunctionProtoType>();
1004 if (T->isVariadic()) {
1005 S.Diag(Loc, diag::err_cconv_varargs) << CConv;
1006 return;
1007 }
1008}
1009
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001010static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001011 // Attribute has no arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001012 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001013 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001014 return;
1015 }
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001016
1017 if (!isa<FunctionDecl>(d)) {
1018 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001019 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001020 return;
1021 }
1022
John McCall4976fd42009-11-04 03:36:09 +00001023 DiagnoseCConv(cast<FunctionDecl>(d), "fastcall", Attr.getLoc(), S);
1024
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001025 // stdcall and fastcall attributes are mutually incompatible.
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001026 if (d->getAttr<StdCallAttr>()) {
Anton Korobeynikov484f05e2008-12-23 22:24:07 +00001027 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
1028 << "fastcall" << "stdcall";
1029 return;
1030 }
1031
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001032 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001033}
1034
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001035static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001036 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001037 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001038 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001039 return;
1040 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001041
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001042 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001043}
1044
Anders Carlssonb8316282008-10-05 23:32:53 +00001045static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1046 // check the attribute arguments.
1047 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001048 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001049 return;
1050 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001051
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001052 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001053}
1054
1055static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1056 // check the attribute arguments.
1057 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001058 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlssonb8316282008-10-05 23:32:53 +00001059 return;
1060 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001061
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001062 d->addAttr(::new (S.Context) PureAttr());
Anders Carlssonb8316282008-10-05 23:32:53 +00001063}
1064
Anders Carlssond277d792009-01-31 01:16:18 +00001065static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001066 if (!Attr.getParameterName()) {
Anders Carlssond277d792009-01-31 01:16:18 +00001067 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1068 return;
1069 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001070
Anders Carlssond277d792009-01-31 01:16:18 +00001071 if (Attr.getNumArgs() != 0) {
1072 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1073 return;
1074 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001075
Anders Carlssond277d792009-01-31 01:16:18 +00001076 VarDecl *VD = dyn_cast<VarDecl>(d);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001077
Anders Carlssond277d792009-01-31 01:16:18 +00001078 if (!VD || !VD->hasLocalStorage()) {
1079 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1080 return;
1081 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001082
Anders Carlssond277d792009-01-31 01:16:18 +00001083 // Look up the function
John McCall9f3059a2009-10-09 21:13:30 +00001084 NamedDecl *CleanupDecl
1085 = S.LookupSingleName(S.TUScope, Attr.getParameterName(),
1086 Sema::LookupOrdinaryName);
Anders Carlssond277d792009-01-31 01:16:18 +00001087 if (!CleanupDecl) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001088 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001089 Attr.getParameterName();
1090 return;
1091 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001092
Anders Carlssond277d792009-01-31 01:16:18 +00001093 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1094 if (!FD) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001095 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001096 Attr.getParameterName();
1097 return;
1098 }
1099
Anders Carlssond277d792009-01-31 01:16:18 +00001100 if (FD->getNumParams() != 1) {
Anders Carlsson723f55d2009-02-07 23:16:50 +00001101 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssond277d792009-01-31 01:16:18 +00001102 Attr.getParameterName();
1103 return;
1104 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001105
Anders Carlsson723f55d2009-02-07 23:16:50 +00001106 // We're currently more strict than GCC about what function types we accept.
1107 // If this ever proves to be a problem it should be easy to fix.
1108 QualType Ty = S.Context.getPointerType(VD->getType());
1109 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmanbd0e6732009-04-26 01:30:08 +00001110 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001111 S.Diag(Attr.getLoc(),
Anders Carlsson723f55d2009-02-07 23:16:50 +00001112 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1113 Attr.getParameterName() << ParamTy << Ty;
1114 return;
1115 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001116
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001117 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssond277d792009-01-31 01:16:18 +00001118}
1119
Mike Stumpd3bb5572009-07-24 19:02:52 +00001120/// Handle __attribute__((format_arg((idx)))) attribute based on
1121/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1122static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001123 if (Attr.getNumArgs() != 1) {
1124 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1125 return;
1126 }
1127 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1128 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1129 << Attr.getName() << 0 /*function*/;
1130 return;
1131 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001132 // FIXME: in C++ the implicit 'this' function parameter also counts. this is
1133 // needed in order to be compatible with GCC the index must start with 1.
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001134 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1135 unsigned FirstIdx = 1;
1136 // checks for the 2nd argument
1137 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1138 llvm::APSInt Idx(32);
1139 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1140 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1141 << "format" << 2 << IdxExpr->getSourceRange();
1142 return;
1143 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001144
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001145 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1146 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1147 << "format" << 2 << IdxExpr->getSourceRange();
1148 return;
1149 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001150
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001151 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001152
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001153 // make sure the format string is really a string
1154 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001155
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001156 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1157 if (not_nsstring_type &&
1158 !isCFStringType(Ty, S.Context) &&
1159 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001160 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001161 // FIXME: Should highlight the actual expression that has the wrong type.
1162 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001163 << (not_nsstring_type ? "a string type" : "an NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001164 << IdxExpr->getSourceRange();
1165 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001166 }
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001167 Ty = getFunctionOrMethodResultType(d);
1168 if (!isNSStringType(Ty, S.Context) &&
1169 !isCFStringType(Ty, S.Context) &&
1170 (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001171 !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001172 // FIXME: Should highlight the actual expression that has the wrong type.
1173 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
Mike Stumpd3bb5572009-07-24 19:02:52 +00001174 << (not_nsstring_type ? "string type" : "NSString")
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001175 << IdxExpr->getSourceRange();
1176 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001177 }
1178
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001179 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001180}
1181
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001182enum FormatAttrKind {
1183 CFStringFormat,
1184 NSStringFormat,
1185 StrftimeFormat,
1186 SupportedFormat,
1187 InvalidFormat
1188};
1189
1190/// getFormatAttrKind - Map from format attribute names to supported format
1191/// types.
1192static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) {
1193 // Check for formats that get handled specially.
1194 if (Format == "NSString")
1195 return NSStringFormat;
1196 if (Format == "CFString")
1197 return CFStringFormat;
1198 if (Format == "strftime")
1199 return StrftimeFormat;
1200
1201 // Otherwise, check for supported formats.
1202 if (Format == "scanf" || Format == "printf" || Format == "printf0" ||
1203 Format == "strfmon" || Format == "cmn_err" || Format == "strftime" ||
1204 Format == "NSString" || Format == "CFString" || Format == "vcmn_err" ||
1205 Format == "zcmn_err")
1206 return SupportedFormat;
1207
1208 return InvalidFormat;
1209}
1210
Mike Stumpd3bb5572009-07-24 19:02:52 +00001211/// Handle __attribute__((format(type,idx,firstarg))) attributes based on
1212/// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001213static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001214
Chris Lattner4a927cb2008-06-28 23:36:30 +00001215 if (!Attr.getParameterName()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001216 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001217 << "format" << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001218 return;
1219 }
1220
Chris Lattner4a927cb2008-06-28 23:36:30 +00001221 if (Attr.getNumArgs() != 2) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001222 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001223 return;
1224 }
1225
Fariborz Jahanian4447e172009-05-15 23:15:03 +00001226 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001227 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001228 << Attr.getName() << 0 /*function*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001229 return;
1230 }
1231
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001232 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001233 unsigned FirstIdx = 1;
1234
Daniel Dunbar07d07852009-10-18 21:17:35 +00001235 llvm::StringRef Format = Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001236
1237 // Normalize the argument, __foo__ becomes foo.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001238 if (Format.startswith("__") && Format.endswith("__"))
1239 Format = Format.substr(2, Format.size() - 4);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001240
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001241 // Check for supported formats.
1242 FormatAttrKind Kind = getFormatAttrKind(Format);
1243 if (Kind == InvalidFormat) {
Chris Lattner3b054132008-11-19 05:08:23 +00001244 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Daniel Dunbar07d07852009-10-18 21:17:35 +00001245 << "format" << Attr.getParameterName()->getName();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001246 return;
1247 }
1248
1249 // checks for the 2nd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001250 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001251 llvm::APSInt Idx(32);
1252 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001253 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001254 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001255 return;
1256 }
1257
Anders Carlssonbe96bc92009-08-25 14:12:34 +00001258 // FIXME: We should handle the implicit 'this' parameter in a more generic
1259 // way that can be used for other arguments.
1260 bool HasImplicitThisParam = false;
1261 if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) {
1262 if (MD->isInstance()) {
1263 HasImplicitThisParam = true;
1264 NumArgs++;
1265 }
1266 }
Mike Stump11289f42009-09-09 15:08:12 +00001267
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001268 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001269 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001270 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001271 return;
1272 }
1273
1274 // FIXME: Do we need to bounds check?
1275 unsigned ArgIdx = Idx.getZExtValue() - 1;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001276
Sebastian Redl6eedcc12009-11-17 18:02:24 +00001277 if (HasImplicitThisParam) {
1278 if (ArgIdx == 0) {
1279 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1280 << "a string type" << IdxExpr->getSourceRange();
1281 return;
1282 }
1283 ArgIdx--;
1284 }
Mike Stump11289f42009-09-09 15:08:12 +00001285
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001286 // make sure the format string is really a string
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001287 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001288
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001289 if (Kind == CFStringFormat) {
Daniel Dunbar980c6692008-09-26 03:32:58 +00001290 if (!isCFStringType(Ty, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001291 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1292 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar980c6692008-09-26 03:32:58 +00001293 return;
1294 }
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001295 } else if (Kind == NSStringFormat) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001296 // FIXME: do we need to check if the type is NSString*? What are the
1297 // semantics?
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001298 if (!isNSStringType(Ty, S.Context)) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001299 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001300 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1301 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001302 return;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001303 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001304 } else if (!Ty->isPointerType() ||
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001305 !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) {
Mike Stump87c57ac2009-05-16 07:39:55 +00001306 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattner3b054132008-11-19 05:08:23 +00001307 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1308 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001309 return;
1310 }
1311
1312 // check the 3rd argument
Chris Lattner4a927cb2008-06-28 23:36:30 +00001313 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001314 llvm::APSInt FirstArg(32);
1315 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001316 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001317 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001318 return;
1319 }
1320
1321 // check if the function is variadic if the 3rd argument non-zero
1322 if (FirstArg != 0) {
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00001323 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001324 ++NumArgs; // +1 for ...
1325 } else {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001326 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001327 return;
1328 }
1329 }
1330
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001331 // strftime requires FirstArg to be 0 because it doesn't read from any
1332 // variable the input is just the current time + the format string.
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001333 if (Kind == StrftimeFormat) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001334 if (FirstArg != 0) {
Chris Lattner3b054132008-11-19 05:08:23 +00001335 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1336 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001337 return;
1338 }
1339 // if 0 it disables parameter checking (to use with e.g. va_list)
1340 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattner3b054132008-11-19 05:08:23 +00001341 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001342 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001343 return;
1344 }
1345
Daniel Dunbarccbd9a42009-10-18 02:09:17 +00001346 d->addAttr(::new (S.Context) FormatAttr(Format, Idx.getZExtValue(),
1347 FirstArg.getZExtValue()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001348}
1349
Chris Lattnera663a0a2008-06-29 00:28:59 +00001350static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1351 Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001352 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001353 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001354 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001355 return;
1356 }
1357
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001358 // Try to find the underlying union declaration.
1359 RecordDecl *RD = 0;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001360 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001361 if (TD && TD->getUnderlyingType()->isUnionType())
1362 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1363 else
1364 RD = dyn_cast<RecordDecl>(d);
1365
1366 if (!RD || !RD->isUnion()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001367 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001368 << Attr.getName() << 1 /*union*/;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001369 return;
1370 }
1371
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001372 if (!RD->isDefinition()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001373 S.Diag(Attr.getLoc(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001374 diag::warn_transparent_union_attribute_not_definition);
1375 return;
1376 }
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001377
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001378 RecordDecl::field_iterator Field = RD->field_begin(),
1379 FieldEnd = RD->field_end();
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001380 if (Field == FieldEnd) {
1381 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1382 return;
1383 }
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001384
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001385 FieldDecl *FirstField = *Field;
1386 QualType FirstType = FirstField->getType();
1387 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001388 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001389 diag::warn_transparent_union_attribute_floating);
1390 return;
1391 }
1392
1393 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1394 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1395 for (; Field != FieldEnd; ++Field) {
1396 QualType FieldType = Field->getType();
1397 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1398 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1399 // Warn if we drop the attribute.
1400 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001401 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001402 : S.Context.getTypeAlign(FieldType);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001403 S.Diag(Field->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001404 diag::warn_transparent_union_attribute_field_size_align)
1405 << isSize << Field->getDeclName() << FieldBits;
1406 unsigned FirstBits = isSize? FirstSize : FirstAlign;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001407 S.Diag(FirstField->getLocation(),
Douglas Gregor0cfbdab2009-04-29 22:16:16 +00001408 diag::note_transparent_union_first_field_size_align)
1409 << isSize << FirstBits;
Eli Friedman7c9ba6a2008-09-02 05:19:23 +00001410 return;
1411 }
1412 }
1413
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001414 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001415}
1416
Chris Lattnera663a0a2008-06-29 00:28:59 +00001417static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001418 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001419 if (Attr.getNumArgs() != 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001420 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001421 return;
1422 }
Chris Lattner30ba6742009-08-10 19:03:04 +00001423 Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0));
1424 StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr);
Mike Stumpd3bb5572009-07-24 19:02:52 +00001425
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001426 // Make sure that there is a string literal as the annotation's single
1427 // argument.
1428 if (!SE) {
Chris Lattner30ba6742009-08-10 19:03:04 +00001429 S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate";
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001430 return;
1431 }
Benjamin Kramer5f089122009-11-30 17:08:26 +00001432 d->addAttr(::new (S.Context) AnnotateAttr(SE->getString()));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001433}
1434
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001435static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001436 // check the attribute arguments.
Chris Lattner4a927cb2008-06-28 23:36:30 +00001437 if (Attr.getNumArgs() > 1) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001438 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001439 return;
1440 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001441
1442 //FIXME: The C++0x version of this attribute has more limited applicabilty
1443 // than GNU's, and should error out when it is used to specify a
1444 // weaker alignment, rather than being silently ignored.
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001445
1446 unsigned Align = 0;
Chris Lattner4a927cb2008-06-28 23:36:30 +00001447 if (Attr.getNumArgs() == 0) {
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001448 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbaraac5bf12009-02-18 20:06:09 +00001449 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001450 Align = 128;
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001451 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001452 return;
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001453 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001454
Chris Lattner4627b742008-06-28 23:50:44 +00001455 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1456 llvm::APSInt Alignment(32);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001457 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattner3b054132008-11-19 05:08:23 +00001458 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1459 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner4627b742008-06-28 23:50:44 +00001460 return;
1461 }
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001462 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
Mike Stumpd3bb5572009-07-24 19:02:52 +00001463 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
Daniel Dunbar6e8c07d2009-02-16 23:37:57 +00001464 << alignmentExpr->getSourceRange();
1465 return;
1466 }
1467
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001468 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner2c6fcf52008-06-26 18:38:35 +00001469}
Chris Lattneracbc2d22008-06-27 22:18:37 +00001470
Mike Stumpd3bb5572009-07-24 19:02:52 +00001471/// HandleModeAttr - This attribute modifies the width of a decl with primitive
1472/// type.
Chris Lattneracbc2d22008-06-27 22:18:37 +00001473///
Mike Stumpd3bb5572009-07-24 19:02:52 +00001474/// Despite what would be logical, the mode attribute is a decl attribute, not a
1475/// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be
1476/// HImode, not an intermediate pointer.
Chris Lattnera663a0a2008-06-29 00:28:59 +00001477static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001478 // This attribute isn't documented, but glibc uses it. It changes
1479 // the width of an int or unsigned int to the specified size.
1480
1481 // Check that there aren't any arguments
1482 if (Attr.getNumArgs() != 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001484 return;
1485 }
1486
1487 IdentifierInfo *Name = Attr.getParameterName();
1488 if (!Name) {
Chris Lattnera663a0a2008-06-29 00:28:59 +00001489 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001490 return;
1491 }
Daniel Dunbarafff4342009-10-18 02:09:24 +00001492
Daniel Dunbar07d07852009-10-18 21:17:35 +00001493 llvm::StringRef Str = Attr.getParameterName()->getName();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001494
1495 // Normalize the attribute name, __foo__ becomes foo.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001496 if (Str.startswith("__") && Str.endswith("__"))
1497 Str = Str.substr(2, Str.size() - 4);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001498
1499 unsigned DestWidth = 0;
1500 bool IntegerMode = true;
Eli Friedman4735374e2009-03-03 06:41:03 +00001501 bool ComplexMode = false;
Daniel Dunbarafff4342009-10-18 02:09:24 +00001502 switch (Str.size()) {
Chris Lattneracbc2d22008-06-27 22:18:37 +00001503 case 2:
Eli Friedman4735374e2009-03-03 06:41:03 +00001504 switch (Str[0]) {
1505 case 'Q': DestWidth = 8; break;
1506 case 'H': DestWidth = 16; break;
1507 case 'S': DestWidth = 32; break;
1508 case 'D': DestWidth = 64; break;
1509 case 'X': DestWidth = 96; break;
1510 case 'T': DestWidth = 128; break;
1511 }
1512 if (Str[1] == 'F') {
1513 IntegerMode = false;
1514 } else if (Str[1] == 'C') {
1515 IntegerMode = false;
1516 ComplexMode = true;
1517 } else if (Str[1] != 'I') {
1518 DestWidth = 0;
1519 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001520 break;
1521 case 4:
1522 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1523 // pointer on PIC16 and other embedded platforms.
Daniel Dunbarafff4342009-10-18 02:09:24 +00001524 if (Str == "word")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001525 DestWidth = S.Context.Target.getPointerWidth(0);
Daniel Dunbarafff4342009-10-18 02:09:24 +00001526 else if (Str == "byte")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001527 DestWidth = S.Context.Target.getCharWidth();
Chris Lattneracbc2d22008-06-27 22:18:37 +00001528 break;
1529 case 7:
Daniel Dunbarafff4342009-10-18 02:09:24 +00001530 if (Str == "pointer")
Chris Lattnera663a0a2008-06-29 00:28:59 +00001531 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001532 break;
1533 }
1534
1535 QualType OldTy;
1536 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1537 OldTy = TD->getUnderlyingType();
1538 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1539 OldTy = VD->getType();
1540 else {
Chris Lattner3b054132008-11-19 05:08:23 +00001541 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1542 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattneracbc2d22008-06-27 22:18:37 +00001543 return;
1544 }
Eli Friedman4735374e2009-03-03 06:41:03 +00001545
John McCall9dd450b2009-09-21 23:43:11 +00001546 if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType())
Eli Friedman4735374e2009-03-03 06:41:03 +00001547 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1548 else if (IntegerMode) {
1549 if (!OldTy->isIntegralType())
1550 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1551 } else if (ComplexMode) {
1552 if (!OldTy->isComplexType())
1553 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1554 } else {
1555 if (!OldTy->isFloatingType())
1556 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1557 }
1558
Mike Stump87c57ac2009-05-16 07:39:55 +00001559 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1560 // and friends, at least with glibc.
1561 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1562 // width on unusual platforms.
Eli Friedman1efaaea2009-02-13 02:31:07 +00001563 // FIXME: Make sure floating-point mappings are accurate
1564 // FIXME: Support XF and TF types
Chris Lattneracbc2d22008-06-27 22:18:37 +00001565 QualType NewTy;
1566 switch (DestWidth) {
1567 case 0:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001568 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001569 return;
1570 default:
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001571 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001572 return;
1573 case 8:
Eli Friedman4735374e2009-03-03 06:41:03 +00001574 if (!IntegerMode) {
1575 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1576 return;
1577 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001578 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001579 NewTy = S.Context.SignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001580 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001581 NewTy = S.Context.UnsignedCharTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001582 break;
1583 case 16:
Eli Friedman4735374e2009-03-03 06:41:03 +00001584 if (!IntegerMode) {
1585 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1586 return;
1587 }
Chris Lattneracbc2d22008-06-27 22:18:37 +00001588 if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001589 NewTy = S.Context.ShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001590 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001591 NewTy = S.Context.UnsignedShortTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001592 break;
1593 case 32:
1594 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001595 NewTy = S.Context.FloatTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001596 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001597 NewTy = S.Context.IntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001598 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001599 NewTy = S.Context.UnsignedIntTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001600 break;
1601 case 64:
1602 if (!IntegerMode)
Chris Lattnera663a0a2008-06-29 00:28:59 +00001603 NewTy = S.Context.DoubleTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001604 else if (OldTy->isSignedIntegerType())
Chris Lattnera663a0a2008-06-29 00:28:59 +00001605 NewTy = S.Context.LongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001606 else
Chris Lattnera663a0a2008-06-29 00:28:59 +00001607 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001608 break;
Eli Friedman4735374e2009-03-03 06:41:03 +00001609 case 96:
1610 NewTy = S.Context.LongDoubleTy;
1611 break;
Eli Friedman1efaaea2009-02-13 02:31:07 +00001612 case 128:
1613 if (!IntegerMode) {
1614 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1615 return;
1616 }
1617 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman4735374e2009-03-03 06:41:03 +00001618 break;
Chris Lattneracbc2d22008-06-27 22:18:37 +00001619 }
1620
Eli Friedman4735374e2009-03-03 06:41:03 +00001621 if (ComplexMode) {
1622 NewTy = S.Context.getComplexType(NewTy);
Chris Lattneracbc2d22008-06-27 22:18:37 +00001623 }
1624
1625 // Install the new type.
John McCall703a3f82009-10-24 08:00:42 +00001626 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
1627 // FIXME: preserve existing source info.
John McCallbcd03502009-12-07 02:54:59 +00001628 TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy));
John McCall703a3f82009-10-24 08:00:42 +00001629 } else
Chris Lattneracbc2d22008-06-27 22:18:37 +00001630 cast<ValueDecl>(D)->setType(NewTy);
1631}
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001632
Mike Stump3722f582009-08-26 22:31:08 +00001633static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001634 // check the attribute arguments.
1635 if (Attr.getNumArgs() > 0) {
1636 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1637 return;
1638 }
Anders Carlsson63784f42009-02-13 08:11:52 +00001639
Anders Carlsson88097122009-02-19 19:16:48 +00001640 if (!isFunctionOrMethod(d)) {
Anders Carlsson76187b42009-02-13 06:46:13 +00001641 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001642 << Attr.getName() << 0 /*function*/;
Anders Carlsson76187b42009-02-13 06:46:13 +00001643 return;
1644 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001645
Mike Stump3722f582009-08-26 22:31:08 +00001646 d->addAttr(::new (S.Context) NoDebugAttr());
Anders Carlsson76187b42009-02-13 06:46:13 +00001647}
1648
Mike Stump3722f582009-08-26 22:31:08 +00001649static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson88097122009-02-19 19:16:48 +00001650 // check the attribute arguments.
1651 if (Attr.getNumArgs() != 0) {
1652 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1653 return;
1654 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001655
Chris Lattner4225e232009-04-14 17:02:11 +00001656 if (!isa<FunctionDecl>(d)) {
Anders Carlsson88097122009-02-19 19:16:48 +00001657 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001658 << Attr.getName() << 0 /*function*/;
Anders Carlsson88097122009-02-19 19:16:48 +00001659 return;
1660 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001661
Mike Stump3722f582009-08-26 22:31:08 +00001662 d->addAttr(::new (S.Context) NoInlineAttr());
Anders Carlsson88097122009-02-19 19:16:48 +00001663}
1664
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001665static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001666 // check the attribute arguments.
1667 if (Attr.getNumArgs() != 0) {
1668 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1669 return;
1670 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001671
Chris Lattner4225e232009-04-14 17:02:11 +00001672 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1673 if (Fn == 0) {
Chris Lattnereaad6b72009-04-14 16:30:50 +00001674 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001675 << Attr.getName() << 0 /*function*/;
Chris Lattnereaad6b72009-04-14 16:30:50 +00001676 return;
1677 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001678
Douglas Gregor35b57532009-10-27 21:01:01 +00001679 if (!Fn->isInlineSpecified()) {
Chris Lattnerddf6ca02009-04-20 19:12:28 +00001680 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattner4225e232009-04-14 17:02:11 +00001681 return;
1682 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001683
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001684 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattnereaad6b72009-04-14 16:30:50 +00001685}
1686
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001687static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1688 // check the attribute arguments.
1689 if (Attr.getNumArgs() != 1) {
Eli Friedman7044b762009-03-27 21:06:47 +00001690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001691 return;
1692 }
Eli Friedman7044b762009-03-27 21:06:47 +00001693
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001694 if (!isFunctionOrMethod(d)) {
1695 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek3b204e42009-05-13 21:07:32 +00001696 << Attr.getName() << 0 /*function*/;
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001697 return;
1698 }
Eli Friedman7044b762009-03-27 21:06:47 +00001699
1700 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1701 llvm::APSInt NumParams(32);
1702 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1703 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1704 << "regparm" << NumParamsExpr->getSourceRange();
1705 return;
1706 }
1707
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001708 if (S.Context.Target.getRegParmMax() == 0) {
1709 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman7044b762009-03-27 21:06:47 +00001710 << NumParamsExpr->getSourceRange();
1711 return;
1712 }
1713
Anton Korobeynikov1dfc5f52009-04-04 10:27:50 +00001714 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov6953ef22009-04-03 23:38:25 +00001715 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1716 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman7044b762009-03-27 21:06:47 +00001717 return;
1718 }
1719
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001720 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahaniana2d609e2009-03-27 18:38:55 +00001721}
1722
Alexis Hunt96d5c762009-11-21 08:43:09 +00001723static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1724 // check the attribute arguments.
1725 if (Attr.getNumArgs() != 0) {
1726 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1727 return;
1728 }
1729
1730 if (!isa<CXXRecordDecl>(d)
1731 && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) {
1732 S.Diag(Attr.getLoc(),
1733 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1734 : diag::warn_attribute_wrong_decl_type)
1735 << Attr.getName() << 7 /*virtual method or class*/;
1736 return;
1737 }
Alexis Hunt54a02542009-11-25 04:20:27 +00001738
1739 // FIXME: Conform to C++0x redeclaration rules.
1740
1741 if (d->getAttr<FinalAttr>()) {
1742 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final";
1743 return;
1744 }
Alexis Hunt96d5c762009-11-21 08:43:09 +00001745
1746 d->addAttr(::new (S.Context) FinalAttr());
1747}
1748
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001749//===----------------------------------------------------------------------===//
Alexis Hunt54a02542009-11-25 04:20:27 +00001750// C++0x member checking attributes
1751//===----------------------------------------------------------------------===//
1752
1753static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1754 if (Attr.getNumArgs() != 0) {
1755 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1756 return;
1757 }
1758
1759 if (!isa<CXXRecordDecl>(d)) {
1760 S.Diag(Attr.getLoc(),
1761 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1762 : diag::warn_attribute_wrong_decl_type)
1763 << Attr.getName() << 9 /*class*/;
1764 return;
1765 }
1766
1767 if (d->getAttr<BaseCheckAttr>()) {
1768 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check";
1769 return;
1770 }
1771
1772 d->addAttr(::new (S.Context) BaseCheckAttr());
1773}
1774
1775static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1776 if (Attr.getNumArgs() != 0) {
1777 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1778 return;
1779 }
1780
1781 if (!isa<RecordDecl>(d->getDeclContext())) {
1782 // FIXME: It's not the type that's the problem
1783 S.Diag(Attr.getLoc(),
1784 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1785 : diag::warn_attribute_wrong_decl_type)
1786 << Attr.getName() << 11 /*member*/;
1787 return;
1788 }
1789
1790 // FIXME: Conform to C++0x redeclaration rules.
1791
1792 if (d->getAttr<HidingAttr>()) {
1793 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding";
1794 return;
1795 }
1796
1797 d->addAttr(::new (S.Context) HidingAttr());
1798}
1799
1800static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1801 if (Attr.getNumArgs() != 0) {
1802 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1803 return;
1804 }
1805
1806 if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) {
1807 // FIXME: It's not the type that's the problem
1808 S.Diag(Attr.getLoc(),
1809 Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type
1810 : diag::warn_attribute_wrong_decl_type)
1811 << Attr.getName() << 10 /*virtual method*/;
1812 return;
1813 }
1814
1815 // FIXME: Conform to C++0x redeclaration rules.
1816
1817 if (d->getAttr<OverrideAttr>()) {
1818 S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override";
1819 return;
1820 }
1821
1822 d->addAttr(::new (S.Context) OverrideAttr());
1823}
1824
1825//===----------------------------------------------------------------------===//
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001826// Checker-specific attribute handlers.
1827//===----------------------------------------------------------------------===//
1828
1829static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1830 Sema &S) {
1831
Ted Kremenek3b204e42009-05-13 21:07:32 +00001832 QualType RetTy;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001833
Ted Kremenek3b204e42009-05-13 21:07:32 +00001834 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1835 RetTy = MD->getResultType();
1836 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1837 RetTy = FD->getResultType();
1838 else {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001839 SourceLocation L = Attr.getLoc();
1840 S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type)
1841 << SourceRange(L, L) << Attr.getName() << 3 /* function or method */;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001842 return;
1843 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001844
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001845 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>()
John McCall9dd450b2009-09-21 23:43:11 +00001846 || RetTy->getAs<ObjCObjectPointerType>())) {
Ted Kremenekeebcf572009-08-19 23:56:48 +00001847 SourceLocation L = Attr.getLoc();
1848 S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type)
1849 << SourceRange(L, L) << Attr.getName();
Mike Stumpd3bb5572009-07-24 19:02:52 +00001850 return;
Ted Kremenek3b204e42009-05-13 21:07:32 +00001851 }
Mike Stumpd3bb5572009-07-24 19:02:52 +00001852
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001853 switch (Attr.getKind()) {
1854 default:
1855 assert(0 && "invalid ownership attribute");
1856 return;
1857 case AttributeList::AT_cf_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001858 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001859 return;
1860 case AttributeList::AT_ns_returns_retained:
Argyrios Kyrtzidisb4b64ca2009-06-30 02:34:44 +00001861 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001862 return;
1863 };
1864}
1865
1866//===----------------------------------------------------------------------===//
Chris Lattner9e2aafe2008-06-29 00:23:49 +00001867// Top Level Sema Entry Points
1868//===----------------------------------------------------------------------===//
1869
Sebastian Redlfc24b632008-12-21 19:24:58 +00001870/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001871/// the attribute applies to decls. If the attribute is a type attribute, just
Alexis Hunt96d5c762009-11-21 08:43:09 +00001872/// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to
1873/// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4).
Mike Stumpd3bb5572009-07-24 19:02:52 +00001874static void ProcessDeclAttribute(Scope *scope, Decl *D,
1875 const AttributeList &Attr, Sema &S) {
Eli Friedman53339e02009-06-08 23:27:34 +00001876 if (Attr.isDeclspecAttribute())
1877 // FIXME: Try to deal with __declspec attributes!
1878 return;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001879 switch (Attr.getKind()) {
Daniel Dunbar032db472008-07-31 22:40:48 +00001880 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001881 case AttributeList::AT_address_space:
Fariborz Jahanian257eac62009-02-18 17:52:36 +00001882 case AttributeList::AT_objc_gc:
John Thompson47981222009-12-04 21:51:28 +00001883 case AttributeList::AT_vector_size:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001884 // Ignore these, these are type attributes, handled by
1885 // ProcessTypeAttributes.
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001886 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001887 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1888 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001889 case AttributeList::AT_always_inline:
Daniel Dunbar03a38442008-10-28 00:17:57 +00001890 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenek40f4ee72009-04-10 00:01:14 +00001891 case AttributeList::AT_analyzer_noreturn:
Mike Stumpd3bb5572009-07-24 19:02:52 +00001892 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001893 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1894 case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break;
Alexis Hunt96d5c762009-11-21 08:43:09 +00001895 case AttributeList::AT_carries_dependency:
Alexis Hunt54a02542009-11-25 04:20:27 +00001896 HandleDependencyAttr (D, Attr, S); break;
1897 case AttributeList::AT_cdecl: HandleCDeclAttr (D, Attr, S); break;
1898 case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break;
1899 case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break;
1900 case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break;
1901 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1902 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001903 case AttributeList::AT_ext_vector_type:
Douglas Gregor758a8692009-06-17 21:51:59 +00001904 HandleExtVectorTypeAttr(scope, D, Attr, S);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001905 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001906 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1907 case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break;
1908 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
1909 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
1910 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break;
1911 case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break;
1912 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
1913 case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break;
1914 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1915 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1916 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
1917 case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break;
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001918
1919 // Checker-specific.
1920 case AttributeList::AT_ns_returns_retained:
1921 case AttributeList::AT_cf_returns_retained:
1922 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1923
Nate Begemanf2758702009-06-26 06:32:41 +00001924 case AttributeList::AT_reqd_wg_size:
1925 HandleReqdWorkGroupSize(D, Attr, S); break;
1926
Alexis Hunt54a02542009-11-25 04:20:27 +00001927 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
1928 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
1929 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
1930 case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break;
1931 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
1932 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001933 case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break;
Chris Lattner237f2752009-02-14 07:37:35 +00001934 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1935 break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001936 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
1937 case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001938 case AttributeList::AT_transparent_union:
1939 HandleTransparentUnionAttr(D, Attr, S);
1940 break;
Chris Lattner677a3582009-02-14 08:09:34 +00001941 case AttributeList::AT_objc_exception:
1942 HandleObjCExceptionAttr(D, Attr, S);
1943 break;
Douglas Gregor4e5cbdc2009-02-11 23:02:49 +00001944 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Alexis Hunt54a02542009-11-25 04:20:27 +00001945 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
1946 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
1947 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
1948 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1949 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
1950 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
1951 case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break;
1952 case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break;
1953 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Mike Stumpd3bb5572009-07-24 19:02:52 +00001954 case AttributeList::IgnoredAttribute:
Chris Lattner6d7ffe02009-04-25 18:44:54 +00001955 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlssonb4f31342009-02-13 08:16:43 +00001956 // Just ignore
1957 break;
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001958 default:
Anders Carlsson76187b42009-02-13 06:46:13 +00001959 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001960 break;
1961 }
1962}
1963
1964/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1965/// attribute list to the specified decl, ignoring any type attributes.
Douglas Gregor758a8692009-06-17 21:51:59 +00001966void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) {
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001967 while (AttrList) {
Douglas Gregor758a8692009-06-17 21:51:59 +00001968 ProcessDeclAttribute(S, D, *AttrList, *this);
Chris Lattnerb632a6e2008-06-29 00:43:07 +00001969 AttrList = AttrList->getNext();
1970 }
1971}
1972
Ryan Flynn7d470f32009-07-30 03:15:39 +00001973/// DeclClonePragmaWeak - clone existing decl (maybe definition),
1974/// #pragma weak needs a non-definition decl and source may not have one
Mike Stump11289f42009-09-09 15:08:12 +00001975NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) {
Ryan Flynnd963a492009-07-31 02:52:19 +00001976 assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND));
Ryan Flynn7d470f32009-07-30 03:15:39 +00001977 NamedDecl *NewD = 0;
1978 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) {
1979 NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(),
1980 FD->getLocation(), DeclarationName(II),
John McCallbcd03502009-12-07 02:54:59 +00001981 FD->getType(), FD->getTypeSourceInfo());
Ryan Flynn7d470f32009-07-30 03:15:39 +00001982 } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) {
1983 NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(),
1984 VD->getLocation(), II,
John McCallbcd03502009-12-07 02:54:59 +00001985 VD->getType(), VD->getTypeSourceInfo(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001986 VD->getStorageClass());
Ryan Flynn7d470f32009-07-30 03:15:39 +00001987 }
1988 return NewD;
1989}
1990
1991/// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak
1992/// applied to it, possibly with an alias.
Ryan Flynnd963a492009-07-31 02:52:19 +00001993void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) {
Chris Lattnere6eab982009-09-08 18:10:11 +00001994 if (W.getUsed()) return; // only do this once
1995 W.setUsed(true);
1996 if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...))
1997 IdentifierInfo *NDId = ND->getIdentifier();
1998 NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias());
1999 NewD->addAttr(::new (Context) AliasAttr(NDId->getName()));
2000 NewD->addAttr(::new (Context) WeakAttr());
2001 WeakTopLevelDecl.push_back(NewD);
2002 // FIXME: "hideous" code from Sema::LazilyCreateBuiltin
2003 // to insert Decl at TU scope, sorry.
2004 DeclContext *SavedContext = CurContext;
2005 CurContext = Context.getTranslationUnitDecl();
2006 PushOnScopeChains(NewD, S);
2007 CurContext = SavedContext;
2008 } else { // just add weak to existing
2009 ND->addAttr(::new (Context) WeakAttr());
Ryan Flynn7d470f32009-07-30 03:15:39 +00002010 }
2011}
2012
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002013/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
2014/// it, apply them to D. This is a bit tricky because PD can have attributes
2015/// specified in many different places, and we need to find and apply them all.
Douglas Gregor758a8692009-06-17 21:51:59 +00002016void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) {
Ryan Flynn7d470f32009-07-30 03:15:39 +00002017 // Handle #pragma weak
2018 if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
2019 if (ND->hasLinkage()) {
2020 WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier());
2021 if (W != WeakInfo()) {
Ryan Flynnd963a492009-07-31 02:52:19 +00002022 // Identifier referenced by #pragma weak before it was declared
2023 DeclApplyPragmaWeak(S, ND, W);
Ryan Flynn7d470f32009-07-30 03:15:39 +00002024 WeakUndeclaredIdentifiers[ND->getIdentifier()] = W;
2025 }
2026 }
2027 }
2028
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002029 // Apply decl attributes from the DeclSpec if present.
2030 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002031 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002032
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002033 // Walk the declarator structure, applying decl attributes that were in a type
2034 // position to the decl itself. This handles cases like:
2035 // int *__attr__(x)** D;
2036 // when X is a decl attribute.
2037 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
2038 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
Douglas Gregor758a8692009-06-17 21:51:59 +00002039 ProcessDeclAttributeList(S, D, Attrs);
Mike Stumpd3bb5572009-07-24 19:02:52 +00002040
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002041 // Finally, apply any attributes on the decl itself.
2042 if (const AttributeList *Attrs = PD.getAttributes())
Douglas Gregor758a8692009-06-17 21:51:59 +00002043 ProcessDeclAttributeList(S, D, Attrs);
Chris Lattner9e2aafe2008-06-29 00:23:49 +00002044}
John McCall28a6aea2009-11-04 02:18:39 +00002045
2046/// PushParsingDeclaration - Enter a new "scope" of deprecation
2047/// warnings.
2048///
2049/// The state token we use is the start index of this scope
2050/// on the warning stack.
2051Action::ParsingDeclStackState Sema::PushParsingDeclaration() {
2052 ParsingDeclDepth++;
2053 return (ParsingDeclStackState) DelayedDeprecationWarnings.size();
2054}
2055
2056static bool isDeclDeprecated(Decl *D) {
2057 do {
2058 if (D->hasAttr<DeprecatedAttr>())
2059 return true;
2060 } while ((D = cast_or_null<Decl>(D->getDeclContext())));
2061 return false;
2062}
2063
2064void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) {
2065 assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack");
2066 ParsingDeclDepth--;
2067
2068 if (DelayedDeprecationWarnings.empty())
2069 return;
2070
2071 unsigned SavedIndex = (unsigned) S;
2072 assert(SavedIndex <= DelayedDeprecationWarnings.size() &&
2073 "saved index is out of bounds");
2074
2075 if (Ctx && !isDeclDeprecated(Ctx.getAs<Decl>())) {
2076 for (unsigned I = 0, E = DelayedDeprecationWarnings.size(); I != E; ++I) {
2077 SourceLocation Loc = DelayedDeprecationWarnings[I].first;
2078 NamedDecl *&ND = DelayedDeprecationWarnings[I].second;
2079 if (ND) {
2080 Diag(Loc, diag::warn_deprecated) << ND->getDeclName();
2081
2082 // Prevent this from triggering multiple times.
2083 ND = 0;
2084 }
2085 }
2086 }
2087
2088 DelayedDeprecationWarnings.set_size(SavedIndex);
2089}
2090
2091void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) {
2092 // Delay if we're currently parsing a declaration.
2093 if (ParsingDeclDepth) {
2094 DelayedDeprecationWarnings.push_back(std::make_pair(Loc, D));
2095 return;
2096 }
2097
2098 // Otherwise, don't warn if our current context is deprecated.
2099 if (isDeclDeprecated(cast<Decl>(CurContext)))
2100 return;
2101
2102 Diag(Loc, diag::warn_deprecated) << D->getDeclName();
2103}