blob: 99b4d77fad42f4a2860c68f688618dd805106e92 [file] [log] [blame]
Chris Lattner6b6b5372008-06-26 18:38:35 +00001//===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements decl-related attribute processing.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Sema.h"
15#include "clang/AST/ASTContext.h"
Daniel Dunbaracc5f3e2008-08-11 06:23:49 +000016#include "clang/AST/DeclObjC.h"
17#include "clang/AST/Expr.h"
Chris Lattnerfbf13472008-06-27 22:18:37 +000018#include "clang/Basic/TargetInfo.h"
Daniel Dunbar12bc6922008-08-11 03:27:53 +000019#include "clang/Parse/DeclSpec.h"
Ted Kremenek6e1eb872008-07-22 16:56:21 +000020#include <llvm/ADT/StringExtras.h>
Chris Lattner6b6b5372008-06-26 18:38:35 +000021using namespace clang;
22
Chris Lattnere5c5ee12008-06-29 00:16:31 +000023//===----------------------------------------------------------------------===//
24// Helper functions
25//===----------------------------------------------------------------------===//
26
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000027static const FunctionType *getFunctionType(Decl *d, bool blocksToo = true) {
Chris Lattner6b6b5372008-06-26 18:38:35 +000028 QualType Ty;
Chris Lattner6b6b5372008-06-26 18:38:35 +000029 if (ValueDecl *decl = dyn_cast<ValueDecl>(d))
30 Ty = decl->getType();
31 else if (FieldDecl *decl = dyn_cast<FieldDecl>(d))
32 Ty = decl->getType();
33 else if (TypedefDecl* decl = dyn_cast<TypedefDecl>(d))
34 Ty = decl->getUnderlyingType();
35 else
36 return 0;
37
38 if (Ty->isFunctionPointerType())
39 Ty = Ty->getAsPointerType()->getPointeeType();
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000040 else if (blocksToo && Ty->isBlockPointerType())
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000041 Ty = Ty->getAsBlockPointerType()->getPointeeType();
Daniel Dunbard3f2c102008-10-19 02:04:16 +000042
43 return Ty->getAsFunctionType();
Chris Lattner6b6b5372008-06-26 18:38:35 +000044}
45
Daniel Dunbar35682492008-09-26 04:12:28 +000046// FIXME: We should provide an abstraction around a method or function
47// to provide the following bits of information.
48
Daniel Dunbard3f2c102008-10-19 02:04:16 +000049/// isFunctionOrMethod - Return true if the given decl has function
50/// type (function or function-typed variable) or an Objective-C
51/// method.
Daniel Dunbar35682492008-09-26 04:12:28 +000052static bool isFunctionOrMethod(Decl *d) {
Fariborz Jahanian755f9d22009-05-18 17:39:25 +000053 return getFunctionType(d, false) || isa<ObjCMethodDecl>(d);
Daniel Dunbar35682492008-09-26 04:12:28 +000054}
55
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000056/// isFunctionOrMethodOrBlock - Return true if the given decl has function
57/// type (function or function-typed variable) or an Objective-C
58/// method or a block.
59static bool isFunctionOrMethodOrBlock(Decl *d) {
60 if (isFunctionOrMethod(d))
61 return true;
62 // check for block is more involved.
63 if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
64 QualType Ty = V->getType();
65 return Ty->isBlockPointerType();
66 }
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000067 return isa<BlockDecl>(d);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000068}
69
Daniel Dunbard3f2c102008-10-19 02:04:16 +000070/// hasFunctionProto - Return true if the given decl has a argument
71/// information. This decl should have already passed
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000072/// isFunctionOrMethod or isFunctionOrMethodOrBlock.
Daniel Dunbard3f2c102008-10-19 02:04:16 +000073static bool hasFunctionProto(Decl *d) {
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000074 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000075 return isa<FunctionProtoType>(FnTy);
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000076 else {
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000077 assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d));
Daniel Dunbard3f2c102008-10-19 02:04:16 +000078 return true;
79 }
80}
81
82/// getFunctionOrMethodNumArgs - Return number of function or method
83/// arguments. It is an error to call this on a K&R function (use
84/// hasFunctionProto first).
Daniel Dunbar35682492008-09-26 04:12:28 +000085static unsigned getFunctionOrMethodNumArgs(Decl *d) {
Chris Lattner89951a82009-02-20 18:43:26 +000086 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000087 return cast<FunctionProtoType>(FnTy)->getNumArgs();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000088 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
89 return BD->getNumParams();
Chris Lattner89951a82009-02-20 18:43:26 +000090 return cast<ObjCMethodDecl>(d)->param_size();
Daniel Dunbar35682492008-09-26 04:12:28 +000091}
92
93static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) {
Chris Lattner89951a82009-02-20 18:43:26 +000094 if (const FunctionType *FnTy = getFunctionType(d))
Douglas Gregor72564e72009-02-26 23:50:07 +000095 return cast<FunctionProtoType>(FnTy)->getArgType(Idx);
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +000096 if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
97 return BD->getParamDecl(Idx)->getType();
Fariborz Jahanian620d89c2009-05-15 23:15:03 +000098
Chris Lattner89951a82009-02-20 18:43:26 +000099 return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType();
Daniel Dunbar35682492008-09-26 04:12:28 +0000100}
101
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000102static QualType getFunctionOrMethodResultType(Decl *d) {
103 if (const FunctionType *FnTy = getFunctionType(d))
104 return cast<FunctionProtoType>(FnTy)->getResultType();
105 return cast<ObjCMethodDecl>(d)->getResultType();
106}
107
Daniel Dunbar35682492008-09-26 04:12:28 +0000108static bool isFunctionOrMethodVariadic(Decl *d) {
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000109 if (const FunctionType *FnTy = getFunctionType(d)) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000110 const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy);
Daniel Dunbar35682492008-09-26 04:12:28 +0000111 return proto->isVariadic();
Fariborz Jahaniand66f22d2009-05-19 17:08:59 +0000112 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d))
113 return BD->IsVariadic();
114 else {
Daniel Dunbar35682492008-09-26 04:12:28 +0000115 return cast<ObjCMethodDecl>(d)->isVariadic();
116 }
117}
118
Chris Lattner6b6b5372008-06-26 18:38:35 +0000119static inline bool isNSStringType(QualType T, ASTContext &Ctx) {
Chris Lattnerb77792e2008-07-26 22:17:49 +0000120 const PointerType *PT = T->getAsPointerType();
121 if (!PT)
Chris Lattner6b6b5372008-06-26 18:38:35 +0000122 return false;
123
Chris Lattnerb77792e2008-07-26 22:17:49 +0000124 const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000125 if (!ClsT)
126 return false;
127
128 IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier();
129
130 // FIXME: Should we walk the chain of classes?
131 return ClsName == &Ctx.Idents.get("NSString") ||
132 ClsName == &Ctx.Idents.get("NSMutableString");
133}
134
Daniel Dunbar085e8f72008-09-26 03:32:58 +0000135static inline bool isCFStringType(QualType T, ASTContext &Ctx) {
136 const PointerType *PT = T->getAsPointerType();
137 if (!PT)
138 return false;
139
140 const RecordType *RT = PT->getPointeeType()->getAsRecordType();
141 if (!RT)
142 return false;
143
144 const RecordDecl *RD = RT->getDecl();
145 if (RD->getTagKind() != TagDecl::TK_struct)
146 return false;
147
148 return RD->getIdentifier() == &Ctx.Idents.get("__CFString");
149}
150
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000151//===----------------------------------------------------------------------===//
Chris Lattnere5c5ee12008-06-29 00:16:31 +0000152// Attribute Implementations
153//===----------------------------------------------------------------------===//
154
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000155// FIXME: All this manual attribute parsing code is gross. At the
156// least add some helper functions to check most argument patterns (#
157// and types of args).
158
Chris Lattner803d0802008-06-29 00:43:07 +0000159static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr,
160 Sema &S) {
Chris Lattner545dd342008-06-28 23:36:30 +0000161 TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d);
162 if (tDecl == 0) {
Chris Lattner803d0802008-06-29 00:43:07 +0000163 S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef);
Chris Lattner545dd342008-06-28 23:36:30 +0000164 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000165 }
166
Chris Lattner6b6b5372008-06-26 18:38:35 +0000167 QualType curType = tDecl->getUnderlyingType();
168 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000169 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000170 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000171 return;
172 }
Chris Lattner545dd342008-06-28 23:36:30 +0000173 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000174 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000175 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000176 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
177 << "ext_vector_type" << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000178 return;
179 }
180 // unlike gcc's vector_size attribute, we do not allow vectors to be defined
181 // in conjunction with complex types (pointers, arrays, functions, etc.).
Chris Lattnerb77792e2008-07-26 22:17:49 +0000182 if (!curType->isIntegerType() && !curType->isRealFloatingType()) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000183 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000184 return;
185 }
186 // unlike gcc's vector_size attribute, the size is specified as the
187 // number of elements, not the number of bytes.
188 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue());
189
190 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000191 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
192 << sizeExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000193 return;
194 }
195 // Instantiate/Install the vector type, the number of elements is > 0.
Chris Lattner803d0802008-06-29 00:43:07 +0000196 tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000197 // Remember this typedef decl, we will need it later for diagnostics.
Chris Lattner803d0802008-06-29 00:43:07 +0000198 S.ExtVectorDecls.push_back(tDecl);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000199}
200
Chris Lattner065c5a82008-06-28 23:48:25 +0000201
202/// HandleVectorSizeAttribute - this attribute is only applicable to
203/// integral and float scalars, although arrays, pointers, and function
204/// return values are allowed in conjunction with this construct. Aggregates
205/// with this attribute are invalid, even if they are of the same size as a
206/// corresponding scalar.
207/// The raw attribute should contain precisely 1 argument, the vector size
208/// for the variable, measured in bytes. If curType and rawAttr are well
209/// formed, this routine will return a new vector type.
Chris Lattner803d0802008-06-29 00:43:07 +0000210static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner065c5a82008-06-28 23:48:25 +0000211 QualType CurType;
212 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
213 CurType = VD->getType();
214 else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
215 CurType = TD->getUnderlyingType();
216 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000217 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
218 << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattner065c5a82008-06-28 23:48:25 +0000219 return;
220 }
221
222 // Check the attribute arugments.
Chris Lattner545dd342008-06-28 23:36:30 +0000223 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000224 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner065c5a82008-06-28 23:48:25 +0000225 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000226 }
Chris Lattner545dd342008-06-28 23:36:30 +0000227 Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000228 llvm::APSInt vecSize(32);
Chris Lattner803d0802008-06-29 00:43:07 +0000229 if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000230 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
231 << "vector_size" << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000232 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000233 }
234 // navigate to the base type - we need to provide for vector pointers,
235 // vector arrays, and functions returning vectors.
Chris Lattnerb77792e2008-07-26 22:17:49 +0000236 if (CurType->isPointerType() || CurType->isArrayType() ||
237 CurType->isFunctionType()) {
Chris Lattner2db15bd2009-05-13 04:00:12 +0000238 S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType;
239 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000240 /* FIXME: rebuild the type from the inside out, vectorizing the inner type.
241 do {
242 if (PointerType *PT = dyn_cast<PointerType>(canonType))
243 canonType = PT->getPointeeType().getTypePtr();
244 else if (ArrayType *AT = dyn_cast<ArrayType>(canonType))
245 canonType = AT->getElementType().getTypePtr();
246 else if (FunctionType *FT = dyn_cast<FunctionType>(canonType))
247 canonType = FT->getResultType().getTypePtr();
248 } while (canonType->isPointerType() || canonType->isArrayType() ||
249 canonType->isFunctionType());
250 */
251 }
Chris Lattner82afa2d2009-05-13 05:13:44 +0000252 // the base type must be integer or float, and can't already be a vector.
253 if (CurType->isVectorType() ||
254 (!CurType->isIntegerType() && !CurType->isRealFloatingType())) {
Chris Lattnerd1625842008-11-24 06:25:27 +0000255 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType;
Chris Lattner065c5a82008-06-28 23:48:25 +0000256 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000257 }
Chris Lattner803d0802008-06-29 00:43:07 +0000258 unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000259 // vecSize is specified in bytes - convert to bits.
260 unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8);
261
262 // the vector size needs to be an integral multiple of the type size.
263 if (vectorSize % typeSize) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000264 S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size)
265 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000266 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000267 }
268 if (vectorSize == 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000269 S.Diag(Attr.getLoc(), diag::err_attribute_zero_size)
270 << sizeExpr->getSourceRange();
Chris Lattner065c5a82008-06-28 23:48:25 +0000271 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000272 }
Chris Lattner065c5a82008-06-28 23:48:25 +0000273
274 // Success! Instantiate the vector type, the number of elements is > 0, and
275 // not required to be a power of 2, unlike GCC.
Chris Lattner803d0802008-06-29 00:43:07 +0000276 CurType = S.Context.getVectorType(CurType, vectorSize/typeSize);
Chris Lattner065c5a82008-06-28 23:48:25 +0000277
278 if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
279 VD->setType(CurType);
280 else
281 cast<TypedefDecl>(D)->setUnderlyingType(CurType);
Chris Lattner6b6b5372008-06-26 18:38:35 +0000282}
283
Chris Lattner803d0802008-06-29 00:43:07 +0000284static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000285 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000286 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000287 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000288 return;
289 }
290
291 if (TagDecl *TD = dyn_cast<TagDecl>(d))
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000292 TD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000293 else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) {
294 // If the alignment is less than or equal to 8 bits, the packed attribute
295 // has no effect.
296 if (!FD->getType()->isIncompleteType() &&
Chris Lattner803d0802008-06-29 00:43:07 +0000297 S.Context.getTypeAlign(FD->getType()) <= 8)
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000298 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000299 << Attr.getName() << FD->getType();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000300 else
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000301 FD->addAttr(::new (S.Context) PackedAttr(1));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000302 } else
Chris Lattner3c73c412008-11-19 08:23:25 +0000303 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +0000304}
305
Ted Kremenek96329d42008-07-15 22:26:48 +0000306static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) {
307 // check the attribute arguments.
308 if (Attr.getNumArgs() > 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000309 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek96329d42008-07-15 22:26:48 +0000310 return;
311 }
312
313 // The IBOutlet attribute only applies to instance variables of Objective-C
314 // classes.
Ted Kremenek32742602009-02-17 22:20:20 +0000315 if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000316 d->addAttr(::new (S.Context) IBOutletAttr());
Ted Kremenek96329d42008-07-15 22:26:48 +0000317 else
Ted Kremenek32742602009-02-17 22:20:20 +0000318 S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet);
Ted Kremenek96329d42008-07-15 22:26:48 +0000319}
320
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000321static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000322 // GCC ignores the nonnull attribute on K&R style function
323 // prototypes, so we ignore it as well
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000324 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000325 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000326 << Attr.getName() << 0 /*function*/;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000327 return;
328 }
329
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000330 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000331
332 // The nonnull attribute only applies to pointers.
333 llvm::SmallVector<unsigned, 10> NonNullArgs;
334
335 for (AttributeList::arg_iterator I=Attr.arg_begin(),
336 E=Attr.arg_end(); I!=E; ++I) {
337
338
339 // The argument must be an integer constant expression.
Ted Kremenekf5e88342008-12-04 19:38:33 +0000340 Expr *Ex = static_cast<Expr *>(*I);
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000341 llvm::APSInt ArgNum(32);
342 if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000343 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
344 << "nonnull" << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000345 return;
346 }
347
348 unsigned x = (unsigned) ArgNum.getZExtValue();
349
350 if (x < 1 || x > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000351 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner30bc9652008-11-19 07:22:31 +0000352 << "nonnull" << I.getArgNum() << Ex->getSourceRange();
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000353 return;
354 }
Ted Kremenek465172f2008-07-21 22:09:15 +0000355
356 --x;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000357
358 // Is the function argument a pointer type?
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000359 QualType T = getFunctionOrMethodArgType(d, x);
360 if (!T->isPointerType() && !T->isBlockPointerType()) {
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000361 // FIXME: Should also highlight argument in decl.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000362 S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only)
363 << "nonnull" << Ex->getSourceRange();
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000364 continue;
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000365 }
366
367 NonNullArgs.push_back(x);
368 }
369
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000370 // If no arguments were specified to __attribute__((nonnull)) then all
371 // pointer arguments have a nonnull attribute.
372 if (NonNullArgs.empty()) {
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000373 for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) {
374 QualType T = getFunctionOrMethodArgType(d, I);
375 if (T->isPointerType() || T->isBlockPointerType())
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000376 NonNullArgs.push_back(I);
Ted Kremenek46bbaca2008-11-18 06:52:58 +0000377 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000378
379 if (NonNullArgs.empty()) {
380 S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers);
381 return;
382 }
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000383 }
Ted Kremenek7fb43c12008-09-01 19:57:52 +0000384
385 unsigned* start = &NonNullArgs[0];
386 unsigned size = NonNullArgs.size();
387 std::sort(start, start + size);
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000388 d->addAttr(::new (S.Context) NonNullAttr(start, size));
Ted Kremenekeb2b2a32008-07-21 21:53:04 +0000389}
390
Chris Lattner803d0802008-06-29 00:43:07 +0000391static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000392 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000393 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000394 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000395 return;
396 }
397
Chris Lattner545dd342008-06-28 23:36:30 +0000398 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000399 Arg = Arg->IgnoreParenCasts();
400 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
401
402 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000403 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000404 << "alias" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000405 return;
406 }
407
408 const char *Alias = Str->getStrData();
409 unsigned AliasLen = Str->getByteLength();
410
411 // FIXME: check if target symbol exists in current file
412
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000413 d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen)));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000414}
415
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000416static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr,
417 Sema &S) {
418 // check the attribute arguments.
419 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000420 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000421 return;
422 }
Anders Carlsson5bab7882009-02-19 19:16:48 +0000423
Chris Lattnerc5197432009-04-14 17:02:11 +0000424 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +0000425 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000426 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +0000427 return;
428 }
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000429
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000430 d->addAttr(::new (S.Context) AlwaysInlineAttr());
Daniel Dunbaraf668b02008-10-28 00:17:57 +0000431}
432
Ted Kremenekb7252322009-04-10 00:01:14 +0000433static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr,
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000434 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000435 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000436 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000437 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenekb7252322009-04-10 00:01:14 +0000438 return false;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000439 }
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000440
Mike Stump19c30c02009-04-29 19:03:13 +0000441 if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) {
442 ValueDecl *VD = dyn_cast<ValueDecl>(d);
443 if (VD == 0 || !VD->getType()->isBlockPointerType()) {
444 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000445 << Attr.getName() << 0 /*function*/;
Mike Stump19c30c02009-04-29 19:03:13 +0000446 return false;
447 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000448 }
449
Ted Kremenekb7252322009-04-10 00:01:14 +0000450 return true;
451}
452
453static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000454 if (HandleCommonNoReturnAttr(d, Attr, S))
Ted Kremenekb7252322009-04-10 00:01:14 +0000455 d->addAttr(::new (S.Context) NoReturnAttr());
456}
457
458static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr,
459 Sema &S) {
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000460 if (HandleCommonNoReturnAttr(d, Attr, S))
Ted Kremenekb7252322009-04-10 00:01:14 +0000461 d->addAttr(::new (S.Context) AnalyzerNoReturnAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000462}
463
Ted Kremenek73798892008-07-25 04:39:19 +0000464static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
465 // check the attribute arguments.
466 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000467 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Ted Kremenek73798892008-07-25 04:39:19 +0000468 return;
469 }
470
Daniel Dunbard3f2c102008-10-19 02:04:16 +0000471 if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000472 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000473 << Attr.getName() << 2 /*variable and function*/;
Ted Kremenek73798892008-07-25 04:39:19 +0000474 return;
475 }
476
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000477 d->addAttr(::new (S.Context) UnusedAttr());
Ted Kremenek73798892008-07-25 04:39:19 +0000478}
479
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000480static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
481 // check the attribute arguments.
482 if (Attr.getNumArgs() != 0) {
483 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
484 return;
485 }
486
487 if (const VarDecl *VD = dyn_cast<VarDecl>(d)) {
Daniel Dunbar186204b2009-02-13 22:48:56 +0000488 if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000489 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
490 return;
491 }
492 } else if (!isFunctionOrMethod(d)) {
493 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000494 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000495 return;
496 }
497
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000498 d->addAttr(::new (S.Context) UsedAttr());
Daniel Dunbarb805dad2009-02-13 19:23:53 +0000499}
500
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000501static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
502 // check the attribute arguments.
503 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000504 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
505 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000506 return;
507 }
508
509 int priority = 65535; // FIXME: Do not hardcode such constants.
510 if (Attr.getNumArgs() > 0) {
511 Expr *E = static_cast<Expr *>(Attr.getArg(0));
512 llvm::APSInt Idx(32);
513 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000514 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000515 << "constructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000516 return;
517 }
518 priority = Idx.getZExtValue();
519 }
520
Chris Lattnerc5197432009-04-14 17:02:11 +0000521 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000522 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000523 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000524 return;
525 }
526
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000527 d->addAttr(::new (S.Context) ConstructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000528}
529
530static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) {
531 // check the attribute arguments.
532 if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000533 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
534 << "0 or 1";
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000535 return;
536 }
537
538 int priority = 65535; // FIXME: Do not hardcode such constants.
539 if (Attr.getNumArgs() > 0) {
540 Expr *E = static_cast<Expr *>(Attr.getArg(0));
541 llvm::APSInt Idx(32);
542 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000543 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000544 << "destructor" << 1 << E->getSourceRange();
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000545 return;
546 }
547 priority = Idx.getZExtValue();
548 }
549
Anders Carlsson6782fc62008-08-22 22:10:48 +0000550 if (!isa<FunctionDecl>(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000551 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000552 << Attr.getName() << 0 /*function*/;
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000553 return;
554 }
555
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000556 d->addAttr(::new (S.Context) DestructorAttr(priority));
Daniel Dunbar3068ae02008-07-31 22:40:48 +0000557}
558
Chris Lattner803d0802008-06-29 00:43:07 +0000559static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000560 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000561 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000562 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000563 return;
564 }
565
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000566 d->addAttr(::new (S.Context) DeprecatedAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000567}
568
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000569static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) {
570 // check the attribute arguments.
571 if (Attr.getNumArgs() != 0) {
572 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
573 return;
574 }
575
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000576 d->addAttr(::new (S.Context) UnavailableAttr());
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000577}
578
Chris Lattner803d0802008-06-29 00:43:07 +0000579static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000580 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000581 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000582 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000583 return;
584 }
585
Chris Lattner545dd342008-06-28 23:36:30 +0000586 Expr *Arg = static_cast<Expr*>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000587 Arg = Arg->IgnoreParenCasts();
588 StringLiteral *Str = dyn_cast<StringLiteral>(Arg);
589
590 if (Str == 0 || Str->isWide()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000591 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000592 << "visibility" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000593 return;
594 }
595
596 const char *TypeStr = Str->getStrData();
597 unsigned TypeLen = Str->getByteLength();
598 VisibilityAttr::VisibilityTypes type;
599
600 if (TypeLen == 7 && !memcmp(TypeStr, "default", 7))
601 type = VisibilityAttr::DefaultVisibility;
602 else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6))
603 type = VisibilityAttr::HiddenVisibility;
604 else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8))
605 type = VisibilityAttr::HiddenVisibility; // FIXME
606 else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9))
607 type = VisibilityAttr::ProtectedVisibility;
608 else {
Chris Lattner08631c52008-11-23 21:45:46 +0000609 S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000610 return;
611 }
612
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000613 d->addAttr(::new (S.Context) VisibilityAttr(type));
Chris Lattner6b6b5372008-06-26 18:38:35 +0000614}
615
Chris Lattner0db29ec2009-02-14 08:09:34 +0000616static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr,
617 Sema &S) {
618 if (Attr.getNumArgs() != 0) {
619 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
620 return;
621 }
622
623 ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D);
624 if (OCI == 0) {
625 S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface);
626 return;
627 }
628
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000629 D->addAttr(::new (S.Context) ObjCExceptionAttr());
Chris Lattner0db29ec2009-02-14 08:09:34 +0000630}
631
632static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000633 if (Attr.getNumArgs() != 0) {
634 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
635 return;
636 }
Chris Lattner0db29ec2009-02-14 08:09:34 +0000637 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) {
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000638 QualType T = TD->getUnderlyingType();
639 if (!T->isPointerType() ||
640 !T->getAsPointerType()->getPointeeType()->isRecordType()) {
641 S.Diag(TD->getLocation(), diag::err_nsobject_attribute);
642 return;
643 }
644 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000645 D->addAttr(::new (S.Context) ObjCNSObjectAttr());
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +0000646}
647
Douglas Gregorf9201e02009-02-11 23:02:49 +0000648static void
649HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) {
650 if (Attr.getNumArgs() != 0) {
651 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
652 return;
653 }
654
655 if (!isa<FunctionDecl>(D)) {
656 S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function);
657 return;
658 }
659
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000660 D->addAttr(::new (S.Context) OverloadableAttr());
Douglas Gregorf9201e02009-02-11 23:02:49 +0000661}
662
Steve Naroff9eae5762008-09-18 16:44:58 +0000663static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) {
664 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000665 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +0000666 << "blocks" << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000667 return;
668 }
669
670 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000671 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Steve Naroff9eae5762008-09-18 16:44:58 +0000672 return;
673 }
Steve Naroff9eae5762008-09-18 16:44:58 +0000674
675 BlocksAttr::BlocksAttrTypes type;
Chris Lattner92e62b02008-11-20 04:42:34 +0000676 if (Attr.getParameterName()->isStr("byref"))
Steve Naroff9eae5762008-09-18 16:44:58 +0000677 type = BlocksAttr::ByRef;
678 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000679 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
Chris Lattner3c73c412008-11-19 08:23:25 +0000680 << "blocks" << Attr.getParameterName();
Steve Naroff9eae5762008-09-18 16:44:58 +0000681 return;
682 }
683
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000684 d->addAttr(::new (S.Context) BlocksAttr(type));
Steve Naroff9eae5762008-09-18 16:44:58 +0000685}
686
Anders Carlsson77091822008-10-05 18:05:59 +0000687static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) {
688 // check the attribute arguments.
689 if (Attr.getNumArgs() > 2) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000690 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments)
691 << "0, 1 or 2";
Anders Carlsson77091822008-10-05 18:05:59 +0000692 return;
693 }
694
695 int sentinel = 0;
696 if (Attr.getNumArgs() > 0) {
697 Expr *E = static_cast<Expr *>(Attr.getArg(0));
698 llvm::APSInt Idx(32);
699 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000700 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000701 << "sentinel" << 1 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000702 return;
703 }
704 sentinel = Idx.getZExtValue();
705
706 if (sentinel < 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000707 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero)
708 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000709 return;
710 }
711 }
712
713 int nullPos = 0;
714 if (Attr.getNumArgs() > 1) {
715 Expr *E = static_cast<Expr *>(Attr.getArg(1));
716 llvm::APSInt Idx(32);
717 if (!E->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000718 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +0000719 << "sentinel" << 2 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000720 return;
721 }
722 nullPos = Idx.getZExtValue();
723
724 if (nullPos > 1 || nullPos < 0) {
725 // FIXME: This error message could be improved, it would be nice
726 // to say what the bounds actually are.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000727 S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one)
728 << E->getSourceRange();
Anders Carlsson77091822008-10-05 18:05:59 +0000729 return;
730 }
731 }
732
733 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) {
Chris Lattner897cd902009-03-17 23:03:47 +0000734 const FunctionType *FT = FD->getType()->getAsFunctionType();
735 assert(FT && "FunctionDecl has non-function type?");
736
737 if (isa<FunctionNoProtoType>(FT)) {
738 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments);
739 return;
740 }
741
742 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000743 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000744 return;
745 }
746 } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) {
747 if (!MD->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000748 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0;
Anders Carlsson77091822008-10-05 18:05:59 +0000749 return;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000750 }
751 } else if (isa<BlockDecl>(d)) {
752 // Note! BlockDecl is typeless. Variadic diagnostics
753 // will be issued by the caller.
754 ;
755 } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) {
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000756 QualType Ty = V->getType();
Fariborz Jahaniandaf04152009-05-15 20:33:25 +0000757 if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) {
758 const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d)
759 : Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType();
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000760 if (!cast<FunctionProtoType>(FT)->isVariadic()) {
Fariborz Jahanian3bba33d2009-05-15 21:18:04 +0000761 int m = Ty->isFunctionPointerType() ? 0 : 1;
762 S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000763 return;
764 }
765 }
766 else {
767 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000768 << Attr.getName() << 6 /*function, method or block */;
Fariborz Jahanian2f7c3922009-05-14 20:53:39 +0000769 return;
770 }
Anders Carlsson77091822008-10-05 18:05:59 +0000771 } else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000772 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Fariborz Jahanianffb00812009-05-14 20:57:28 +0000773 << Attr.getName() << 6 /*function, method or block */;
Anders Carlsson77091822008-10-05 18:05:59 +0000774 return;
775 }
Fariborz Jahanian88f1ba02009-05-13 23:20:50 +0000776 d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos));
Anders Carlsson77091822008-10-05 18:05:59 +0000777}
778
Chris Lattner026dc962009-02-14 07:37:35 +0000779static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) {
780 // check the attribute arguments.
781 if (Attr.getNumArgs() != 0) {
782 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
783 return;
784 }
785
786 // TODO: could also be applied to methods?
787 FunctionDecl *Fn = dyn_cast<FunctionDecl>(D);
788 if (!Fn) {
789 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000790 << Attr.getName() << 0 /*function*/;
Chris Lattner026dc962009-02-14 07:37:35 +0000791 return;
792 }
793
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000794 Fn->addAttr(::new (S.Context) WarnUnusedResultAttr());
Chris Lattner026dc962009-02-14 07:37:35 +0000795}
796
797static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000798 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000799 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000800 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000801 return;
802 }
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000803
804 // TODO: could also be applied to methods?
805 if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
806 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000807 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000808 return;
809 }
Chris Lattner6b6b5372008-06-26 18:38:35 +0000810
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000811 D->addAttr(::new (S.Context) WeakAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000812}
813
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000814static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
815 // check the attribute arguments.
816 if (Attr.getNumArgs() != 0) {
817 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
818 return;
819 }
820
821 // weak_import only applies to variable & function declarations.
822 bool isDef = false;
823 if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
824 isDef = (!VD->hasExternalStorage() || VD->getInit());
825 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Douglas Gregor72971342009-04-18 00:02:19 +0000826 isDef = FD->getBody(S.Context);
Fariborz Jahaniand4edddd2009-05-04 19:35:12 +0000827 } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) {
828 // We ignore weak import on properties and methods
Mike Stump1c90f4d2009-03-18 17:39:31 +0000829 return;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000830 } else {
831 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000832 << Attr.getName() << 2 /*variable and function*/;
Daniel Dunbar6e775db2009-03-06 06:39:57 +0000833 return;
834 }
835
836 // Merge should handle any subsequent violations.
837 if (isDef) {
838 S.Diag(Attr.getLoc(),
839 diag::warn_attribute_weak_import_invalid_on_definition)
840 << "weak_import" << 2 /*variable and function*/;
841 return;
842 }
843
844 D->addAttr(::new (S.Context) WeakImportAttr());
845}
846
Chris Lattner026dc962009-02-14 07:37:35 +0000847static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000848 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000849 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000850 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000851 return;
852 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000853
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000854 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000855 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000856 D->addAttr(::new (S.Context) DLLImportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000857 return;
858 }
859
Chris Lattner026dc962009-02-14 07:37:35 +0000860 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000861 if (!FD) {
862 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000863 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000864 return;
865 }
866
867 // Currently, the dllimport attribute is ignored for inlined functions.
868 // Warning is emitted.
869 if (FD->isInline()) {
870 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
871 return;
872 }
873
874 // The attribute is also overridden by a subsequent declaration as dllexport.
875 // Warning is emitted.
876 for (AttributeList *nextAttr = Attr.getNext(); nextAttr;
877 nextAttr = nextAttr->getNext()) {
878 if (nextAttr->getKind() == AttributeList::AT_dllexport) {
879 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
880 return;
881 }
882 }
883
Chris Lattner026dc962009-02-14 07:37:35 +0000884 if (D->getAttr<DLLExportAttr>()) {
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000885 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport";
886 return;
887 }
888
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000889 D->addAttr(::new (S.Context) DLLImportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000890}
891
Chris Lattner026dc962009-02-14 07:37:35 +0000892static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000893 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000894 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000895 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000896 return;
897 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000898
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000899 // Attribute can be applied only to functions or variables.
Chris Lattner026dc962009-02-14 07:37:35 +0000900 if (isa<VarDecl>(D)) {
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000901 D->addAttr(::new (S.Context) DLLExportAttr());
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000902 return;
903 }
904
Chris Lattner026dc962009-02-14 07:37:35 +0000905 FunctionDecl *FD = dyn_cast<FunctionDecl>(D);
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000906 if (!FD) {
907 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000908 << Attr.getName() << 2 /*variable and function*/;
Anton Korobeynikov2f402702008-12-26 00:52:02 +0000909 return;
910 }
911
912 // Currently, the dllexport attribute is ignored for inlined functions,
913 // unless the -fkeep-inline-functions flag has been used. Warning is emitted;
914 if (FD->isInline()) {
915 // FIXME: ... unless the -fkeep-inline-functions flag has been used.
916 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport";
917 return;
918 }
919
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000920 D->addAttr(::new (S.Context) DLLExportAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000921}
922
Chris Lattner026dc962009-02-14 07:37:35 +0000923static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000924 // Attribute has no arguments.
925 if (Attr.getNumArgs() != 1) {
926 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
927 return;
928 }
929
930 // Make sure that there is a string literal as the sections's single
931 // argument.
932 StringLiteral *SE =
933 dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0)));
934 if (!SE) {
935 // FIXME
936 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
937 return;
938 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000939 D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(),
940 SE->getByteLength())));
Daniel Dunbar17f194f2009-02-12 17:28:23 +0000941}
942
Chris Lattner803d0802008-06-29 00:43:07 +0000943static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000944 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000945 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000946 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000947 return;
948 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000949
950 // Attribute can be applied only to functions.
951 if (!isa<FunctionDecl>(d)) {
952 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000953 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000954 return;
955 }
956
957 // stdcall and fastcall attributes are mutually incompatible.
958 if (d->getAttr<FastCallAttr>()) {
959 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
960 << "stdcall" << "fastcall";
961 return;
962 }
963
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000964 d->addAttr(::new (S.Context) StdCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000965}
966
Chris Lattner803d0802008-06-29 00:43:07 +0000967static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000968 // Attribute has no arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000969 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000970 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000971 return;
972 }
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000973
974 if (!isa<FunctionDecl>(d)) {
975 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +0000976 << Attr.getName() << 0 /*function*/;
Anton Korobeynikov7b0a52f2008-12-23 22:24:07 +0000977 return;
978 }
979
980 // stdcall and fastcall attributes are mutually incompatible.
981 if (d->getAttr<StdCallAttr>()) {
982 S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible)
983 << "fastcall" << "stdcall";
984 return;
985 }
986
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000987 d->addAttr(::new (S.Context) FastCallAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000988}
989
Chris Lattner803d0802008-06-29 00:43:07 +0000990static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +0000991 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +0000992 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000993 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +0000994 return;
995 }
996
Chris Lattner0b2b6e12009-03-04 06:34:08 +0000997 d->addAttr(::new (S.Context) NoThrowAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +0000998}
999
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001000static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1001 // check the attribute arguments.
1002 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001003 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001004 return;
1005 }
1006
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001007 d->addAttr(::new (S.Context) ConstAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001008}
1009
1010static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1011 // check the attribute arguments.
1012 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001013 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001014 return;
1015 }
1016
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001017 d->addAttr(::new (S.Context) PureAttr());
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001018}
1019
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001020static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001021 // Match gcc which ignores cleanup attrs when compiling C++.
1022 if (S.getLangOptions().CPlusPlus)
1023 return;
1024
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001025 if (!Attr.getParameterName()) {
1026 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1027 return;
1028 }
1029
1030 if (Attr.getNumArgs() != 0) {
1031 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1032 return;
1033 }
1034
1035 VarDecl *VD = dyn_cast<VarDecl>(d);
1036
1037 if (!VD || !VD->hasLocalStorage()) {
1038 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup";
1039 return;
1040 }
1041
1042 // Look up the function
Douglas Gregor47b9a1c2009-02-04 17:27:36 +00001043 NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(),
1044 Sema::LookupOrdinaryName);
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001045 if (!CleanupDecl) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001046 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001047 Attr.getParameterName();
1048 return;
1049 }
1050
1051 FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl);
1052 if (!FD) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001053 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001054 Attr.getParameterName();
1055 return;
1056 }
1057
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001058 if (FD->getNumParams() != 1) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001059 S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) <<
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001060 Attr.getParameterName();
1061 return;
1062 }
1063
Anders Carlsson89941c12009-02-07 23:16:50 +00001064 // We're currently more strict than GCC about what function types we accept.
1065 // If this ever proves to be a problem it should be easy to fix.
1066 QualType Ty = S.Context.getPointerType(VD->getType());
1067 QualType ParamTy = FD->getParamDecl(0)->getType();
Eli Friedmand5e3e8e2009-04-26 01:30:08 +00001068 if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) {
Anders Carlsson89941c12009-02-07 23:16:50 +00001069 S.Diag(Attr.getLoc(),
1070 diag::err_attribute_cleanup_func_arg_incompatible_type) <<
1071 Attr.getParameterName() << ParamTy << Ty;
1072 return;
1073 }
1074
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001075 d->addAttr(::new (S.Context) CleanupAttr(FD));
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001076}
1077
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001078/// Handle __attribute__((format_arg((idx)))) attribute
1079/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
1080static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1081 if (Attr.getNumArgs() != 1) {
1082 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
1083 return;
1084 }
1085 if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) {
1086 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1087 << Attr.getName() << 0 /*function*/;
1088 return;
1089 }
1090 // FIXME: in C++ the implicit 'this' function parameter also counts.
1091 // this is needed in order to be compatible with GCC
1092 // the index must start with 1.
1093 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
1094 unsigned FirstIdx = 1;
1095 // checks for the 2nd argument
1096 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
1097 llvm::APSInt Idx(32);
1098 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
1099 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
1100 << "format" << 2 << IdxExpr->getSourceRange();
1101 return;
1102 }
1103
1104 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
1105 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
1106 << "format" << 2 << IdxExpr->getSourceRange();
1107 return;
1108 }
1109
1110 unsigned ArgIdx = Idx.getZExtValue() - 1;
1111
1112 // make sure the format string is really a string
1113 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
1114
1115 bool not_nsstring_type = !isNSStringType(Ty, S.Context);
1116 if (not_nsstring_type &&
1117 !isCFStringType(Ty, S.Context) &&
1118 (!Ty->isPointerType() ||
1119 !Ty->getAsPointerType()->getPointeeType()->isCharType())) {
1120 // FIXME: Should highlight the actual expression that has the wrong type.
1121 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1122 << (not_nsstring_type ? "a string type" : "an NSString")
1123 << IdxExpr->getSourceRange();
1124 return;
1125 }
1126 Ty = getFunctionOrMethodResultType(d);
1127 if (!isNSStringType(Ty, S.Context) &&
1128 !isCFStringType(Ty, S.Context) &&
1129 (!Ty->isPointerType() ||
1130 !Ty->getAsPointerType()->getPointeeType()->isCharType())) {
1131 // FIXME: Should highlight the actual expression that has the wrong type.
1132 S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not)
1133 << (not_nsstring_type ? "string type" : "NSString")
1134 << IdxExpr->getSourceRange();
1135 return;
1136 }
1137
1138 d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue()));
1139}
1140
Chris Lattner6b6b5372008-06-26 18:38:35 +00001141/// Handle __attribute__((format(type,idx,firstarg))) attributes
1142/// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
Chris Lattner803d0802008-06-29 00:43:07 +00001143static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001144
Chris Lattner545dd342008-06-28 23:36:30 +00001145 if (!Attr.getParameterName()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001146 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string)
Chris Lattner3c73c412008-11-19 08:23:25 +00001147 << "format" << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001148 return;
1149 }
1150
Chris Lattner545dd342008-06-28 23:36:30 +00001151 if (Attr.getNumArgs() != 2) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001152 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001153 return;
1154 }
1155
Fariborz Jahanian620d89c2009-05-15 23:15:03 +00001156 if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001157 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001158 << Attr.getName() << 0 /*function*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001159 return;
1160 }
1161
1162 // FIXME: in C++ the implicit 'this' function parameter also counts.
1163 // this is needed in order to be compatible with GCC
1164 // the index must start in 1 and the limit is numargs+1
Daniel Dunbar35682492008-09-26 04:12:28 +00001165 unsigned NumArgs = getFunctionOrMethodNumArgs(d);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001166 unsigned FirstIdx = 1;
1167
Chris Lattner545dd342008-06-28 23:36:30 +00001168 const char *Format = Attr.getParameterName()->getName();
1169 unsigned FormatLen = Attr.getParameterName()->getLength();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001170
1171 // Normalize the argument, __foo__ becomes foo.
1172 if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' &&
1173 Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') {
1174 Format += 2;
1175 FormatLen -= 4;
1176 }
1177
1178 bool Supported = false;
1179 bool is_NSString = false;
1180 bool is_strftime = false;
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001181 bool is_CFString = false;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001182
1183 switch (FormatLen) {
1184 default: break;
Chris Lattner803d0802008-06-29 00:43:07 +00001185 case 5: Supported = !memcmp(Format, "scanf", 5); break;
1186 case 6: Supported = !memcmp(Format, "printf", 6); break;
1187 case 7: Supported = !memcmp(Format, "strfmon", 7); break;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001188 case 8:
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001189 Supported = (is_strftime = !memcmp(Format, "strftime", 8)) ||
1190 (is_NSString = !memcmp(Format, "NSString", 8)) ||
1191 (is_CFString = !memcmp(Format, "CFString", 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001192 break;
1193 }
1194
1195 if (!Supported) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001196 S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported)
1197 << "format" << Attr.getParameterName()->getName();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001198 return;
1199 }
1200
1201 // checks for the 2nd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001202 Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner803d0802008-06-29 00:43:07 +00001203 llvm::APSInt Idx(32);
1204 if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001205 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001206 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001207 return;
1208 }
1209
1210 if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001211 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001212 << "format" << 2 << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001213 return;
1214 }
1215
1216 // FIXME: Do we need to bounds check?
1217 unsigned ArgIdx = Idx.getZExtValue() - 1;
1218
1219 // make sure the format string is really a string
Daniel Dunbar35682492008-09-26 04:12:28 +00001220 QualType Ty = getFunctionOrMethodArgType(d, ArgIdx);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001221
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001222 if (is_CFString) {
1223 if (!isCFStringType(Ty, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001224 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1225 << "a CFString" << IdxExpr->getSourceRange();
Daniel Dunbar085e8f72008-09-26 03:32:58 +00001226 return;
1227 }
1228 } else if (is_NSString) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001229 // FIXME: do we need to check if the type is NSString*? What are the
1230 // semantics?
Chris Lattner803d0802008-06-29 00:43:07 +00001231 if (!isNSStringType(Ty, S.Context)) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001232 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001233 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1234 << "an NSString" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001235 return;
1236 }
1237 } else if (!Ty->isPointerType() ||
1238 !Ty->getAsPointerType()->getPointeeType()->isCharType()) {
Mike Stump390b4cc2009-05-16 07:39:55 +00001239 // FIXME: Should highlight the actual expression that has the wrong type.
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001240 S.Diag(Attr.getLoc(), diag::err_format_attribute_not)
1241 << "a string type" << IdxExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001242 return;
1243 }
1244
1245 // check the 3rd argument
Chris Lattner545dd342008-06-28 23:36:30 +00001246 Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1));
Chris Lattner803d0802008-06-29 00:43:07 +00001247 llvm::APSInt FirstArg(32);
1248 if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001249 S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int)
Chris Lattner3c73c412008-11-19 08:23:25 +00001250 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001251 return;
1252 }
1253
1254 // check if the function is variadic if the 3rd argument non-zero
1255 if (FirstArg != 0) {
Daniel Dunbar35682492008-09-26 04:12:28 +00001256 if (isFunctionOrMethodVariadic(d)) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001257 ++NumArgs; // +1 for ...
1258 } else {
Chris Lattner803d0802008-06-29 00:43:07 +00001259 S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001260 return;
1261 }
1262 }
1263
Chris Lattner3c73c412008-11-19 08:23:25 +00001264 // strftime requires FirstArg to be 0 because it doesn't read from any
1265 // variable the input is just the current time + the format string.
Chris Lattner6b6b5372008-06-26 18:38:35 +00001266 if (is_strftime) {
1267 if (FirstArg != 0) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001268 S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter)
1269 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001270 return;
1271 }
1272 // if 0 it disables parameter checking (to use with e.g. va_list)
1273 } else if (FirstArg != 0 && FirstArg != NumArgs) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001274 S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds)
Chris Lattner3c73c412008-11-19 08:23:25 +00001275 << "format" << 3 << FirstArgExpr->getSourceRange();
Chris Lattner6b6b5372008-06-26 18:38:35 +00001276 return;
1277 }
1278
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001279 d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen),
Chris Lattner6b6b5372008-06-26 18:38:35 +00001280 Idx.getZExtValue(), FirstArg.getZExtValue()));
1281}
1282
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001283static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr,
1284 Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001285 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001286 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001287 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001288 return;
1289 }
1290
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001291 // Try to find the underlying union declaration.
1292 RecordDecl *RD = 0;
Eli Friedmanbc887452008-09-02 05:19:23 +00001293 TypedefDecl *TD = dyn_cast<TypedefDecl>(d);
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001294 if (TD && TD->getUnderlyingType()->isUnionType())
1295 RD = TD->getUnderlyingType()->getAsUnionType()->getDecl();
1296 else
1297 RD = dyn_cast<RecordDecl>(d);
1298
1299 if (!RD || !RD->isUnion()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001300 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001301 << Attr.getName() << 1 /*union*/;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001302 return;
1303 }
1304
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001305 if (!RD->isDefinition()) {
1306 S.Diag(Attr.getLoc(),
1307 diag::warn_transparent_union_attribute_not_definition);
1308 return;
1309 }
Chris Lattner6b6b5372008-06-26 18:38:35 +00001310
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001311 RecordDecl::field_iterator Field = RD->field_begin(S.Context),
1312 FieldEnd = RD->field_end(S.Context);
1313 if (Field == FieldEnd) {
1314 S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields);
1315 return;
1316 }
Eli Friedmanbc887452008-09-02 05:19:23 +00001317
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001318 FieldDecl *FirstField = *Field;
1319 QualType FirstType = FirstField->getType();
1320 if (FirstType->isFloatingType() || FirstType->isVectorType()) {
1321 S.Diag(FirstField->getLocation(),
1322 diag::warn_transparent_union_attribute_floating);
1323 return;
1324 }
1325
1326 uint64_t FirstSize = S.Context.getTypeSize(FirstType);
1327 uint64_t FirstAlign = S.Context.getTypeAlign(FirstType);
1328 for (; Field != FieldEnd; ++Field) {
1329 QualType FieldType = Field->getType();
1330 if (S.Context.getTypeSize(FieldType) != FirstSize ||
1331 S.Context.getTypeAlign(FieldType) != FirstAlign) {
1332 // Warn if we drop the attribute.
1333 bool isSize = S.Context.getTypeSize(FieldType) != FirstSize;
1334 unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType)
1335 : S.Context.getTypeAlign(FieldType);
1336 S.Diag(Field->getLocation(),
1337 diag::warn_transparent_union_attribute_field_size_align)
1338 << isSize << Field->getDeclName() << FieldBits;
1339 unsigned FirstBits = isSize? FirstSize : FirstAlign;
1340 S.Diag(FirstField->getLocation(),
1341 diag::note_transparent_union_first_field_size_align)
1342 << isSize << FirstBits;
Eli Friedmanbc887452008-09-02 05:19:23 +00001343 return;
1344 }
1345 }
1346
Douglas Gregor0c74e8a2009-04-29 22:16:16 +00001347 RD->addAttr(::new (S.Context) TransparentUnionAttr());
Chris Lattner6b6b5372008-06-26 18:38:35 +00001348}
1349
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001350static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001351 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001352 if (Attr.getNumArgs() != 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001353 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001354 return;
1355 }
Chris Lattner545dd342008-06-28 23:36:30 +00001356 Expr *argExpr = static_cast<Expr *>(Attr.getArg(0));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001357 StringLiteral *SE = dyn_cast<StringLiteral>(argExpr);
1358
1359 // Make sure that there is a string literal as the annotation's single
1360 // argument.
1361 if (!SE) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001362 S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string);
Chris Lattner6b6b5372008-06-26 18:38:35 +00001363 return;
1364 }
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001365 d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(),
1366 SE->getByteLength())));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001367}
1368
Chris Lattner803d0802008-06-29 00:43:07 +00001369static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001370 // check the attribute arguments.
Chris Lattner545dd342008-06-28 23:36:30 +00001371 if (Attr.getNumArgs() > 1) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001372 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001373 return;
1374 }
1375
1376 unsigned Align = 0;
Chris Lattner545dd342008-06-28 23:36:30 +00001377 if (Attr.getNumArgs() == 0) {
Chris Lattner6b6b5372008-06-26 18:38:35 +00001378 // FIXME: This should be the target specific maximum alignment.
Daniel Dunbar7549c552009-02-18 20:06:09 +00001379 // (For now we just use 128 bits which is the maximum on X86).
Chris Lattner6b6b5372008-06-26 18:38:35 +00001380 Align = 128;
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001381 d->addAttr(::new (S.Context) AlignedAttr(Align));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001382 return;
Chris Lattner6b6b5372008-06-26 18:38:35 +00001383 }
Chris Lattner49e2d342008-06-28 23:50:44 +00001384
1385 Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0));
1386 llvm::APSInt Alignment(32);
Chris Lattner803d0802008-06-29 00:43:07 +00001387 if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001388 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1389 << "aligned" << alignmentExpr->getSourceRange();
Chris Lattner49e2d342008-06-28 23:50:44 +00001390 return;
1391 }
Daniel Dunbar396b2a22009-02-16 23:37:57 +00001392 if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) {
1393 S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two)
1394 << alignmentExpr->getSourceRange();
1395 return;
1396 }
1397
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001398 d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8));
Chris Lattner6b6b5372008-06-26 18:38:35 +00001399}
Chris Lattnerfbf13472008-06-27 22:18:37 +00001400
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001401/// HandleModeAttr - This attribute modifies the width of a decl with
Chris Lattner065c5a82008-06-28 23:48:25 +00001402/// primitive type.
Chris Lattnerfbf13472008-06-27 22:18:37 +00001403///
1404/// Despite what would be logical, the mode attribute is a decl attribute,
1405/// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make
1406/// 'G' be HImode, not an intermediate pointer.
1407///
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001408static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) {
Chris Lattnerfbf13472008-06-27 22:18:37 +00001409 // This attribute isn't documented, but glibc uses it. It changes
1410 // the width of an int or unsigned int to the specified size.
1411
1412 // Check that there aren't any arguments
1413 if (Attr.getNumArgs() != 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001414 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001415 return;
1416 }
1417
1418 IdentifierInfo *Name = Attr.getParameterName();
1419 if (!Name) {
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001420 S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001421 return;
1422 }
1423 const char *Str = Name->getName();
1424 unsigned Len = Name->getLength();
1425
1426 // Normalize the attribute name, __foo__ becomes foo.
1427 if (Len > 4 && Str[0] == '_' && Str[1] == '_' &&
1428 Str[Len - 2] == '_' && Str[Len - 1] == '_') {
1429 Str += 2;
1430 Len -= 4;
1431 }
1432
1433 unsigned DestWidth = 0;
1434 bool IntegerMode = true;
Eli Friedman73397492009-03-03 06:41:03 +00001435 bool ComplexMode = false;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001436 switch (Len) {
1437 case 2:
Eli Friedman73397492009-03-03 06:41:03 +00001438 switch (Str[0]) {
1439 case 'Q': DestWidth = 8; break;
1440 case 'H': DestWidth = 16; break;
1441 case 'S': DestWidth = 32; break;
1442 case 'D': DestWidth = 64; break;
1443 case 'X': DestWidth = 96; break;
1444 case 'T': DestWidth = 128; break;
1445 }
1446 if (Str[1] == 'F') {
1447 IntegerMode = false;
1448 } else if (Str[1] == 'C') {
1449 IntegerMode = false;
1450 ComplexMode = true;
1451 } else if (Str[1] != 'I') {
1452 DestWidth = 0;
1453 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001454 break;
1455 case 4:
1456 // FIXME: glibc uses 'word' to define register_t; this is narrower than a
1457 // pointer on PIC16 and other embedded platforms.
1458 if (!memcmp(Str, "word", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001459 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001460 if (!memcmp(Str, "byte", 4))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001461 DestWidth = S.Context.Target.getCharWidth();
Chris Lattnerfbf13472008-06-27 22:18:37 +00001462 break;
1463 case 7:
1464 if (!memcmp(Str, "pointer", 7))
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001465 DestWidth = S.Context.Target.getPointerWidth(0);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001466 break;
1467 }
1468
1469 QualType OldTy;
1470 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1471 OldTy = TD->getUnderlyingType();
1472 else if (ValueDecl *VD = dyn_cast<ValueDecl>(D))
1473 OldTy = VD->getType();
1474 else {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001475 S.Diag(D->getLocation(), diag::err_attr_wrong_decl)
1476 << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc());
Chris Lattnerfbf13472008-06-27 22:18:37 +00001477 return;
1478 }
Eli Friedman73397492009-03-03 06:41:03 +00001479
1480 if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType())
1481 S.Diag(Attr.getLoc(), diag::err_mode_not_primitive);
1482 else if (IntegerMode) {
1483 if (!OldTy->isIntegralType())
1484 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1485 } else if (ComplexMode) {
1486 if (!OldTy->isComplexType())
1487 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1488 } else {
1489 if (!OldTy->isFloatingType())
1490 S.Diag(Attr.getLoc(), diag::err_mode_wrong_type);
1491 }
1492
Mike Stump390b4cc2009-05-16 07:39:55 +00001493 // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t
1494 // and friends, at least with glibc.
1495 // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong
1496 // width on unusual platforms.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001497 // FIXME: Make sure floating-point mappings are accurate
1498 // FIXME: Support XF and TF types
Chris Lattnerfbf13472008-06-27 22:18:37 +00001499 QualType NewTy;
1500 switch (DestWidth) {
1501 case 0:
Chris Lattner3c73c412008-11-19 08:23:25 +00001502 S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001503 return;
1504 default:
Chris Lattner3c73c412008-11-19 08:23:25 +00001505 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001506 return;
1507 case 8:
Eli Friedman73397492009-03-03 06:41:03 +00001508 if (!IntegerMode) {
1509 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1510 return;
1511 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001512 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001513 NewTy = S.Context.SignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001514 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001515 NewTy = S.Context.UnsignedCharTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001516 break;
1517 case 16:
Eli Friedman73397492009-03-03 06:41:03 +00001518 if (!IntegerMode) {
1519 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1520 return;
1521 }
Chris Lattnerfbf13472008-06-27 22:18:37 +00001522 if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001523 NewTy = S.Context.ShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001524 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001525 NewTy = S.Context.UnsignedShortTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001526 break;
1527 case 32:
1528 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001529 NewTy = S.Context.FloatTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001530 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001531 NewTy = S.Context.IntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001532 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001533 NewTy = S.Context.UnsignedIntTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001534 break;
1535 case 64:
1536 if (!IntegerMode)
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001537 NewTy = S.Context.DoubleTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001538 else if (OldTy->isSignedIntegerType())
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001539 NewTy = S.Context.LongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001540 else
Chris Lattner0b2f4da2008-06-29 00:28:59 +00001541 NewTy = S.Context.UnsignedLongLongTy;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001542 break;
Eli Friedman73397492009-03-03 06:41:03 +00001543 case 96:
1544 NewTy = S.Context.LongDoubleTy;
1545 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +00001546 case 128:
1547 if (!IntegerMode) {
1548 S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name;
1549 return;
1550 }
1551 NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType());
Eli Friedman73397492009-03-03 06:41:03 +00001552 break;
Chris Lattnerfbf13472008-06-27 22:18:37 +00001553 }
1554
Eli Friedman73397492009-03-03 06:41:03 +00001555 if (ComplexMode) {
1556 NewTy = S.Context.getComplexType(NewTy);
Chris Lattnerfbf13472008-06-27 22:18:37 +00001557 }
1558
1559 // Install the new type.
1560 if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D))
1561 TD->setUnderlyingType(NewTy);
1562 else
1563 cast<ValueDecl>(D)->setType(NewTy);
1564}
Chris Lattner0744e5f2008-06-29 00:23:49 +00001565
Anders Carlssond87df372009-02-13 06:46:13 +00001566static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1567 // check the attribute arguments.
1568 if (Attr.getNumArgs() > 0) {
1569 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1570 return;
1571 }
Anders Carlssone896d982009-02-13 08:11:52 +00001572
Anders Carlsson5bab7882009-02-19 19:16:48 +00001573 if (!isFunctionOrMethod(d)) {
Anders Carlssond87df372009-02-13 06:46:13 +00001574 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001575 << Attr.getName() << 0 /*function*/;
Anders Carlssond87df372009-02-13 06:46:13 +00001576 return;
1577 }
1578
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001579 d->addAttr(::new (S.Context) NodebugAttr());
Anders Carlssond87df372009-02-13 06:46:13 +00001580}
1581
Anders Carlsson5bab7882009-02-19 19:16:48 +00001582static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1583 // check the attribute arguments.
1584 if (Attr.getNumArgs() != 0) {
1585 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1586 return;
1587 }
1588
Chris Lattnerc5197432009-04-14 17:02:11 +00001589 if (!isa<FunctionDecl>(d)) {
Anders Carlsson5bab7882009-02-19 19:16:48 +00001590 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001591 << Attr.getName() << 0 /*function*/;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001592 return;
1593 }
1594
Chris Lattner0b2b6e12009-03-04 06:34:08 +00001595 d->addAttr(::new (S.Context) NoinlineAttr());
Anders Carlsson5bab7882009-02-19 19:16:48 +00001596}
1597
Chris Lattnercf2a7212009-04-20 19:12:28 +00001598static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) {
Chris Lattner26e25542009-04-14 16:30:50 +00001599 // check the attribute arguments.
1600 if (Attr.getNumArgs() != 0) {
1601 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
1602 return;
1603 }
1604
Chris Lattnerc5197432009-04-14 17:02:11 +00001605 FunctionDecl *Fn = dyn_cast<FunctionDecl>(d);
1606 if (Fn == 0) {
Chris Lattner26e25542009-04-14 16:30:50 +00001607 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001608 << Attr.getName() << 0 /*function*/;
Chris Lattner26e25542009-04-14 16:30:50 +00001609 return;
1610 }
1611
Chris Lattnerc5197432009-04-14 17:02:11 +00001612 if (!Fn->isInline()) {
Chris Lattnercf2a7212009-04-20 19:12:28 +00001613 S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline);
Chris Lattnerc5197432009-04-14 17:02:11 +00001614 return;
1615 }
1616
Douglas Gregor9f9bf252009-04-28 06:37:30 +00001617 d->addAttr(::new (S.Context) GNUInlineAttr());
Chris Lattner26e25542009-04-14 16:30:50 +00001618}
1619
Fariborz Jahanianee760332009-03-27 18:38:55 +00001620static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) {
1621 // check the attribute arguments.
1622 if (Attr.getNumArgs() != 1) {
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001623 S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001624 return;
1625 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001626
Fariborz Jahanianee760332009-03-27 18:38:55 +00001627 if (!isFunctionOrMethod(d)) {
1628 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001629 << Attr.getName() << 0 /*function*/;
Fariborz Jahanianee760332009-03-27 18:38:55 +00001630 return;
1631 }
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001632
1633 Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0));
1634 llvm::APSInt NumParams(32);
1635 if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) {
1636 S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int)
1637 << "regparm" << NumParamsExpr->getSourceRange();
1638 return;
1639 }
1640
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001641 if (S.Context.Target.getRegParmMax() == 0) {
1642 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform)
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001643 << NumParamsExpr->getSourceRange();
1644 return;
1645 }
1646
Anton Korobeynikov348f28a2009-04-04 10:27:50 +00001647 if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) {
Anton Korobeynikov264a76c2009-04-03 23:38:25 +00001648 S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number)
1649 << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange();
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001650 return;
1651 }
1652
1653 d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue()));
Fariborz Jahanianee760332009-03-27 18:38:55 +00001654}
1655
Chris Lattner0744e5f2008-06-29 00:23:49 +00001656//===----------------------------------------------------------------------===//
Ted Kremenekb71368d2009-05-09 02:44:38 +00001657// Checker-specific attribute handlers.
1658//===----------------------------------------------------------------------===//
1659
1660static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr,
1661 Sema &S) {
1662
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001663 QualType RetTy;
1664
1665 if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d))
1666 RetTy = MD->getResultType();
1667 else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d))
1668 RetTy = FD->getResultType();
1669 else {
1670 S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
1671 << Attr.getName() << 3 /* function or method */;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001672 return;
1673 }
1674
Ted Kremenek5dc53c92009-05-13 21:07:32 +00001675 if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAsPointerType())) {
1676 S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type)
1677 << Attr.getName();
1678 return;
1679 }
1680
Ted Kremenekb71368d2009-05-09 02:44:38 +00001681 switch (Attr.getKind()) {
1682 default:
1683 assert(0 && "invalid ownership attribute");
1684 return;
1685 case AttributeList::AT_cf_returns_retained:
1686 d->addAttr(::new (S.Context) CFReturnsRetainedAttr());
1687 return;
1688 case AttributeList::AT_ns_returns_retained:
1689 d->addAttr(::new (S.Context) NSReturnsRetainedAttr());
1690 return;
1691 };
1692}
1693
1694//===----------------------------------------------------------------------===//
Chris Lattner0744e5f2008-06-29 00:23:49 +00001695// Top Level Sema Entry Points
1696//===----------------------------------------------------------------------===//
1697
Sebastian Redla89d82c2008-12-21 19:24:58 +00001698/// ProcessDeclAttribute - Apply the specific attribute to the specified decl if
Chris Lattner803d0802008-06-29 00:43:07 +00001699/// the attribute applies to decls. If the attribute is a type attribute, just
1700/// silently ignore it.
1701static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) {
1702 switch (Attr.getKind()) {
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001703 case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001704 case AttributeList::AT_address_space:
Fariborz Jahanianba372b82009-02-18 17:52:36 +00001705 case AttributeList::AT_objc_gc:
1706 // Ignore these, these are type attributes, handled by ProcessTypeAttributes.
Chris Lattner803d0802008-06-29 00:43:07 +00001707 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001708 case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break;
1709 case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break;
Daniel Dunbaraf668b02008-10-28 00:17:57 +00001710 case AttributeList::AT_always_inline:
1711 HandleAlwaysInlineAttr (D, Attr, S); break;
Ted Kremenekb7252322009-04-10 00:01:14 +00001712 case AttributeList::AT_analyzer_noreturn:
1713 HandleAnalyzerNoReturnAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001714 case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break;
1715 case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break;
1716 case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break;
1717 case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break;
1718 case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break;
1719 case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001720 case AttributeList::AT_ext_vector_type:
1721 HandleExtVectorTypeAttr(D, Attr, S);
1722 break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001723 case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break;
1724 case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break;
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001725 case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break;
Chris Lattnercf2a7212009-04-20 19:12:28 +00001726 case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001727 case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001728 case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break;
1729 case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break;
1730 case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break;
Ted Kremenekb71368d2009-05-09 02:44:38 +00001731
1732 // Checker-specific.
1733 case AttributeList::AT_ns_returns_retained:
1734 case AttributeList::AT_cf_returns_retained:
1735 HandleNSReturnsRetainedAttr(D, Attr, S); break;
1736
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001737 case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break;
Daniel Dunbar17f194f2009-02-12 17:28:23 +00001738 case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001739 case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break;
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +00001740 case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001741 case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break;
Daniel Dunbarb805dad2009-02-13 19:23:53 +00001742 case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break;
Daniel Dunbar3068ae02008-07-31 22:40:48 +00001743 case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001744 case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break;
Chris Lattner026dc962009-02-14 07:37:35 +00001745 case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S);
1746 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001747 case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break;
Daniel Dunbar6e775db2009-03-06 06:39:57 +00001748 case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break;
Chris Lattner803d0802008-06-29 00:43:07 +00001749 case AttributeList::AT_transparent_union:
1750 HandleTransparentUnionAttr(D, Attr, S);
1751 break;
Chris Lattner0db29ec2009-02-14 08:09:34 +00001752 case AttributeList::AT_objc_exception:
1753 HandleObjCExceptionAttr(D, Attr, S);
1754 break;
Douglas Gregorf9201e02009-02-11 23:02:49 +00001755 case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break;
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00001756 case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break;
Steve Naroff9eae5762008-09-18 16:44:58 +00001757 case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break;
Anders Carlsson77091822008-10-05 18:05:59 +00001758 case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break;
Anders Carlsson232eb7d2008-10-05 23:32:53 +00001759 case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break;
1760 case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break;
Anders Carlssonf6e35d02009-01-31 01:16:18 +00001761 case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break;
Anders Carlssond87df372009-02-13 06:46:13 +00001762 case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break;
Anders Carlsson5bab7882009-02-19 19:16:48 +00001763 case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break;
Eli Friedman55d3aaf2009-03-27 21:06:47 +00001764 case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break;
Anders Carlsson05f8e472009-02-13 08:16:43 +00001765 case AttributeList::IgnoredAttribute:
Chris Lattner5e204482009-04-25 18:44:54 +00001766 case AttributeList::AT_no_instrument_function: // Interacts with -pg.
Anders Carlsson05f8e472009-02-13 08:16:43 +00001767 // Just ignore
1768 break;
Chris Lattner803d0802008-06-29 00:43:07 +00001769 default:
Anders Carlssond87df372009-02-13 06:46:13 +00001770 S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName();
Chris Lattner803d0802008-06-29 00:43:07 +00001771 break;
1772 }
1773}
1774
1775/// ProcessDeclAttributeList - Apply all the decl attributes in the specified
1776/// attribute list to the specified decl, ignoring any type attributes.
1777void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) {
1778 while (AttrList) {
1779 ProcessDeclAttribute(D, *AttrList, *this);
1780 AttrList = AttrList->getNext();
1781 }
1782}
1783
Chris Lattner0744e5f2008-06-29 00:23:49 +00001784/// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in
1785/// it, apply them to D. This is a bit tricky because PD can have attributes
1786/// specified in many different places, and we need to find and apply them all.
1787void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) {
1788 // Apply decl attributes from the DeclSpec if present.
1789 if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes())
1790 ProcessDeclAttributeList(D, Attrs);
Chris Lattner803d0802008-06-29 00:43:07 +00001791
Chris Lattner0744e5f2008-06-29 00:23:49 +00001792 // Walk the declarator structure, applying decl attributes that were in a type
1793 // position to the decl itself. This handles cases like:
1794 // int *__attr__(x)** D;
1795 // when X is a decl attribute.
1796 for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i)
1797 if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs())
1798 ProcessDeclAttributeList(D, Attrs);
1799
1800 // Finally, apply any attributes on the decl itself.
1801 if (const AttributeList *Attrs = PD.getAttributes())
1802 ProcessDeclAttributeList(D, Attrs);
1803}