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