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