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)) |
| 121 | return BD->IsVariadic(); |
| 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 | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 132 | const ObjCInterfaceType *ClsT =PT->getPointeeType()->getAs<ObjCInterfaceType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 133 | if (!ClsT) |
| 134 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 136 | IdentifierInfo* ClsName = ClsT->getDecl()->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(); |
| 153 | if (RD->getTagKind() != TagDecl::TK_struct) |
| 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 | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 262 | static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 263 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 264 | // ignore it as well |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 265 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 266 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 267 | << Attr.getName() << 0 /*function*/; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 268 | return; |
| 269 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 270 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 271 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 272 | |
| 273 | // The nonnull attribute only applies to pointers. |
| 274 | llvm::SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 275 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 276 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 277 | E=Attr.arg_end(); I!=E; ++I) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 278 | |
| 279 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 280 | // The argument must be an integer constant expression. |
Ted Kremenek | f5e8834 | 2008-12-04 19:38:33 +0000 | [diff] [blame] | 281 | Expr *Ex = static_cast<Expr *>(*I); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 282 | llvm::APSInt ArgNum(32); |
| 283 | if (!Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 284 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 285 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 286 | return; |
| 287 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 289 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 291 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 292 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 293 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 294 | return; |
| 295 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 297 | --x; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 298 | |
| 299 | // Is the function argument a pointer type? |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 300 | QualType T = getFunctionOrMethodArgType(d, x); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 301 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 302 | // FIXME: Should also highlight argument in decl. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 303 | S.Diag(Attr.getLoc(), diag::err_nonnull_pointers_only) |
| 304 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 305 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 306 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 307 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 308 | NonNullArgs.push_back(x); |
| 309 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 310 | |
| 311 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 312 | // arguments have a nonnull attribute. |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 313 | if (NonNullArgs.empty()) { |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 314 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) { |
| 315 | QualType T = getFunctionOrMethodArgType(d, I); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 316 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 317 | NonNullArgs.push_back(I); |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 318 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 319 | |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 320 | if (NonNullArgs.empty()) { |
| 321 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
| 322 | return; |
| 323 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 324 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 325 | |
| 326 | unsigned* start = &NonNullArgs[0]; |
| 327 | unsigned size = NonNullArgs.size(); |
| 328 | std::sort(start, start + size); |
Ted Kremenek | 5961611 | 2010-02-11 07:31:47 +0000 | [diff] [blame] | 329 | d->addAttr(::new (S.Context) NonNullAttr(S.Context, start, size)); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 332 | static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 333 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 334 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 335 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 336 | return; |
| 337 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 338 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 339 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 340 | Arg = Arg->IgnoreParenCasts(); |
| 341 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 342 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 343 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 344 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 345 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 346 | return; |
| 347 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 349 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 350 | |
Ted Kremenek | 3d2c43e | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 351 | d->addAttr(::new (S.Context) AliasAttr(S.Context, Str->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 354 | static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr, |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 355 | Sema &S) { |
| 356 | // check the attribute arguments. |
| 357 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 358 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 359 | return; |
| 360 | } |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 361 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 362 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 363 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 364 | << Attr.getName() << 0 /*function*/; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 365 | return; |
| 366 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 367 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 368 | d->addAttr(::new (S.Context) AlwaysInlineAttr()); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 369 | } |
| 370 | |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 371 | static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 372 | // check the attribute arguments. |
| 373 | if (Attr.getNumArgs() != 0) { |
| 374 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 375 | return; |
| 376 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 378 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 379 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 380 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
| 381 | d->addAttr(::new (S.Context) MallocAttr()); |
| 382 | return; |
| 383 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 386 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 389 | static bool HandleCommonNoReturnAttr(Decl *d, const AttributeList &Attr, |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 390 | Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 391 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 392 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 393 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 394 | return false; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 395 | } |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 396 | |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 397 | if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) { |
| 398 | ValueDecl *VD = dyn_cast<ValueDecl>(d); |
Mike Stump | 3ee7764 | 2009-12-15 03:11:10 +0000 | [diff] [blame] | 399 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 400 | && !VD->getType()->isFunctionPointerType())) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 401 | S.Diag(Attr.getLoc(), |
| 402 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 403 | : diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 404 | << Attr.getName() << 0 /*function*/; |
Mike Stump | 19c30c0 | 2009-04-29 19:03:13 +0000 | [diff] [blame] | 405 | return false; |
| 406 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 407 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 408 | |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 409 | return true; |
| 410 | } |
| 411 | |
| 412 | static void HandleNoReturnAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 413 | // Don't apply as a decl attribute to ValueDecl. |
| 414 | // FIXME: probably ought to diagnose this. |
| 415 | if (isa<ValueDecl>(d)) |
| 416 | return; |
| 417 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 418 | if (HandleCommonNoReturnAttr(d, Attr, S)) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 419 | d->addAttr(::new (S.Context) NoReturnAttr()); |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr, |
| 423 | Sema &S) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 424 | if (HandleCommonNoReturnAttr(d, Attr, S)) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 425 | d->addAttr(::new (S.Context) AnalyzerNoReturnAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 428 | static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 429 | if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) { |
| 430 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 431 | << Attr.getName() << 8 /*function, method, or parameter*/; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | // FIXME: Actually store the attribute on the declaration |
| 435 | } |
| 436 | |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 437 | static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 438 | // check the attribute arguments. |
| 439 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 440 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 441 | return; |
| 442 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 443 | |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 444 | if (!isa<VarDecl>(d) && !isFunctionOrMethod(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 445 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 446 | << Attr.getName() << 2 /*variable and function*/; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 447 | return; |
| 448 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 449 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 450 | d->addAttr(::new (S.Context) UnusedAttr()); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 453 | static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 454 | // check the attribute arguments. |
| 455 | if (Attr.getNumArgs() != 0) { |
| 456 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 457 | return; |
| 458 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 459 | |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 460 | if (const VarDecl *VD = dyn_cast<VarDecl>(d)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 461 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 462 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 463 | return; |
| 464 | } |
| 465 | } else if (!isFunctionOrMethod(d)) { |
| 466 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 467 | << Attr.getName() << 2 /*variable and function*/; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 468 | return; |
| 469 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 470 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 471 | d->addAttr(::new (S.Context) UsedAttr()); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 474 | static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 475 | // check the attribute arguments. |
| 476 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 477 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 478 | << "0 or 1"; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 479 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 480 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 481 | |
| 482 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 483 | if (Attr.getNumArgs() > 0) { |
| 484 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 485 | llvm::APSInt Idx(32); |
| 486 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 487 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 488 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | priority = Idx.getZExtValue(); |
| 492 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 493 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 494 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 495 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 496 | << Attr.getName() << 0 /*function*/; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 497 | return; |
| 498 | } |
| 499 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 500 | d->addAttr(::new (S.Context) ConstructorAttr(priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 504 | // check the attribute arguments. |
| 505 | if (Attr.getNumArgs() != 0 && Attr.getNumArgs() != 1) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 506 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 507 | << "0 or 1"; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 508 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 509 | } |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 510 | |
| 511 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 512 | if (Attr.getNumArgs() > 0) { |
| 513 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 514 | llvm::APSInt Idx(32); |
| 515 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 516 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 517 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 518 | return; |
| 519 | } |
| 520 | priority = Idx.getZExtValue(); |
| 521 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 522 | |
Anders Carlsson | 6782fc6 | 2008-08-22 22:10:48 +0000 | [diff] [blame] | 523 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 524 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 525 | << Attr.getName() << 0 /*function*/; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 529 | d->addAttr(::new (S.Context) DestructorAttr(priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 532 | static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 533 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 534 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 535 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 536 | return; |
| 537 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 538 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 539 | d->addAttr(::new (S.Context) DeprecatedAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 540 | } |
| 541 | |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 542 | static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 543 | // check the attribute arguments. |
| 544 | if (Attr.getNumArgs() != 0) { |
| 545 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 546 | return; |
| 547 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 548 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 549 | d->addAttr(::new (S.Context) UnavailableAttr()); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 552 | static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 553 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 554 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 555 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 556 | return; |
| 557 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 559 | Expr *Arg = static_cast<Expr*>(Attr.getArg(0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 560 | Arg = Arg->IgnoreParenCasts(); |
| 561 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 562 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 563 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 564 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 565 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 566 | return; |
| 567 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 568 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 569 | llvm::StringRef TypeStr = Str->getString(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 570 | VisibilityAttr::VisibilityTypes type; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 571 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 572 | if (TypeStr == "default") |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 573 | type = VisibilityAttr::DefaultVisibility; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 574 | else if (TypeStr == "hidden") |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 575 | type = VisibilityAttr::HiddenVisibility; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 576 | else if (TypeStr == "internal") |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 577 | type = VisibilityAttr::HiddenVisibility; // FIXME |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 578 | else if (TypeStr == "protected") |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 579 | type = VisibilityAttr::ProtectedVisibility; |
| 580 | else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 581 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 582 | return; |
| 583 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 584 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 585 | d->addAttr(::new (S.Context) VisibilityAttr(type)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 588 | static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr, |
| 589 | Sema &S) { |
| 590 | if (Attr.getNumArgs() != 0) { |
| 591 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 592 | return; |
| 593 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 594 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 595 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 596 | if (OCI == 0) { |
| 597 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 598 | return; |
| 599 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 600 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 601 | D->addAttr(::new (S.Context) ObjCExceptionAttr()); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 605 | if (Attr.getNumArgs() != 0) { |
| 606 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 607 | return; |
| 608 | } |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 609 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 610 | QualType T = TD->getUnderlyingType(); |
| 611 | if (!T->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 612 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 613 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 614 | return; |
| 615 | } |
| 616 | } |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 617 | D->addAttr(::new (S.Context) ObjCNSObjectAttr()); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 620 | static void |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 621 | HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 622 | if (Attr.getNumArgs() != 0) { |
| 623 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | if (!isa<FunctionDecl>(D)) { |
| 628 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 629 | return; |
| 630 | } |
| 631 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 632 | D->addAttr(::new (S.Context) OverloadableAttr()); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 635 | static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 636 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 637 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 638 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 639 | return; |
| 640 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 641 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 642 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 643 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 644 | return; |
| 645 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 646 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 647 | BlocksAttr::BlocksAttrTypes type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 648 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 649 | type = BlocksAttr::ByRef; |
| 650 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 651 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 652 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 653 | return; |
| 654 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 655 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 656 | d->addAttr(::new (S.Context) BlocksAttr(type)); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 659 | static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 660 | // check the attribute arguments. |
| 661 | if (Attr.getNumArgs() > 2) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 662 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 663 | << "0, 1 or 2"; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 664 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 667 | int sentinel = 0; |
| 668 | if (Attr.getNumArgs() > 0) { |
| 669 | Expr *E = static_cast<Expr *>(Attr.getArg(0)); |
| 670 | llvm::APSInt Idx(32); |
| 671 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 672 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 673 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 674 | return; |
| 675 | } |
| 676 | sentinel = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 677 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 678 | if (sentinel < 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 679 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 680 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 681 | return; |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | int nullPos = 0; |
| 686 | if (Attr.getNumArgs() > 1) { |
| 687 | Expr *E = static_cast<Expr *>(Attr.getArg(1)); |
| 688 | llvm::APSInt Idx(32); |
| 689 | if (!E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 690 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 691 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 692 | return; |
| 693 | } |
| 694 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 695 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 696 | if (nullPos > 1 || nullPos < 0) { |
| 697 | // FIXME: This error message could be improved, it would be nice |
| 698 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 699 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 700 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 701 | return; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 706 | const FunctionType *FT = FD->getType()->getAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 707 | assert(FT && "FunctionDecl has non-function type?"); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 708 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 709 | if (isa<FunctionNoProtoType>(FT)) { |
| 710 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 711 | return; |
| 712 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 714 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 715 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 716 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 717 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 718 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) { |
| 719 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 720 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 721 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 722 | } |
| 723 | } else if (isa<BlockDecl>(d)) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 724 | // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the |
| 725 | // caller. |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 726 | ; |
| 727 | } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 728 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 729 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 730 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d) |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 731 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 732 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 733 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 734 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 735 | return; |
| 736 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 737 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 738 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Fariborz Jahanian | ffb0081 | 2009-05-14 20:57:28 +0000 | [diff] [blame] | 739 | << Attr.getName() << 6 /*function, method or block */; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 740 | return; |
| 741 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 742 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 743 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Fariborz Jahanian | ffb0081 | 2009-05-14 20:57:28 +0000 | [diff] [blame] | 744 | << Attr.getName() << 6 /*function, method or block */; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 745 | return; |
| 746 | } |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 747 | d->addAttr(::new (S.Context) SentinelAttr(sentinel, nullPos)); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 750 | static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) { |
| 751 | // check the attribute arguments. |
| 752 | if (Attr.getNumArgs() != 0) { |
| 753 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 754 | return; |
| 755 | } |
| 756 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 757 | if (!isFunctionOrMethod(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 758 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 759 | << Attr.getName() << 0 /*function*/; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 760 | return; |
| 761 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 762 | |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 763 | if (getFunctionType(D)->getResultType()->isVoidType()) { |
| 764 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function) |
| 765 | << Attr.getName(); |
| 766 | return; |
| 767 | } |
| 768 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 769 | D->addAttr(::new (S.Context) WarnUnusedResultAttr()); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 773 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 774 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 775 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 776 | return; |
| 777 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 778 | |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 779 | /* weak only applies to non-static declarations */ |
| 780 | bool isStatic = false; |
| 781 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 782 | isStatic = VD->getStorageClass() == VarDecl::Static; |
| 783 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 784 | isStatic = FD->getStorageClass() == FunctionDecl::Static; |
| 785 | } |
| 786 | if (isStatic) { |
| 787 | S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) << |
| 788 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
| 789 | return; |
| 790 | } |
| 791 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 792 | // TODO: could also be applied to methods? |
| 793 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
| 794 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 795 | << Attr.getName() << 2 /*variable and function*/; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 796 | return; |
| 797 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 798 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 799 | D->addAttr(::new (S.Context) WeakAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 800 | } |
| 801 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 802 | static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 803 | // check the attribute arguments. |
| 804 | if (Attr.getNumArgs() != 0) { |
| 805 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 806 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 807 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 808 | |
| 809 | // weak_import only applies to variable & function declarations. |
| 810 | bool isDef = false; |
| 811 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 812 | isDef = (!VD->hasExternalStorage() || VD->getInit()); |
| 813 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Argyrios Kyrtzidis | 6fb0aee | 2009-06-30 02:35:26 +0000 | [diff] [blame] | 814 | isDef = FD->getBody(); |
Fariborz Jahanian | d4edddd | 2009-05-04 19:35:12 +0000 | [diff] [blame] | 815 | } else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D)) { |
| 816 | // We ignore weak import on properties and methods |
Mike Stump | 1c90f4d | 2009-03-18 17:39:31 +0000 | [diff] [blame] | 817 | return; |
Fariborz Jahanian | 5f8f857 | 2009-11-17 19:08:08 +0000 | [diff] [blame] | 818 | } else if (!(S.LangOpts.ObjCNonFragileABI && isa<ObjCInterfaceDecl>(D))) { |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 819 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 820 | << Attr.getName() << 2 /*variable and function*/; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 821 | return; |
| 822 | } |
| 823 | |
| 824 | // Merge should handle any subsequent violations. |
| 825 | if (isDef) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 826 | S.Diag(Attr.getLoc(), |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 827 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 828 | << "weak_import" << 2 /*variable and function*/; |
| 829 | return; |
| 830 | } |
| 831 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 832 | D->addAttr(::new (S.Context) WeakImportAttr()); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 835 | static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr, |
| 836 | Sema &S) { |
| 837 | // Attribute has 3 arguments. |
| 838 | if (Attr.getNumArgs() != 3) { |
| 839 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 840 | return; |
| 841 | } |
| 842 | |
| 843 | unsigned WGSize[3]; |
| 844 | for (unsigned i = 0; i < 3; ++i) { |
| 845 | Expr *E = static_cast<Expr *>(Attr.getArg(i)); |
| 846 | llvm::APSInt ArgNum(32); |
| 847 | if (!E->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 848 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 849 | << "reqd_work_group_size" << E->getSourceRange(); |
| 850 | return; |
| 851 | } |
| 852 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 853 | } |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 854 | D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(WGSize[0], WGSize[1], |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 855 | WGSize[2])); |
| 856 | } |
| 857 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 858 | static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 859 | // Attribute has no arguments. |
| 860 | if (Attr.getNumArgs() != 1) { |
| 861 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 862 | return; |
| 863 | } |
| 864 | |
| 865 | // Make sure that there is a string literal as the sections's single |
| 866 | // argument. |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 867 | Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 868 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 869 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 870 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 871 | return; |
| 872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 873 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 874 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | bb377ed | 2009-11-30 17:08:26 +0000 | [diff] [blame] | 875 | std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 876 | if (!Error.empty()) { |
| 877 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 878 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 879 | return; |
| 880 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 881 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 882 | // This attribute cannot be applied to local variables. |
| 883 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 884 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 885 | return; |
| 886 | } |
| 887 | |
Ted Kremenek | 3d2c43e | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 888 | D->addAttr(::new (S.Context) SectionAttr(S.Context, SE->getString())); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 892 | static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 893 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 894 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 895 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 896 | return; |
| 897 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 898 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 899 | d->addAttr(::new (S.Context) NoThrowAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 902 | static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 903 | // check the attribute arguments. |
| 904 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 905 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 906 | return; |
| 907 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 908 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 909 | d->addAttr(::new (S.Context) ConstAttr()); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 913 | // check the attribute arguments. |
| 914 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 915 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 916 | return; |
| 917 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 918 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 919 | d->addAttr(::new (S.Context) PureAttr()); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 922 | static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 923 | if (!Attr.getParameterName()) { |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 924 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 925 | return; |
| 926 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 927 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 928 | if (Attr.getNumArgs() != 0) { |
| 929 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 930 | return; |
| 931 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 932 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 933 | VarDecl *VD = dyn_cast<VarDecl>(d); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 934 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 935 | if (!VD || !VD->hasLocalStorage()) { |
| 936 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 937 | return; |
| 938 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 939 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 940 | // Look up the function |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 941 | NamedDecl *CleanupDecl |
| 942 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 943 | Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 944 | if (!CleanupDecl) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 945 | S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 946 | Attr.getParameterName(); |
| 947 | return; |
| 948 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 949 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 950 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 951 | if (!FD) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 952 | S.Diag(Attr.getLoc(), diag::err_attribute_cleanup_arg_not_function) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 953 | Attr.getParameterName(); |
| 954 | return; |
| 955 | } |
| 956 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 957 | if (FD->getNumParams() != 1) { |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 958 | 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] | 959 | Attr.getParameterName(); |
| 960 | return; |
| 961 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 962 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 963 | // We're currently more strict than GCC about what function types we accept. |
| 964 | // If this ever proves to be a problem it should be easy to fix. |
| 965 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 966 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Eli Friedman | d5e3e8e | 2009-04-26 01:30:08 +0000 | [diff] [blame] | 967 | if (S.CheckAssignmentConstraints(ParamTy, Ty) != Sema::Compatible) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 968 | S.Diag(Attr.getLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 969 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 970 | Attr.getParameterName() << ParamTy << Ty; |
| 971 | return; |
| 972 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 973 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 974 | d->addAttr(::new (S.Context) CleanupAttr(FD)); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 977 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
| 978 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
| 979 | static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 980 | if (Attr.getNumArgs() != 1) { |
| 981 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 982 | return; |
| 983 | } |
| 984 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
| 985 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 986 | << Attr.getName() << 0 /*function*/; |
| 987 | return; |
| 988 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 989 | // FIXME: in C++ the implicit 'this' function parameter also counts. this is |
| 990 | // 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] | 991 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
| 992 | unsigned FirstIdx = 1; |
| 993 | // checks for the 2nd argument |
| 994 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 995 | llvm::APSInt Idx(32); |
| 996 | if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
| 997 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 998 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 999 | return; |
| 1000 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1001 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1002 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 1003 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1004 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1005 | return; |
| 1006 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1007 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1008 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1009 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1010 | // make sure the format string is really a string |
| 1011 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1012 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1013 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 1014 | if (not_nsstring_type && |
| 1015 | !isCFStringType(Ty, S.Context) && |
| 1016 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1017 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1018 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1019 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1020 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1021 | << IdxExpr->getSourceRange(); |
| 1022 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1023 | } |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1024 | Ty = getFunctionOrMethodResultType(d); |
| 1025 | if (!isNSStringType(Ty, S.Context) && |
| 1026 | !isCFStringType(Ty, S.Context) && |
| 1027 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1028 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1029 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1030 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1031 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1032 | << IdxExpr->getSourceRange(); |
| 1033 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1036 | d->addAttr(::new (S.Context) FormatArgAttr(Idx.getZExtValue())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1039 | enum FormatAttrKind { |
| 1040 | CFStringFormat, |
| 1041 | NSStringFormat, |
| 1042 | StrftimeFormat, |
| 1043 | SupportedFormat, |
| 1044 | InvalidFormat |
| 1045 | }; |
| 1046 | |
| 1047 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 1048 | /// types. |
| 1049 | static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) { |
| 1050 | // Check for formats that get handled specially. |
| 1051 | if (Format == "NSString") |
| 1052 | return NSStringFormat; |
| 1053 | if (Format == "CFString") |
| 1054 | return CFStringFormat; |
| 1055 | if (Format == "strftime") |
| 1056 | return StrftimeFormat; |
| 1057 | |
| 1058 | // Otherwise, check for supported formats. |
| 1059 | if (Format == "scanf" || Format == "printf" || Format == "printf0" || |
| 1060 | Format == "strfmon" || Format == "cmn_err" || Format == "strftime" || |
| 1061 | Format == "NSString" || Format == "CFString" || Format == "vcmn_err" || |
| 1062 | Format == "zcmn_err") |
| 1063 | return SupportedFormat; |
| 1064 | |
| 1065 | return InvalidFormat; |
| 1066 | } |
| 1067 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1068 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
| 1069 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1070 | static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1071 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1072 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1073 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1074 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1075 | return; |
| 1076 | } |
| 1077 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1078 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1079 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1080 | return; |
| 1081 | } |
| 1082 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 1083 | if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1084 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1085 | << Attr.getName() << 0 /*function*/; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1086 | return; |
| 1087 | } |
| 1088 | |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1089 | unsigned NumArgs = getFunctionOrMethodNumArgs(d); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1090 | unsigned FirstIdx = 1; |
| 1091 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1092 | llvm::StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1093 | |
| 1094 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1095 | if (Format.startswith("__") && Format.endswith("__")) |
| 1096 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1097 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1098 | // Check for supported formats. |
| 1099 | FormatAttrKind Kind = getFormatAttrKind(Format); |
| 1100 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1101 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1102 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1103 | return; |
| 1104 | } |
| 1105 | |
| 1106 | // checks for the 2nd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1107 | Expr *IdxExpr = static_cast<Expr *>(Attr.getArg(0)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1108 | llvm::APSInt Idx(32); |
| 1109 | if (!IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1110 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1111 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1112 | return; |
| 1113 | } |
| 1114 | |
Anders Carlsson | 4fb7720 | 2009-08-25 14:12:34 +0000 | [diff] [blame] | 1115 | // FIXME: We should handle the implicit 'this' parameter in a more generic |
| 1116 | // way that can be used for other arguments. |
| 1117 | bool HasImplicitThisParam = false; |
| 1118 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(d)) { |
| 1119 | if (MD->isInstance()) { |
| 1120 | HasImplicitThisParam = true; |
| 1121 | NumArgs++; |
| 1122 | } |
| 1123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1124 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1125 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1126 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1127 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1128 | return; |
| 1129 | } |
| 1130 | |
| 1131 | // FIXME: Do we need to bounds check? |
| 1132 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1133 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1134 | if (HasImplicitThisParam) { |
| 1135 | if (ArgIdx == 0) { |
| 1136 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1137 | << "a string type" << IdxExpr->getSourceRange(); |
| 1138 | return; |
| 1139 | } |
| 1140 | ArgIdx--; |
| 1141 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1142 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1143 | // make sure the format string is really a string |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1144 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1145 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1146 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1147 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1148 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1149 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1150 | return; |
| 1151 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1152 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1153 | // FIXME: do we need to check if the type is NSString*? What are the |
| 1154 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1155 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1156 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1157 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1158 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1159 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1160 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1161 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1162 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1163 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1164 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1165 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1166 | return; |
| 1167 | } |
| 1168 | |
| 1169 | // check the 3rd argument |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1170 | Expr *FirstArgExpr = static_cast<Expr *>(Attr.getArg(1)); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1171 | llvm::APSInt FirstArg(32); |
| 1172 | if (!FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1173 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1174 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1175 | return; |
| 1176 | } |
| 1177 | |
| 1178 | // check if the function is variadic if the 3rd argument non-zero |
| 1179 | if (FirstArg != 0) { |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1180 | if (isFunctionOrMethodVariadic(d)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1181 | ++NumArgs; // +1 for ... |
| 1182 | } else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1183 | S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1184 | return; |
| 1185 | } |
| 1186 | } |
| 1187 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1188 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 1189 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1190 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1191 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1192 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 1193 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1194 | return; |
| 1195 | } |
| 1196 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 1197 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1198 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1199 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1200 | return; |
| 1201 | } |
| 1202 | |
Ted Kremenek | 3d2c43e | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 1203 | d->addAttr(::new (S.Context) FormatAttr(S.Context, Format, Idx.getZExtValue(), |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1204 | FirstArg.getZExtValue())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1207 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 1208 | Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1209 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1210 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1211 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1212 | return; |
| 1213 | } |
| 1214 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1215 | // Try to find the underlying union declaration. |
| 1216 | RecordDecl *RD = 0; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1217 | TypedefDecl *TD = dyn_cast<TypedefDecl>(d); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1218 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 1219 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 1220 | else |
| 1221 | RD = dyn_cast<RecordDecl>(d); |
| 1222 | |
| 1223 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1224 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1225 | << Attr.getName() << 1 /*union*/; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1226 | return; |
| 1227 | } |
| 1228 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1229 | if (!RD->isDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1230 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1231 | diag::warn_transparent_union_attribute_not_definition); |
| 1232 | return; |
| 1233 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1234 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1235 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 1236 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1237 | if (Field == FieldEnd) { |
| 1238 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 1239 | return; |
| 1240 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1241 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1242 | FieldDecl *FirstField = *Field; |
| 1243 | QualType FirstType = FirstField->getType(); |
| 1244 | if (FirstType->isFloatingType() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1245 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1246 | diag::warn_transparent_union_attribute_floating); |
| 1247 | return; |
| 1248 | } |
| 1249 | |
| 1250 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 1251 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 1252 | for (; Field != FieldEnd; ++Field) { |
| 1253 | QualType FieldType = Field->getType(); |
| 1254 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 1255 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 1256 | // Warn if we drop the attribute. |
| 1257 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1258 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1259 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1260 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1261 | diag::warn_transparent_union_attribute_field_size_align) |
| 1262 | << isSize << Field->getDeclName() << FieldBits; |
| 1263 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1264 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1265 | diag::note_transparent_union_first_field_size_align) |
| 1266 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1267 | return; |
| 1268 | } |
| 1269 | } |
| 1270 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1271 | RD->addAttr(::new (S.Context) TransparentUnionAttr()); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1274 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1275 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1276 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1277 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1278 | return; |
| 1279 | } |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1280 | Expr *ArgExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1281 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1282 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1283 | // Make sure that there is a string literal as the annotation's single |
| 1284 | // argument. |
| 1285 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1286 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1287 | return; |
| 1288 | } |
Ted Kremenek | 3d2c43e | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 1289 | d->addAttr(::new (S.Context) AnnotateAttr(S.Context, SE->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1292 | static void HandleAlignedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1293 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1294 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1295 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1296 | return; |
| 1297 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1298 | |
| 1299 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 1300 | // than GNU's, and should error out when it is used to specify a |
| 1301 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1302 | |
| 1303 | unsigned Align = 0; |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1304 | if (Attr.getNumArgs() == 0) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1305 | // FIXME: This should be the target specific maximum alignment. |
Daniel Dunbar | 7549c55 | 2009-02-18 20:06:09 +0000 | [diff] [blame] | 1306 | // (For now we just use 128 bits which is the maximum on X86). |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1307 | Align = 128; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1308 | d->addAttr(::new (S.Context) AlignedAttr(Align)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1309 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1310 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1311 | |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 1312 | Expr *alignmentExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1313 | llvm::APSInt Alignment(32); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1314 | if (!alignmentExpr->isIntegerConstantExpr(Alignment, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1315 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1316 | << "aligned" << alignmentExpr->getSourceRange(); |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 1317 | return; |
| 1318 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 1319 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1320 | S.Diag(Attr.getLoc(), diag::err_attribute_aligned_not_power_of_two) |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 1321 | << alignmentExpr->getSourceRange(); |
| 1322 | return; |
| 1323 | } |
| 1324 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1325 | d->addAttr(::new (S.Context) AlignedAttr(Alignment.getZExtValue() * 8)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1326 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1327 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1328 | /// HandleModeAttr - This attribute modifies the width of a decl with primitive |
| 1329 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1330 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1331 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 1332 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 1333 | /// HImode, not an intermediate pointer. |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1334 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1335 | // This attribute isn't documented, but glibc uses it. It changes |
| 1336 | // the width of an int or unsigned int to the specified size. |
| 1337 | |
| 1338 | // Check that there aren't any arguments |
| 1339 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1340 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | IdentifierInfo *Name = Attr.getParameterName(); |
| 1345 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1346 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1347 | return; |
| 1348 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1349 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1350 | llvm::StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1351 | |
| 1352 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1353 | if (Str.startswith("__") && Str.endswith("__")) |
| 1354 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1355 | |
| 1356 | unsigned DestWidth = 0; |
| 1357 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1358 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1359 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1360 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1361 | switch (Str[0]) { |
| 1362 | case 'Q': DestWidth = 8; break; |
| 1363 | case 'H': DestWidth = 16; break; |
| 1364 | case 'S': DestWidth = 32; break; |
| 1365 | case 'D': DestWidth = 64; break; |
| 1366 | case 'X': DestWidth = 96; break; |
| 1367 | case 'T': DestWidth = 128; break; |
| 1368 | } |
| 1369 | if (Str[1] == 'F') { |
| 1370 | IntegerMode = false; |
| 1371 | } else if (Str[1] == 'C') { |
| 1372 | IntegerMode = false; |
| 1373 | ComplexMode = true; |
| 1374 | } else if (Str[1] != 'I') { |
| 1375 | DestWidth = 0; |
| 1376 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1377 | break; |
| 1378 | case 4: |
| 1379 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 1380 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1381 | if (Str == "word") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1382 | DestWidth = S.Context.Target.getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1383 | else if (Str == "byte") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1384 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1385 | break; |
| 1386 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 1387 | if (Str == "pointer") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1388 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1389 | break; |
| 1390 | } |
| 1391 | |
| 1392 | QualType OldTy; |
| 1393 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 1394 | OldTy = TD->getUnderlyingType(); |
| 1395 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 1396 | OldTy = VD->getType(); |
| 1397 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1398 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 1399 | << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1400 | return; |
| 1401 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1402 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1403 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1404 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 1405 | else if (IntegerMode) { |
| 1406 | if (!OldTy->isIntegralType()) |
| 1407 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1408 | } else if (ComplexMode) { |
| 1409 | if (!OldTy->isComplexType()) |
| 1410 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1411 | } else { |
| 1412 | if (!OldTy->isFloatingType()) |
| 1413 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 1414 | } |
| 1415 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1416 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 1417 | // and friends, at least with glibc. |
| 1418 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 1419 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1420 | // FIXME: Make sure floating-point mappings are accurate |
| 1421 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1422 | QualType NewTy; |
| 1423 | switch (DestWidth) { |
| 1424 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1425 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1426 | return; |
| 1427 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1428 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1429 | return; |
| 1430 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1431 | if (!IntegerMode) { |
| 1432 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1433 | return; |
| 1434 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1435 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1436 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1437 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1438 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1439 | break; |
| 1440 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1441 | if (!IntegerMode) { |
| 1442 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1443 | return; |
| 1444 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1445 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1446 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1447 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1448 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1449 | break; |
| 1450 | case 32: |
| 1451 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1452 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1453 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1454 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1455 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1456 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1457 | break; |
| 1458 | case 64: |
| 1459 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1460 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1461 | else if (OldTy->isSignedIntegerType()) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 1462 | if (S.Context.Target.getLongWidth() == 64) |
| 1463 | NewTy = S.Context.LongTy; |
| 1464 | else |
| 1465 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1466 | else |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 1467 | if (S.Context.Target.getLongWidth() == 64) |
| 1468 | NewTy = S.Context.UnsignedLongTy; |
| 1469 | else |
| 1470 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1471 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1472 | case 96: |
| 1473 | NewTy = S.Context.LongDoubleTy; |
| 1474 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 1475 | case 128: |
| 1476 | if (!IntegerMode) { |
| 1477 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 1478 | return; |
| 1479 | } |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 1480 | if (OldTy->isSignedIntegerType()) |
| 1481 | NewTy = S.Context.Int128Ty; |
| 1482 | else |
| 1483 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1484 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 1487 | if (ComplexMode) { |
| 1488 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | // Install the new type. |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 1492 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 1493 | // FIXME: preserve existing source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1494 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 1495 | } else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 1496 | cast<ValueDecl>(D)->setType(NewTy); |
| 1497 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1498 | |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 1499 | static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1500 | // check the attribute arguments. |
| 1501 | if (Attr.getNumArgs() > 0) { |
| 1502 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1503 | return; |
| 1504 | } |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 1505 | |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1506 | if (!isFunctionOrMethod(d)) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1507 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1508 | << Attr.getName() << 0 /*function*/; |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1509 | return; |
| 1510 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1511 | |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 1512 | d->addAttr(::new (S.Context) NoDebugAttr()); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 1515 | static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1516 | // check the attribute arguments. |
| 1517 | if (Attr.getNumArgs() != 0) { |
| 1518 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1519 | return; |
| 1520 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1521 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1522 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1523 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1524 | << Attr.getName() << 0 /*function*/; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1525 | return; |
| 1526 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1527 | |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 1528 | d->addAttr(::new (S.Context) NoInlineAttr()); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1531 | static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1532 | // check the attribute arguments. |
| 1533 | if (Attr.getNumArgs() != 0) { |
| 1534 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1535 | return; |
| 1536 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1537 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1538 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
| 1539 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1540 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1541 | << Attr.getName() << 0 /*function*/; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1542 | return; |
| 1543 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1544 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 1545 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 1546 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 1547 | return; |
| 1548 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1549 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1550 | d->addAttr(::new (S.Context) GNUInlineAttr()); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1553 | static void HandleRegparmAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1554 | // check the attribute arguments. |
| 1555 | if (Attr.getNumArgs() != 1) { |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1556 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1557 | return; |
| 1558 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1559 | |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1560 | if (!isFunctionOrMethod(d)) { |
| 1561 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1562 | << Attr.getName() << 0 /*function*/; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1563 | return; |
| 1564 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1565 | |
| 1566 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArg(0)); |
| 1567 | llvm::APSInt NumParams(32); |
| 1568 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 1569 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1570 | << "regparm" << NumParamsExpr->getSourceRange(); |
| 1571 | return; |
| 1572 | } |
| 1573 | |
Anton Korobeynikov | 264a76c | 2009-04-03 23:38:25 +0000 | [diff] [blame] | 1574 | if (S.Context.Target.getRegParmMax() == 0) { |
| 1575 | S.Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1576 | << NumParamsExpr->getSourceRange(); |
| 1577 | return; |
| 1578 | } |
| 1579 | |
Anton Korobeynikov | 348f28a | 2009-04-04 10:27:50 +0000 | [diff] [blame] | 1580 | if (NumParams.getLimitedValue(255) > S.Context.Target.getRegParmMax()) { |
Anton Korobeynikov | 264a76c | 2009-04-03 23:38:25 +0000 | [diff] [blame] | 1581 | S.Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
| 1582 | << S.Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 1583 | return; |
| 1584 | } |
| 1585 | |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1586 | d->addAttr(::new (S.Context) RegparmAttr(NumParams.getZExtValue())); |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1589 | static void HandleFinalAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1590 | // check the attribute arguments. |
| 1591 | if (Attr.getNumArgs() != 0) { |
| 1592 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1593 | return; |
| 1594 | } |
| 1595 | |
| 1596 | if (!isa<CXXRecordDecl>(d) |
| 1597 | && (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual())) { |
| 1598 | S.Diag(Attr.getLoc(), |
| 1599 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 1600 | : diag::warn_attribute_wrong_decl_type) |
| 1601 | << Attr.getName() << 7 /*virtual method or class*/; |
| 1602 | return; |
| 1603 | } |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1604 | |
| 1605 | // FIXME: Conform to C++0x redeclaration rules. |
| 1606 | |
| 1607 | if (d->getAttr<FinalAttr>()) { |
| 1608 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "final"; |
| 1609 | return; |
| 1610 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1611 | |
| 1612 | d->addAttr(::new (S.Context) FinalAttr()); |
| 1613 | } |
| 1614 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1615 | //===----------------------------------------------------------------------===// |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1616 | // C++0x member checking attributes |
| 1617 | //===----------------------------------------------------------------------===// |
| 1618 | |
| 1619 | static void HandleBaseCheckAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1620 | if (Attr.getNumArgs() != 0) { |
| 1621 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1622 | return; |
| 1623 | } |
| 1624 | |
| 1625 | if (!isa<CXXRecordDecl>(d)) { |
| 1626 | S.Diag(Attr.getLoc(), |
| 1627 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 1628 | : diag::warn_attribute_wrong_decl_type) |
| 1629 | << Attr.getName() << 9 /*class*/; |
| 1630 | return; |
| 1631 | } |
| 1632 | |
| 1633 | if (d->getAttr<BaseCheckAttr>()) { |
| 1634 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "base_check"; |
| 1635 | return; |
| 1636 | } |
| 1637 | |
| 1638 | d->addAttr(::new (S.Context) BaseCheckAttr()); |
| 1639 | } |
| 1640 | |
| 1641 | static void HandleHidingAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1642 | if (Attr.getNumArgs() != 0) { |
| 1643 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1644 | return; |
| 1645 | } |
| 1646 | |
| 1647 | if (!isa<RecordDecl>(d->getDeclContext())) { |
| 1648 | // FIXME: It's not the type that's the problem |
| 1649 | S.Diag(Attr.getLoc(), |
| 1650 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 1651 | : diag::warn_attribute_wrong_decl_type) |
| 1652 | << Attr.getName() << 11 /*member*/; |
| 1653 | return; |
| 1654 | } |
| 1655 | |
| 1656 | // FIXME: Conform to C++0x redeclaration rules. |
| 1657 | |
| 1658 | if (d->getAttr<HidingAttr>()) { |
| 1659 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "hiding"; |
| 1660 | return; |
| 1661 | } |
| 1662 | |
| 1663 | d->addAttr(::new (S.Context) HidingAttr()); |
| 1664 | } |
| 1665 | |
| 1666 | static void HandleOverrideAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1667 | if (Attr.getNumArgs() != 0) { |
| 1668 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1669 | return; |
| 1670 | } |
| 1671 | |
| 1672 | if (!isa<CXXMethodDecl>(d) || !cast<CXXMethodDecl>(d)->isVirtual()) { |
| 1673 | // FIXME: It's not the type that's the problem |
| 1674 | S.Diag(Attr.getLoc(), |
| 1675 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 1676 | : diag::warn_attribute_wrong_decl_type) |
| 1677 | << Attr.getName() << 10 /*virtual method*/; |
| 1678 | return; |
| 1679 | } |
| 1680 | |
| 1681 | // FIXME: Conform to C++0x redeclaration rules. |
| 1682 | |
| 1683 | if (d->getAttr<OverrideAttr>()) { |
| 1684 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "override"; |
| 1685 | return; |
| 1686 | } |
| 1687 | |
| 1688 | d->addAttr(::new (S.Context) OverrideAttr()); |
| 1689 | } |
| 1690 | |
| 1691 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1692 | // Checker-specific attribute handlers. |
| 1693 | //===----------------------------------------------------------------------===// |
| 1694 | |
| 1695 | static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &Attr, |
| 1696 | Sema &S) { |
| 1697 | |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1698 | QualType RetTy; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1699 | |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1700 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) |
| 1701 | RetTy = MD->getResultType(); |
| 1702 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) |
| 1703 | RetTy = FD->getResultType(); |
| 1704 | else { |
Ted Kremenek | 21531fa | 2009-08-19 23:56:48 +0000 | [diff] [blame] | 1705 | SourceLocation L = Attr.getLoc(); |
| 1706 | S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) |
| 1707 | << SourceRange(L, L) << Attr.getName() << 3 /* function or method */; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1708 | return; |
| 1709 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1710 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1711 | if (!(S.Context.isObjCNSObjectType(RetTy) || RetTy->getAs<PointerType>() |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1712 | || RetTy->getAs<ObjCObjectPointerType>())) { |
Ted Kremenek | 21531fa | 2009-08-19 23:56:48 +0000 | [diff] [blame] | 1713 | SourceLocation L = Attr.getLoc(); |
| 1714 | S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 1715 | << SourceRange(L, L) << Attr.getName(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1716 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 1717 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1718 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1719 | switch (Attr.getKind()) { |
| 1720 | default: |
| 1721 | assert(0 && "invalid ownership attribute"); |
| 1722 | return; |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 1723 | case AttributeList::AT_cf_returns_not_retained: |
| 1724 | d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr()); |
| 1725 | return; |
| 1726 | case AttributeList::AT_ns_returns_not_retained: |
| 1727 | d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr()); |
| 1728 | return; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1729 | case AttributeList::AT_cf_returns_retained: |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1730 | d->addAttr(::new (S.Context) CFReturnsRetainedAttr()); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1731 | return; |
| 1732 | case AttributeList::AT_ns_returns_retained: |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 1733 | d->addAttr(::new (S.Context) NSReturnsRetainedAttr()); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1734 | return; |
| 1735 | }; |
| 1736 | } |
| 1737 | |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 1738 | static bool isKnownDeclSpecAttr(const AttributeList &Attr) { |
| 1739 | return Attr.getKind() == AttributeList::AT_dllimport || |
| 1740 | Attr.getKind() == AttributeList::AT_dllexport; |
| 1741 | } |
| 1742 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1743 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1744 | // Top Level Sema Entry Points |
| 1745 | //===----------------------------------------------------------------------===// |
| 1746 | |
Sebastian Redl | a89d82c | 2008-12-21 19:24:58 +0000 | [diff] [blame] | 1747 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1748 | /// 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] | 1749 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 1750 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1751 | static void ProcessDeclAttribute(Scope *scope, Decl *D, |
| 1752 | const AttributeList &Attr, Sema &S) { |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 1753 | if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) |
| 1754 | // FIXME: Try to deal with other __declspec attributes! |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 1755 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1756 | switch (Attr.getKind()) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame^] | 1757 | case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break; |
| 1758 | case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1759 | case AttributeList::AT_address_space: |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 1760 | case AttributeList::AT_objc_gc: |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 1761 | case AttributeList::AT_vector_size: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1762 | // Ignore these, these are type attributes, handled by |
| 1763 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1764 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1765 | case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; |
| 1766 | case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1767 | case AttributeList::AT_always_inline: |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1768 | HandleAlwaysInlineAttr (D, Attr, S); break; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1769 | case AttributeList::AT_analyzer_noreturn: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1770 | HandleAnalyzerNoReturnAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1771 | case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; |
| 1772 | case AttributeList::AT_base_check: HandleBaseCheckAttr (D, Attr, S); break; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1773 | case AttributeList::AT_carries_dependency: |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1774 | HandleDependencyAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1775 | case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break; |
| 1776 | case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break; |
| 1777 | case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1778 | case AttributeList::AT_ext_vector_type: |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1779 | HandleExtVectorTypeAttr(scope, D, Attr, S); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1780 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1781 | case AttributeList::AT_final: HandleFinalAttr (D, Attr, S); break; |
| 1782 | case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; |
| 1783 | case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break; |
| 1784 | case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break; |
| 1785 | case AttributeList::AT_hiding: HandleHidingAttr (D, Attr, S); break; |
| 1786 | case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; |
| 1787 | case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break; |
| 1788 | case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; |
| 1789 | case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; |
| 1790 | case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; |
| 1791 | case AttributeList::AT_override: HandleOverrideAttr (D, Attr, S); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1792 | |
| 1793 | // Checker-specific. |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 1794 | case AttributeList::AT_ns_returns_not_retained: |
| 1795 | case AttributeList::AT_cf_returns_not_retained: |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 1796 | case AttributeList::AT_ns_returns_retained: |
| 1797 | case AttributeList::AT_cf_returns_retained: |
| 1798 | HandleNSReturnsRetainedAttr(D, Attr, S); break; |
| 1799 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1800 | case AttributeList::AT_reqd_wg_size: |
| 1801 | HandleReqdWorkGroupSize(D, Attr, S); break; |
| 1802 | |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1803 | case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; |
| 1804 | case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1805 | case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break; |
| 1806 | case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; |
| 1807 | case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1808 | case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1809 | case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S); |
| 1810 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1811 | case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; |
| 1812 | case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1813 | case AttributeList::AT_transparent_union: |
| 1814 | HandleTransparentUnionAttr(D, Attr, S); |
| 1815 | break; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1816 | case AttributeList::AT_objc_exception: |
| 1817 | HandleObjCExceptionAttr(D, Attr, S); |
| 1818 | break; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1819 | case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 1820 | case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break; |
| 1821 | case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break; |
| 1822 | case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break; |
| 1823 | case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break; |
| 1824 | case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break; |
| 1825 | case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break; |
| 1826 | case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break; |
| 1827 | case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break; |
| 1828 | case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1829 | case AttributeList::IgnoredAttribute: |
Chris Lattner | 5e20448 | 2009-04-25 18:44:54 +0000 | [diff] [blame] | 1830 | case AttributeList::AT_no_instrument_function: // Interacts with -pg. |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 1831 | // Just ignore |
| 1832 | break; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 1833 | case AttributeList::AT_stdcall: |
| 1834 | case AttributeList::AT_cdecl: |
| 1835 | case AttributeList::AT_fastcall: |
| 1836 | // These are all treated as type attributes. |
| 1837 | break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1838 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 1839 | // Ask target about the attribute. |
| 1840 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 1841 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
| 1842 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1843 | break; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 1848 | /// attribute list to the specified decl, ignoring any type attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1849 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, const AttributeList *AttrList) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1850 | while (AttrList) { |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1851 | ProcessDeclAttribute(S, D, *AttrList, *this); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1852 | AttrList = AttrList->getNext(); |
| 1853 | } |
| 1854 | } |
| 1855 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 1856 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
| 1857 | /// #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] | 1858 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 1859 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 1860 | NamedDecl *NewD = 0; |
| 1861 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 1862 | NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 1863 | FD->getLocation(), DeclarationName(II), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1864 | FD->getType(), FD->getTypeSourceInfo()); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 1865 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 1866 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
| 1867 | VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 1868 | VD->getType(), VD->getTypeSourceInfo(), |
Argyrios Kyrtzidis | a1d5662 | 2009-08-19 01:27:57 +0000 | [diff] [blame] | 1869 | VD->getStorageClass()); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 1870 | } |
| 1871 | return NewD; |
| 1872 | } |
| 1873 | |
| 1874 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak |
| 1875 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 1876 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 1877 | if (W.getUsed()) return; // only do this once |
| 1878 | W.setUsed(true); |
| 1879 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 1880 | IdentifierInfo *NDId = ND->getIdentifier(); |
| 1881 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias()); |
Ted Kremenek | 3d2c43e | 2010-02-11 05:28:37 +0000 | [diff] [blame] | 1882 | NewD->addAttr(::new (Context) AliasAttr(Context, NDId->getName())); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 1883 | NewD->addAttr(::new (Context) WeakAttr()); |
| 1884 | WeakTopLevelDecl.push_back(NewD); |
| 1885 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 1886 | // to insert Decl at TU scope, sorry. |
| 1887 | DeclContext *SavedContext = CurContext; |
| 1888 | CurContext = Context.getTranslationUnitDecl(); |
| 1889 | PushOnScopeChains(NewD, S); |
| 1890 | CurContext = SavedContext; |
| 1891 | } else { // just add weak to existing |
| 1892 | ND->addAttr(::new (Context) WeakAttr()); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 1893 | } |
| 1894 | } |
| 1895 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1896 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 1897 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 1898 | /// 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] | 1899 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 1900 | // Handle #pragma weak |
| 1901 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 1902 | if (ND->hasLinkage()) { |
| 1903 | WeakInfo W = WeakUndeclaredIdentifiers.lookup(ND->getIdentifier()); |
| 1904 | if (W != WeakInfo()) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 1905 | // Identifier referenced by #pragma weak before it was declared |
| 1906 | DeclApplyPragmaWeak(S, ND, W); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 1907 | WeakUndeclaredIdentifiers[ND->getIdentifier()] = W; |
| 1908 | } |
| 1909 | } |
| 1910 | } |
| 1911 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1912 | // Apply decl attributes from the DeclSpec if present. |
| 1913 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes()) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1914 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1915 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1916 | // Walk the declarator structure, applying decl attributes that were in a type |
| 1917 | // position to the decl itself. This handles cases like: |
| 1918 | // int *__attr__(x)** D; |
| 1919 | // when X is a decl attribute. |
| 1920 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 1921 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1922 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1923 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1924 | // Finally, apply any attributes on the decl itself. |
| 1925 | if (const AttributeList *Attrs = PD.getAttributes()) |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 1926 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 1927 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1928 | |
| 1929 | /// PushParsingDeclaration - Enter a new "scope" of deprecation |
| 1930 | /// warnings. |
| 1931 | /// |
| 1932 | /// The state token we use is the start index of this scope |
| 1933 | /// on the warning stack. |
| 1934 | Action::ParsingDeclStackState Sema::PushParsingDeclaration() { |
| 1935 | ParsingDeclDepth++; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 1936 | return (ParsingDeclStackState) DelayedDiagnostics.size(); |
| 1937 | } |
| 1938 | |
| 1939 | void Sema::PopParsingDeclaration(ParsingDeclStackState S, DeclPtrTy Ctx) { |
| 1940 | assert(ParsingDeclDepth > 0 && "empty ParsingDeclaration stack"); |
| 1941 | ParsingDeclDepth--; |
| 1942 | |
| 1943 | if (DelayedDiagnostics.empty()) |
| 1944 | return; |
| 1945 | |
| 1946 | unsigned SavedIndex = (unsigned) S; |
| 1947 | assert(SavedIndex <= DelayedDiagnostics.size() && |
| 1948 | "saved index is out of bounds"); |
| 1949 | |
| 1950 | // We only want to actually emit delayed diagnostics when we |
| 1951 | // successfully parsed a decl. |
| 1952 | Decl *D = Ctx ? Ctx.getAs<Decl>() : 0; |
| 1953 | if (D) { |
| 1954 | // We really do want to start with 0 here. We get one push for a |
| 1955 | // decl spec and another for each declarator; in a decl group like: |
| 1956 | // deprecated_typedef foo, *bar, baz(); |
| 1957 | // only the declarator pops will be passed decls. This is correct; |
| 1958 | // we really do need to consider delayed diagnostics from the decl spec |
| 1959 | // for each of the different declarations. |
| 1960 | for (unsigned I = 0, E = DelayedDiagnostics.size(); I != E; ++I) { |
| 1961 | if (DelayedDiagnostics[I].Triggered) |
| 1962 | continue; |
| 1963 | |
| 1964 | switch (DelayedDiagnostics[I].Kind) { |
| 1965 | case DelayedDiagnostic::Deprecation: |
| 1966 | HandleDelayedDeprecationCheck(DelayedDiagnostics[I], D); |
| 1967 | break; |
| 1968 | |
| 1969 | case DelayedDiagnostic::Access: |
| 1970 | HandleDelayedAccessCheck(DelayedDiagnostics[I], D); |
| 1971 | break; |
| 1972 | } |
| 1973 | } |
| 1974 | } |
| 1975 | |
| 1976 | DelayedDiagnostics.set_size(SavedIndex); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
| 1979 | static bool isDeclDeprecated(Decl *D) { |
| 1980 | do { |
| 1981 | if (D->hasAttr<DeprecatedAttr>()) |
| 1982 | return true; |
| 1983 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 1984 | return false; |
| 1985 | } |
| 1986 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 1987 | void Sema::HandleDelayedDeprecationCheck(Sema::DelayedDiagnostic &DD, |
| 1988 | Decl *Ctx) { |
| 1989 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1990 | return; |
| 1991 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 1992 | DD.Triggered = true; |
| 1993 | Diag(DD.Loc, diag::warn_deprecated) |
| 1994 | << DD.DeprecationData.Decl->getDeclName(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | void Sema::EmitDeprecationWarning(NamedDecl *D, SourceLocation Loc) { |
| 1998 | // Delay if we're currently parsing a declaration. |
| 1999 | if (ParsingDeclDepth) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 2000 | DelayedDiagnostics.push_back(DelayedDiagnostic::makeDeprecation(Loc, D)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2001 | return; |
| 2002 | } |
| 2003 | |
| 2004 | // Otherwise, don't warn if our current context is deprecated. |
| 2005 | if (isDeclDeprecated(cast<Decl>(CurContext))) |
| 2006 | return; |
| 2007 | |
| 2008 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 2009 | } |