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