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