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