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