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 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 394 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 403 | static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr, |
| 404 | Sema &S, const char *attrName) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 405 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 406 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 407 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 408 | return false; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 409 | } |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 410 | |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 411 | if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) { |
| 412 | ValueDecl *VD = dyn_cast<ValueDecl>(d); |
| 413 | if (VD == 0 || !VD->getType()->isBlockPointerType()) { |
| 414 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 415 | << attrName << 0 /*function*/; |
| 416 | return false; |
| 417 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 420 | return true; |
| 421 | } |
| 422 | |
| 423 | static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 424 | if (HandleCommonNoReturnAttr(d, Attr, S, "noreturn")) |
| 425 | d->addAttr(::new (S.Context) NoReturnAttr()); |
| 426 | } |
| 427 | |
| 428 | static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr, |
| 429 | Sema &S) { |
| 430 | if (HandleCommonNoReturnAttr(d, Attr, S, "analyzer_noreturn")) |
| 431 | d->addAttr(::new (S.Context) AnalyzerNoReturnAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 434 | static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 435 | // check the attribute arguments. |
| 436 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 437 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 438 | return; |
| 439 | } |
| 440 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 441 | if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 442 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 443 | << "unused" << 2 /*variable and function*/; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 444 | return; |
| 445 | } |
| 446 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 447 | d->addAttr(::new (S.Context) UnusedAttr()); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 450 | static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 451 | // check the attribute arguments. |
| 452 | if (Attr.getNumArgs() != 0) { |
| 453 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | if (const VarDecl *VD = dyn_cast<VarDecl>(d)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 458 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 459 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 460 | return; |
| 461 | } |
| 462 | } else if (!isFunctionOrMethod(d)) { |
| 463 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 464 | << "used" << 2 /*variable and function*/; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 465 | return; |
| 466 | } |
| 467 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 468 | d->addAttr(::new (S.Context) UsedAttr()); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 471 | static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 472 | // check the attribute arguments. |
| 473 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 474 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 475 | << "0 or 1"; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 476 | return; |
| 477 | } |
| 478 | |
| 479 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 480 | if (Attr.getNumArgs() > 0) { |
| 481 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 482 | llvm::APSInt Idx(32); |
| 483 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 484 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 485 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 486 | return; |
| 487 | } |
| 488 | priority = Idx.getZExtValue(); |
| 489 | } |
| 490 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 491 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 492 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 493 | << "constructor" << 0 /*function*/; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 497 | d->addAttr(::new (S.Context) ConstructorAttr(priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 501 | // check the attribute arguments. |
| 502 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 503 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 504 | << "0 or 1"; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 505 | return; |
| 506 | } |
| 507 | |
| 508 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 509 | if (Attr.getNumArgs() > 0) { |
| 510 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 511 | llvm::APSInt Idx(32); |
| 512 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 513 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 514 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 515 | return; |
| 516 | } |
| 517 | priority = Idx.getZExtValue(); |
| 518 | } |
| 519 | |
Anders Carlsson | 6782fc6 | 2008-08-22 22:10:48 +0000 | [diff] [blame] | 520 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 521 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 522 | << "destructor" << 0 /*function*/; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 523 | return; |
| 524 | } |
| 525 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 526 | d->addAttr(::new (S.Context) DestructorAttr(priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 529 | static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 530 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 531 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 532 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 533 | return; |
| 534 | } |
| 535 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 536 | d->addAttr(::new (S.Context) DeprecatedAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 539 | static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 540 | // check the attribute arguments. |
| 541 | if (Attr.getNumArgs() != 0) { |
| 542 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 543 | return; |
| 544 | } |
| 545 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 546 | d->addAttr(::new (S.Context) UnavailableAttr()); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 549 | static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 550 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 551 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 552 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 553 | return; |
| 554 | } |
| 555 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 556 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 557 | Arg = Arg->IgnoreParenCasts(); |
| 558 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 559 | |
| 560 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 561 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 562 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 563 | return; |
| 564 | } |
| 565 | |
| 566 | const char *TypeStr = Str->getStrData(); |
| 567 | unsigned TypeLen = Str->getByteLength(); |
| 568 | VisibilityAttr::VisibilityTypes type; |
| 569 | |
| 570 | if (TypeLen == 7 && !memcmp(TypeStr, "default", 7)) |
| 571 | type = VisibilityAttr::DefaultVisibility; |
| 572 | else if (TypeLen == 6 && !memcmp(TypeStr, "hidden", 6)) |
| 573 | type = VisibilityAttr::HiddenVisibility; |
| 574 | else if (TypeLen == 8 && !memcmp(TypeStr, "internal", 8)) |
| 575 | type = VisibilityAttr::HiddenVisibility; // FIXME |
| 576 | else if (TypeLen == 9 && !memcmp(TypeStr, "protected", 9)) |
| 577 | type = VisibilityAttr::ProtectedVisibility; |
| 578 | else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 579 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 580 | return; |
| 581 | } |
| 582 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 583 | d->addAttr(::new (S.Context) VisibilityAttr(type)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 586 | static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr, |
| 587 | Sema &S) { |
| 588 | if (Attr.getNumArgs() != 0) { |
| 589 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 590 | return; |
| 591 | } |
| 592 | |
| 593 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 594 | if (OCI == 0) { |
| 595 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 596 | return; |
| 597 | } |
| 598 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 599 | D->addAttr(::new (S.Context) ObjCExceptionAttr()); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 603 | if (Attr.getNumArgs() != 0) { |
| 604 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 605 | return; |
| 606 | } |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 607 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 608 | QualType T = TD->getUnderlyingType(); |
| 609 | if (!T->isPointerType() || |
| 610 | !T->getAsPointerType()->getPointeeType()->isRecordType()) { |
| 611 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 612 | return; |
| 613 | } |
| 614 | } |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 615 | D->addAttr(::new (S.Context) ObjCNSObjectAttr()); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 618 | static void |
| 619 | HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 620 | if (Attr.getNumArgs() != 0) { |
| 621 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | if (!isa<FunctionDecl>(D)) { |
| 626 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 627 | return; |
| 628 | } |
| 629 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 630 | D->addAttr(::new (S.Context) OverloadableAttr()); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 633 | static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 634 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 635 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 636 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 637 | return; |
| 638 | } |
| 639 | |
| 640 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 641 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 642 | return; |
| 643 | } |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 644 | |
| 645 | BlocksAttr::BlocksAttrTypes type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 646 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 647 | type = BlocksAttr::ByRef; |
| 648 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 649 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 650 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 654 | d->addAttr(::new (S.Context) BlocksAttr(type)); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 657 | static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 658 | // check the attribute arguments. |
| 659 | if (Attr.getNumArgs() > 2) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 660 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 661 | << "0, 1 or 2"; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 662 | return; |
| 663 | } |
| 664 | |
| 665 | int sentinel = 0; |
| 666 | if (Attr.getNumArgs() > 0) { |
| 667 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 668 | llvm::APSInt Idx(32); |
| 669 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 670 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 671 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 672 | return; |
| 673 | } |
| 674 | sentinel = Idx.getZExtValue(); |
| 675 | |
| 676 | if (sentinel < 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 677 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 678 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 679 | return; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | int nullPos = 0; |
| 684 | if (Attr.getNumArgs() > 1) { |
| 685 | Expr *E = static_cast<Expr *>(Attr.getArg(1)); |
| 686 | llvm::APSInt Idx(32); |
| 687 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 688 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 689 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 690 | return; |
| 691 | } |
| 692 | nullPos = Idx.getZExtValue(); |
| 693 | |
| 694 | if (nullPos > 1 || nullPos < 0) { |
| 695 | // FIXME: This error message could be improved, it would be nice |
| 696 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 697 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 698 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 699 | return; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 704 | const FunctionType *FT = FD->getType()->getAsFunctionType(); |
| 705 | assert(FT && "FunctionDecl has non-function type?"); |
| 706 | |
| 707 | if (isa<FunctionNoProtoType>(FT)) { |
| 708 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 709 | return; |
| 710 | } |
| 711 | |
| 712 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 713 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic); |
| 714 | return; |
| 715 | } |
| 716 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) { |
| 717 | if (!MD->isVariadic()) { |
| 718 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic); |
| 719 | return; |
| 720 | } |
| 721 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 722 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 723 | << "sentinel" << 3 /*function or method*/; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 724 | return; |
| 725 | } |
| 726 | |
| 727 | // FIXME: Actually create the attribute. |
| 728 | } |
| 729 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 730 | static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) { |
| 731 | // check the attribute arguments. |
| 732 | if (Attr.getNumArgs() != 0) { |
| 733 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | // TODO: could also be applied to methods? |
| 738 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
| 739 | if (!Fn) { |
| 740 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 741 | << "warn_unused_result" << 0 /*function*/; |
| 742 | return; |
| 743 | } |
| 744 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 745 | Fn->addAttr(::new (S.Context) WarnUnusedResultAttr()); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 749 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 750 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 751 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 752 | return; |
| 753 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 754 | |
| 755 | // TODO: could also be applied to methods? |
| 756 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
| 757 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 758 | << "weak" << 2 /*variable and function*/; |
| 759 | return; |
| 760 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 761 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 762 | D->addAttr(::new (S.Context) WeakAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 765 | static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 766 | // check the attribute arguments. |
| 767 | if (Attr.getNumArgs() != 0) { |
| 768 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | // weak_import only applies to variable & function declarations. |
| 773 | bool isDef = false; |
| 774 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 775 | isDef = (!VD->hasExternalStorage() || VD->getInit()); |
| 776 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 777 | isDef = FD->getBody(S.Context); |
Mike Stump | 862a2c5 | 2009-03-18 15:05:17 +0000 | [diff] [blame] | 778 | } else if (isa<ObjCPropertyDecl>(D)) { |
| 779 | // We ignore weak import on properties |
Mike Stump | 1c90f4d | 2009-03-18 17:39:31 +0000 | [diff] [blame] | 780 | return; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 781 | } else { |
| 782 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 783 | << "weak_import" << 2 /*variable and function*/; |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | // Merge should handle any subsequent violations. |
| 788 | if (isDef) { |
| 789 | S.Diag(Attr.getLoc(), |
| 790 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 791 | << "weak_import" << 2 /*variable and function*/; |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | D->addAttr(::new (S.Context) WeakImportAttr()); |
| 796 | } |
| 797 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 798 | static void HandleDLLImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 799 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 800 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 801 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 802 | return; |
| 803 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 804 | |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 805 | // Attribute can be applied only to functions or variables. |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 806 | if (isa<VarDecl>(D)) { |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 807 | D->addAttr(::new (S.Context) DLLImportAttr()); |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 808 | return; |
| 809 | } |
| 810 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 811 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 812 | if (!FD) { |
| 813 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 814 | << "dllimport" << 2 /*variable and function*/; |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 815 | return; |
| 816 | } |
| 817 | |
| 818 | // Currently, the dllimport attribute is ignored for inlined functions. |
| 819 | // Warning is emitted. |
| 820 | if (FD->isInline()) { |
| 821 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 822 | return; |
| 823 | } |
| 824 | |
| 825 | // The attribute is also overridden by a subsequent declaration as dllexport. |
| 826 | // Warning is emitted. |
| 827 | for (AttributeList *nextAttr = Attr.getNext(); nextAttr; |
| 828 | nextAttr = nextAttr->getNext()) { |
| 829 | if (nextAttr->getKind() == AttributeList::AT_dllexport) { |
| 830 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 831 | return; |
| 832 | } |
| 833 | } |
| 834 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 835 | if (D->getAttr<DLLExportAttr>()) { |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 836 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllimport"; |
| 837 | return; |
| 838 | } |
| 839 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 840 | D->addAttr(::new (S.Context) DLLImportAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 843 | static void HandleDLLExportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 844 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 845 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 846 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 847 | return; |
| 848 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 849 | |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 850 | // Attribute can be applied only to functions or variables. |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 851 | if (isa<VarDecl>(D)) { |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 852 | D->addAttr(::new (S.Context) DLLExportAttr()); |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 853 | return; |
| 854 | } |
| 855 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 856 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 857 | if (!FD) { |
| 858 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 859 | << "dllexport" << 2 /*variable and function*/; |
Anton Korobeynikov | 2f40270 | 2008-12-26 00:52:02 +0000 | [diff] [blame] | 860 | return; |
| 861 | } |
| 862 | |
| 863 | // Currently, the dllexport attribute is ignored for inlined functions, |
| 864 | // unless the -fkeep-inline-functions flag has been used. Warning is emitted; |
| 865 | if (FD->isInline()) { |
| 866 | // FIXME: ... unless the -fkeep-inline-functions flag has been used. |
| 867 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "dllexport"; |
| 868 | return; |
| 869 | } |
| 870 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 871 | D->addAttr(::new (S.Context) DLLExportAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 874 | static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 875 | // Attribute has no arguments. |
| 876 | if (Attr.getNumArgs() != 1) { |
| 877 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 878 | return; |
| 879 | } |
| 880 | |
| 881 | // Make sure that there is a string literal as the sections's single |
| 882 | // argument. |
| 883 | StringLiteral *SE = |
| 884 | dyn_cast<StringLiteral>(static_cast<Expr *>(Attr.getArg(0))); |
| 885 | if (!SE) { |
| 886 | // FIXME |
| 887 | S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string); |
| 888 | return; |
| 889 | } |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 890 | D->addAttr(::new (S.Context) SectionAttr(std::string(SE->getStrData(), |
| 891 | SE->getByteLength()))); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 894 | static void HandleStdCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 895 | // Attribute has no arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 896 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 897 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 898 | return; |
| 899 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 900 | |
| 901 | // Attribute can be applied only to functions. |
| 902 | if (!isa<FunctionDecl>(d)) { |
| 903 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 904 | << "stdcall" << 0 /*function*/; |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 905 | return; |
| 906 | } |
| 907 | |
| 908 | // stdcall and fastcall attributes are mutually incompatible. |
| 909 | if (d->getAttr<FastCallAttr>()) { |
| 910 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 911 | << "stdcall" << "fastcall"; |
| 912 | return; |
| 913 | } |
| 914 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 915 | d->addAttr(::new (S.Context) StdCallAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 918 | static void HandleFastCallAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 919 | // Attribute has no arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 920 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 921 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 922 | return; |
| 923 | } |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 924 | |
| 925 | if (!isa<FunctionDecl>(d)) { |
| 926 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 927 | << "fastcall" << 0 /*function*/; |
Anton Korobeynikov | 7b0a52f | 2008-12-23 22:24:07 +0000 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | |
| 931 | // stdcall and fastcall attributes are mutually incompatible. |
| 932 | if (d->getAttr<StdCallAttr>()) { |
| 933 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 934 | << "fastcall" << "stdcall"; |
| 935 | return; |
| 936 | } |
| 937 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 938 | d->addAttr(::new (S.Context) FastCallAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 941 | static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 942 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 943 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 944 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 945 | return; |
| 946 | } |
| 947 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 948 | d->addAttr(::new (S.Context) NoThrowAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 951 | static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 952 | // check the attribute arguments. |
| 953 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 954 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 955 | return; |
| 956 | } |
| 957 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 958 | d->addAttr(::new (S.Context) ConstAttr()); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 962 | // check the attribute arguments. |
| 963 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 964 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 965 | return; |
| 966 | } |
| 967 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 968 | d->addAttr(::new (S.Context) PureAttr()); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 971 | static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 972 | // Match gcc which ignores cleanup attrs when compiling C++. |
| 973 | if (S.getLangOptions().CPlusPlus) |
| 974 | return; |
| 975 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 976 | if (!Attr.getParameterName()) { |
| 977 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 978 | return; |
| 979 | } |
| 980 | |
| 981 | if (Attr.getNumArgs() != 0) { |
| 982 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 983 | return; |
| 984 | } |
| 985 | |
| 986 | VarDecl *VD = dyn_cast<VarDecl>(d); |
| 987 | |
| 988 | if (!VD || !VD->hasLocalStorage()) { |
| 989 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 990 | return; |
| 991 | } |
| 992 | |
| 993 | // Look up the function |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 994 | NamedDecl *CleanupDecl = S.LookupName(S.TUScope, Attr.getParameterName(), |
| 995 | Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 996 | if (!CleanupDecl) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 997 | S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 998 | Attr.getParameterName(); |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 1003 | if (!FD) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1004 | S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1005 | Attr.getParameterName(); |
| 1006 | return; |
| 1007 | } |
| 1008 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1009 | if (FD->getNumParams() != 1) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1010 | 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] | 1011 | Attr.getParameterName(); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1015 | // We're currently more strict than GCC about what function types we accept. |
| 1016 | // If this ever proves to be a problem it should be easy to fix. |
| 1017 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 1018 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Eli Friedman | d5e3e8e | 2009-04-26 01:30:08 +0000 | [diff] [blame] | 1019 | if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1020 | S.Diag(Attr.getLoc(), |
| 1021 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 1022 | Attr.getParameterName() << ParamTy << Ty; |
| 1023 | return; |
| 1024 | } |
| 1025 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1026 | d->addAttr(::new (S.Context) CleanupAttr(FD)); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1029 | /// Handle __attribute__((format(type,idx,firstarg))) attributes |
| 1030 | /// based on http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1031 | static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1032 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1033 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1034 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1035 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1036 | return; |
| 1037 | } |
| 1038 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1039 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1040 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 1044 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1045 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1046 | << "format" << 0 /*function*/; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1047 | return; |
| 1048 | } |
| 1049 | |
| 1050 | // FIXME: in C++ the implicit 'this' function parameter also counts. |
| 1051 | // this is needed in order to be compatible with GCC |
| 1052 | // the index must start in 1 and the limit is numargs+1 |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1053 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1054 | unsigned FirstIdx = 1; |
| 1055 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1056 | const char *Format = Attr.getParameterName()->getName(); |
| 1057 | unsigned FormatLen = Attr.getParameterName()->getLength(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1058 | |
| 1059 | // Normalize the argument, __foo__ becomes foo. |
| 1060 | if (FormatLen > 4 && Format[0] == '_' && Format[1] == '_' && |
| 1061 | Format[FormatLen - 2] == '_' && Format[FormatLen - 1] == '_') { |
| 1062 | Format += 2; |
| 1063 | FormatLen -= 4; |
| 1064 | } |
| 1065 | |
| 1066 | bool Supported = false; |
| 1067 | bool is_NSString = false; |
| 1068 | bool is_strftime = false; |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1069 | bool is_CFString = false; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1070 | |
| 1071 | switch (FormatLen) { |
| 1072 | default: break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1073 | case 5: Supported = !memcmp(Format, "scanf", 5); break; |
| 1074 | case 6: Supported = !memcmp(Format, "printf", 6); break; |
| 1075 | case 7: Supported = !memcmp(Format, "strfmon", 7); break; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1076 | case 8: |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1077 | Supported = (is_strftime = !memcmp(Format, "strftime", 8)) || |
| 1078 | (is_NSString = !memcmp(Format, "NSString", 8)) || |
| 1079 | (is_CFString = !memcmp(Format, "CFString", 8)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1080 | break; |
| 1081 | } |
| 1082 | |
| 1083 | if (!Supported) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1084 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 1085 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | // checks for the 2nd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1090 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1091 | llvm::APSInt Idx(32); |
| 1092 | if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1093 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1094 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1095 | return; |
| 1096 | } |
| 1097 | |
| 1098 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1099 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1100 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | // FIXME: Do we need to bounds check? |
| 1105 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
| 1106 | |
| 1107 | // make sure the format string is really a string |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1108 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1109 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1110 | if (is_CFString) { |
| 1111 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1112 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1113 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1114 | return; |
| 1115 | } |
| 1116 | } else if (is_NSString) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1117 | // FIXME: do we need to check if the type is NSString*? What are |
| 1118 | // the semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1119 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1120 | // FIXME: Should highlight the actual expression that has the |
| 1121 | // wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1122 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1123 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1124 | return; |
| 1125 | } |
| 1126 | } else if (!Ty->isPointerType() || |
| 1127 | !Ty->getAsPointerType()->getPointeeType()->isCharType()) { |
| 1128 | // FIXME: Should highlight the actual expression that has the |
| 1129 | // wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1130 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1131 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1132 | return; |
| 1133 | } |
| 1134 | |
| 1135 | // check the 3rd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1136 | Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1137 | llvm::APSInt FirstArg(32); |
| 1138 | if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1139 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1140 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1141 | return; |
| 1142 | } |
| 1143 | |
| 1144 | // check if the function is variadic if the 3rd argument non-zero |
| 1145 | if (FirstArg != 0) { |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1146 | if (isFunctionOrMethodVariadic(d)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1147 | ++NumArgs; // +1 for ... |
| 1148 | } else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1149 | S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1150 | return; |
| 1151 | } |
| 1152 | } |
| 1153 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1154 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 1155 | // variable the input is just the current time + the format string. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1156 | if (is_strftime) { |
| 1157 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1158 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 1159 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1160 | return; |
| 1161 | } |
| 1162 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 1163 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1164 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1165 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1166 | return; |
| 1167 | } |
| 1168 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1169 | d->addAttr(::new (S.Context) FormatAttr(std::string(Format, FormatLen), |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1170 | Idx.getZExtValue(), FirstArg.getZExtValue())); |
| 1171 | } |
| 1172 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1173 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 1174 | Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1175 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1176 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1177 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1178 | return; |
| 1179 | } |
| 1180 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame^] | 1181 | // Try to find the underlying union declaration. |
| 1182 | RecordDecl *RD = 0; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1183 | TypedefDecl *TD = dyn_cast<TypedefDecl>(d); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame^] | 1184 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 1185 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 1186 | else |
| 1187 | RD = dyn_cast<RecordDecl>(d); |
| 1188 | |
| 1189 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1190 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1191 | << "transparent_union" << 1 /*union*/; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1192 | return; |
| 1193 | } |
| 1194 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame^] | 1195 | if (!RD->isDefinition()) { |
| 1196 | S.Diag(Attr.getLoc(), |
| 1197 | diag::warn_transparent_union_attribute_not_definition); |
| 1198 | return; |
| 1199 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1200 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame^] | 1201 | RecordDecl::field_iterator Field = RD->field_begin(S.Context), |
| 1202 | FieldEnd = RD->field_end(S.Context); |
| 1203 | if (Field == FieldEnd) { |
| 1204 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 1205 | return; |
| 1206 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1207 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame^] | 1208 | FieldDecl *FirstField = *Field; |
| 1209 | QualType FirstType = FirstField->getType(); |
| 1210 | if (FirstType->isFloatingType() || FirstType->isVectorType()) { |
| 1211 | S.Diag(FirstField->getLocation(), |
| 1212 | diag::warn_transparent_union_attribute_floating); |
| 1213 | return; |
| 1214 | } |
| 1215 | |
| 1216 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 1217 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 1218 | for (; Field != FieldEnd; ++Field) { |
| 1219 | QualType FieldType = Field->getType(); |
| 1220 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 1221 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 1222 | // Warn if we drop the attribute. |
| 1223 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
| 1224 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
| 1225 | : S.Context.getTypeAlign(FieldType); |
| 1226 | S.Diag(Field->getLocation(), |
| 1227 | diag::warn_transparent_union_attribute_field_size_align) |
| 1228 | << isSize << Field->getDeclName() << FieldBits; |
| 1229 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
| 1230 | S.Diag(FirstField->getLocation(), |
| 1231 | diag::note_transparent_union_first_field_size_align) |
| 1232 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1233 | return; |
| 1234 | } |
| 1235 | } |
| 1236 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame^] | 1237 | RD->addAttr(::new (S.Context) TransparentUnionAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1240 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1241 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1242 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1243 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1244 | return; |
| 1245 | } |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1246 | Expr *argExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1247 | StringLiteral *SE = dyn_cast<StringLiteral>(argExpr); |
| 1248 | |
| 1249 | // Make sure that there is a string literal as the annotation's single |
| 1250 | // argument. |
| 1251 | if (!SE) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1252 | S.Diag(Attr.getLoc(), diag::err_attribute_annotate_no_string); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1253 | return; |
| 1254 | } |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1255 | d->addAttr(::new (S.Context) AnnotateAttr(std::string(SE->getStrData(), |
| 1256 | SE->getByteLength()))); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1259 | static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1260 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1261 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1262 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1263 | return; |
| 1264 | } |
| 1265 | |
| 1266 | unsigned Align = 0; |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1267 | if (Attr.getNumArgs() == 0) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1268 | // FIXME: This should be the target specific maximum alignment. |
Daniel Dunbar | 7549c55 | 2009-02-18 20:06:09 +0000 | [diff] [blame] | 1269 | // (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] | 1270 | Align = 128; |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1271 | d->addAttr(::new (S.Context) AlignedAttr(Align)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1272 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1273 | } |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 1274 | |
| 1275 | Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1276 | llvm::APSInt Alignment(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1277 | if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1278 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1279 | << "aligned" << alignmentExpr->getSourceRange(); |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 1280 | return; |
| 1281 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 1282 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
| 1283 | S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two) |
| 1284 | << alignmentExpr->getSourceRange(); |
| 1285 | return; |
| 1286 | } |
| 1287 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1288 | d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1289 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1291 | /// HandleModeAttr - This attribute modifies the width of a decl with |
Chris Lattner | 065c5a8 | 2008-06-28 23:48:25 +0000 | [diff] [blame] | 1292 | /// primitive type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1293 | /// |
| 1294 | /// Despite what would be logical, the mode attribute is a decl attribute, |
| 1295 | /// not a type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make |
| 1296 | /// 'G' be HImode, not an intermediate pointer. |
| 1297 | /// |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1298 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1299 | // This attribute isn't documented, but glibc uses it. It changes |
| 1300 | // the width of an int or unsigned int to the specified size. |
| 1301 | |
| 1302 | // Check that there aren't any arguments |
| 1303 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1304 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1305 | return; |
| 1306 | } |
| 1307 | |
| 1308 | IdentifierInfo *Name = Attr.getParameterName(); |
| 1309 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1310 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1311 | return; |
| 1312 | } |
| 1313 | const char *Str = Name->getName(); |
| 1314 | unsigned Len = Name->getLength(); |
| 1315 | |
| 1316 | // Normalize the attribute name, __foo__ becomes foo. |
| 1317 | if (Len > 4 && Str[0] == '_' && Str[1] == '_' && |
| 1318 | Str[Len - 2] == '_' && Str[Len - 1] == '_') { |
| 1319 | Str += 2; |
| 1320 | Len -= 4; |
| 1321 | } |
| 1322 | |
| 1323 | unsigned DestWidth = 0; |
| 1324 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1325 | bool ComplexMode = false; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1326 | switch (Len) { |
| 1327 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1328 | switch (Str[0]) { |
| 1329 | case 'Q': DestWidth = 8; break; |
| 1330 | case 'H': DestWidth = 16; break; |
| 1331 | case 'S': DestWidth = 32; break; |
| 1332 | case 'D': DestWidth = 64; break; |
| 1333 | case 'X': DestWidth = 96; break; |
| 1334 | case 'T': DestWidth = 128; break; |
| 1335 | } |
| 1336 | if (Str[1] == 'F') { |
| 1337 | IntegerMode = false; |
| 1338 | } else if (Str[1] == 'C') { |
| 1339 | IntegerMode = false; |
| 1340 | ComplexMode = true; |
| 1341 | } else if (Str[1] != 'I') { |
| 1342 | DestWidth = 0; |
| 1343 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1344 | break; |
| 1345 | case 4: |
| 1346 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 1347 | // pointer on PIC16 and other embedded platforms. |
| 1348 | if (!memcmp(Str, "word", 4)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1349 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1350 | if (!memcmp(Str, "byte", 4)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1351 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1352 | break; |
| 1353 | case 7: |
| 1354 | if (!memcmp(Str, "pointer", 7)) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1355 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1356 | break; |
| 1357 | } |
| 1358 | |
| 1359 | QualType OldTy; |
| 1360 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 1361 | OldTy = TD->getUnderlyingType(); |
| 1362 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 1363 | OldTy = VD->getType(); |
| 1364 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1365 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 1366 | << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1367 | return; |
| 1368 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1369 | |
| 1370 | if (!OldTy->getAsBuiltinType() && !OldTy->isComplexType()) |
| 1371 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 1372 | else if (IntegerMode) { |
| 1373 | if (!OldTy->isIntegralType()) |
| 1374 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1375 | } else if (ComplexMode) { |
| 1376 | if (!OldTy->isComplexType()) |
| 1377 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1378 | } else { |
| 1379 | if (!OldTy->isFloatingType()) |
| 1380 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1381 | } |
| 1382 | |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1383 | // FIXME: Sync this with InitializePredefinedMacros; we need to match |
| 1384 | // int8_t and friends, at least with glibc. |
| 1385 | // FIXME: Make sure 32/64-bit integers don't get defined to types of |
| 1386 | // the wrong width on unusual platforms. |
| 1387 | // FIXME: Make sure floating-point mappings are accurate |
| 1388 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1389 | QualType NewTy; |
| 1390 | switch (DestWidth) { |
| 1391 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1392 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1393 | return; |
| 1394 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1395 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1396 | return; |
| 1397 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1398 | if (!IntegerMode) { |
| 1399 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1400 | return; |
| 1401 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1402 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1403 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1404 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1405 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1406 | break; |
| 1407 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1408 | if (!IntegerMode) { |
| 1409 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1410 | return; |
| 1411 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1412 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1413 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1414 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1415 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1416 | break; |
| 1417 | case 32: |
| 1418 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1419 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1420 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1421 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1422 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1423 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1424 | break; |
| 1425 | case 64: |
| 1426 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1427 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1428 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1429 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1430 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1431 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1432 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1433 | case 96: |
| 1434 | NewTy = S.Context.LongDoubleTy; |
| 1435 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1436 | case 128: |
| 1437 | if (!IntegerMode) { |
| 1438 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1439 | return; |
| 1440 | } |
| 1441 | NewTy = S.Context.getFixedWidthIntType(128, OldTy->isSignedIntegerType()); |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1442 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1445 | if (ComplexMode) { |
| 1446 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | // Install the new type. |
| 1450 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 1451 | TD->setUnderlyingType(NewTy); |
| 1452 | else |
| 1453 | cast<ValueDecl>(D)->setType(NewTy); |
| 1454 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1455 | |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1456 | static void HandleNodebugAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1457 | // check the attribute arguments. |
| 1458 | if (Attr.getNumArgs() > 0) { |
| 1459 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1460 | return; |
| 1461 | } |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 1462 | |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1463 | if (!isFunctionOrMethod(d)) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1464 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1465 | << "nodebug" << 0 /*function*/; |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1466 | return; |
| 1467 | } |
| 1468 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1469 | d->addAttr(::new (S.Context) NodebugAttr()); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1472 | static void HandleNoinlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1473 | // check the attribute arguments. |
| 1474 | if (Attr.getNumArgs() != 0) { |
| 1475 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1476 | return; |
| 1477 | } |
| 1478 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1479 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1480 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1481 | << "noinline" << 0 /*function*/; |
| 1482 | return; |
| 1483 | } |
| 1484 | |
Chris Lattner | 0b2b6e1 | 2009-03-04 06:34:08 +0000 | [diff] [blame] | 1485 | d->addAttr(::new (S.Context) NoinlineAttr()); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1488 | static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1489 | // check the attribute arguments. |
| 1490 | if (Attr.getNumArgs() != 0) { |
| 1491 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1492 | return; |
| 1493 | } |
| 1494 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1495 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
| 1496 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1497 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1498 | << "gnu_inline" << 0 /*function*/; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1499 | return; |
| 1500 | } |
| 1501 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1502 | if (!Fn->isInline()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1503 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1504 | return; |
| 1505 | } |
| 1506 | |
Douglas Gregor | 9f9bf25 | 2009-04-28 06:37:30 +0000 | [diff] [blame] | 1507 | d->addAttr(::new (S.Context) GNUInlineAttr()); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1510 | static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1511 | // check the attribute arguments. |
| 1512 | if (Attr.getNumArgs() != 1) { |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1513 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1514 | return; |
| 1515 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1516 | |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1517 | if (!isFunctionOrMethod(d)) { |
| 1518 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1519 | << "regparm" << 0 /*function*/; |
| 1520 | return; |
| 1521 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1522 | |
| 1523 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1524 | llvm::APSInt NumParams(32); |
| 1525 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 1526 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1527 | << "regparm" << NumParamsExpr->getSourceRange(); |
| 1528 | return; |
| 1529 | } |
| 1530 | |
Anton Korobeynikov | 264a76c | 2009-04-03 23:38:25 +0000 | [diff] [blame] | 1531 | if (S.Context.Target.getRegParmMax() == 0) { |
| 1532 | S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1533 | << NumParamsExpr->getSourceRange(); |
| 1534 | return; |
| 1535 | } |
| 1536 | |
Anton Korobeynikov | 348f28a | 2009-04-04 10:27:50 +0000 | [diff] [blame] | 1537 | if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) { |
Anton Korobeynikov | 264a76c | 2009-04-03 23:38:25 +0000 | [diff] [blame] | 1538 | S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
| 1539 | << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1540 | return; |
| 1541 | } |
| 1542 | |
| 1543 | d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue())); |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1546 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 0fc169e | 2009-04-24 23:09:54 +0000 | [diff] [blame] | 1547 | // Checker-specific attribute handlers. |
| 1548 | //===----------------------------------------------------------------------===// |
| 1549 | |
| 1550 | static void HandleObjCOwnershipReturnsAttr(Decl *d, const AttributeList &Attr, |
| 1551 | Sema &S) { |
| 1552 | |
| 1553 | if (!isa<ObjCMethodDecl>(d) && !isa<FunctionDecl>(d)) { |
| 1554 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1555 | "objc_ownership_returns" << 3 /* function or method */; |
| 1556 | return; |
| 1557 | } |
| 1558 | |
| 1559 | d->addAttr(::new (S.Context) ObjCOwnershipReturnsAttr()); |
| 1560 | } |
| 1561 | |
Ted Kremenek | 22afc65 | 2009-04-27 18:41:18 +0000 | [diff] [blame] | 1562 | static void HandleObjCOwnershipParmAttr(Decl *d, const AttributeList &Attr, |
| 1563 | Sema &S) { |
Ted Kremenek | de9a81b | 2009-04-25 00:17:17 +0000 | [diff] [blame] | 1564 | |
| 1565 | if (!isa<ParmVarDecl>(d)) { |
Ted Kremenek | 22afc65 | 2009-04-27 18:41:18 +0000 | [diff] [blame] | 1566 | const char *name; |
| 1567 | |
| 1568 | switch (Attr.getKind()) { |
| 1569 | default: |
| 1570 | assert(0 && "invalid ownership attribute"); |
| 1571 | return; |
Ted Kremenek | c6a59e4 | 2009-04-27 19:36:56 +0000 | [diff] [blame] | 1572 | case AttributeList::AT_objc_ownership_cfrelease: |
Ted Kremenek | bf20dbd | 2009-04-28 22:32:26 +0000 | [diff] [blame] | 1573 | name = "objc_ownership_cfrelease"; break; |
Ted Kremenek | 22afc65 | 2009-04-27 18:41:18 +0000 | [diff] [blame] | 1574 | case AttributeList::AT_objc_ownership_cfretain: |
| 1575 | name = "objc_ownership_cfretain"; break; |
Ted Kremenek | bf20dbd | 2009-04-28 22:32:26 +0000 | [diff] [blame] | 1576 | case AttributeList::AT_objc_ownership_make_collectable: |
| 1577 | name = "objc_ownership_make_collectable"; break; |
| 1578 | case AttributeList::AT_objc_ownership_release: |
| 1579 | name = "objc_ownership_release"; break; |
| 1580 | case AttributeList::AT_objc_ownership_retain: |
| 1581 | name = "objc_ownership_retain"; break; |
Ted Kremenek | 22afc65 | 2009-04-27 18:41:18 +0000 | [diff] [blame] | 1582 | }; |
Ted Kremenek | de9a81b | 2009-04-25 00:17:17 +0000 | [diff] [blame] | 1583 | |
Ted Kremenek | 22afc65 | 2009-04-27 18:41:18 +0000 | [diff] [blame] | 1584 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << name |
| 1585 | << 4 /* parameter */; |
Ted Kremenek | 4064de9 | 2009-04-27 18:27:22 +0000 | [diff] [blame] | 1586 | return; |
| 1587 | } |
| 1588 | |
Ted Kremenek | 22afc65 | 2009-04-27 18:41:18 +0000 | [diff] [blame] | 1589 | switch (Attr.getKind()) { |
| 1590 | default: |
| 1591 | assert(0 && "invalid ownership attribute"); |
| 1592 | return; |
Ted Kremenek | c6a59e4 | 2009-04-27 19:36:56 +0000 | [diff] [blame] | 1593 | case AttributeList::AT_objc_ownership_cfrelease: |
| 1594 | d->addAttr(::new (S.Context) ObjCOwnershipCFReleaseAttr()); return; |
Ted Kremenek | 22afc65 | 2009-04-27 18:41:18 +0000 | [diff] [blame] | 1595 | case AttributeList::AT_objc_ownership_cfretain: |
| 1596 | d->addAttr(::new (S.Context) ObjCOwnershipCFRetainAttr()); return; |
Ted Kremenek | bf20dbd | 2009-04-28 22:32:26 +0000 | [diff] [blame] | 1597 | case AttributeList::AT_objc_ownership_make_collectable: |
| 1598 | d->addAttr(::new (S.Context) ObjCOwnershipMakeCollectableAttr()); return; |
| 1599 | case AttributeList::AT_objc_ownership_release: |
| 1600 | d->addAttr(::new (S.Context) ObjCOwnershipReleaseAttr()); return; |
| 1601 | case AttributeList::AT_objc_ownership_retain: |
| 1602 | d->addAttr(::new (S.Context) ObjCOwnershipRetainAttr()); return; |
Ted Kremenek | 22afc65 | 2009-04-27 18:41:18 +0000 | [diff] [blame] | 1603 | } |
Ted Kremenek | 4064de9 | 2009-04-27 18:27:22 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Ted Kremenek | 0fc169e | 2009-04-24 23:09:54 +0000 | [diff] [blame] | 1606 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1607 | // Top Level Sema Entry Points |
| 1608 | //===----------------------------------------------------------------------===// |
| 1609 | |
Sebastian Redl | a89d82c | 2008-12-21 19:24:58 +0000 | [diff] [blame] | 1610 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1611 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 1612 | /// silently ignore it. |
| 1613 | static void ProcessDeclAttribute(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1614 | switch (Attr.getKind()) { |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1615 | case AttributeList::AT_IBOutlet: HandleIBOutletAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1616 | case AttributeList::AT_address_space: |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1617 | case AttributeList::AT_objc_gc: |
| 1618 | // Ignore these, these are type attributes, handled by ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1619 | break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1620 | case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; |
| 1621 | case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1622 | case AttributeList::AT_always_inline: |
| 1623 | HandleAlwaysInlineAttr (D, Attr, S); break; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1624 | case AttributeList::AT_analyzer_noreturn: |
| 1625 | HandleAnalyzerNoReturnAttr (D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1626 | case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; |
| 1627 | case AttributeList::AT_constructor: HandleConstructorAttr(D, Attr, S); break; |
| 1628 | case AttributeList::AT_deprecated: HandleDeprecatedAttr(D, Attr, S); break; |
| 1629 | case AttributeList::AT_destructor: HandleDestructorAttr(D, Attr, S); break; |
| 1630 | case AttributeList::AT_dllexport: HandleDLLExportAttr (D, Attr, S); break; |
| 1631 | case AttributeList::AT_dllimport: HandleDLLImportAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1632 | case AttributeList::AT_ext_vector_type: |
| 1633 | HandleExtVectorTypeAttr(D, Attr, S); |
| 1634 | break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1635 | case AttributeList::AT_fastcall: HandleFastCallAttr (D, Attr, S); break; |
| 1636 | case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1637 | case AttributeList::AT_gnu_inline: HandleGNUInlineAttr(D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1638 | case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1639 | case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; |
| 1640 | case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; |
| 1641 | case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; |
Ted Kremenek | 0fc169e | 2009-04-24 23:09:54 +0000 | [diff] [blame] | 1642 | |
| 1643 | // Checker-specific. |
Ted Kremenek | bf20dbd | 2009-04-28 22:32:26 +0000 | [diff] [blame] | 1644 | case AttributeList::AT_objc_ownership_cfrelease: |
Ted Kremenek | 22afc65 | 2009-04-27 18:41:18 +0000 | [diff] [blame] | 1645 | case AttributeList::AT_objc_ownership_cfretain: |
Ted Kremenek | bf20dbd | 2009-04-28 22:32:26 +0000 | [diff] [blame] | 1646 | case AttributeList::AT_objc_ownership_make_collectable: |
| 1647 | case AttributeList::AT_objc_ownership_release: |
| 1648 | case AttributeList::AT_objc_ownership_retain: |
Ted Kremenek | 22afc65 | 2009-04-27 18:41:18 +0000 | [diff] [blame] | 1649 | HandleObjCOwnershipParmAttr(D, Attr, S); break; |
Ted Kremenek | 0fc169e | 2009-04-24 23:09:54 +0000 | [diff] [blame] | 1650 | case AttributeList::AT_objc_ownership_returns: |
| 1651 | HandleObjCOwnershipReturnsAttr(D, Attr, S); break; |
| 1652 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1653 | case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1654 | case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1655 | case AttributeList::AT_stdcall: HandleStdCallAttr (D, Attr, S); break; |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1656 | case AttributeList::AT_unavailable: HandleUnavailableAttr(D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1657 | case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1658 | case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1659 | case AttributeList::AT_vector_size: HandleVectorSizeAttr(D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1660 | case AttributeList::AT_visibility: HandleVisibilityAttr(D, Attr, S); break; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1661 | case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S); |
| 1662 | break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1663 | case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1664 | case AttributeList::AT_weak_import: HandleWeakImportAttr(D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1665 | case AttributeList::AT_transparent_union: |
| 1666 | HandleTransparentUnionAttr(D, Attr, S); |
| 1667 | break; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1668 | case AttributeList::AT_objc_exception: |
| 1669 | HandleObjCExceptionAttr(D, Attr, S); |
| 1670 | break; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1671 | case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1672 | case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1673 | case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1674 | case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1675 | case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break; |
| 1676 | case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break; |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1677 | case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break; |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1678 | case AttributeList::AT_nodebug: HandleNodebugAttr (D, Attr, S); break; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1679 | case AttributeList::AT_noinline: HandleNoinlineAttr (D, Attr, S); break; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1680 | case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break; |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 1681 | case AttributeList::IgnoredAttribute: |
Chris Lattner | 5e20448 | 2009-04-25 18:44:54 +0000 | [diff] [blame] | 1682 | case AttributeList::AT_no_instrument_function: // Interacts with -pg. |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 1683 | // Just ignore |
| 1684 | break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1685 | default: |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1686 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 1692 | /// attribute list to the specified decl, ignoring any type attributes. |
| 1693 | void Sema::ProcessDeclAttributeList(Decl *D, const AttributeList *AttrList) { |
| 1694 | while (AttrList) { |
| 1695 | ProcessDeclAttribute(D, *AttrList, *this); |
| 1696 | AttrList = AttrList->getNext(); |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1701 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 1702 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 1703 | /// specified in many different places, and we need to find and apply them all. |
| 1704 | void Sema::ProcessDeclAttributes(Decl *D, const Declarator &PD) { |
| 1705 | // Apply decl attributes from the DeclSpec if present. |
| 1706 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes()) |
| 1707 | ProcessDeclAttributeList(D, Attrs); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1708 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1709 | // Walk the declarator structure, applying decl attributes that were in a type |
| 1710 | // position to the decl itself. This handles cases like: |
| 1711 | // int *__attr__(x)** D; |
| 1712 | // when X is a decl attribute. |
| 1713 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 1714 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
| 1715 | ProcessDeclAttributeList(D, Attrs); |
| 1716 | |
| 1717 | // Finally, apply any attributes on the decl itself. |
| 1718 | if (const AttributeList *Attrs = PD.getAttributes()) |
| 1719 | ProcessDeclAttributeList(D, Attrs); |
| 1720 | } |
| 1721 | |