Chris Lattner | 2c6fcf5 | 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 | |
Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 14 | #include "clang/Sema/Sema.h" |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetAttributesSema.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
| 18 | #include "clang/AST/Expr.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Daniel Dunbar | 34fb672 | 2008-08-11 03:27:53 +0000 | [diff] [blame] | 20 | #include "clang/Parse/DeclSpec.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 22 | using namespace clang; |
| 23 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
| 25 | // Helper functions |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 28 | static const FunctionType *getFunctionType(const Decl *d, |
| 29 | bool blocksToo = true) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 30 | QualType Ty; |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 31 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(d)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 32 | Ty = decl->getType(); |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 33 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 34 | Ty = decl->getType(); |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 35 | else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 36 | Ty = decl->getUnderlyingType(); |
| 37 | else |
| 38 | return 0; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 40 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 41 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 28c433d | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 42 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 43 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 44 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 45 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 48 | // FIXME: We should provide an abstraction around a method or function |
| 49 | // to provide the following bits of information. |
| 50 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 51 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 52 | /// type (function or function-typed variable). |
| 53 | static bool isFunction(const Decl *d) { |
| 54 | return getFunctionType(d, false) != NULL; |
| 55 | } |
| 56 | |
| 57 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 58 | /// type (function or function-typed variable) or an Objective-C |
| 59 | /// method. |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 60 | static bool isFunctionOrMethod(const Decl *d) { |
| 61 | return isFunction(d)|| isa<ObjCMethodDecl>(d); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 64 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 65 | /// type (function or function-typed variable) or an Objective-C |
| 66 | /// method or a block. |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 67 | static bool isFunctionOrMethodOrBlock(const Decl *d) { |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 68 | if (isFunctionOrMethod(d)) |
| 69 | return true; |
| 70 | // check for block is more involved. |
| 71 | if (const VarDecl *V = dyn_cast<VarDecl>(d)) { |
| 72 | QualType Ty = V->getType(); |
| 73 | return Ty->isBlockPointerType(); |
| 74 | } |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 75 | return isa<BlockDecl>(d); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 78 | /// hasFunctionProto - Return true if the given decl has a argument |
| 79 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 80 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 81 | static bool hasFunctionProto(const Decl *d) { |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 82 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 83 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 84 | else { |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 85 | assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d)); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 86 | return true; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 91 | /// arguments. It is an error to call this on a K&R function (use |
| 92 | /// hasFunctionProto first). |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 93 | static unsigned getFunctionOrMethodNumArgs(const Decl *d) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 94 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 95 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 96 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) |
| 97 | return BD->getNumParams(); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 98 | return cast<ObjCMethodDecl>(d)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 101 | static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 102 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 103 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 104 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) |
| 105 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 106 | |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 107 | return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 110 | static QualType getFunctionOrMethodResultType(const Decl *d) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 111 | if (const FunctionType *FnTy = getFunctionType(d)) |
| 112 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
| 113 | return cast<ObjCMethodDecl>(d)->getResultType(); |
| 114 | } |
| 115 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 116 | static bool isFunctionOrMethodVariadic(const Decl *d) { |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 117 | if (const FunctionType *FnTy = getFunctionType(d)) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 118 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 119 | return proto->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 120 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 121 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 122 | else { |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 123 | return cast<ObjCMethodDecl>(d)->isVariadic(); |
| 124 | } |
| 125 | } |
| 126 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 127 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 128 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 129 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 130 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 131 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 132 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 133 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 134 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 135 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 136 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 138 | // FIXME: Should we walk the chain of classes? |
| 139 | return ClsName == &Ctx.Idents.get("NSString") || |
| 140 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 141 | } |
| 142 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 143 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 144 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 145 | if (!PT) |
| 146 | return false; |
| 147 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 148 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 149 | if (!RT) |
| 150 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 151 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 152 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 153 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 154 | return false; |
| 155 | |
| 156 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 157 | } |
| 158 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 159 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 160 | // Attribute Implementations |
| 161 | //===----------------------------------------------------------------------===// |
| 162 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 163 | // FIXME: All this manual attribute parsing code is gross. At the |
| 164 | // least add some helper functions to check most argument patterns (# |
| 165 | // and types of args). |
| 166 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 167 | static void HandleExtVectorTypeAttr(Scope *scope, Decl *d, |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 168 | const AttributeList &Attr, Sema &S) { |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 169 | TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d); |
| 170 | if (tDecl == 0) { |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 171 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 172 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 173 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 174 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 175 | QualType curType = tDecl->getUnderlyingType(); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 176 | |
| 177 | Expr *sizeExpr; |
| 178 | |
| 179 | // Special case where the argument is a template id. |
| 180 | if (Attr.getParameterName()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 181 | CXXScopeSpec SS; |
| 182 | UnqualifiedId id; |
| 183 | id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); |
| 184 | sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>(); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 185 | } else { |
| 186 | // check the attribute arguments. |
| 187 | if (Attr.getNumArgs() != 1) { |
| 188 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 189 | return; |
| 190 | } |
| 191 | sizeExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 192 | } |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 193 | |
| 194 | // Instantiate/Install the vector type, and let Sema build the type for us. |
| 195 | // This will run the reguired checks. |
| 196 | QualType T = S.BuildExtVectorType(curType, S.Owned(sizeExpr), Attr.getLoc()); |
| 197 | if (!T.isNull()) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 198 | // FIXME: preserve the old source info. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 199 | tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 200 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 201 | // Remember this typedef decl, we will need it later for diagnostics. |
| 202 | S.ExtVectorDecls.push_back(tDecl); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 203 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 206 | static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 207 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 208 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 209 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 210 | return; |
| 211 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 213 | if (TagDecl *TD = dyn_cast<TagDecl>(d)) |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 214 | TD->addAttr(::new (S.Context) PackedAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 215 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { |
| 216 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 217 | // has no effect. |
| 218 | if (!FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 219 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 220 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 221 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 222 | else |
Anders Carlsson | 68e0b68 | 2009-08-08 18:23:56 +0000 | [diff] [blame] | 223 | FD->addAttr(::new (S.Context) PackedAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 224 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 225 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 228 | static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) { |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 229 | // check the attribute arguments. |
| 230 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 231 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 232 | return; |
| 233 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 235 | // The IBAction attributes only apply to instance methods. |
| 236 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) |
| 237 | if (MD->isInstanceMethod()) { |
| 238 | d->addAttr(::new (S.Context) IBActionAttr()); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | S.Diag(Attr.getLoc(), diag::err_attribute_ibaction) << Attr.getName(); |
| 243 | } |
| 244 | |
| 245 | static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) { |
| 246 | // check the attribute arguments. |
| 247 | if (Attr.getNumArgs() > 0) { |
| 248 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | // The IBOutlet attributes only apply to instance variables of |
Ted Kremenek | 06be968 | 2010-02-17 02:37:45 +0000 | [diff] [blame] | 253 | // Objective-C classes. |
| 254 | if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) { |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 255 | d->addAttr(::new (S.Context) IBOutletAttr()); |
| 256 | return; |
Ted Kremenek | 06be968 | 2010-02-17 02:37:45 +0000 | [diff] [blame] | 257 | } |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 258 | |
| 259 | S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName(); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 262 | static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr, |
| 263 | Sema &S) { |
| 264 | |
| 265 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 266 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 267 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 268 | return; |
| 269 | } |
| 270 | |
| 271 | // The IBOutletCollection attributes only apply to instance variables of |
| 272 | // Objective-C classes. |
| 273 | if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) { |
| 274 | S.Diag(Attr.getLoc(), diag::err_attribute_iboutlet) << Attr.getName(); |
| 275 | return; |
| 276 | } |
Fariborz Jahanian | 798f832 | 2010-08-17 21:39:27 +0000 | [diff] [blame^] | 277 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(d)) |
| 278 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
| 279 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type) |
| 280 | << VD->getType() << 0; |
| 281 | return; |
| 282 | } |
| 283 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d)) |
| 284 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
| 285 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type) |
| 286 | << PD->getType() << 1; |
| 287 | return; |
| 288 | } |
| 289 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 290 | IdentifierInfo *II = Attr.getParameterName(); |
| 291 | if (!II) |
| 292 | II = &S.Context.Idents.get("id"); |
Fariborz Jahanian | 798f832 | 2010-08-17 21:39:27 +0000 | [diff] [blame^] | 293 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 294 | Sema::TypeTy *TypeRep = S.getTypeName(*II, Attr.getLoc(), |
| 295 | S.getScopeForContext(d->getDeclContext()->getParent())); |
| 296 | if (!TypeRep) { |
| 297 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 298 | return; |
| 299 | } |
| 300 | QualType QT(QualType::getFromOpaquePtr(TypeRep)); |
| 301 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 302 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 303 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 304 | // treated as __attribute__((iboutletcollection())). |
| 305 | if (!QT->isObjCIdType() && !QT->isObjCClassType() && |
| 306 | !QT->isObjCObjectType()) { |
| 307 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 308 | return; |
| 309 | } |
| 310 | d->addAttr(::new (S.Context) IBOutletCollectionAttr(QT)); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 313 | static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 314 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 315 | // ignore it as well |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 316 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 317 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 318 | << Attr.getName() << 0 /*function*/; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 319 | return; |
| 320 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 321 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 322 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 323 | |
| 324 | // The nonnull attribute only applies to pointers. |
| 325 | llvm::SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 327 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 328 | E=Attr.arg_end(); I!=E; ++I) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 329 | |
| 330 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 331 | // The argument must be an integer constant expression. |
Ted Kremenek | 7d71db7 | 2008-12-04 19:38:33 +0000 | [diff] [blame] | 332 | Expr *Ex = static_cast<Expr *>(*I); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 333 | llvm::APSInt ArgNum(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 334 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 335 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 336 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 337 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 338 | return; |
| 339 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 340 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 341 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 342 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 343 | if (x < 1 || x > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 344 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 91aea71 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 345 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 346 | return; |
| 347 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 348 | |
Ted Kremenek | 5224e6a | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 349 | --x; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 350 | |
| 351 | // Is the function argument a pointer type? |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 352 | QualType T = getFunctionOrMethodArgType(d, x); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 353 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 354 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | 62157e5 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 355 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 356 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 357 | continue; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 358 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 359 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 360 | NonNullArgs.push_back(x); |
| 361 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 362 | |
| 363 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 364 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 365 | if (NonNullArgs.empty()) { |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 366 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) { |
| 367 | QualType T = getFunctionOrMethodArgType(d, I); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 368 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 369 | NonNullArgs.push_back(I); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 370 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 371 | |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 372 | if (NonNullArgs.empty()) { |
| 373 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
| 374 | return; |
| 375 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 376 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 377 | |
| 378 | unsigned* start = &NonNullArgs[0]; |
| 379 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 380 | llvm::array_pod_sort(start, start + size); |
Ted Kremenek | 510ee25 | 2010-02-11 07:31:47 +0000 | [diff] [blame] | 381 | d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size)); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 384 | static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) { |
| 385 | // This attribute must be applied to a function declaration. |
| 386 | // The first argument to the attribute must be a string, |
| 387 | // the name of the resource, for example "malloc". |
| 388 | // The following arguments must be argument indexes, the arguments must be |
| 389 | // of integer type for Returns, otherwise of pointer type. |
| 390 | // The difference between Holds and Takes is that a pointer may still be used |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 391 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 392 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 393 | |
| 394 | if (!AL.getParameterName()) { |
| 395 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 396 | << AL.getName()->getName() << 1; |
| 397 | return; |
| 398 | } |
| 399 | // Figure out our Kind, and check arguments while we're at it. |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 400 | attr::Kind K; |
| 401 | switch (AL.getKind()) { |
| 402 | case AttributeList::AT_ownership_takes: |
| 403 | K = attr::OwnershipTakes; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 404 | if (AL.getNumArgs() < 1) { |
| 405 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 406 | return; |
| 407 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 408 | break; |
| 409 | case AttributeList::AT_ownership_holds: |
| 410 | K = attr::OwnershipHolds; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 411 | if (AL.getNumArgs() < 1) { |
| 412 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 413 | return; |
| 414 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 415 | break; |
| 416 | case AttributeList::AT_ownership_returns: |
| 417 | K = attr::OwnershipReturns; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 418 | if (AL.getNumArgs() > 1) { |
| 419 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 420 | << AL.getNumArgs() + 1; |
| 421 | return; |
| 422 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 423 | break; |
| 424 | default: |
| 425 | // This should never happen given how we are called. |
| 426 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | if (!isFunction(d) || !hasFunctionProto(d)) { |
| 430 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) << AL.getName() |
| 431 | << 0 /*function*/; |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
| 436 | |
| 437 | llvm::StringRef Module = AL.getParameterName()->getName(); |
| 438 | |
| 439 | // Normalize the argument, __foo__ becomes foo. |
| 440 | if (Module.startswith("__") && Module.endswith("__")) |
| 441 | Module = Module.substr(2, Module.size() - 4); |
| 442 | |
| 443 | llvm::SmallVector<unsigned, 10> OwnershipArgs; |
| 444 | |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 445 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 446 | ++I) { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 447 | |
| 448 | Expr *IdxExpr = static_cast<Expr *>(*I); |
| 449 | llvm::APSInt ArgNum(32); |
| 450 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 451 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 452 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 453 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 454 | continue; |
| 455 | } |
| 456 | |
| 457 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 458 | |
| 459 | if (x > NumArgs || x < 1) { |
| 460 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 461 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 462 | continue; |
| 463 | } |
| 464 | --x; |
| 465 | switch (K) { |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 466 | case attr::OwnershipTakes: |
| 467 | case attr::OwnershipHolds: { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 468 | // Is the function argument a pointer type? |
| 469 | QualType T = getFunctionOrMethodArgType(d, x); |
| 470 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 471 | // FIXME: Should also highlight argument in decl. |
| 472 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 473 | << ((K==attr::OwnershipTakes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 474 | << "pointer" |
| 475 | << IdxExpr->getSourceRange(); |
| 476 | continue; |
| 477 | } |
| 478 | break; |
| 479 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 480 | case attr::OwnershipReturns: { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 481 | if (AL.getNumArgs() > 1) { |
| 482 | // Is the function argument an integer type? |
| 483 | Expr *IdxExpr = static_cast<Expr *>(AL.getArg(0)); |
| 484 | llvm::APSInt ArgNum(32); |
| 485 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 486 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 487 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 488 | << "ownership_returns" << "integer" |
| 489 | << IdxExpr->getSourceRange(); |
| 490 | return; |
| 491 | } |
| 492 | } |
| 493 | break; |
| 494 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 495 | default: |
| 496 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 497 | } // switch |
| 498 | |
| 499 | // Check we don't have a conflict with another ownership attribute. |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 500 | if (K != attr::OwnershipReturns && d->hasAttrs()) { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 501 | for (const Attr *attr = d->getAttrs(); attr; attr = attr->getNext()) { |
| 502 | if (const OwnershipAttr* Att = dyn_cast<OwnershipAttr>(attr)) { |
| 503 | // Two ownership attributes of the same kind can't conflict, |
| 504 | // except returns attributes. |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 505 | if (Att->getKind() != K) { |
| 506 | for (const unsigned *I = Att->begin(), *E = Att->end(); I!=E; ++I) { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 507 | if (x == *I) { |
| 508 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 509 | << AL.getName()->getName() << "ownership_*"; |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | OwnershipArgs.push_back(x); |
| 517 | } |
| 518 | |
| 519 | unsigned* start = OwnershipArgs.data(); |
| 520 | unsigned size = OwnershipArgs.size(); |
| 521 | llvm::array_pod_sort(start, start + size); |
| 522 | switch (K) { |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 523 | case attr::OwnershipTakes: { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 524 | if (OwnershipArgs.empty()) { |
| 525 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 526 | return; |
| 527 | } |
| 528 | d->addAttr(::new (S.Context) OwnershipTakesAttr(S.Context, start, size, |
| 529 | Module)); |
| 530 | break; |
| 531 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 532 | case attr::OwnershipHolds: { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 533 | if (OwnershipArgs.empty()) { |
| 534 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 535 | return; |
| 536 | } |
| 537 | d->addAttr(::new (S.Context) OwnershipHoldsAttr(S.Context, start, size, |
| 538 | Module)); |
| 539 | break; |
| 540 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 541 | case attr::OwnershipReturns: { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 542 | d->addAttr(::new (S.Context) OwnershipReturnsAttr(S.Context, start, size, |
| 543 | Module)); |
| 544 | break; |
| 545 | } |
| 546 | default: |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 547 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 551 | static bool isStaticVarOrStaticFunciton(Decl *D) { |
| 552 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 553 | return VD->getStorageClass() == VarDecl::Static; |
| 554 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 555 | return FD->getStorageClass() == FunctionDecl::Static; |
| 556 | return false; |
| 557 | } |
| 558 | |
| 559 | static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 560 | // Check the attribute arguments. |
| 561 | if (Attr.getNumArgs() > 1) { |
| 562 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | // gcc rejects |
| 567 | // class c { |
| 568 | // static int a __attribute__((weakref ("v2"))); |
| 569 | // static int b() __attribute__((weakref ("f3"))); |
| 570 | // }; |
| 571 | // and ignores the attributes of |
| 572 | // void f(void) { |
| 573 | // static int a __attribute__((weakref ("v2"))); |
| 574 | // } |
| 575 | // we reject them |
| 576 | if (const DeclContext *Ctx = d->getDeclContext()) { |
| 577 | Ctx = Ctx->getLookupContext(); |
| 578 | if (!isa<TranslationUnitDecl>(Ctx) && !isa<NamespaceDecl>(Ctx) ) { |
| 579 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 580 | dyn_cast<NamedDecl>(d)->getNameAsString(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 581 | return; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | // The GCC manual says |
| 586 | // |
| 587 | // At present, a declaration to which `weakref' is attached can only |
| 588 | // be `static'. |
| 589 | // |
| 590 | // It also says |
| 591 | // |
| 592 | // Without a TARGET, |
| 593 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 594 | // equivalent to `weak'. |
| 595 | // |
| 596 | // gcc 4.4.1 will accept |
| 597 | // int a7 __attribute__((weakref)); |
| 598 | // as |
| 599 | // int a7 __attribute__((weak)); |
| 600 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 601 | // it if this behaviour is actually used. |
| 602 | |
| 603 | if (!isStaticVarOrStaticFunciton(d)) { |
| 604 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) << |
| 605 | dyn_cast<NamedDecl>(d)->getNameAsString(); |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | // GCC rejects |
| 610 | // static ((alias ("y"), weakref)). |
| 611 | // Should we? How to check that weakref is before or after alias? |
| 612 | |
| 613 | if (Attr.getNumArgs() == 1) { |
| 614 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
| 615 | Arg = Arg->IgnoreParenCasts(); |
| 616 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 617 | |
| 618 | if (Str == 0 || Str->isWide()) { |
| 619 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 620 | << "weakref" << 1; |
| 621 | return; |
| 622 | } |
| 623 | // GCC will accept anything as the argument of weakref. Should we |
| 624 | // check for an existing decl? |
| 625 | d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString())); |
| 626 | } |
| 627 | |
| 628 | d->addAttr(::new (S.Context) WeakRefAttr()); |
| 629 | } |
| 630 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 631 | static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 632 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 633 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 634 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 635 | return; |
| 636 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 638 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 639 | Arg = Arg->IgnoreParenCasts(); |
| 640 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 641 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 642 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 643 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 644 | << "alias" << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 645 | return; |
| 646 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 647 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 648 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 649 | |
Ted Kremenek | 7f4945a | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 650 | d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 653 | static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr, |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 654 | Sema &S) { |
| 655 | // check the attribute arguments. |
| 656 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 657 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 658 | return; |
| 659 | } |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 660 | |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 661 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 662 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 663 | << Attr.getName() << 0 /*function*/; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 664 | return; |
| 665 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 666 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 667 | d->addAttr(::new (S.Context) AlwaysInlineAttr()); |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 670 | static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 671 | // check the attribute arguments. |
| 672 | if (Attr.getNumArgs() != 0) { |
| 673 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 674 | return; |
| 675 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 676 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 677 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 679 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
| 680 | d->addAttr(::new (S.Context) MallocAttr()); |
| 681 | return; |
| 682 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 685 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 688 | static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr, |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 689 | Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 690 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 691 | if (Attr.getNumArgs() != 0) { |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 692 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 693 | return false; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 694 | } |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 695 | |
Mike Stump | 88788fe | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 696 | if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) { |
| 697 | ValueDecl *VD = dyn_cast<ValueDecl>(d); |
Mike Stump | feb1945 | 2009-12-15 03:11:10 +0000 | [diff] [blame] | 698 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 699 | && !VD->getType()->isFunctionPointerType())) { |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 700 | S.Diag(Attr.getLoc(), |
| 701 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 702 | : diag::warn_attribute_wrong_decl_type) |
| 703 | << Attr.getName() << 0 /*function*/; |
Mike Stump | 88788fe | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 704 | return false; |
| 705 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 706 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 707 | |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 708 | return true; |
| 709 | } |
| 710 | |
| 711 | static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 712 | /* Diagnostics (if any) was emitted by Sema::ProcessFnAttr(). */ |
| 713 | assert(Attr.isInvalid() == false); |
| 714 | d->addAttr(::new (S.Context) NoReturnAttr()); |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr, |
| 718 | Sema &S) { |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 719 | if (HandleCommonNoReturnAttr(d, Attr, S)) |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 720 | d->addAttr(::new (S.Context) AnalyzerNoReturnAttr()); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 721 | } |
| 722 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 723 | // PS3 PPU-specific. |
| 724 | static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr, |
| 725 | Sema &S) { |
| 726 | /* |
| 727 | Returning a Vector Class in Registers |
| 728 | |
| 729 | According to the PPU ABI specifications, a class with a single member of vector type is returned in |
| 730 | memory when used as the return value of a function. This results in inefficient code when implementing |
| 731 | vector classes. To return the value in a single vector register, add the vecreturn attribute to the class |
| 732 | definition. This attribute is also applicable to struct types. |
| 733 | |
| 734 | Example: |
| 735 | |
| 736 | struct Vector |
| 737 | { |
| 738 | __vector float xyzw; |
| 739 | } __attribute__((vecreturn)); |
| 740 | |
| 741 | Vector Add(Vector lhs, Vector rhs) |
| 742 | { |
| 743 | Vector result; |
| 744 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 745 | return result; // This will be returned in a register |
| 746 | } |
| 747 | */ |
| 748 | if (!isa<CXXRecordDecl>(d)) { |
| 749 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 750 | << Attr.getName() << 9 /*class*/; |
| 751 | return; |
| 752 | } |
| 753 | |
| 754 | if (d->getAttr<VecReturnAttr>()) { |
| 755 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | d->addAttr(::new (S.Context) VecReturnAttr()); |
| 760 | } |
| 761 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 762 | static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 763 | if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) { |
| 764 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 765 | << Attr.getName() << 8 /*function, method, or parameter*/; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 766 | return; |
| 767 | } |
| 768 | // FIXME: Actually store the attribute on the declaration |
| 769 | } |
| 770 | |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 771 | static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 772 | // check the attribute arguments. |
| 773 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 774 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 775 | return; |
| 776 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 777 | |
John McCall | cef1582 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 778 | if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) && |
| 779 | !isa<TypeDecl>(d)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 780 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 781 | << Attr.getName() << 2 /*variable and function*/; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 782 | return; |
| 783 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 784 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 785 | d->addAttr(::new (S.Context) UnusedAttr()); |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 788 | static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 789 | // check the attribute arguments. |
| 790 | if (Attr.getNumArgs() != 0) { |
| 791 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 792 | return; |
| 793 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 794 | |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 795 | if (const VarDecl *VD = dyn_cast<VarDecl>(d)) { |
Daniel Dunbar | 311bf29 | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 796 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 797 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 798 | return; |
| 799 | } |
| 800 | } else if (!isFunctionOrMethod(d)) { |
| 801 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 802 | << Attr.getName() << 2 /*variable and function*/; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 803 | return; |
| 804 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 805 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 806 | d->addAttr(::new (S.Context) UsedAttr()); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 809 | static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 810 | // check the attribute arguments. |
| 811 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 812 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 813 | << "0 or 1"; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 814 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 815 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 816 | |
| 817 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 818 | if (Attr.getNumArgs() > 0) { |
| 819 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 820 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 821 | if (E->isTypeDependent() || E->isValueDependent() || |
| 822 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 823 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 824 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 825 | return; |
| 826 | } |
| 827 | priority = Idx.getZExtValue(); |
| 828 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 829 | |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 830 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 831 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 832 | << Attr.getName() << 0 /*function*/; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 833 | return; |
| 834 | } |
| 835 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 836 | d->addAttr(::new (S.Context) ConstructorAttr(priority)); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 840 | // check the attribute arguments. |
| 841 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 842 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 843 | << "0 or 1"; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 844 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 845 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 846 | |
| 847 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 848 | if (Attr.getNumArgs() > 0) { |
| 849 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 850 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 851 | if (E->isTypeDependent() || E->isValueDependent() || |
| 852 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 853 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 854 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 855 | return; |
| 856 | } |
| 857 | priority = Idx.getZExtValue(); |
| 858 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 859 | |
Anders Carlsson | 8e82b7f | 2008-08-22 22:10:48 +0000 | [diff] [blame] | 860 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 861 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 862 | << Attr.getName() << 0 /*function*/; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 863 | return; |
| 864 | } |
| 865 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 866 | d->addAttr(::new (S.Context) DestructorAttr(priority)); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 869 | static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 870 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 871 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 872 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 873 | return; |
| 874 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 875 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 876 | d->addAttr(::new (S.Context) DeprecatedAttr()); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 879 | static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 880 | // check the attribute arguments. |
| 881 | if (Attr.getNumArgs() != 0) { |
| 882 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 883 | return; |
| 884 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 885 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 886 | d->addAttr(::new (S.Context) UnavailableAttr()); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 889 | static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 890 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 891 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 892 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 893 | return; |
| 894 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 895 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 896 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 897 | Arg = Arg->IgnoreParenCasts(); |
| 898 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 899 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 900 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 901 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 902 | << "visibility" << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 903 | return; |
| 904 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 905 | |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 906 | llvm::StringRef TypeStr = Str->getString(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 907 | VisibilityAttr::VisibilityTypes type; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 908 | |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 909 | if (TypeStr == "default") |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 910 | type = VisibilityAttr::DefaultVisibility; |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 911 | else if (TypeStr == "hidden") |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 912 | type = VisibilityAttr::HiddenVisibility; |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 913 | else if (TypeStr == "internal") |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 914 | type = VisibilityAttr::HiddenVisibility; // FIXME |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 915 | else if (TypeStr == "protected") |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 916 | type = VisibilityAttr::ProtectedVisibility; |
| 917 | else { |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 918 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 919 | return; |
| 920 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 921 | |
Eli Friedman | 570024a | 2010-08-05 06:57:20 +0000 | [diff] [blame] | 922 | d->addAttr(::new (S.Context) VisibilityAttr(type, false)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 925 | static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr, |
| 926 | Sema &S) { |
| 927 | if (Attr.getNumArgs() != 0) { |
| 928 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 929 | return; |
| 930 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 931 | |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 932 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 933 | if (OCI == 0) { |
| 934 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 935 | return; |
| 936 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 937 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 938 | D->addAttr(::new (S.Context) ObjCExceptionAttr()); |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 942 | if (Attr.getNumArgs() != 0) { |
John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 943 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 944 | return; |
| 945 | } |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 946 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 947 | QualType T = TD->getUnderlyingType(); |
| 948 | if (!T->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 949 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 950 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 951 | return; |
| 952 | } |
| 953 | } |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 954 | D->addAttr(::new (S.Context) ObjCNSObjectAttr()); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 957 | static void |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 958 | HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 959 | if (Attr.getNumArgs() != 0) { |
John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 960 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 961 | return; |
| 962 | } |
| 963 | |
| 964 | if (!isa<FunctionDecl>(D)) { |
| 965 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 966 | return; |
| 967 | } |
| 968 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 969 | D->addAttr(::new (S.Context) OverloadableAttr()); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 972 | static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 973 | if (!Attr.getParameterName()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 974 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 975 | << "blocks" << 1; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 976 | return; |
| 977 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 978 | |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 979 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 980 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 981 | return; |
| 982 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 983 | |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 984 | BlocksAttr::BlocksAttrTypes type; |
Chris Lattner | 68e4868 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 985 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 986 | type = BlocksAttr::ByRef; |
| 987 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 988 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 989 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 990 | return; |
| 991 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 992 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 993 | d->addAttr(::new (S.Context) BlocksAttr(type)); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 994 | } |
| 995 | |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 996 | static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 997 | // check the attribute arguments. |
| 998 | if (Attr.getNumArgs() > 2) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 999 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1000 | << "0, 1 or 2"; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1001 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1004 | int sentinel = 0; |
| 1005 | if (Attr.getNumArgs() > 0) { |
| 1006 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 1007 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1008 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1009 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1010 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1011 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1012 | return; |
| 1013 | } |
| 1014 | sentinel = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1015 | |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1016 | if (sentinel < 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1017 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 1018 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1019 | return; |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | int nullPos = 0; |
| 1024 | if (Attr.getNumArgs() > 1) { |
| 1025 | Expr *E = static_cast<Expr *>(Attr.getArg(1)); |
| 1026 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1027 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1028 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1029 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1030 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1031 | return; |
| 1032 | } |
| 1033 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1034 | |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1035 | if (nullPos > 1 || nullPos < 0) { |
| 1036 | // FIXME: This error message could be improved, it would be nice |
| 1037 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1038 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 1039 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1040 | return; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1045 | const FunctionType *FT = FD->getType()->getAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1046 | assert(FT && "FunctionDecl has non-function type?"); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1048 | if (isa<FunctionNoProtoType>(FT)) { |
| 1049 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 1050 | return; |
| 1051 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1053 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1054 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1055 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1056 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1057 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) { |
| 1058 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1059 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1060 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1061 | } |
| 1062 | } else if (isa<BlockDecl>(d)) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1063 | // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the |
| 1064 | // caller. |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1065 | ; |
| 1066 | } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1067 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 1068 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1069 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d) |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1070 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1071 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1072 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 1073 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1074 | return; |
| 1075 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1076 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1077 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Fariborz Jahanian | 3133a2f | 2009-05-14 20:57:28 +0000 | [diff] [blame] | 1078 | << Attr.getName() << 6 /*function, method or block */; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1079 | return; |
| 1080 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1081 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1082 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Fariborz Jahanian | 3133a2f | 2009-05-14 20:57:28 +0000 | [diff] [blame] | 1083 | << Attr.getName() << 6 /*function, method or block */; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1084 | return; |
| 1085 | } |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1086 | d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos)); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1089 | static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1090 | // check the attribute arguments. |
| 1091 | if (Attr.getNumArgs() != 0) { |
| 1092 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1093 | return; |
| 1094 | } |
| 1095 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1096 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1097 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1098 | << Attr.getName() << 0 /*function*/; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1099 | return; |
| 1100 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1101 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1102 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 1103 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1104 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 1105 | return; |
| 1106 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1107 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 1108 | if (MD->getResultType()->isVoidType()) { |
| 1109 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1110 | << Attr.getName() << 1; |
| 1111 | return; |
| 1112 | } |
| 1113 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 1114 | D->addAttr(::new (S.Context) WarnUnusedResultAttr()); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1118 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1119 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1120 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1121 | return; |
| 1122 | } |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1123 | |
Fariborz Jahanian | 41136ee | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 1124 | /* weak only applies to non-static declarations */ |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1125 | if (isStaticVarOrStaticFunciton(D)) { |
Fariborz Jahanian | 41136ee | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 1126 | S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) << |
| 1127 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
| 1128 | return; |
| 1129 | } |
| 1130 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1131 | // TODO: could also be applied to methods? |
| 1132 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
| 1133 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1134 | << Attr.getName() << 2 /*variable and function*/; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1135 | return; |
| 1136 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1137 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1138 | D->addAttr(::new (S.Context) WeakAttr()); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1141 | static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1142 | // check the attribute arguments. |
| 1143 | if (Attr.getNumArgs() != 0) { |
| 1144 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1145 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1146 | } |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1147 | |
| 1148 | // weak_import only applies to variable & function declarations. |
| 1149 | bool isDef = false; |
| 1150 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1151 | isDef = (!VD->hasExternalStorage() || VD->getInit()); |
| 1152 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 1153 | isDef = FD->hasBody(); |
Fariborz Jahanian | 6063798 | 2009-05-04 19:35:12 +0000 | [diff] [blame] | 1154 | } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) { |
| 1155 | // We ignore weak import on properties and methods |
Mike Stump | 367fee6 | 2009-03-18 17:39:31 +0000 | [diff] [blame] | 1156 | return; |
Fariborz Jahanian | d361239 | 2009-11-17 19:08:08 +0000 | [diff] [blame] | 1157 | } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) { |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 1158 | // Don't issue the warning for darwin as target; yet, ignore the attribute. |
Fariborz Jahanian | 5ea6bdd | 2010-04-12 16:57:31 +0000 | [diff] [blame] | 1159 | if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin || |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 1160 | !isa<ObjCInterfaceDecl>(D)) |
| 1161 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Fariborz Jahanian | 5ea6bdd | 2010-04-12 16:57:31 +0000 | [diff] [blame] | 1162 | << Attr.getName() << 2 /*variable and function*/; |
| 1163 | return; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | // Merge should handle any subsequent violations. |
| 1167 | if (isDef) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1168 | S.Diag(Attr.getLoc(), |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1169 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 1170 | << "weak_import" << 2 /*variable and function*/; |
| 1171 | return; |
| 1172 | } |
| 1173 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1174 | D->addAttr(::new (S.Context) WeakImportAttr()); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1177 | static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr, |
| 1178 | Sema &S) { |
| 1179 | // Attribute has 3 arguments. |
| 1180 | if (Attr.getNumArgs() != 3) { |
John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1181 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1182 | return; |
| 1183 | } |
| 1184 | |
| 1185 | unsigned WGSize[3]; |
| 1186 | for (unsigned i = 0; i < 3; ++i) { |
| 1187 | Expr *E = static_cast<Expr *>(Attr.getArg(i)); |
| 1188 | llvm::APSInt ArgNum(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1189 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1190 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1191 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1192 | << "reqd_work_group_size" << E->getSourceRange(); |
| 1193 | return; |
| 1194 | } |
| 1195 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 1196 | } |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1197 | D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1], |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1198 | WGSize[2])); |
| 1199 | } |
| 1200 | |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1201 | static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1202 | // Attribute has no arguments. |
| 1203 | if (Attr.getNumArgs() != 1) { |
| 1204 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1205 | return; |
| 1206 | } |
| 1207 | |
| 1208 | // Make sure that there is a string literal as the sections's single |
| 1209 | // argument. |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1210 | Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1211 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1212 | if (!SE) { |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1213 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1214 | return; |
| 1215 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1216 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1217 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 5f08912 | 2009-11-30 17:08:26 +0000 | [diff] [blame] | 1218 | std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString()); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1219 | if (!Error.empty()) { |
| 1220 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 1221 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1222 | return; |
| 1223 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1224 | |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1225 | // This attribute cannot be applied to local variables. |
| 1226 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 1227 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 1228 | return; |
| 1229 | } |
| 1230 | |
Ted Kremenek | 7f4945a | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 1231 | D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString())); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1234 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1235 | static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1236 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1237 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1238 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1239 | return; |
| 1240 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1241 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1242 | d->addAttr(::new (S.Context) NoThrowAttr()); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1245 | static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1246 | // check the attribute arguments. |
| 1247 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1248 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1249 | return; |
| 1250 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1251 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1252 | d->addAttr(::new (S.Context) ConstAttr()); |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1256 | // check the attribute arguments. |
| 1257 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1258 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1259 | return; |
| 1260 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1261 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1262 | d->addAttr(::new (S.Context) PureAttr()); |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1265 | static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1266 | if (!Attr.getParameterName()) { |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1267 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1268 | return; |
| 1269 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1270 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1271 | if (Attr.getNumArgs() != 0) { |
| 1272 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1273 | return; |
| 1274 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1275 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1276 | VarDecl *VD = dyn_cast<VarDecl>(d); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1277 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1278 | if (!VD || !VD->hasLocalStorage()) { |
| 1279 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 1280 | return; |
| 1281 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1282 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1283 | // Look up the function |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1284 | // FIXME: Lookup probably isn't looking in the right place |
| 1285 | // FIXME: The lookup source location should be in the attribute, not the |
| 1286 | // start of the attribute. |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1287 | NamedDecl *CleanupDecl |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1288 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), Attr.getLoc(), |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1289 | Sema::LookupOrdinaryName); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1290 | if (!CleanupDecl) { |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1291 | S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1292 | Attr.getParameterName(); |
| 1293 | return; |
| 1294 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1295 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1296 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 1297 | if (!FD) { |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1298 | S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) << |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1299 | Attr.getParameterName(); |
| 1300 | return; |
| 1301 | } |
| 1302 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1303 | if (FD->getNumParams() != 1) { |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1304 | S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_func_must_take_one_arg) << |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1305 | Attr.getParameterName(); |
| 1306 | return; |
| 1307 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1308 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1309 | // We're currently more strict than GCC about what function types we accept. |
| 1310 | // If this ever proves to be a problem it should be easy to fix. |
| 1311 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 1312 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Eli Friedman | bd0e673 | 2009-04-26 01:30:08 +0000 | [diff] [blame] | 1313 | if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1314 | S.Diag(Attr.getLoc(), |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1315 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 1316 | Attr.getParameterName() << ParamTy << Ty; |
| 1317 | return; |
| 1318 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1319 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1320 | d->addAttr(::new (S.Context) CleanupAttr(FD)); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1323 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
| 1324 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
| 1325 | static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1326 | if (Attr.getNumArgs() != 1) { |
| 1327 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1328 | return; |
| 1329 | } |
| 1330 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
| 1331 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1332 | << Attr.getName() << 0 /*function*/; |
| 1333 | return; |
| 1334 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1335 | // FIXME: in C++ the implicit 'this' function parameter also counts. this is |
| 1336 | // needed in order to be compatible with GCC the index must start with 1. |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1337 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
| 1338 | unsigned FirstIdx = 1; |
| 1339 | // checks for the 2nd argument |
| 1340 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1341 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1342 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 1343 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1344 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 1345 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1346 | return; |
| 1347 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1348 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1349 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 1350 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1351 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1352 | return; |
| 1353 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1354 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1355 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1356 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1357 | // make sure the format string is really a string |
| 1358 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1359 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1360 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 1361 | if (not_nsstring_type && |
| 1362 | !isCFStringType(Ty, S.Context) && |
| 1363 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1364 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1365 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1366 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1367 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1368 | << IdxExpr->getSourceRange(); |
| 1369 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1370 | } |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1371 | Ty = getFunctionOrMethodResultType(d); |
| 1372 | if (!isNSStringType(Ty, S.Context) && |
| 1373 | !isCFStringType(Ty, S.Context) && |
| 1374 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1375 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1376 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1377 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1378 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1379 | << IdxExpr->getSourceRange(); |
| 1380 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1383 | d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1386 | enum FormatAttrKind { |
| 1387 | CFStringFormat, |
| 1388 | NSStringFormat, |
| 1389 | StrftimeFormat, |
| 1390 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1391 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1392 | InvalidFormat |
| 1393 | }; |
| 1394 | |
| 1395 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 1396 | /// types. |
| 1397 | static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) { |
| 1398 | // Check for formats that get handled specially. |
| 1399 | if (Format == "NSString") |
| 1400 | return NSStringFormat; |
| 1401 | if (Format == "CFString") |
| 1402 | return CFStringFormat; |
| 1403 | if (Format == "strftime") |
| 1404 | return StrftimeFormat; |
| 1405 | |
| 1406 | // Otherwise, check for supported formats. |
| 1407 | if (Format == "scanf" || Format == "printf" || Format == "printf0" || |
| 1408 | Format == "strfmon" || Format == "cmn_err" || Format == "strftime" || |
| 1409 | Format == "NSString" || Format == "CFString" || Format == "vcmn_err" || |
| 1410 | Format == "zcmn_err") |
| 1411 | return SupportedFormat; |
| 1412 | |
Duncan Sands | de4fe35 | 2010-03-23 14:44:19 +0000 | [diff] [blame] | 1413 | if (Format == "gcc_diag" || Format == "gcc_cdiag" || |
| 1414 | Format == "gcc_cxxdiag" || Format == "gcc_tdiag") |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1415 | return IgnoredFormat; |
| 1416 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1417 | return InvalidFormat; |
| 1418 | } |
| 1419 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1420 | /// Handle __attribute__((init_priority(priority))) attributes based on |
| 1421 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
| 1422 | static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr, |
| 1423 | Sema &S) { |
| 1424 | if (!S.getLangOptions().CPlusPlus) { |
| 1425 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1426 | return; |
| 1427 | } |
| 1428 | |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 1429 | if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) { |
| 1430 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 1431 | Attr.setInvalid(); |
| 1432 | return; |
| 1433 | } |
| 1434 | QualType T = dyn_cast<VarDecl>(d)->getType(); |
| 1435 | if (S.Context.getAsArrayType(T)) |
| 1436 | T = S.Context.getBaseElementType(T); |
| 1437 | if (!T->getAs<RecordType>()) { |
| 1438 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 1439 | Attr.setInvalid(); |
| 1440 | return; |
| 1441 | } |
| 1442 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1443 | if (Attr.getNumArgs() != 1) { |
| 1444 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1445 | Attr.setInvalid(); |
| 1446 | return; |
| 1447 | } |
| 1448 | Expr *priorityExpr = static_cast<Expr *>(Attr.getArg(0)); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 1449 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1450 | llvm::APSInt priority(32); |
| 1451 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 1452 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 1453 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1454 | << "init_priority" << priorityExpr->getSourceRange(); |
| 1455 | Attr.setInvalid(); |
| 1456 | return; |
| 1457 | } |
Fariborz Jahanian | 9f2a4ee | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 1458 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1459 | if (prioritynum < 101 || prioritynum > 65535) { |
| 1460 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 1461 | << priorityExpr->getSourceRange(); |
| 1462 | Attr.setInvalid(); |
| 1463 | return; |
| 1464 | } |
| 1465 | d->addAttr(::new (S.Context) InitPriorityAttr(prioritynum)); |
| 1466 | } |
| 1467 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1468 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
| 1469 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1470 | static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1471 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1472 | if (!Attr.getParameterName()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1473 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1474 | << "format" << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1475 | return; |
| 1476 | } |
| 1477 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1478 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1479 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1480 | return; |
| 1481 | } |
| 1482 | |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 1483 | if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1484 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1485 | << Attr.getName() << 0 /*function*/; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1486 | return; |
| 1487 | } |
| 1488 | |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1489 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1490 | unsigned FirstIdx = 1; |
| 1491 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1492 | llvm::StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1493 | |
| 1494 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1495 | if (Format.startswith("__") && Format.endswith("__")) |
| 1496 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1497 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1498 | // Check for supported formats. |
| 1499 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1500 | |
| 1501 | if (Kind == IgnoredFormat) |
| 1502 | return; |
| 1503 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1504 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1505 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1506 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1507 | return; |
| 1508 | } |
| 1509 | |
| 1510 | // checks for the 2nd argument |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1511 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1512 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1513 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 1514 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1515 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1516 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1517 | return; |
| 1518 | } |
| 1519 | |
Anders Carlsson | be96bc9 | 2009-08-25 14:12:34 +0000 | [diff] [blame] | 1520 | // FIXME: We should handle the implicit 'this' parameter in a more generic |
| 1521 | // way that can be used for other arguments. |
| 1522 | bool HasImplicitThisParam = false; |
| 1523 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) { |
| 1524 | if (MD->isInstance()) { |
| 1525 | HasImplicitThisParam = true; |
| 1526 | NumArgs++; |
| 1527 | } |
| 1528 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1529 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1530 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1531 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1532 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1533 | return; |
| 1534 | } |
| 1535 | |
| 1536 | // FIXME: Do we need to bounds check? |
| 1537 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1538 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1539 | if (HasImplicitThisParam) { |
| 1540 | if (ArgIdx == 0) { |
| 1541 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1542 | << "a string type" << IdxExpr->getSourceRange(); |
| 1543 | return; |
| 1544 | } |
| 1545 | ArgIdx--; |
| 1546 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1547 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1548 | // make sure the format string is really a string |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1549 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1550 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1551 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1552 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1553 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1554 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1555 | return; |
| 1556 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1557 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1558 | // FIXME: do we need to check if the type is NSString*? What are the |
| 1559 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1560 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1561 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1562 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1563 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1564 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1565 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1566 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1567 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1568 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1569 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1570 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1571 | return; |
| 1572 | } |
| 1573 | |
| 1574 | // check the 3rd argument |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1575 | Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1)); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1576 | llvm::APSInt FirstArg(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1577 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 1578 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1579 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1580 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1581 | return; |
| 1582 | } |
| 1583 | |
| 1584 | // check if the function is variadic if the 3rd argument non-zero |
| 1585 | if (FirstArg != 0) { |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1586 | if (isFunctionOrMethodVariadic(d)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1587 | ++NumArgs; // +1 for ... |
| 1588 | } else { |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1589 | S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1590 | return; |
| 1591 | } |
| 1592 | } |
| 1593 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1594 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 1595 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1596 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1597 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1598 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 1599 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1600 | return; |
| 1601 | } |
| 1602 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 1603 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1604 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1605 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1606 | return; |
| 1607 | } |
| 1608 | |
Ted Kremenek | 7f4945a | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 1609 | d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(), |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1610 | FirstArg.getZExtValue())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1613 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 1614 | Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1615 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1616 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1617 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1618 | return; |
| 1619 | } |
| 1620 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1621 | // Try to find the underlying union declaration. |
| 1622 | RecordDecl *RD = 0; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1623 | TypedefDecl *TD = dyn_cast<TypedefDecl>(d); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1624 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 1625 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 1626 | else |
| 1627 | RD = dyn_cast<RecordDecl>(d); |
| 1628 | |
| 1629 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1630 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1631 | << Attr.getName() << 1 /*union*/; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1632 | return; |
| 1633 | } |
| 1634 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1635 | if (!RD->isDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1636 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1637 | diag::warn_transparent_union_attribute_not_definition); |
| 1638 | return; |
| 1639 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1640 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1641 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 1642 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1643 | if (Field == FieldEnd) { |
| 1644 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 1645 | return; |
| 1646 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1647 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1648 | FieldDecl *FirstField = *Field; |
| 1649 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 1650 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1651 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 1652 | diag::warn_transparent_union_attribute_floating) |
| 1653 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1654 | return; |
| 1655 | } |
| 1656 | |
| 1657 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 1658 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 1659 | for (; Field != FieldEnd; ++Field) { |
| 1660 | QualType FieldType = Field->getType(); |
| 1661 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 1662 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 1663 | // Warn if we drop the attribute. |
| 1664 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1665 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1666 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1667 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1668 | diag::warn_transparent_union_attribute_field_size_align) |
| 1669 | << isSize << Field->getDeclName() << FieldBits; |
| 1670 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1671 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1672 | diag::note_transparent_union_first_field_size_align) |
| 1673 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1674 | return; |
| 1675 | } |
| 1676 | } |
| 1677 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1678 | RD->addAttr(::new (S.Context) TransparentUnionAttr()); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1681 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1682 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1683 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1684 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1685 | return; |
| 1686 | } |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1687 | Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1688 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1689 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1690 | // Make sure that there is a string literal as the annotation's single |
| 1691 | // argument. |
| 1692 | if (!SE) { |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1693 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1694 | return; |
| 1695 | } |
Ted Kremenek | 7f4945a | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 1696 | d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 1699 | static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1700 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1701 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1702 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1703 | return; |
| 1704 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1705 | |
| 1706 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 1707 | // than GNU's, and should error out when it is used to specify a |
| 1708 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1709 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1710 | if (Attr.getNumArgs() == 0) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1711 | // FIXME: This should be the target specific maximum alignment. |
Daniel Dunbar | aac5bf1 | 2009-02-18 20:06:09 +0000 | [diff] [blame] | 1712 | // (For now we just use 128 bits which is the maximum on X86). |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 1713 | D->addAttr(::new (S.Context) AlignedAttr(128)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1714 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1715 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1716 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 1717 | S.AddAlignedAttr(Attr.getLoc(), D, static_cast<Expr *>(Attr.getArg(0))); |
| 1718 | } |
| 1719 | |
| 1720 | void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) { |
| 1721 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 1722 | // Save dependent expressions in the AST to be instantiated. |
| 1723 | D->addAttr(::new (Context) AlignedAttr(E)); |
| 1724 | return; |
| 1725 | } |
| 1726 | |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 1727 | llvm::APSInt Alignment(32); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 1728 | if (!E->isIntegerConstantExpr(Alignment, Context)) { |
| 1729 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
| 1730 | << "aligned" << E->getSourceRange(); |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 1731 | return; |
| 1732 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 1733 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 1734 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 1735 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 1736 | return; |
| 1737 | } |
| 1738 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 1739 | D->addAttr(::new (Context) AlignedAttr(Alignment.getZExtValue() * 8)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1740 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1741 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1742 | /// HandleModeAttr - This attribute modifies the width of a decl with primitive |
| 1743 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1744 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1745 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 1746 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 1747 | /// HImode, not an intermediate pointer. |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1748 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1749 | // This attribute isn't documented, but glibc uses it. It changes |
| 1750 | // the width of an int or unsigned int to the specified size. |
| 1751 | |
| 1752 | // Check that there aren't any arguments |
| 1753 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1754 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1755 | return; |
| 1756 | } |
| 1757 | |
| 1758 | IdentifierInfo *Name = Attr.getParameterName(); |
| 1759 | if (!Name) { |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1760 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1761 | return; |
| 1762 | } |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1763 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1764 | llvm::StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1765 | |
| 1766 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1767 | if (Str.startswith("__") && Str.endswith("__")) |
| 1768 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1769 | |
| 1770 | unsigned DestWidth = 0; |
| 1771 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1772 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1773 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1774 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1775 | switch (Str[0]) { |
| 1776 | case 'Q': DestWidth = 8; break; |
| 1777 | case 'H': DestWidth = 16; break; |
| 1778 | case 'S': DestWidth = 32; break; |
| 1779 | case 'D': DestWidth = 64; break; |
| 1780 | case 'X': DestWidth = 96; break; |
| 1781 | case 'T': DestWidth = 128; break; |
| 1782 | } |
| 1783 | if (Str[1] == 'F') { |
| 1784 | IntegerMode = false; |
| 1785 | } else if (Str[1] == 'C') { |
| 1786 | IntegerMode = false; |
| 1787 | ComplexMode = true; |
| 1788 | } else if (Str[1] != 'I') { |
| 1789 | DestWidth = 0; |
| 1790 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1791 | break; |
| 1792 | case 4: |
| 1793 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 1794 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1795 | if (Str == "word") |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1796 | DestWidth = S.Context.Target.getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1797 | else if (Str == "byte") |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1798 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1799 | break; |
| 1800 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1801 | if (Str == "pointer") |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1802 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1803 | break; |
| 1804 | } |
| 1805 | |
| 1806 | QualType OldTy; |
| 1807 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 1808 | OldTy = TD->getUnderlyingType(); |
| 1809 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 1810 | OldTy = VD->getType(); |
| 1811 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1812 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 1813 | << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1814 | return; |
| 1815 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1816 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1817 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1818 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 1819 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 1820 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1821 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1822 | } else if (ComplexMode) { |
| 1823 | if (!OldTy->isComplexType()) |
| 1824 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1825 | } else { |
| 1826 | if (!OldTy->isFloatingType()) |
| 1827 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1828 | } |
| 1829 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1830 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 1831 | // and friends, at least with glibc. |
| 1832 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 1833 | // width on unusual platforms. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1834 | // FIXME: Make sure floating-point mappings are accurate |
| 1835 | // FIXME: Support XF and TF types |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1836 | QualType NewTy; |
| 1837 | switch (DestWidth) { |
| 1838 | case 0: |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1839 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1840 | return; |
| 1841 | default: |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1842 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1843 | return; |
| 1844 | case 8: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1845 | if (!IntegerMode) { |
| 1846 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1847 | return; |
| 1848 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1849 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1850 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1851 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1852 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1853 | break; |
| 1854 | case 16: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1855 | if (!IntegerMode) { |
| 1856 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1857 | return; |
| 1858 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1859 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1860 | NewTy = S.Context.ShortTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1861 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1862 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1863 | break; |
| 1864 | case 32: |
| 1865 | if (!IntegerMode) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1866 | NewTy = S.Context.FloatTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1867 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1868 | NewTy = S.Context.IntTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1869 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1870 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1871 | break; |
| 1872 | case 64: |
| 1873 | if (!IntegerMode) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1874 | NewTy = S.Context.DoubleTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1875 | else if (OldTy->isSignedIntegerType()) |
Chandler Carruth | 7234370 | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 1876 | if (S.Context.Target.getLongWidth() == 64) |
| 1877 | NewTy = S.Context.LongTy; |
| 1878 | else |
| 1879 | NewTy = S.Context.LongLongTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1880 | else |
Chandler Carruth | 7234370 | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 1881 | if (S.Context.Target.getLongWidth() == 64) |
| 1882 | NewTy = S.Context.UnsignedLongTy; |
| 1883 | else |
| 1884 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1885 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1886 | case 96: |
| 1887 | NewTy = S.Context.LongDoubleTy; |
| 1888 | break; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1889 | case 128: |
| 1890 | if (!IntegerMode) { |
| 1891 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1892 | return; |
| 1893 | } |
Anders Carlsson | 88ea245 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 1894 | if (OldTy->isSignedIntegerType()) |
| 1895 | NewTy = S.Context.Int128Ty; |
| 1896 | else |
| 1897 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1898 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1901 | if (ComplexMode) { |
| 1902 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | // Install the new type. |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 1906 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 1907 | // FIXME: preserve existing source info. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1908 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 1909 | } else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1910 | cast<ValueDecl>(D)->setType(NewTy); |
| 1911 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1912 | |
Mike Stump | 3722f58 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 1913 | static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1914 | // check the attribute arguments. |
| 1915 | if (Attr.getNumArgs() > 0) { |
| 1916 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1917 | return; |
| 1918 | } |
Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 1919 | |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1920 | if (!isFunctionOrMethod(d)) { |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1921 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1922 | << Attr.getName() << 0 /*function*/; |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1923 | return; |
| 1924 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1925 | |
Mike Stump | 3722f58 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 1926 | d->addAttr(::new (S.Context) NoDebugAttr()); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1927 | } |
| 1928 | |
Mike Stump | 3722f58 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 1929 | static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1930 | // check the attribute arguments. |
| 1931 | if (Attr.getNumArgs() != 0) { |
| 1932 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1933 | return; |
| 1934 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1935 | |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1936 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1937 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1938 | << Attr.getName() << 0 /*function*/; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1939 | return; |
| 1940 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1941 | |
Mike Stump | 3722f58 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 1942 | d->addAttr(::new (S.Context) NoInlineAttr()); |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 1945 | static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr, |
| 1946 | Sema &S) { |
| 1947 | // check the attribute arguments. |
| 1948 | if (Attr.getNumArgs() != 0) { |
| 1949 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1950 | return; |
| 1951 | } |
| 1952 | |
| 1953 | if (!isa<FunctionDecl>(d)) { |
| 1954 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1955 | << Attr.getName() << 0 /*function*/; |
| 1956 | return; |
| 1957 | } |
| 1958 | |
| 1959 | d->addAttr(::new (S.Context) NoInstrumentFunctionAttr()); |
| 1960 | } |
| 1961 | |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1962 | static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1963 | // check the attribute arguments. |
| 1964 | if (Attr.getNumArgs() != 0) { |
| 1965 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1966 | return; |
| 1967 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1968 | |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1969 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
| 1970 | if (Fn == 0) { |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1971 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1972 | << Attr.getName() << 0 /*function*/; |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1973 | return; |
| 1974 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1975 | |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1976 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1977 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1978 | return; |
| 1979 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1980 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1981 | d->addAttr(::new (S.Context) GNUInlineAttr()); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1982 | } |
| 1983 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1984 | static void HandleCallConvAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1985 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
| 1986 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 1987 | assert(Attr.isInvalid() == false); |
| 1988 | |
| 1989 | switch (Attr.getKind()) { |
| 1990 | case AttributeList::AT_fastcall: |
| 1991 | d->addAttr(::new (S.Context) FastCallAttr()); |
| 1992 | return; |
| 1993 | case AttributeList::AT_stdcall: |
| 1994 | d->addAttr(::new (S.Context) StdCallAttr()); |
| 1995 | return; |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 1996 | case AttributeList::AT_thiscall: |
| 1997 | d->addAttr(::new (S.Context) ThisCallAttr()); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 1998 | case AttributeList::AT_cdecl: |
| 1999 | d->addAttr(::new (S.Context) CDeclAttr()); |
| 2000 | return; |
| 2001 | default: |
| 2002 | llvm_unreachable("unexpected attribute kind"); |
| 2003 | return; |
| 2004 | } |
| 2005 | } |
| 2006 | |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2007 | static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2008 | // check the attribute arguments. |
| 2009 | if (Attr.getNumArgs() != 1) { |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2010 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2011 | return; |
| 2012 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2013 | |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2014 | if (!isFunctionOrMethod(d)) { |
| 2015 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2016 | << Attr.getName() << 0 /*function*/; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2017 | return; |
| 2018 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2019 | |
| 2020 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 2021 | llvm::APSInt NumParams(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2022 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
| 2023 | !NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2024 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 2025 | << "regparm" << NumParamsExpr->getSourceRange(); |
| 2026 | return; |
| 2027 | } |
| 2028 | |
Anton Korobeynikov | 6953ef2 | 2009-04-03 23:38:25 +0000 | [diff] [blame] | 2029 | if (S.Context.Target.getRegParmMax() == 0) { |
| 2030 | S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2031 | << NumParamsExpr->getSourceRange(); |
| 2032 | return; |
| 2033 | } |
| 2034 | |
Anton Korobeynikov | 1dfc5f5 | 2009-04-04 10:27:50 +0000 | [diff] [blame] | 2035 | if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) { |
Anton Korobeynikov | 6953ef2 | 2009-04-03 23:38:25 +0000 | [diff] [blame] | 2036 | S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
| 2037 | << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2038 | return; |
| 2039 | } |
| 2040 | |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 2041 | d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue())); |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2044 | static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2045 | // check the attribute arguments. |
| 2046 | if (Attr.getNumArgs() != 0) { |
| 2047 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2048 | return; |
| 2049 | } |
| 2050 | |
| 2051 | if (!isa<CXXRecordDecl>(d) |
| 2052 | && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) { |
| 2053 | S.Diag(Attr.getLoc(), |
| 2054 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 2055 | : diag::warn_attribute_wrong_decl_type) |
| 2056 | << Attr.getName() << 7 /*virtual method or class*/; |
| 2057 | return; |
| 2058 | } |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2059 | |
| 2060 | // FIXME: Conform to C++0x redeclaration rules. |
| 2061 | |
| 2062 | if (d->getAttr<FinalAttr>()) { |
| 2063 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final"; |
| 2064 | return; |
| 2065 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2066 | |
| 2067 | d->addAttr(::new (S.Context) FinalAttr()); |
| 2068 | } |
| 2069 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2070 | //===----------------------------------------------------------------------===// |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2071 | // C++0x member checking attributes |
| 2072 | //===----------------------------------------------------------------------===// |
| 2073 | |
| 2074 | static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2075 | if (Attr.getNumArgs() != 0) { |
| 2076 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2077 | return; |
| 2078 | } |
| 2079 | |
| 2080 | if (!isa<CXXRecordDecl>(d)) { |
| 2081 | S.Diag(Attr.getLoc(), |
| 2082 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 2083 | : diag::warn_attribute_wrong_decl_type) |
| 2084 | << Attr.getName() << 9 /*class*/; |
| 2085 | return; |
| 2086 | } |
| 2087 | |
| 2088 | if (d->getAttr<BaseCheckAttr>()) { |
| 2089 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check"; |
| 2090 | return; |
| 2091 | } |
| 2092 | |
| 2093 | d->addAttr(::new (S.Context) BaseCheckAttr()); |
| 2094 | } |
| 2095 | |
| 2096 | static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2097 | if (Attr.getNumArgs() != 0) { |
| 2098 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2099 | return; |
| 2100 | } |
| 2101 | |
| 2102 | if (!isa<RecordDecl>(d->getDeclContext())) { |
| 2103 | // FIXME: It's not the type that's the problem |
| 2104 | S.Diag(Attr.getLoc(), |
| 2105 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 2106 | : diag::warn_attribute_wrong_decl_type) |
| 2107 | << Attr.getName() << 11 /*member*/; |
| 2108 | return; |
| 2109 | } |
| 2110 | |
| 2111 | // FIXME: Conform to C++0x redeclaration rules. |
| 2112 | |
| 2113 | if (d->getAttr<HidingAttr>()) { |
| 2114 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding"; |
| 2115 | return; |
| 2116 | } |
| 2117 | |
| 2118 | d->addAttr(::new (S.Context) HidingAttr()); |
| 2119 | } |
| 2120 | |
| 2121 | static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2122 | if (Attr.getNumArgs() != 0) { |
| 2123 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2124 | return; |
| 2125 | } |
| 2126 | |
| 2127 | if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) { |
| 2128 | // FIXME: It's not the type that's the problem |
| 2129 | S.Diag(Attr.getLoc(), |
| 2130 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 2131 | : diag::warn_attribute_wrong_decl_type) |
| 2132 | << Attr.getName() << 10 /*virtual method*/; |
| 2133 | return; |
| 2134 | } |
| 2135 | |
| 2136 | // FIXME: Conform to C++0x redeclaration rules. |
| 2137 | |
| 2138 | if (d->getAttr<OverrideAttr>()) { |
| 2139 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override"; |
| 2140 | return; |
| 2141 | } |
| 2142 | |
| 2143 | d->addAttr(::new (S.Context) OverrideAttr()); |
| 2144 | } |
| 2145 | |
| 2146 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2147 | // Checker-specific attribute handlers. |
| 2148 | //===----------------------------------------------------------------------===// |
| 2149 | |
| 2150 | static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr, |
| 2151 | Sema &S) { |
| 2152 | |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2153 | QualType RetTy; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2154 | |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2155 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) |
| 2156 | RetTy = MD->getResultType(); |
| 2157 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) |
| 2158 | RetTy = FD->getResultType(); |
| 2159 | else { |
Ted Kremenek | eebcf57 | 2009-08-19 23:56:48 +0000 | [diff] [blame] | 2160 | SourceLocation L = Attr.getLoc(); |
| 2161 | S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) |
| 2162 | << SourceRange(L, L) << Attr.getName() << 3 /* function or method */; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2163 | return; |
| 2164 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2165 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2166 | if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>() |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2167 | || RetTy->getAs<ObjCObjectPointerType>())) { |
Ted Kremenek | eebcf57 | 2009-08-19 23:56:48 +0000 | [diff] [blame] | 2168 | SourceLocation L = Attr.getLoc(); |
| 2169 | S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 2170 | << SourceRange(L, L) << Attr.getName(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2171 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2172 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2173 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2174 | switch (Attr.getKind()) { |
| 2175 | default: |
| 2176 | assert(0 && "invalid ownership attribute"); |
| 2177 | return; |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2178 | case AttributeList::AT_cf_returns_not_retained: |
| 2179 | d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr()); |
| 2180 | return; |
| 2181 | case AttributeList::AT_ns_returns_not_retained: |
| 2182 | d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr()); |
| 2183 | return; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2184 | case AttributeList::AT_cf_returns_retained: |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 2185 | d->addAttr(::new (S.Context) CFReturnsRetainedAttr()); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2186 | return; |
| 2187 | case AttributeList::AT_ns_returns_retained: |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 2188 | d->addAttr(::new (S.Context) NSReturnsRetainedAttr()); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2189 | return; |
| 2190 | }; |
| 2191 | } |
| 2192 | |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 2193 | static bool isKnownDeclSpecAttr(const AttributeList &Attr) { |
| 2194 | return Attr.getKind() == AttributeList::AT_dllimport || |
| 2195 | Attr.getKind() == AttributeList::AT_dllexport; |
| 2196 | } |
| 2197 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2198 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2199 | // Top Level Sema Entry Points |
| 2200 | //===----------------------------------------------------------------------===// |
| 2201 | |
Sebastian Redl | fc24b63 | 2008-12-21 19:24:58 +0000 | [diff] [blame] | 2202 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2203 | /// the attribute applies to decls. If the attribute is a type attribute, just |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2204 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 2205 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2206 | static void ProcessDeclAttribute(Scope *scope, Decl *D, |
| 2207 | const AttributeList &Attr, Sema &S) { |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2208 | if (Attr.isInvalid()) |
| 2209 | return; |
| 2210 | |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 2211 | if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) |
| 2212 | // FIXME: Try to deal with other __declspec attributes! |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2213 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2214 | switch (Attr.getKind()) { |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 2215 | case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 2216 | case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break; |
| 2217 | case AttributeList::AT_IBOutletCollection: |
| 2218 | HandleIBOutletCollection(D, Attr, S); break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2219 | case AttributeList::AT_address_space: |
Fariborz Jahanian | 257eac6 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 2220 | case AttributeList::AT_objc_gc: |
John Thompson | 4798122 | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 2221 | case AttributeList::AT_vector_size: |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2222 | // Ignore these, these are type attributes, handled by |
| 2223 | // ProcessTypeAttributes. |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2224 | break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2225 | case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; |
| 2226 | case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2227 | case AttributeList::AT_always_inline: |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 2228 | HandleAlwaysInlineAttr (D, Attr, S); break; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 2229 | case AttributeList::AT_analyzer_noreturn: |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2230 | HandleAnalyzerNoReturnAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2231 | case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; |
| 2232 | case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2233 | case AttributeList::AT_carries_dependency: |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2234 | HandleDependencyAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2235 | case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break; |
| 2236 | case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break; |
| 2237 | case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2238 | case AttributeList::AT_ext_vector_type: |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2239 | HandleExtVectorTypeAttr(scope, D, Attr, S); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2240 | break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2241 | case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break; |
| 2242 | case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; |
| 2243 | case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break; |
| 2244 | case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break; |
| 2245 | case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break; |
| 2246 | case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; |
| 2247 | case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break; |
| 2248 | case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 2249 | case AttributeList::AT_ownership_returns: |
| 2250 | case AttributeList::AT_ownership_takes: |
| 2251 | case AttributeList::AT_ownership_holds: |
| 2252 | HandleOwnershipAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2253 | case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; |
| 2254 | case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; |
| 2255 | case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2256 | case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2257 | |
| 2258 | // Checker-specific. |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2259 | case AttributeList::AT_ns_returns_not_retained: |
| 2260 | case AttributeList::AT_cf_returns_not_retained: |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2261 | case AttributeList::AT_ns_returns_retained: |
| 2262 | case AttributeList::AT_cf_returns_retained: |
| 2263 | HandleNSReturnsRetainedAttr(D, Attr, S); break; |
| 2264 | |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2265 | case AttributeList::AT_reqd_wg_size: |
| 2266 | HandleReqdWorkGroupSize(D, Attr, S); break; |
| 2267 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2268 | case AttributeList::AT_init_priority: |
| 2269 | HandleInitPriorityAttr(D, Attr, S); break; |
| 2270 | |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2271 | case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; |
| 2272 | case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2273 | case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break; |
| 2274 | case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; |
| 2275 | case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2276 | case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2277 | case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S); |
| 2278 | break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2279 | case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2280 | case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2281 | case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2282 | case AttributeList::AT_transparent_union: |
| 2283 | HandleTransparentUnionAttr(D, Attr, S); |
| 2284 | break; |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2285 | case AttributeList::AT_objc_exception: |
| 2286 | HandleObjCExceptionAttr(D, Attr, S); |
| 2287 | break; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2288 | case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2289 | case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break; |
| 2290 | case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break; |
| 2291 | case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break; |
| 2292 | case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break; |
| 2293 | case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break; |
| 2294 | case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break; |
| 2295 | case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break; |
| 2296 | case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break; |
| 2297 | case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2298 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | b4f3134 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 2299 | // Just ignore |
| 2300 | break; |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2301 | case AttributeList::AT_no_instrument_function: // Interacts with -pg. |
| 2302 | HandleNoInstrumentFunctionAttr(D, Attr, S); |
| 2303 | break; |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2304 | case AttributeList::AT_stdcall: |
| 2305 | case AttributeList::AT_cdecl: |
| 2306 | case AttributeList::AT_fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2307 | case AttributeList::AT_thiscall: |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2308 | HandleCallConvAttr(D, Attr, S); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2309 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2310 | default: |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2311 | // Ask target about the attribute. |
| 2312 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 2313 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Chandler Carruth | dd1bc0f | 2010-07-08 09:42:26 +0000 | [diff] [blame] | 2314 | S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) |
| 2315 | << Attr.getName(); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2316 | break; |
| 2317 | } |
| 2318 | } |
| 2319 | |
| 2320 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 2321 | /// attribute list to the specified decl, ignoring any type attributes. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2322 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2323 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
| 2324 | ProcessDeclAttribute(S, D, *l, *this); |
| 2325 | } |
| 2326 | |
| 2327 | // GCC accepts |
| 2328 | // static int a9 __attribute__((weakref)); |
| 2329 | // but that looks really pointless. We reject it. |
| 2330 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
| 2331 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 2332 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2333 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2334 | } |
| 2335 | } |
| 2336 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2337 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
| 2338 | /// #pragma weak needs a non-definition decl and source may not have one |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2339 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 2340 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2341 | NamedDecl *NewD = 0; |
| 2342 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 2343 | NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 2344 | FD->getLocation(), DeclarationName(II), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2345 | FD->getType(), FD->getTypeSourceInfo()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2346 | if (FD->getQualifier()) { |
| 2347 | FunctionDecl *NewFD = cast<FunctionDecl>(NewD); |
| 2348 | NewFD->setQualifierInfo(FD->getQualifier(), FD->getQualifierRange()); |
| 2349 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2350 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 2351 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
| 2352 | VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2353 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2354 | VD->getStorageClass(), |
| 2355 | VD->getStorageClassAsWritten()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2356 | if (VD->getQualifier()) { |
| 2357 | VarDecl *NewVD = cast<VarDecl>(NewD); |
| 2358 | NewVD->setQualifierInfo(VD->getQualifier(), VD->getQualifierRange()); |
| 2359 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2360 | } |
| 2361 | return NewD; |
| 2362 | } |
| 2363 | |
| 2364 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak |
| 2365 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 2366 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 2367 | if (W.getUsed()) return; // only do this once |
| 2368 | W.setUsed(true); |
| 2369 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 2370 | IdentifierInfo *NDId = ND->getIdentifier(); |
| 2371 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias()); |
Ted Kremenek | 7f4945a | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 2372 | NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 2373 | NewD->addAttr(::new (Context) WeakAttr()); |
| 2374 | WeakTopLevelDecl.push_back(NewD); |
| 2375 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 2376 | // to insert Decl at TU scope, sorry. |
| 2377 | DeclContext *SavedContext = CurContext; |
| 2378 | CurContext = Context.getTranslationUnitDecl(); |
| 2379 | PushOnScopeChains(NewD, S); |
| 2380 | CurContext = SavedContext; |
| 2381 | } else { // just add weak to existing |
| 2382 | ND->addAttr(::new (Context) WeakAttr()); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2383 | } |
| 2384 | } |
| 2385 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2386 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 2387 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 2388 | /// specified in many different places, and we need to find and apply them all. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2389 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2390 | // Handle #pragma weak |
| 2391 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 2392 | if (ND->hasLinkage()) { |
| 2393 | WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier()); |
| 2394 | if (W != WeakInfo()) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 2395 | // Identifier referenced by #pragma weak before it was declared |
| 2396 | DeclApplyPragmaWeak(S, ND, W); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2397 | WeakUndeclaredIdentifiers[ND->getIdentifier()] = W; |
| 2398 | } |
| 2399 | } |
| 2400 | } |
| 2401 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2402 | // Apply decl attributes from the DeclSpec if present. |
| 2403 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes()) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2404 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2405 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2406 | // Walk the declarator structure, applying decl attributes that were in a type |
| 2407 | // position to the decl itself. This handles cases like: |
| 2408 | // int *__attr__(x)** D; |
| 2409 | // when X is a decl attribute. |
| 2410 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 2411 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2412 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2413 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2414 | // Finally, apply any attributes on the decl itself. |
| 2415 | if (const AttributeList *Attrs = PD.getAttributes()) |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2416 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2417 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2418 | |
| 2419 | /// PushParsingDeclaration - Enter a new "scope" of deprecation |
| 2420 | /// warnings. |
| 2421 | /// |
| 2422 | /// The state token we use is the start index of this scope |
| 2423 | /// on the warning stack. |
| 2424 | Action::ParsingDeclStackState Sema::PushParsingDeclaration() { |
| 2425 | ParsingDeclDepth++; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 2426 | return (ParsingDeclStackState) DelayedDiagnostics.size(); |
| 2427 | } |
| 2428 | |
| 2429 | void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) { |
| 2430 | assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack"); |
| 2431 | ParsingDeclDepth--; |
| 2432 | |
| 2433 | if (DelayedDiagnostics.empty()) |
| 2434 | return; |
| 2435 | |
| 2436 | unsigned SavedIndex = (unsigned) S; |
| 2437 | assert(SavedIndex <= DelayedDiagnostics.size() && |
| 2438 | "saved index is out of bounds"); |
| 2439 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2440 | unsigned E = DelayedDiagnostics.size(); |
| 2441 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 2442 | // We only want to actually emit delayed diagnostics when we |
| 2443 | // successfully parsed a decl. |
| 2444 | Decl *D = Ctx ? Ctx.getAs<Decl>() : 0; |
| 2445 | if (D) { |
| 2446 | // We really do want to start with 0 here. We get one push for a |
| 2447 | // decl spec and another for each declarator; in a decl group like: |
| 2448 | // deprecated_typedef foo, *bar, baz(); |
| 2449 | // only the declarator pops will be passed decls. This is correct; |
| 2450 | // we really do need to consider delayed diagnostics from the decl spec |
| 2451 | // for each of the different declarations. |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2452 | for (unsigned I = 0; I != E; ++I) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 2453 | if (DelayedDiagnostics[I].Triggered) |
| 2454 | continue; |
| 2455 | |
| 2456 | switch (DelayedDiagnostics[I].Kind) { |
| 2457 | case DelayedDiagnostic::Deprecation: |
| 2458 | HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D); |
| 2459 | break; |
| 2460 | |
| 2461 | case DelayedDiagnostic::Access: |
| 2462 | HandleDelayedAccessCheck(DelayedDiagnostics[I], D); |
| 2463 | break; |
| 2464 | } |
| 2465 | } |
| 2466 | } |
| 2467 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 2468 | // Destroy all the delayed diagnostics we're about to pop off. |
| 2469 | for (unsigned I = SavedIndex; I != E; ++I) |
| 2470 | DelayedDiagnostics[I].destroy(); |
| 2471 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 2472 | DelayedDiagnostics.set_size(SavedIndex); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | static bool isDeclDeprecated(Decl *D) { |
| 2476 | do { |
| 2477 | if (D->hasAttr<DeprecatedAttr>()) |
| 2478 | return true; |
| 2479 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 2480 | return false; |
| 2481 | } |
| 2482 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 2483 | void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD, |
| 2484 | Decl *Ctx) { |
| 2485 | if (isDeclDeprecated(Ctx)) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2486 | return; |
| 2487 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 2488 | DD.Triggered = true; |
| 2489 | Diag(DD.Loc, diag::warn_deprecated) |
| 2490 | << DD.DeprecationData.Decl->getDeclName(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2491 | } |
| 2492 | |
| 2493 | void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) { |
| 2494 | // Delay if we're currently parsing a declaration. |
| 2495 | if (ParsingDeclDepth) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 2496 | DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2497 | return; |
| 2498 | } |
| 2499 | |
| 2500 | // Otherwise, don't warn if our current context is deprecated. |
| 2501 | if (isDeclDeprecated(cast<Decl>(CurContext))) |
| 2502 | return; |
| 2503 | |
| 2504 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 2505 | } |