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