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