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