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