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