Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- 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 Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclObjC.h" |
| 17 | #include "clang/AST/Expr.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 18 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 12bc692 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 19 | #include "clang/Parse/DeclSpec.h" |
Ted Kremenek | 6e1eb87 | 2008-07-22 16:56:21 +0000 | [diff] [blame] | 20 | #include <llvm/ADT/StringExtras.h> |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | // Helper functions |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame^] | 27 | static const FunctionType *getFunctionType(Decl *d, bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 28 | QualType Ty; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 29 | 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 Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame^] | 40 | else if (blocksToo && Ty->isBlockPointerType()) |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 41 | Ty = Ty->getAsBlockPointerType()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 42 | |
| 43 | return Ty->getAsFunctionType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 46 | // FIXME: We should provide an abstraction around a method or function |
| 47 | // to provide the following bits of information. |
| 48 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 49 | /// isFunctionOrMethod - Return true if the given decl has function |
| 50 | /// type (function or function-typed variable) or an Objective-C |
| 51 | /// method. |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 52 | static bool isFunctionOrMethod(Decl *d) { |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame^] | 53 | return getFunctionType(d, false) || isa<ObjCMethodDecl>(d); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 56 | /// 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. |
| 59 | static 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 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 70 | /// hasFunctionProto - Return true if the given decl has a argument |
| 71 | /// information. This decl should have already passed |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 72 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 73 | static bool hasFunctionProto(Decl *d) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 74 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 75 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 76 | else { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 77 | assert(isa<ObjCMethodDecl>(d)); |
| 78 | 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 Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 85 | static unsigned getFunctionOrMethodNumArgs(Decl *d) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 86 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 87 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 88 | return cast<ObjCMethodDecl>(d)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static QualType getFunctionOrMethodArgType(Decl *d, unsigned Idx) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 92 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 93 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 94 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 96 | return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static bool isFunctionOrMethodVariadic(Decl *d) { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 100 | if (const FunctionType *FnTy = getFunctionType(d)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 101 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 102 | return proto->isVariadic(); |
| 103 | } else { |
| 104 | return cast<ObjCMethodDecl>(d)->isVariadic(); |
| 105 | } |
| 106 | } |
| 107 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 108 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 109 | const PointerType *PT = T->getAsPointerType(); |
| 110 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 111 | return false; |
| 112 | |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 113 | const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAsObjCInterfaceType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 114 | if (!ClsT) |
| 115 | return false; |
| 116 | |
| 117 | IdentifierInfo* ClsName = ClsT->getDecl()->getIdentifier(); |
| 118 | |
| 119 | // FIXME: Should we walk the chain of classes? |
| 120 | return ClsName == &Ctx.Idents.get("NSString") || |
| 121 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 122 | } |
| 123 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 124 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
| 125 | const PointerType *PT = T->getAsPointerType(); |
| 126 | if (!PT) |
| 127 | return false; |
| 128 | |
| 129 | const RecordType *RT = PT->getPointeeType()->getAsRecordType(); |
| 130 | if (!RT) |
| 131 | return false; |
| 132 | |
| 133 | const RecordDecl *RD = RT->getDecl(); |
| 134 | if (RD->getTagKind() != TagDecl::TK_struct) |
| 135 | return false; |
| 136 | |
| 137 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 138 | } |
| 139 | |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 140 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 141 | // Attribute Implementations |
| 142 | //===----------------------------------------------------------------------===// |
| 143 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 144 | // FIXME: All this manual attribute parsing code is gross. At the |
| 145 | // least add some helper functions to check most argument patterns (# |
| 146 | // and types of args). |
| 147 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 148 | static void HandleExtVectorTypeAttr(Decl *d, const AttributeList &Attr, |
| 149 | Sema &S) { |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 150 | TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d); |
| 151 | if (tDecl == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 152 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 153 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 156 | QualType curType = tDecl->getUnderlyingType(); |
| 157 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 158 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 159 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 160 | return; |
| 161 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 162 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 163 | llvm::APSInt vecSize(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 164 | if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 165 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 166 | << "ext_vector_type" << sizeExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | // unlike gcc's vector_size attribute, we do not allow vectors to be defined |
| 170 | // in conjunction with complex types (pointers, arrays, functions, etc.). |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 171 | if (!curType->isIntegerType() && !curType->isRealFloatingType()) { |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 172 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << curType; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 173 | return; |
| 174 | } |
| 175 | // unlike gcc's vector_size attribute, the size is specified as the |
| 176 | // number of elements, not the number of bytes. |
| 177 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue()); |
| 178 | |
| 179 | if (vectorSize == 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 180 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size) |
| 181 | << sizeExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 182 | return; |
| 183 | } |
| 184 | // Instantiate/Install the vector type, the number of elements is > 0. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 185 | tDecl->setUnderlyingType(S.Context.getExtVectorType(curType, vectorSize)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 186 | // Remember this typedef decl, we will need it later for diagnostics. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 187 | S.ExtVectorDecls.push_back(tDecl); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 190 | |
| 191 | /// HandleVectorSizeAttribute - this attribute is only applicable to |
| 192 | /// integral and float scalars, although arrays, pointers, and function |
| 193 | /// return values are allowed in conjunction with this construct. Aggregates |
| 194 | /// with this attribute are invalid, even if they are of the same size as a |
| 195 | /// corresponding scalar. |
| 196 | /// The raw attribute should contain precisely 1 argument, the vector size |
| 197 | /// for the variable, measured in bytes. If curType and rawAttr are well |
| 198 | /// formed, this routine will return a new vector type. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 199 | static void HandleVectorSizeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 200 | QualType CurType; |
| 201 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 202 | CurType = VD->getType(); |
| 203 | else if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 204 | CurType = TD->getUnderlyingType(); |
| 205 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 206 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 207 | << "vector_size" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 208 | return; |
| 209 | } |
| 210 | |
| 211 | // Check the attribute arugments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 212 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 213 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 214 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 216 | Expr *sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 217 | llvm::APSInt vecSize(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 218 | if (!sizeExpr->isIntegerConstantExpr(vecSize, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 219 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 220 | << "vector_size" << sizeExpr->getSourceRange(); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 221 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 222 | } |
| 223 | // navigate to the base type - we need to provide for vector pointers, |
| 224 | // vector arrays, and functions returning vectors. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 225 | if (CurType->isPointerType() || CurType->isArrayType() || |
| 226 | CurType->isFunctionType()) { |
Chris Lattner | 2db15bd | 2009-05-13 04:00:12 +0000 | [diff] [blame] | 227 | S.Diag(Attr.getLoc(), diag::err_unsupported_vector_size) << CurType; |
| 228 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 229 | /* FIXME: rebuild the type from the inside out, vectorizing the inner type. |
| 230 | do { |
| 231 | if (PointerType *PT = dyn_cast<PointerType>(canonType)) |
| 232 | canonType = PT->getPointeeType().getTypePtr(); |
| 233 | else if (ArrayType *AT = dyn_cast<ArrayType>(canonType)) |
| 234 | canonType = AT->getElementType().getTypePtr(); |
| 235 | else if (FunctionType *FT = dyn_cast<FunctionType>(canonType)) |
| 236 | canonType = FT->getResultType().getTypePtr(); |
| 237 | } while (canonType->isPointerType() || canonType->isArrayType() || |
| 238 | canonType->isFunctionType()); |
| 239 | */ |
| 240 | } |
Chris Lattner | 82afa2d | 2009-05-13 05:13:44 +0000 | [diff] [blame] | 241 | // the base type must be integer or float, and can't already be a vector. |
| 242 | if (CurType->isVectorType() || |
| 243 | (!CurType->isIntegerType() && !CurType->isRealFloatingType())) { |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 244 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_vector_type) << CurType; |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 245 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 246 | } |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 247 | unsigned typeSize = static_cast<unsigned>(S.Context.getTypeSize(CurType)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 248 | // vecSize is specified in bytes - convert to bits. |
| 249 | unsigned vectorSize = static_cast<unsigned>(vecSize.getZExtValue() * 8); |
| 250 | |
| 251 | // the vector size needs to be an integral multiple of the type size. |
| 252 | if (vectorSize % typeSize) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 253 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_size) |
| 254 | << sizeExpr->getSourceRange(); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 255 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 256 | } |
| 257 | if (vectorSize == 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 258 | S.Diag(Attr.getLoc(), diag::err_attribute_zero_size) |
| 259 | << sizeExpr->getSourceRange(); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 260 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 261 | } |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 262 | |
| 263 | // Success! Instantiate the vector type, the number of elements is > 0, and |
| 264 | // not required to be a power of 2, unlike GCC. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 265 | CurType = S.Context.getVectorType(CurType, vectorSize/typeSize); |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 266 | |
| 267 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 268 | VD->setType(CurType); |
| 269 | else |
| 270 | cast<TypedefDecl>(D)->setUnderlyingType(CurType); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 273 | static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 274 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 275 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 276 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 277 | return; |
| 278 | } |
| 279 | |
| 280 | if (TagDecl *TD = dyn_cast<TagDecl>(d)) |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 281 | TD->addAttr(::new (S.Context) PackedAttr(1)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 282 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { |
| 283 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 284 | // has no effect. |
| 285 | if (!FD->getType()->isIncompleteType() && |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 286 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 287 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 288 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 289 | else |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 290 | FD->addAttr(::new (S.Context) PackedAttr(1)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 291 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 292 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 295 | static void HandleIBOutletAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 296 | // check the attribute arguments. |
| 297 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 298 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 299 | return; |
| 300 | } |
| 301 | |
| 302 | // The IBOutlet attribute only applies to instance variables of Objective-C |
| 303 | // classes. |
Ted Kremenek | 3274260 | 2009-02-17 22:20:20 +0000 | [diff] [blame] | 304 | if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 305 | d->addAttr(::new (S.Context) IBOutletAttr()); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 306 | else |
Ted Kremenek | 3274260 | 2009-02-17 22:20:20 +0000 | [diff] [blame] | 307 | S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 310 | static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 311 | // GCC ignores the nonnull attribute on K&R style function |
| 312 | // prototypes, so we ignore it as well |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 313 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 314 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 315 | << Attr.getName() << 0 /*function*/; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 316 | return; |
| 317 | } |
| 318 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 319 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 320 | |
| 321 | // The nonnull attribute only applies to pointers. |
| 322 | llvm::SmallVector<unsigned, 10> NonNullArgs; |
| 323 | |
| 324 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 325 | E=Attr.arg_end(); I!=E; ++I) { |
| 326 | |
| 327 | |
| 328 | // The argument must be an integer constant expression. |
Ted Kremenek | f5e8834 | 2008-12-04 19:38:33 +0000 | [diff] [blame] | 329 | Expr *Ex = static_cast<Expr *>(*I); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 330 | llvm::APSInt ArgNum(32); |
| 331 | if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 332 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 333 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 334 | return; |
| 335 | } |
| 336 | |
| 337 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 338 | |
| 339 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 340 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 341 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 342 | return; |
| 343 | } |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 344 | |
| 345 | --x; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 346 | |
| 347 | // Is the function argument a pointer type? |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 348 | QualType T = getFunctionOrMethodArgType(d, x); |
| 349 | if (!T->isPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 350 | // FIXME: Should also highlight argument in decl. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 351 | S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only) |
| 352 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 353 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | NonNullArgs.push_back(x); |
| 357 | } |
| 358 | |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 359 | // If no arguments were specified to __attribute__((nonnull)) then all |
| 360 | // pointer arguments have a nonnull attribute. |
| 361 | if (NonNullArgs.empty()) { |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 362 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) { |
| 363 | QualType T = getFunctionOrMethodArgType(d, I); |
| 364 | if (T->isPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 365 | NonNullArgs.push_back(I); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 366 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 367 | |
| 368 | if (NonNullArgs.empty()) { |
| 369 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
| 370 | return; |
| 371 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 372 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 373 | |
| 374 | unsigned* start = &NonNullArgs[0]; |
| 375 | unsigned size = NonNullArgs.size(); |
| 376 | std::sort(start, start + size); |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 377 | d->addAttr(::new (S.Context) NonNullAttr(start, size)); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 380 | static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 381 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 382 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 383 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 384 | return; |
| 385 | } |
| 386 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 387 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 388 | Arg = Arg->IgnoreParenCasts(); |
| 389 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 390 | |
| 391 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 392 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 393 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 394 | return; |
| 395 | } |
| 396 | |
| 397 | const char *Alias = Str->getStrData(); |
| 398 | unsigned AliasLen = Str->getByteLength(); |
| 399 | |
| 400 | // FIXME: check if target symbol exists in current file |
| 401 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 402 | d->addAttr(::new (S.Context) AliasAttr(std::string(Alias, AliasLen))); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 405 | static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr, |
| 406 | Sema &S) { |
| 407 | // check the attribute arguments. |
| 408 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 409 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 410 | return; |
| 411 | } |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 412 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 413 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 414 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 415 | << Attr.getName() << 0 /*function*/; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 416 | return; |
| 417 | } |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 418 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 419 | d->addAttr(::new (S.Context) AlwaysInlineAttr()); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 422 | static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr, |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 423 | Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 424 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 425 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 426 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 427 | return false; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 428 | } |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 429 | |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 430 | if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) { |
| 431 | ValueDecl *VD = dyn_cast<ValueDecl>(d); |
| 432 | if (VD == 0 || !VD->getType()->isBlockPointerType()) { |
| 433 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 434 | << Attr.getName() << 0 /*function*/; |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 435 | return false; |
| 436 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 439 | return true; |
| 440 | } |
| 441 | |
| 442 | static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 443 | if (HandleCommonNoReturnAttr(d, Attr, S)) |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 444 | d->addAttr(::new (S.Context) NoReturnAttr()); |
| 445 | } |
| 446 | |
| 447 | static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr, |
| 448 | Sema &S) { |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 449 | if (HandleCommonNoReturnAttr(d, Attr, S)) |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 450 | d->addAttr(::new (S.Context) AnalyzerNoReturnAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 453 | static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 454 | // check the attribute arguments. |
| 455 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 456 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 460 | if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 461 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 462 | << Attr.getName() << 2 /*variable and function*/; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 466 | d->addAttr(::new (S.Context) UnusedAttr()); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 469 | static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 470 | // check the attribute arguments. |
| 471 | if (Attr.getNumArgs() != 0) { |
| 472 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 473 | return; |
| 474 | } |
| 475 | |
| 476 | if (const VarDecl *VD = dyn_cast<VarDecl>(d)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 477 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 478 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 479 | return; |
| 480 | } |
| 481 | } else if (!isFunctionOrMethod(d)) { |
| 482 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 483 | << Attr.getName() << 2 /*variable and function*/; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 484 | return; |
| 485 | } |
| 486 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 487 | d->addAttr(::new (S.Context) UsedAttr()); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 488 | } |
| 489 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 490 | static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 491 | // check the attribute arguments. |
| 492 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 493 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 494 | << "0 or 1"; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 495 | return; |
| 496 | } |
| 497 | |
| 498 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 499 | if (Attr.getNumArgs() > 0) { |
| 500 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 501 | llvm::APSInt Idx(32); |
| 502 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 503 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 504 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 505 | return; |
| 506 | } |
| 507 | priority = Idx.getZExtValue(); |
| 508 | } |
| 509 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 510 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 511 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 512 | << Attr.getName() << 0 /*function*/; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 513 | return; |
| 514 | } |
| 515 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 516 | d->addAttr(::new (S.Context) ConstructorAttr(priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 520 | // check the attribute arguments. |
| 521 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 522 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 523 | << "0 or 1"; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 524 | return; |
| 525 | } |
| 526 | |
| 527 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 528 | if (Attr.getNumArgs() > 0) { |
| 529 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 530 | llvm::APSInt Idx(32); |
| 531 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 532 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 533 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 534 | return; |
| 535 | } |
| 536 | priority = Idx.getZExtValue(); |
| 537 | } |
| 538 | |
Anders Carlsson | 6782fc6 | 2008-08-22 22:10:48 +0000 | [diff] [blame] | 539 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 540 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 541 | << Attr.getName() << 0 /*function*/; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 542 | return; |
| 543 | } |
| 544 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 545 | d->addAttr(::new (S.Context) DestructorAttr(priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 548 | static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 549 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 550 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 551 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 552 | return; |
| 553 | } |
| 554 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 555 | d->addAttr(::new (S.Context) DeprecatedAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 558 | static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 559 | // check the attribute arguments. |
| 560 | if (Attr.getNumArgs() != 0) { |
| 561 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 562 | return; |
| 563 | } |
| 564 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 565 | d->addAttr(::new (S.Context) UnavailableAttr()); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 568 | static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 569 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 570 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 571 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 572 | return; |
| 573 | } |
| 574 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 575 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 576 | Arg = Arg->IgnoreParenCasts(); |
| 577 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 578 | |
| 579 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 580 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 581 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 582 | return; |
| 583 | } |
| 584 | |
| 585 | const char *TypeStr = Str->getStrData(); |
| 586 | unsigned TypeLen = Str->getByteLength(); |
| 587 | VisibilityAttr::VisibilityTypes type; |
| 588 | |
| 589 | if (TypeLen == 7 && !memcmp(TypeStr, "default", 7)) |
| 590 | type = VisibilityAttr::DefaultVisibility; |
| 591 | else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6)) |
| 592 | type = VisibilityAttr::HiddenVisibility; |
| 593 | else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8)) |
| 594 | type = VisibilityAttr::HiddenVisibility; // FIXME |
| 595 | else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9)) |
| 596 | type = VisibilityAttr::ProtectedVisibility; |
| 597 | else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 598 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 599 | return; |
| 600 | } |
| 601 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 602 | d->addAttr(::new (S.Context) VisibilityAttr(type)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 605 | static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr, |
| 606 | Sema &S) { |
| 607 | if (Attr.getNumArgs() != 0) { |
| 608 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 613 | if (OCI == 0) { |
| 614 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 615 | return; |
| 616 | } |
| 617 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 618 | D->addAttr(::new (S.Context) ObjCExceptionAttr()); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 622 | if (Attr.getNumArgs() != 0) { |
| 623 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 624 | return; |
| 625 | } |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 626 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 627 | QualType T = TD->getUnderlyingType(); |
| 628 | if (!T->isPointerType() || |
| 629 | !T->getAsPointerType()->getPointeeType()->isRecordType()) { |
| 630 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 631 | return; |
| 632 | } |
| 633 | } |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 634 | D->addAttr(::new (S.Context) ObjCNSObjectAttr()); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 635 | } |
| 636 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 637 | static void |
| 638 | HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 639 | if (Attr.getNumArgs() != 0) { |
| 640 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | if (!isa<FunctionDecl>(D)) { |
| 645 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 646 | return; |
| 647 | } |
| 648 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 649 | D->addAttr(::new (S.Context) OverloadableAttr()); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 652 | static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 653 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 654 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 655 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 656 | return; |
| 657 | } |
| 658 | |
| 659 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 660 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 661 | return; |
| 662 | } |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 663 | |
| 664 | BlocksAttr::BlocksAttrTypes type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 665 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 666 | type = BlocksAttr::ByRef; |
| 667 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 668 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 669 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 670 | return; |
| 671 | } |
| 672 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 673 | d->addAttr(::new (S.Context) BlocksAttr(type)); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 676 | static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 677 | // check the attribute arguments. |
| 678 | if (Attr.getNumArgs() > 2) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 679 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 680 | << "0, 1 or 2"; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 681 | return; |
| 682 | } |
| 683 | |
| 684 | int sentinel = 0; |
| 685 | if (Attr.getNumArgs() > 0) { |
| 686 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 687 | llvm::APSInt Idx(32); |
| 688 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 689 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 690 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 691 | return; |
| 692 | } |
| 693 | sentinel = Idx.getZExtValue(); |
| 694 | |
| 695 | if (sentinel < 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 696 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 697 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 698 | return; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | int nullPos = 0; |
| 703 | if (Attr.getNumArgs() > 1) { |
| 704 | Expr *E = static_cast<Expr *>(Attr.getArg(1)); |
| 705 | llvm::APSInt Idx(32); |
| 706 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 707 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 708 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 709 | return; |
| 710 | } |
| 711 | nullPos = Idx.getZExtValue(); |
| 712 | |
| 713 | if (nullPos > 1 || nullPos < 0) { |
| 714 | // FIXME: This error message could be improved, it would be nice |
| 715 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 716 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 717 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 718 | return; |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 723 | const FunctionType *FT = FD->getType()->getAsFunctionType(); |
| 724 | assert(FT && "FunctionDecl has non-function type?"); |
| 725 | |
| 726 | if (isa<FunctionNoProtoType>(FT)) { |
| 727 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 728 | return; |
| 729 | } |
| 730 | |
| 731 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 732 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 733 | return; |
| 734 | } |
| 735 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) { |
| 736 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 737 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 738 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 739 | } |
| 740 | } else if (isa<BlockDecl>(d)) { |
| 741 | // Note! BlockDecl is typeless. Variadic diagnostics |
| 742 | // will be issued by the caller. |
| 743 | ; |
| 744 | } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 745 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 746 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
| 747 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d) |
| 748 | : Ty->getAsBlockPointerType()->getPointeeType()->getAsFunctionType(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 749 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 750 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 751 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 752 | return; |
| 753 | } |
| 754 | } |
| 755 | else { |
| 756 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Fariborz Jahanian | ffb0081 | 2009-05-14 20:57:28 +0000 | [diff] [blame] | 757 | << Attr.getName() << 6 /*function, method or block */; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 758 | return; |
| 759 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 760 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 761 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Fariborz Jahanian | ffb0081 | 2009-05-14 20:57:28 +0000 | [diff] [blame] | 762 | << Attr.getName() << 6 /*function, method or block */; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 763 | return; |
| 764 | } |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 765 | d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos)); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 768 | static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) { |
| 769 | // check the attribute arguments. |
| 770 | if (Attr.getNumArgs() != 0) { |
| 771 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 772 | return; |
| 773 | } |
| 774 | |
| 775 | // TODO: could also be applied to methods? |
| 776 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
| 777 | if (!Fn) { |
| 778 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 779 | << Attr.getName() << 0 /*function*/; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 780 | return; |
| 781 | } |
| 782 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 783 | Fn->addAttr(::new (S.Context) WarnUnusedResultAttr()); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 787 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 788 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 789 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 790 | return; |
| 791 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 792 | |
| 793 | // TODO: could also be applied to methods? |
| 794 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
| 795 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 796 | << Attr.getName() << 2 /*variable and function*/; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 797 | return; |
| 798 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 799 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 800 | D->addAttr(::new (S.Context) WeakAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 803 | static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 804 | // check the attribute arguments. |
| 805 | if (Attr.getNumArgs() != 0) { |
| 806 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 807 | return; |
| 808 | } |
| 809 | |
| 810 | // weak_import only applies to variable & function declarations. |
| 811 | bool isDef = false; |
| 812 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 813 | isDef = (!VD->hasExternalStorage() || VD->getInit()); |
| 814 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 815 | isDef = FD->getBody(S.Context); |
Fariborz Jahanian | d4edddd | 2009-05-04 19:35:12 +0000 | [diff] [blame] | 816 | } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) { |
| 817 | // We ignore weak import on properties and methods |
Mike Stump | 1c90f4d | 2009-03-18 17:39:31 +0000 | [diff] [blame] | 818 | return; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 819 | } else { |
| 820 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 821 | << Attr.getName() << 2 /*variable and function*/; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 822 | return; |
| 823 | } |
| 824 | |
| 825 | // Merge should handle any subsequent violations. |
| 826 | if (isDef) { |
| 827 | S.Diag(Attr.getLoc(), |
| 828 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 829 | << "weak_import" << 2 /*variable and function*/; |
| 830 | return; |
| 831 | } |
| 832 | |
| 833 | D->addAttr(::new (S.Context) WeakImportAttr()); |
| 834 | } |
| 835 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 836 | static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 837 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 838 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 839 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 840 | return; |
| 841 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 842 | |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 843 | // Attribute can be applied only to functions or variables. |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 844 | if (isa<VarDecl>(D)) { |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 845 | D->addAttr(::new (S.Context) DLLImportAttr()); |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 846 | return; |
| 847 | } |
| 848 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 849 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 850 | if (!FD) { |
| 851 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 852 | << Attr.getName() << 2 /*variable and function*/; |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 853 | return; |
| 854 | } |
| 855 | |
| 856 | // Currently, the dllimport attribute is ignored for inlined functions. |
| 857 | // Warning is emitted. |
| 858 | if (FD->isInline()) { |
| 859 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 860 | return; |
| 861 | } |
| 862 | |
| 863 | // The attribute is also overridden by a subsequent declaration as dllexport. |
| 864 | // Warning is emitted. |
| 865 | for (AttributeList *nextAttr = Attr.getNext(); nextAttr; |
| 866 | nextAttr = nextAttr->getNext()) { |
| 867 | if (nextAttr->getKind() == AttributeList::AT_dllexport) { |
| 868 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 869 | return; |
| 870 | } |
| 871 | } |
| 872 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 873 | if (D->getAttr<DLLExportAttr>()) { |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 874 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 875 | return; |
| 876 | } |
| 877 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 878 | D->addAttr(::new (S.Context) DLLImportAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 881 | static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 882 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 883 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 884 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 885 | return; |
| 886 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 887 | |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 888 | // Attribute can be applied only to functions or variables. |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 889 | if (isa<VarDecl>(D)) { |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 890 | D->addAttr(::new (S.Context) DLLExportAttr()); |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 891 | return; |
| 892 | } |
| 893 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 894 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 895 | if (!FD) { |
| 896 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 897 | << Attr.getName() << 2 /*variable and function*/; |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 898 | return; |
| 899 | } |
| 900 | |
| 901 | // Currently, the dllexport attribute is ignored for inlined functions, |
| 902 | // unless the -fkeep-inline-functions flag has been used. Warning is emitted; |
| 903 | if (FD->isInline()) { |
| 904 | // FIXME: ... unless the -fkeep-inline-functions flag has been used. |
| 905 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport"; |
| 906 | return; |
| 907 | } |
| 908 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 909 | D->addAttr(::new (S.Context) DLLExportAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 912 | static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 913 | // Attribute has no arguments. |
| 914 | if (Attr.getNumArgs() != 1) { |
| 915 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 916 | return; |
| 917 | } |
| 918 | |
| 919 | // Make sure that there is a string literal as the sections's single |
| 920 | // argument. |
| 921 | StringLiteral *SE = |
| 922 | dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0))); |
| 923 | if (!SE) { |
| 924 | // FIXME |
| 925 | S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string); |
| 926 | return; |
| 927 | } |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 928 | D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(), |
| 929 | SE->getByteLength()))); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 932 | static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 933 | // Attribute has no arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 934 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 935 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 936 | return; |
| 937 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 938 | |
| 939 | // Attribute can be applied only to functions. |
| 940 | if (!isa<FunctionDecl>(d)) { |
| 941 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 942 | << Attr.getName() << 0 /*function*/; |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 943 | return; |
| 944 | } |
| 945 | |
| 946 | // stdcall and fastcall attributes are mutually incompatible. |
| 947 | if (d->getAttr<FastCallAttr>()) { |
| 948 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 949 | << "stdcall" << "fastcall"; |
| 950 | return; |
| 951 | } |
| 952 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 953 | d->addAttr(::new (S.Context) StdCallAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 956 | static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 957 | // Attribute has no arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 958 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 959 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 960 | return; |
| 961 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 962 | |
| 963 | if (!isa<FunctionDecl>(d)) { |
| 964 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 965 | << Attr.getName() << 0 /*function*/; |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 966 | return; |
| 967 | } |
| 968 | |
| 969 | // stdcall and fastcall attributes are mutually incompatible. |
| 970 | if (d->getAttr<StdCallAttr>()) { |
| 971 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 972 | << "fastcall" << "stdcall"; |
| 973 | return; |
| 974 | } |
| 975 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 976 | d->addAttr(::new (S.Context) FastCallAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 979 | static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 980 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 981 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 982 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 983 | return; |
| 984 | } |
| 985 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 986 | d->addAttr(::new (S.Context) NoThrowAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 987 | } |
| 988 | |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 989 | static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 990 | // check the attribute arguments. |
| 991 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 992 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 993 | return; |
| 994 | } |
| 995 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 996 | d->addAttr(::new (S.Context) ConstAttr()); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1000 | // check the attribute arguments. |
| 1001 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1002 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1003 | return; |
| 1004 | } |
| 1005 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1006 | d->addAttr(::new (S.Context) PureAttr()); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1009 | static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1010 | // Match gcc which ignores cleanup attrs when compiling C++. |
| 1011 | if (S.getLangOptions().CPlusPlus) |
| 1012 | return; |
| 1013 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1014 | if (!Attr.getParameterName()) { |
| 1015 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1016 | return; |
| 1017 | } |
| 1018 | |
| 1019 | if (Attr.getNumArgs() != 0) { |
| 1020 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1021 | return; |
| 1022 | } |
| 1023 | |
| 1024 | VarDecl *VD = dyn_cast<VarDecl>(d); |
| 1025 | |
| 1026 | if (!VD || !VD->hasLocalStorage()) { |
| 1027 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 1028 | return; |
| 1029 | } |
| 1030 | |
| 1031 | // Look up the function |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1032 | NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(), |
| 1033 | Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1034 | if (!CleanupDecl) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1035 | S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1036 | Attr.getParameterName(); |
| 1037 | return; |
| 1038 | } |
| 1039 | |
| 1040 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 1041 | if (!FD) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1042 | S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1043 | Attr.getParameterName(); |
| 1044 | return; |
| 1045 | } |
| 1046 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1047 | if (FD->getNumParams() != 1) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1048 | S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1049 | Attr.getParameterName(); |
| 1050 | return; |
| 1051 | } |
| 1052 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1053 | // We're currently more strict than GCC about what function types we accept. |
| 1054 | // If this ever proves to be a problem it should be easy to fix. |
| 1055 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 1056 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Eli Friedman | d5e3e8e | 2009-04-26 01:30:08 +0000 | [diff] [blame] | 1057 | if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1058 | S.Diag(Attr.getLoc(), |
| 1059 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 1060 | Attr.getParameterName() << ParamTy << Ty; |
| 1061 | return; |
| 1062 | } |
| 1063 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1064 | d->addAttr(::new (S.Context) CleanupAttr(FD)); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1067 | /// Handle __attribute__((format(type,idx,firstarg))) attributes |
| 1068 | /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1069 | static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1070 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1071 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1072 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1073 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1074 | return; |
| 1075 | } |
| 1076 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1077 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1078 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1079 | return; |
| 1080 | } |
| 1081 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 1082 | if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1083 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1084 | << Attr.getName() << 0 /*function*/; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1085 | return; |
| 1086 | } |
| 1087 | |
| 1088 | // FIXME: in C++ the implicit 'this' function parameter also counts. |
| 1089 | // this is needed in order to be compatible with GCC |
| 1090 | // the index must start in 1 and the limit is numargs+1 |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1091 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1092 | unsigned FirstIdx = 1; |
| 1093 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1094 | const char *Format = Attr.getParameterName()->getName(); |
| 1095 | unsigned FormatLen = Attr.getParameterName()->getLength(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1096 | |
| 1097 | // Normalize the argument, __foo__ becomes foo. |
| 1098 | if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' && |
| 1099 | Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') { |
| 1100 | Format += 2; |
| 1101 | FormatLen -= 4; |
| 1102 | } |
| 1103 | |
| 1104 | bool Supported = false; |
| 1105 | bool is_NSString = false; |
| 1106 | bool is_strftime = false; |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1107 | bool is_CFString = false; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1108 | |
| 1109 | switch (FormatLen) { |
| 1110 | default: break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1111 | case 5: Supported = !memcmp(Format, "scanf", 5); break; |
| 1112 | case 6: Supported = !memcmp(Format, "printf", 6); break; |
| 1113 | case 7: Supported = !memcmp(Format, "strfmon", 7); break; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1114 | case 8: |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1115 | Supported = (is_strftime = !memcmp(Format, "strftime", 8)) || |
| 1116 | (is_NSString = !memcmp(Format, "NSString", 8)) || |
| 1117 | (is_CFString = !memcmp(Format, "CFString", 8)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1118 | break; |
| 1119 | } |
| 1120 | |
| 1121 | if (!Supported) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1122 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 1123 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1124 | return; |
| 1125 | } |
| 1126 | |
| 1127 | // checks for the 2nd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1128 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1129 | llvm::APSInt Idx(32); |
| 1130 | if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1131 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1132 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1133 | return; |
| 1134 | } |
| 1135 | |
| 1136 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1137 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1138 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | // FIXME: Do we need to bounds check? |
| 1143 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
| 1144 | |
| 1145 | // make sure the format string is really a string |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1146 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1147 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1148 | if (is_CFString) { |
| 1149 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1150 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1151 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1152 | return; |
| 1153 | } |
| 1154 | } else if (is_NSString) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1155 | // FIXME: do we need to check if the type is NSString*? What are the |
| 1156 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1157 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1158 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1159 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1160 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1161 | return; |
| 1162 | } |
| 1163 | } else if (!Ty->isPointerType() || |
| 1164 | !Ty->getAsPointerType()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1165 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1166 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1167 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1168 | return; |
| 1169 | } |
| 1170 | |
| 1171 | // check the 3rd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1172 | Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1173 | llvm::APSInt FirstArg(32); |
| 1174 | if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1175 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1176 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1177 | return; |
| 1178 | } |
| 1179 | |
| 1180 | // check if the function is variadic if the 3rd argument non-zero |
| 1181 | if (FirstArg != 0) { |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1182 | if (isFunctionOrMethodVariadic(d)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1183 | ++NumArgs; // +1 for ... |
| 1184 | } else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1185 | S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1186 | return; |
| 1187 | } |
| 1188 | } |
| 1189 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1190 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 1191 | // variable the input is just the current time + the format string. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1192 | if (is_strftime) { |
| 1193 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1194 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 1195 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1196 | return; |
| 1197 | } |
| 1198 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 1199 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1200 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1201 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1202 | return; |
| 1203 | } |
| 1204 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1205 | d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen), |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1206 | Idx.getZExtValue(), FirstArg.getZExtValue())); |
| 1207 | } |
| 1208 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1209 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 1210 | Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1211 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1212 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1213 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1214 | return; |
| 1215 | } |
| 1216 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1217 | // Try to find the underlying union declaration. |
| 1218 | RecordDecl *RD = 0; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1219 | TypedefDecl *TD = dyn_cast<TypedefDecl>(d); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1220 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 1221 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 1222 | else |
| 1223 | RD = dyn_cast<RecordDecl>(d); |
| 1224 | |
| 1225 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1226 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1227 | << Attr.getName() << 1 /*union*/; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1228 | return; |
| 1229 | } |
| 1230 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1231 | if (!RD->isDefinition()) { |
| 1232 | S.Diag(Attr.getLoc(), |
| 1233 | diag::warn_transparent_union_attribute_not_definition); |
| 1234 | return; |
| 1235 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1236 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1237 | RecordDecl::field_iterator Field = RD->field_begin(S.Context), |
| 1238 | FieldEnd = RD->field_end(S.Context); |
| 1239 | if (Field == FieldEnd) { |
| 1240 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 1241 | return; |
| 1242 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1243 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1244 | FieldDecl *FirstField = *Field; |
| 1245 | QualType FirstType = FirstField->getType(); |
| 1246 | if (FirstType->isFloatingType() || FirstType->isVectorType()) { |
| 1247 | S.Diag(FirstField->getLocation(), |
| 1248 | diag::warn_transparent_union_attribute_floating); |
| 1249 | return; |
| 1250 | } |
| 1251 | |
| 1252 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 1253 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 1254 | for (; Field != FieldEnd; ++Field) { |
| 1255 | QualType FieldType = Field->getType(); |
| 1256 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 1257 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 1258 | // Warn if we drop the attribute. |
| 1259 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
| 1260 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
| 1261 | : S.Context.getTypeAlign(FieldType); |
| 1262 | S.Diag(Field->getLocation(), |
| 1263 | diag::warn_transparent_union_attribute_field_size_align) |
| 1264 | << isSize << Field->getDeclName() << FieldBits; |
| 1265 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
| 1266 | S.Diag(FirstField->getLocation(), |
| 1267 | diag::note_transparent_union_first_field_size_align) |
| 1268 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1269 | return; |
| 1270 | } |
| 1271 | } |
| 1272 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1273 | RD->addAttr(::new (S.Context) TransparentUnionAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1276 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1277 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1278 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1279 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1280 | return; |
| 1281 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1282 | Expr *argExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1283 | StringLiteral *SE = dyn_cast<StringLiteral>(argExpr); |
| 1284 | |
| 1285 | // Make sure that there is a string literal as the annotation's single |
| 1286 | // argument. |
| 1287 | if (!SE) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1288 | S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1289 | return; |
| 1290 | } |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1291 | d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(), |
| 1292 | SE->getByteLength()))); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1295 | static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1296 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1297 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1298 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1299 | return; |
| 1300 | } |
| 1301 | |
| 1302 | unsigned Align = 0; |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1303 | if (Attr.getNumArgs() == 0) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1304 | // FIXME: This should be the target specific maximum alignment. |
Daniel Dunbar | 7549c55 | 2009-02-18 20:06:09 +0000 | [diff] [blame] | 1305 | // (For now we just use 128 bits which is the maximum on X86). |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1306 | Align = 128; |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1307 | d->addAttr(::new (S.Context) AlignedAttr(Align)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1308 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1309 | } |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 1310 | |
| 1311 | Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1312 | llvm::APSInt Alignment(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1313 | if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1314 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1315 | << "aligned" << alignmentExpr->getSourceRange(); |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 1316 | return; |
| 1317 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 1318 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
| 1319 | S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two) |
| 1320 | << alignmentExpr->getSourceRange(); |
| 1321 | return; |
| 1322 | } |
| 1323 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1324 | d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1325 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1326 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1327 | /// HandleModeAttr - This attribute modifies the width of a decl with |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 1328 | /// primitive type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1329 | /// |
| 1330 | /// Despite what would be logical, the mode attribute is a decl attribute, |
| 1331 | /// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make |
| 1332 | /// 'G' be HImode, not an intermediate pointer. |
| 1333 | /// |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1334 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1335 | // This attribute isn't documented, but glibc uses it. It changes |
| 1336 | // the width of an int or unsigned int to the specified size. |
| 1337 | |
| 1338 | // Check that there aren't any arguments |
| 1339 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1340 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | IdentifierInfo *Name = Attr.getParameterName(); |
| 1345 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1346 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1347 | return; |
| 1348 | } |
| 1349 | const char *Str = Name->getName(); |
| 1350 | unsigned Len = Name->getLength(); |
| 1351 | |
| 1352 | // Normalize the attribute name, __foo__ becomes foo. |
| 1353 | if (Len > 4 && Str[0] == '_' && Str[1] == '_' && |
| 1354 | Str[Len - 2] == '_' && Str[Len - 1] == '_') { |
| 1355 | Str += 2; |
| 1356 | Len -= 4; |
| 1357 | } |
| 1358 | |
| 1359 | unsigned DestWidth = 0; |
| 1360 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1361 | bool ComplexMode = false; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1362 | switch (Len) { |
| 1363 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1364 | switch (Str[0]) { |
| 1365 | case 'Q': DestWidth = 8; break; |
| 1366 | case 'H': DestWidth = 16; break; |
| 1367 | case 'S': DestWidth = 32; break; |
| 1368 | case 'D': DestWidth = 64; break; |
| 1369 | case 'X': DestWidth = 96; break; |
| 1370 | case 'T': DestWidth = 128; break; |
| 1371 | } |
| 1372 | if (Str[1] == 'F') { |
| 1373 | IntegerMode = false; |
| 1374 | } else if (Str[1] == 'C') { |
| 1375 | IntegerMode = false; |
| 1376 | ComplexMode = true; |
| 1377 | } else if (Str[1] != 'I') { |
| 1378 | DestWidth = 0; |
| 1379 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1380 | break; |
| 1381 | case 4: |
| 1382 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 1383 | // pointer on PIC16 and other embedded platforms. |
| 1384 | if (!memcmp(Str, "word", 4)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1385 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1386 | if (!memcmp(Str, "byte", 4)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1387 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1388 | break; |
| 1389 | case 7: |
| 1390 | if (!memcmp(Str, "pointer", 7)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1391 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1392 | break; |
| 1393 | } |
| 1394 | |
| 1395 | QualType OldTy; |
| 1396 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 1397 | OldTy = TD->getUnderlyingType(); |
| 1398 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 1399 | OldTy = VD->getType(); |
| 1400 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1401 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 1402 | << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1403 | return; |
| 1404 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1405 | |
| 1406 | if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType()) |
| 1407 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 1408 | else if (IntegerMode) { |
| 1409 | if (!OldTy->isIntegralType()) |
| 1410 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1411 | } else if (ComplexMode) { |
| 1412 | if (!OldTy->isComplexType()) |
| 1413 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1414 | } else { |
| 1415 | if (!OldTy->isFloatingType()) |
| 1416 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1417 | } |
| 1418 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1419 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 1420 | // and friends, at least with glibc. |
| 1421 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 1422 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1423 | // FIXME: Make sure floating-point mappings are accurate |
| 1424 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1425 | QualType NewTy; |
| 1426 | switch (DestWidth) { |
| 1427 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1428 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1429 | return; |
| 1430 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1431 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1432 | return; |
| 1433 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1434 | if (!IntegerMode) { |
| 1435 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1436 | return; |
| 1437 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1438 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1439 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1440 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1441 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1442 | break; |
| 1443 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1444 | if (!IntegerMode) { |
| 1445 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1446 | return; |
| 1447 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1448 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1449 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1450 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1451 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1452 | break; |
| 1453 | case 32: |
| 1454 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1455 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1456 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1457 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1458 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1459 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1460 | break; |
| 1461 | case 64: |
| 1462 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1463 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1464 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1465 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1466 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1467 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1468 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1469 | case 96: |
| 1470 | NewTy = S.Context.LongDoubleTy; |
| 1471 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1472 | case 128: |
| 1473 | if (!IntegerMode) { |
| 1474 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1475 | return; |
| 1476 | } |
| 1477 | NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType()); |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1478 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1481 | if (ComplexMode) { |
| 1482 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | // Install the new type. |
| 1486 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 1487 | TD->setUnderlyingType(NewTy); |
| 1488 | else |
| 1489 | cast<ValueDecl>(D)->setType(NewTy); |
| 1490 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1491 | |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1492 | static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1493 | // check the attribute arguments. |
| 1494 | if (Attr.getNumArgs() > 0) { |
| 1495 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1496 | return; |
| 1497 | } |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 1498 | |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1499 | if (!isFunctionOrMethod(d)) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1500 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1501 | << Attr.getName() << 0 /*function*/; |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1502 | return; |
| 1503 | } |
| 1504 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1505 | d->addAttr(::new (S.Context) NodebugAttr()); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1508 | static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1509 | // check the attribute arguments. |
| 1510 | if (Attr.getNumArgs() != 0) { |
| 1511 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1512 | return; |
| 1513 | } |
| 1514 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1515 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1516 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1517 | << Attr.getName() << 0 /*function*/; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1518 | return; |
| 1519 | } |
| 1520 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1521 | d->addAttr(::new (S.Context) NoinlineAttr()); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1524 | static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1525 | // check the attribute arguments. |
| 1526 | if (Attr.getNumArgs() != 0) { |
| 1527 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1528 | return; |
| 1529 | } |
| 1530 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1531 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
| 1532 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1533 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1534 | << Attr.getName() << 0 /*function*/; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1535 | return; |
| 1536 | } |
| 1537 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1538 | if (!Fn->isInline()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1539 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1540 | return; |
| 1541 | } |
| 1542 | |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1543 | d->addAttr(::new (S.Context) GNUInlineAttr()); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1546 | static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1547 | // check the attribute arguments. |
| 1548 | if (Attr.getNumArgs() != 1) { |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1549 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1550 | return; |
| 1551 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1552 | |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1553 | if (!isFunctionOrMethod(d)) { |
| 1554 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1555 | << Attr.getName() << 0 /*function*/; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1556 | return; |
| 1557 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1558 | |
| 1559 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1560 | llvm::APSInt NumParams(32); |
| 1561 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 1562 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1563 | << "regparm" << NumParamsExpr->getSourceRange(); |
| 1564 | return; |
| 1565 | } |
| 1566 | |
Anton Korobeynikov | 264a76c | 2009-04-03 23:38:25 +0000 | [diff] [blame] | 1567 | if (S.Context.Target.getRegParmMax() == 0) { |
| 1568 | S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1569 | << NumParamsExpr->getSourceRange(); |
| 1570 | return; |
| 1571 | } |
| 1572 | |
Anton Korobeynikov | 348f28a | 2009-04-04 10:27:50 +0000 | [diff] [blame] | 1573 | if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) { |
Anton Korobeynikov | 264a76c | 2009-04-03 23:38:25 +0000 | [diff] [blame] | 1574 | S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
| 1575 | << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1576 | return; |
| 1577 | } |
| 1578 | |
| 1579 | d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue())); |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1582 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1583 | // Checker-specific attribute handlers. |
| 1584 | //===----------------------------------------------------------------------===// |
| 1585 | |
| 1586 | static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr, |
| 1587 | Sema &S) { |
| 1588 | |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1589 | QualType RetTy; |
| 1590 | |
| 1591 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) |
| 1592 | RetTy = MD->getResultType(); |
| 1593 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) |
| 1594 | RetTy = FD->getResultType(); |
| 1595 | else { |
| 1596 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1597 | << Attr.getName() << 3 /* function or method */; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1598 | return; |
| 1599 | } |
| 1600 | |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1601 | if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAsPointerType())) { |
| 1602 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 1603 | << Attr.getName(); |
| 1604 | return; |
| 1605 | } |
| 1606 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1607 | switch (Attr.getKind()) { |
| 1608 | default: |
| 1609 | assert(0 && "invalid ownership attribute"); |
| 1610 | return; |
| 1611 | case AttributeList::AT_cf_returns_retained: |
| 1612 | d->addAttr(::new (S.Context) CFReturnsRetainedAttr()); |
| 1613 | return; |
| 1614 | case AttributeList::AT_ns_returns_retained: |
| 1615 | d->addAttr(::new (S.Context) NSReturnsRetainedAttr()); |
| 1616 | return; |
| 1617 | }; |
| 1618 | } |
| 1619 | |
| 1620 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1621 | // Top Level Sema Entry Points |
| 1622 | //===----------------------------------------------------------------------===// |
| 1623 | |
Sebastian Redl | a89d82c | 2008-12-21 19:24:58 +0000 | [diff] [blame] | 1624 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1625 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 1626 | /// silently ignore it. |
| 1627 | static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1628 | switch (Attr.getKind()) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1629 | case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1630 | case AttributeList::AT_address_space: |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1631 | case AttributeList::AT_objc_gc: |
| 1632 | // Ignore these, these are type attributes, handled by ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1633 | break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1634 | case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; |
| 1635 | case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1636 | case AttributeList::AT_always_inline: |
| 1637 | HandleAlwaysInlineAttr (D, Attr, S); break; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1638 | case AttributeList::AT_analyzer_noreturn: |
| 1639 | HandleAnalyzerNoReturnAttr (D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1640 | case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; |
| 1641 | case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break; |
| 1642 | case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break; |
| 1643 | case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break; |
| 1644 | case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break; |
| 1645 | case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1646 | case AttributeList::AT_ext_vector_type: |
| 1647 | HandleExtVectorTypeAttr(D, Attr, S); |
| 1648 | break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1649 | case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break; |
| 1650 | case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1651 | case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1652 | case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1653 | case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; |
| 1654 | case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; |
| 1655 | case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1656 | |
| 1657 | // Checker-specific. |
| 1658 | case AttributeList::AT_ns_returns_retained: |
| 1659 | case AttributeList::AT_cf_returns_retained: |
| 1660 | HandleNSReturnsRetainedAttr(D, Attr, S); break; |
| 1661 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1662 | case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1663 | case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1664 | case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break; |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1665 | case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1666 | case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1667 | case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1668 | case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1669 | case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1670 | case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S); |
| 1671 | break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1672 | case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1673 | case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1674 | case AttributeList::AT_transparent_union: |
| 1675 | HandleTransparentUnionAttr(D, Attr, S); |
| 1676 | break; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1677 | case AttributeList::AT_objc_exception: |
| 1678 | HandleObjCExceptionAttr(D, Attr, S); |
| 1679 | break; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1680 | case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1681 | case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1682 | case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1683 | case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1684 | case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break; |
| 1685 | case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1686 | case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break; |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1687 | case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1688 | case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1689 | case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break; |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 1690 | case AttributeList::IgnoredAttribute: |
Chris Lattner | 5e20448 | 2009-04-25 18:44:54 +0000 | [diff] [blame] | 1691 | case AttributeList::AT_no_instrument_function: // Interacts with -pg. |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 1692 | // Just ignore |
| 1693 | break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1694 | default: |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1695 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1696 | break; |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 1701 | /// attribute list to the specified decl, ignoring any type attributes. |
| 1702 | void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) { |
| 1703 | while (AttrList) { |
| 1704 | ProcessDeclAttribute(D, *AttrList, *this); |
| 1705 | AttrList = AttrList->getNext(); |
| 1706 | } |
| 1707 | } |
| 1708 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1709 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 1710 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 1711 | /// specified in many different places, and we need to find and apply them all. |
| 1712 | void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) { |
| 1713 | // Apply decl attributes from the DeclSpec if present. |
| 1714 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes()) |
| 1715 | ProcessDeclAttributeList(D, Attrs); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1716 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1717 | // Walk the declarator structure, applying decl attributes that were in a type |
| 1718 | // position to the decl itself. This handles cases like: |
| 1719 | // int *__attr__(x)** D; |
| 1720 | // when X is a decl attribute. |
| 1721 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 1722 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
| 1723 | ProcessDeclAttributeList(D, Attrs); |
| 1724 | |
| 1725 | // Finally, apply any attributes on the decl itself. |
| 1726 | if (const AttributeList *Attrs = PD.getAttributes()) |
| 1727 | ProcessDeclAttributeList(D, Attrs); |
| 1728 | } |