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