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