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