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