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