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