Chris Lattner | 2c6fcf5 | 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 | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetAttributesSema.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/Expr.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 21 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 22 | #include "clang/Sema/DelayedDiagnostic.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 24 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 25 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 26 | |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 27 | /// These constants match the enumerated choices of |
| 28 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
| 29 | enum { |
| 30 | ExpectedFunction, |
| 31 | ExpectedUnion, |
| 32 | ExpectedVariableOrFunction, |
| 33 | ExpectedFunctionOrMethod, |
| 34 | ExpectedParameter, |
| 35 | ExpectedParameterOrMethod, |
| 36 | ExpectedFunctionMethodOrBlock, |
| 37 | ExpectedClassOrVirtualMethod, |
| 38 | ExpectedFunctionMethodOrParameter, |
| 39 | ExpectedClass, |
| 40 | ExpectedVirtualMethod, |
| 41 | ExpectedClassMember, |
| 42 | ExpectedVariable, |
| 43 | ExpectedMethod, |
| 44 | ExpectedVariableFunctionOrLabel |
| 45 | }; |
| 46 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // Helper functions |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 51 | static const FunctionType *getFunctionType(const Decl *d, |
| 52 | bool blocksToo = true) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 53 | QualType Ty; |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 54 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(d)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 55 | Ty = decl->getType(); |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 56 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 57 | Ty = decl->getType(); |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 58 | else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 59 | Ty = decl->getUnderlyingType(); |
| 60 | else |
| 61 | return 0; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 63 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 64 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 28c433d | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 65 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 66 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 67 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 68 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 71 | // FIXME: We should provide an abstraction around a method or function |
| 72 | // to provide the following bits of information. |
| 73 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 74 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 75 | /// type (function or function-typed variable). |
| 76 | static bool isFunction(const Decl *d) { |
| 77 | return getFunctionType(d, false) != NULL; |
| 78 | } |
| 79 | |
| 80 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 81 | /// type (function or function-typed variable) or an Objective-C |
| 82 | /// method. |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 83 | static bool isFunctionOrMethod(const Decl *d) { |
| 84 | return isFunction(d)|| isa<ObjCMethodDecl>(d); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 87 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 88 | /// type (function or function-typed variable) or an Objective-C |
| 89 | /// method or a block. |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 90 | static bool isFunctionOrMethodOrBlock(const Decl *d) { |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 91 | if (isFunctionOrMethod(d)) |
| 92 | return true; |
| 93 | // check for block is more involved. |
| 94 | if (const VarDecl *V = dyn_cast<VarDecl>(d)) { |
| 95 | QualType Ty = V->getType(); |
| 96 | return Ty->isBlockPointerType(); |
| 97 | } |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 98 | return isa<BlockDecl>(d); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 99 | } |
| 100 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 101 | /// Return true if the given decl has a declarator that should have |
| 102 | /// been processed by Sema::GetTypeForDeclarator. |
| 103 | static bool hasDeclarator(const Decl *d) { |
| 104 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
| 105 | return isa<DeclaratorDecl>(d) || isa<BlockDecl>(d) || isa<TypedefDecl>(d); |
| 106 | } |
| 107 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 108 | /// hasFunctionProto - Return true if the given decl has a argument |
| 109 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 110 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 111 | static bool hasFunctionProto(const Decl *d) { |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 112 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 113 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 114 | else { |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 115 | assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d)); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 121 | /// arguments. It is an error to call this on a K&R function (use |
| 122 | /// hasFunctionProto first). |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 123 | static unsigned getFunctionOrMethodNumArgs(const Decl *d) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 124 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 125 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 126 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) |
| 127 | return BD->getNumParams(); |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 128 | return cast<ObjCMethodDecl>(d)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 131 | static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) { |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 132 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 133 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 134 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) |
| 135 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 136 | |
Chris Lattner | a499715 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 137 | return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 140 | static QualType getFunctionOrMethodResultType(const Decl *d) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 141 | if (const FunctionType *FnTy = getFunctionType(d)) |
| 142 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
| 143 | return cast<ObjCMethodDecl>(d)->getResultType(); |
| 144 | } |
| 145 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 146 | static bool isFunctionOrMethodVariadic(const Decl *d) { |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 147 | if (const FunctionType *FnTy = getFunctionType(d)) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 148 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 149 | return proto->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 150 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 151 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 152 | else { |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 153 | return cast<ObjCMethodDecl>(d)->isVariadic(); |
| 154 | } |
| 155 | } |
| 156 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 157 | static bool isInstanceMethod(const Decl *d) { |
| 158 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(d)) |
| 159 | return MethodDecl->isInstance(); |
| 160 | return false; |
| 161 | } |
| 162 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 163 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 164 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 165 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 166 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 167 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 168 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 169 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 170 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 171 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 172 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 174 | // FIXME: Should we walk the chain of classes? |
| 175 | return ClsName == &Ctx.Idents.get("NSString") || |
| 176 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 177 | } |
| 178 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 179 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 180 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 181 | if (!PT) |
| 182 | return false; |
| 183 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 184 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 185 | if (!RT) |
| 186 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 188 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 189 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 190 | return false; |
| 191 | |
| 192 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 193 | } |
| 194 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 195 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 196 | // Attribute Implementations |
| 197 | //===----------------------------------------------------------------------===// |
| 198 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 199 | // FIXME: All this manual attribute parsing code is gross. At the |
| 200 | // least add some helper functions to check most argument patterns (# |
| 201 | // and types of args). |
| 202 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 203 | static void HandleExtVectorTypeAttr(Scope *scope, Decl *d, |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 204 | const AttributeList &Attr, Sema &S) { |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 205 | TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d); |
| 206 | if (tDecl == 0) { |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 207 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 208 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 209 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 211 | QualType curType = tDecl->getUnderlyingType(); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 212 | |
| 213 | Expr *sizeExpr; |
| 214 | |
| 215 | // Special case where the argument is a template id. |
| 216 | if (Attr.getParameterName()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 217 | CXXScopeSpec SS; |
| 218 | UnqualifiedId id; |
| 219 | id.setIdentifier(Attr.getParameterName(), Attr.getLoc()); |
| 220 | sizeExpr = S.ActOnIdExpression(scope, SS, id, false, false).takeAs<Expr>(); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 221 | } else { |
| 222 | // check the attribute arguments. |
| 223 | if (Attr.getNumArgs() != 1) { |
| 224 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 225 | return; |
| 226 | } |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 227 | sizeExpr = Attr.getArg(0); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 228 | } |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 229 | |
| 230 | // Instantiate/Install the vector type, and let Sema build the type for us. |
| 231 | // This will run the reguired checks. |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 232 | QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc()); |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 233 | if (!T.isNull()) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 234 | // FIXME: preserve the old source info. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 235 | tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 237 | // Remember this typedef decl, we will need it later for diagnostics. |
| 238 | S.ExtVectorDecls.push_back(tDecl); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 242 | static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 243 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 244 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 245 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 246 | return; |
| 247 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 249 | if (TagDecl *TD = dyn_cast<TagDecl>(d)) |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 250 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 251 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(d)) { |
| 252 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 253 | // has no effect. |
| 254 | if (!FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 255 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 256 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 257 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 258 | else |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 259 | FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 260 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 261 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 264 | static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) { |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 265 | // check the attribute arguments. |
| 266 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 267 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 268 | return; |
| 269 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 271 | // The IBAction attributes only apply to instance methods. |
| 272 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) |
| 273 | if (MD->isInstanceMethod()) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 274 | d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
Ted Kremenek | d68ec81 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 278 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static void HandleIBOutlet(Decl *d, const AttributeList &Attr, Sema &S) { |
| 282 | // check the attribute arguments. |
| 283 | if (Attr.getNumArgs() > 0) { |
| 284 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | // The IBOutlet attributes only apply to instance variables of |
Ted Kremenek | 06be968 | 2010-02-17 02:37:45 +0000 | [diff] [blame] | 289 | // Objective-C classes. |
| 290 | if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 291 | d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 292 | return; |
Ted Kremenek | 06be968 | 2010-02-17 02:37:45 +0000 | [diff] [blame] | 293 | } |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | d68ec81 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 295 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 298 | static void HandleIBOutletCollection(Decl *d, const AttributeList &Attr, |
| 299 | Sema &S) { |
| 300 | |
| 301 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 302 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 303 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 304 | return; |
| 305 | } |
| 306 | |
| 307 | // The IBOutletCollection attributes only apply to instance variables of |
| 308 | // Objective-C classes. |
| 309 | if (!(isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d))) { |
Ted Kremenek | d68ec81 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 310 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 311 | return; |
| 312 | } |
Fariborz Jahanian | 798f832 | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 313 | if (const ValueDecl *VD = dyn_cast<ValueDecl>(d)) |
| 314 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
| 315 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type) |
| 316 | << VD->getType() << 0; |
| 317 | return; |
| 318 | } |
| 319 | if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(d)) |
| 320 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
| 321 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_object_type) |
| 322 | << PD->getType() << 1; |
| 323 | return; |
| 324 | } |
| 325 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 326 | IdentifierInfo *II = Attr.getParameterName(); |
| 327 | if (!II) |
| 328 | II = &S.Context.Idents.get("id"); |
Fariborz Jahanian | 798f832 | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 329 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 330 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 331 | S.getScopeForContext(d->getDeclContext()->getParent())); |
| 332 | if (!TypeRep) { |
| 333 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 334 | return; |
| 335 | } |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 336 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 337 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 338 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 339 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 340 | // treated as __attribute__((iboutletcollection())). |
| 341 | if (!QT->isObjCIdType() && !QT->isObjCClassType() && |
| 342 | !QT->isObjCObjectType()) { |
| 343 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 344 | return; |
| 345 | } |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 346 | d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context, |
| 347 | QT)); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 350 | static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 351 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 352 | // ignore it as well |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 353 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 354 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 355 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 356 | return; |
| 357 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 358 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 359 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 360 | // counted from one. |
| 361 | bool HasImplicitThisParam = isInstanceMethod(d); |
| 362 | unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 363 | |
| 364 | // The nonnull attribute only applies to pointers. |
| 365 | llvm::SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 366 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 367 | for (AttributeList::arg_iterator I=Attr.arg_begin(), |
| 368 | E=Attr.arg_end(); I!=E; ++I) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 369 | |
| 370 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 371 | // The argument must be an integer constant expression. |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 372 | Expr *Ex = *I; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 373 | llvm::APSInt ArgNum(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 374 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 375 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 376 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 377 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 378 | return; |
| 379 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 381 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 382 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 383 | if (x < 1 || x > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 384 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 91aea71 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 385 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 386 | return; |
| 387 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | 5224e6a | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 389 | --x; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 390 | if (HasImplicitThisParam) { |
| 391 | if (x == 0) { |
| 392 | S.Diag(Attr.getLoc(), |
| 393 | diag::err_attribute_invalid_implicit_this_argument) |
| 394 | << "nonnull" << Ex->getSourceRange(); |
| 395 | return; |
| 396 | } |
| 397 | --x; |
| 398 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 399 | |
| 400 | // Is the function argument a pointer type? |
Douglas Gregor | 1e2cdf5 | 2010-12-15 15:41:46 +0000 | [diff] [blame] | 401 | QualType T = getFunctionOrMethodArgType(d, x).getNonReferenceType(); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 402 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 403 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | 62157e5 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 404 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 405 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 406 | continue; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 407 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 408 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 409 | NonNullArgs.push_back(x); |
| 410 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 411 | |
| 412 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 413 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 414 | if (NonNullArgs.empty()) { |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 415 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) { |
Douglas Gregor | 1e2cdf5 | 2010-12-15 15:41:46 +0000 | [diff] [blame] | 416 | QualType T = getFunctionOrMethodArgType(d, I).getNonReferenceType(); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 417 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 418 | NonNullArgs.push_back(I); |
Fariborz Jahanian | 3567c42 | 2010-09-27 22:42:37 +0000 | [diff] [blame] | 419 | else if (const RecordType *UT = T->getAsUnionType()) { |
| 420 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 421 | RecordDecl *UD = UT->getDecl(); |
| 422 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 423 | itend = UD->field_end(); it != itend; ++it) { |
| 424 | T = it->getType(); |
| 425 | if (T->isAnyPointerType() || T->isBlockPointerType()) { |
| 426 | NonNullArgs.push_back(I); |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | } |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 432 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 433 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 434 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 435 | if (NonNullArgs.empty()) { |
| 436 | // Warn the trivial case only if attribute is not coming from a |
| 437 | // macro instantiation. |
| 438 | if (Attr.getLoc().isFileID()) |
| 439 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 440 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 441 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 442 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 443 | |
| 444 | unsigned* start = &NonNullArgs[0]; |
| 445 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 446 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 447 | d->addAttr(::new (S.Context) NonNullAttr(Attr.getLoc(), S.Context, start, |
| 448 | size)); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 451 | static void HandleOwnershipAttr(Decl *d, const AttributeList &AL, Sema &S) { |
| 452 | // This attribute must be applied to a function declaration. |
| 453 | // The first argument to the attribute must be a string, |
| 454 | // the name of the resource, for example "malloc". |
| 455 | // The following arguments must be argument indexes, the arguments must be |
| 456 | // of integer type for Returns, otherwise of pointer type. |
| 457 | // The difference between Holds and Takes is that a pointer may still be used |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 458 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 459 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 460 | |
| 461 | if (!AL.getParameterName()) { |
| 462 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 463 | << AL.getName()->getName() << 1; |
| 464 | return; |
| 465 | } |
| 466 | // Figure out our Kind, and check arguments while we're at it. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 467 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 468 | switch (AL.getKind()) { |
| 469 | case AttributeList::AT_ownership_takes: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 470 | K = OwnershipAttr::Takes; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 471 | if (AL.getNumArgs() < 1) { |
| 472 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 473 | return; |
| 474 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 475 | break; |
| 476 | case AttributeList::AT_ownership_holds: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 477 | K = OwnershipAttr::Holds; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 478 | if (AL.getNumArgs() < 1) { |
| 479 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 480 | return; |
| 481 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 482 | break; |
| 483 | case AttributeList::AT_ownership_returns: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 484 | K = OwnershipAttr::Returns; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 485 | if (AL.getNumArgs() > 1) { |
| 486 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 487 | << AL.getNumArgs() + 1; |
| 488 | return; |
| 489 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 490 | break; |
| 491 | default: |
| 492 | // This should never happen given how we are called. |
| 493 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | if (!isFunction(d) || !hasFunctionProto(d)) { |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 497 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 498 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 502 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 503 | // counted from one. |
| 504 | bool HasImplicitThisParam = isInstanceMethod(d); |
| 505 | unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 506 | |
| 507 | llvm::StringRef Module = AL.getParameterName()->getName(); |
| 508 | |
| 509 | // Normalize the argument, __foo__ becomes foo. |
| 510 | if (Module.startswith("__") && Module.endswith("__")) |
| 511 | Module = Module.substr(2, Module.size() - 4); |
| 512 | |
| 513 | llvm::SmallVector<unsigned, 10> OwnershipArgs; |
| 514 | |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 515 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 516 | ++I) { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 517 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 518 | Expr *IdxExpr = *I; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 519 | llvm::APSInt ArgNum(32); |
| 520 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 521 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 522 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 523 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 524 | continue; |
| 525 | } |
| 526 | |
| 527 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 528 | |
| 529 | if (x > NumArgs || x < 1) { |
| 530 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 531 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 532 | continue; |
| 533 | } |
| 534 | --x; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 535 | if (HasImplicitThisParam) { |
| 536 | if (x == 0) { |
| 537 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 538 | << "ownership" << IdxExpr->getSourceRange(); |
| 539 | return; |
| 540 | } |
| 541 | --x; |
| 542 | } |
| 543 | |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 544 | switch (K) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 545 | case OwnershipAttr::Takes: |
| 546 | case OwnershipAttr::Holds: { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 547 | // Is the function argument a pointer type? |
| 548 | QualType T = getFunctionOrMethodArgType(d, x); |
| 549 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 550 | // FIXME: Should also highlight argument in decl. |
| 551 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 552 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 553 | << "pointer" |
| 554 | << IdxExpr->getSourceRange(); |
| 555 | continue; |
| 556 | } |
| 557 | break; |
| 558 | } |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 559 | case OwnershipAttr::Returns: { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 560 | if (AL.getNumArgs() > 1) { |
| 561 | // Is the function argument an integer type? |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 562 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 563 | llvm::APSInt ArgNum(32); |
| 564 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 565 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 566 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 567 | << "ownership_returns" << "integer" |
| 568 | << IdxExpr->getSourceRange(); |
| 569 | return; |
| 570 | } |
| 571 | } |
| 572 | break; |
| 573 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 574 | default: |
| 575 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 576 | } // switch |
| 577 | |
| 578 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 579 | for (specific_attr_iterator<OwnershipAttr> |
| 580 | i = d->specific_attr_begin<OwnershipAttr>(), |
| 581 | e = d->specific_attr_end<OwnershipAttr>(); |
| 582 | i != e; ++i) { |
| 583 | if ((*i)->getOwnKind() != K) { |
| 584 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 585 | I!=E; ++I) { |
| 586 | if (x == *I) { |
| 587 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 588 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | OwnershipArgs.push_back(x); |
| 594 | } |
| 595 | |
| 596 | unsigned* start = OwnershipArgs.data(); |
| 597 | unsigned size = OwnershipArgs.size(); |
| 598 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 599 | |
| 600 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 601 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 602 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 603 | } |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 604 | |
| 605 | d->addAttr(::new (S.Context) OwnershipAttr(AL.getLoc(), S.Context, K, Module, |
| 606 | start, size)); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 607 | } |
| 608 | |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 609 | /// Whether this declaration has internal linkage for the purposes of |
| 610 | /// things that want to complain about things not have internal linkage. |
| 611 | static bool hasEffectivelyInternalLinkage(NamedDecl *D) { |
| 612 | switch (D->getLinkage()) { |
| 613 | case NoLinkage: |
| 614 | case InternalLinkage: |
| 615 | return true; |
| 616 | |
| 617 | // Template instantiations that go from external to unique-external |
| 618 | // shouldn't get diagnosed. |
| 619 | case UniqueExternalLinkage: |
| 620 | return true; |
| 621 | |
| 622 | case ExternalLinkage: |
| 623 | return false; |
| 624 | } |
| 625 | llvm_unreachable("unknown linkage kind!"); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 626 | return false; |
| 627 | } |
| 628 | |
| 629 | static void HandleWeakRefAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 630 | // Check the attribute arguments. |
| 631 | if (Attr.getNumArgs() > 1) { |
| 632 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 633 | return; |
| 634 | } |
| 635 | |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 636 | if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) { |
| 637 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 638 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | |
| 642 | NamedDecl *nd = cast<NamedDecl>(d); |
| 643 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 644 | // gcc rejects |
| 645 | // class c { |
| 646 | // static int a __attribute__((weakref ("v2"))); |
| 647 | // static int b() __attribute__((weakref ("f3"))); |
| 648 | // }; |
| 649 | // and ignores the attributes of |
| 650 | // void f(void) { |
| 651 | // static int a __attribute__((weakref ("v2"))); |
| 652 | // } |
| 653 | // we reject them |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 654 | const DeclContext *Ctx = d->getDeclContext()->getRedeclContext(); |
| 655 | if (!Ctx->isFileContext()) { |
| 656 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 657 | nd->getNameAsString(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 658 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // The GCC manual says |
| 662 | // |
| 663 | // At present, a declaration to which `weakref' is attached can only |
| 664 | // be `static'. |
| 665 | // |
| 666 | // It also says |
| 667 | // |
| 668 | // Without a TARGET, |
| 669 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 670 | // equivalent to `weak'. |
| 671 | // |
| 672 | // gcc 4.4.1 will accept |
| 673 | // int a7 __attribute__((weakref)); |
| 674 | // as |
| 675 | // int a7 __attribute__((weak)); |
| 676 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 677 | // it if this behaviour is actually used. |
| 678 | |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 679 | if (!hasEffectivelyInternalLinkage(nd)) { |
| 680 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 681 | return; |
| 682 | } |
| 683 | |
| 684 | // GCC rejects |
| 685 | // static ((alias ("y"), weakref)). |
| 686 | // Should we? How to check that weakref is before or after alias? |
| 687 | |
| 688 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 689 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 690 | Arg = Arg->IgnoreParenCasts(); |
| 691 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 692 | |
| 693 | if (Str == 0 || Str->isWide()) { |
| 694 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 695 | << "weakref" << 1; |
| 696 | return; |
| 697 | } |
| 698 | // GCC will accept anything as the argument of weakref. Should we |
| 699 | // check for an existing decl? |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 700 | d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, |
| 701 | Str->getString())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 704 | d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context)); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 707 | static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 708 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 709 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 710 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 711 | return; |
| 712 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 713 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 714 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 715 | Arg = Arg->IgnoreParenCasts(); |
| 716 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 718 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 719 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 720 | << "alias" << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 721 | return; |
| 722 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 723 | |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 724 | if (S.Context.Target.getTriple().getOS() == llvm::Triple::Darwin) { |
| 725 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 726 | return; |
| 727 | } |
| 728 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 729 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 730 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 731 | d->addAttr(::new (S.Context) AliasAttr(Attr.getLoc(), S.Context, |
| 732 | Str->getString())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 735 | static void HandleNakedAttr(Decl *d, const AttributeList &Attr, |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 736 | Sema &S) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 737 | // Check the attribute arguments. |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 738 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 739 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 740 | return; |
| 741 | } |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 742 | |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 743 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 744 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 745 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 746 | return; |
| 747 | } |
| 748 | |
| 749 | d->addAttr(::new (S.Context) NakedAttr(Attr.getLoc(), S.Context)); |
| 750 | } |
| 751 | |
| 752 | static void HandleAlwaysInlineAttr(Decl *d, const AttributeList &Attr, |
| 753 | Sema &S) { |
| 754 | // Check the attribute arguments. |
| 755 | if (Attr.getNumArgs() != 0) { |
| 756 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | if (!isa<FunctionDecl>(d)) { |
| 761 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 762 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 763 | return; |
| 764 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 765 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 766 | d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 769 | static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 770 | // Check the attribute arguments. |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 771 | if (Attr.getNumArgs() != 0) { |
| 772 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 773 | return; |
| 774 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 776 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 778 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 779 | d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 780 | return; |
| 781 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 784 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 787 | static void HandleMayAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 788 | // check the attribute arguments. |
| 789 | if (Attr.getNumArgs() != 0) { |
| 790 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 791 | return; |
| 792 | } |
| 793 | |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 794 | d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context)); |
| 795 | } |
| 796 | |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 797 | static void HandleNoCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 798 | assert(Attr.isInvalid() == false); |
Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 799 | if (isa<VarDecl>(d)) |
| 800 | d->addAttr(::new (S.Context) NoCommonAttr(Attr.getLoc(), S.Context)); |
| 801 | else |
| 802 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 803 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | static void HandleCommonAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 807 | assert(Attr.isInvalid() == false); |
Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 808 | if (isa<VarDecl>(d)) |
| 809 | d->addAttr(::new (S.Context) CommonAttr(Attr.getLoc(), S.Context)); |
| 810 | else |
| 811 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 812 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 813 | } |
| 814 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 815 | static void HandleNoReturnAttr(Decl *d, const AttributeList &attr, Sema &S) { |
| 816 | if (hasDeclarator(d)) return; |
| 817 | |
| 818 | if (S.CheckNoReturnAttr(attr)) return; |
| 819 | |
| 820 | if (!isa<ObjCMethodDecl>(d)) { |
| 821 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 822 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 823 | return; |
| 824 | } |
| 825 | |
| 826 | d->addAttr(::new (S.Context) NoReturnAttr(attr.getLoc(), S.Context)); |
| 827 | } |
| 828 | |
| 829 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
| 830 | if (attr.getNumArgs() != 0) { |
| 831 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 832 | attr.setInvalid(); |
| 833 | return true; |
| 834 | } |
| 835 | |
| 836 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | static void HandleAnalyzerNoReturnAttr(Decl *d, const AttributeList &Attr, |
| 840 | Sema &S) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 841 | |
| 842 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 843 | // because 'analyzer_noreturn' does not impact the type. |
| 844 | |
| 845 | if (Attr.getNumArgs() != 0) { |
| 846 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | if (!isFunctionOrMethod(d) && !isa<BlockDecl>(d)) { |
| 851 | ValueDecl *VD = dyn_cast<ValueDecl>(d); |
| 852 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 853 | && !VD->getType()->isFunctionPointerType())) { |
| 854 | S.Diag(Attr.getLoc(), |
| 855 | Attr.isCXX0XAttribute() ? diag::err_attribute_wrong_decl_type |
| 856 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 857 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 858 | return; |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | d->addAttr(::new (S.Context) AnalyzerNoReturnAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 863 | } |
| 864 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 865 | // PS3 PPU-specific. |
| 866 | static void HandleVecReturnAttr(Decl *d, const AttributeList &Attr, |
| 867 | Sema &S) { |
| 868 | /* |
| 869 | Returning a Vector Class in Registers |
| 870 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 871 | According to the PPU ABI specifications, a class with a single member of |
| 872 | vector type is returned in memory when used as the return value of a function. |
| 873 | This results in inefficient code when implementing vector classes. To return |
| 874 | the value in a single vector register, add the vecreturn attribute to the |
| 875 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 876 | |
| 877 | Example: |
| 878 | |
| 879 | struct Vector |
| 880 | { |
| 881 | __vector float xyzw; |
| 882 | } __attribute__((vecreturn)); |
| 883 | |
| 884 | Vector Add(Vector lhs, Vector rhs) |
| 885 | { |
| 886 | Vector result; |
| 887 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 888 | return result; // This will be returned in a register |
| 889 | } |
| 890 | */ |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 891 | if (!isa<RecordDecl>(d)) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 892 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 893 | << Attr.getName() << ExpectedClass; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 894 | return; |
| 895 | } |
| 896 | |
| 897 | if (d->getAttr<VecReturnAttr>()) { |
| 898 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 899 | return; |
| 900 | } |
| 901 | |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 902 | RecordDecl *record = cast<RecordDecl>(d); |
| 903 | int count = 0; |
| 904 | |
| 905 | if (!isa<CXXRecordDecl>(record)) { |
| 906 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 907 | return; |
| 908 | } |
| 909 | |
| 910 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 911 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 912 | return; |
| 913 | } |
| 914 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 915 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 916 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 917 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 918 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 919 | return; |
| 920 | } |
| 921 | count++; |
| 922 | } |
| 923 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 924 | d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context)); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 927 | static void HandleDependencyAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 928 | if (!isFunctionOrMethod(d) && !isa<ParmVarDecl>(d)) { |
| 929 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 930 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 931 | return; |
| 932 | } |
| 933 | // FIXME: Actually store the attribute on the declaration |
| 934 | } |
| 935 | |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 936 | static void HandleUnusedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 937 | // check the attribute arguments. |
| 938 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 939 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 940 | return; |
| 941 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 942 | |
John McCall | cef1582 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 943 | if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) && |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 944 | !isa<TypeDecl>(d) && !isa<LabelDecl>(d)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 945 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 946 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 947 | return; |
| 948 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 949 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 950 | d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 953 | static void HandleUsedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 954 | // check the attribute arguments. |
| 955 | if (Attr.getNumArgs() != 0) { |
| 956 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 957 | return; |
| 958 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 959 | |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 960 | if (const VarDecl *VD = dyn_cast<VarDecl>(d)) { |
Daniel Dunbar | 311bf29 | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 961 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 962 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 963 | return; |
| 964 | } |
| 965 | } else if (!isFunctionOrMethod(d)) { |
| 966 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 967 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 968 | return; |
| 969 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 970 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 971 | d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 974 | static void HandleConstructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 975 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 976 | if (Attr.getNumArgs() > 1) { |
| 977 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 978 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 979 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 980 | |
| 981 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 982 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 983 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 984 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 985 | if (E->isTypeDependent() || E->isValueDependent() || |
| 986 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 987 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 988 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 989 | return; |
| 990 | } |
| 991 | priority = Idx.getZExtValue(); |
| 992 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 993 | |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 994 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 995 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 996 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 997 | return; |
| 998 | } |
| 999 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1000 | d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, |
| 1001 | priority)); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | static void HandleDestructorAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1005 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1006 | if (Attr.getNumArgs() > 1) { |
| 1007 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1008 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1009 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1010 | |
| 1011 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1012 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1013 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1014 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1015 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1016 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1017 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1018 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1019 | return; |
| 1020 | } |
| 1021 | priority = Idx.getZExtValue(); |
| 1022 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1023 | |
Anders Carlsson | 8e82b7f | 2008-08-22 22:10:48 +0000 | [diff] [blame] | 1024 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1025 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1026 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1027 | return; |
| 1028 | } |
| 1029 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1030 | d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, |
| 1031 | priority)); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1034 | static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1035 | unsigned NumArgs = Attr.getNumArgs(); |
| 1036 | if (NumArgs > 1) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1037 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1038 | return; |
| 1039 | } |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1040 | |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1041 | // Handle the case where deprecated attribute has a text message. |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1042 | llvm::StringRef Str; |
| 1043 | if (NumArgs == 1) { |
| 1044 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1045 | if (!SE) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1046 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
| 1047 | << "deprecated"; |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1048 | return; |
| 1049 | } |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1050 | Str = SE->getString(); |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1051 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1053 | d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1056 | static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1057 | unsigned NumArgs = Attr.getNumArgs(); |
| 1058 | if (NumArgs > 1) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1059 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1060 | return; |
| 1061 | } |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1062 | |
Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1063 | // Handle the case where unavailable attribute has a text message. |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1064 | llvm::StringRef Str; |
| 1065 | if (NumArgs == 1) { |
| 1066 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1067 | if (!SE) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1068 | S.Diag(Attr.getArg(0)->getLocStart(), |
Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1069 | diag::err_attribute_not_string) << "unavailable"; |
| 1070 | return; |
| 1071 | } |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1072 | Str = SE->getString(); |
Fariborz Jahanian | c74073c | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1073 | } |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1074 | d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str)); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1077 | static void HandleAvailabilityAttr(Decl *d, const AttributeList &Attr, |
| 1078 | Sema &S) { |
| 1079 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 1080 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
| 1081 | |
| 1082 | llvm::StringRef PlatformName |
| 1083 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1084 | if (PlatformName.empty()) { |
| 1085 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 1086 | << Platform; |
| 1087 | |
| 1088 | PlatformName = Platform->getName(); |
| 1089 | } |
| 1090 | |
| 1091 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1092 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1093 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
| 1094 | |
| 1095 | // Ensure that Introduced < Deprecated < Obsoleted (although not all |
| 1096 | // of these steps are needed). |
| 1097 | if (Introduced.isValid() && Deprecated.isValid() && |
| 1098 | !(Introduced.Version < Deprecated.Version)) { |
| 1099 | S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) |
| 1100 | << 1 << PlatformName << Deprecated.Version.getAsString() |
| 1101 | << 0 << Introduced.Version.getAsString(); |
| 1102 | return; |
| 1103 | } |
| 1104 | |
| 1105 | if (Introduced.isValid() && Obsoleted.isValid() && |
| 1106 | !(Introduced.Version < Obsoleted.Version)) { |
| 1107 | S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) |
| 1108 | << 2 << PlatformName << Obsoleted.Version.getAsString() |
| 1109 | << 0 << Introduced.Version.getAsString(); |
| 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | if (Deprecated.isValid() && Obsoleted.isValid() && |
| 1114 | !(Deprecated.Version < Obsoleted.Version)) { |
| 1115 | S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering) |
| 1116 | << 2 << PlatformName << Obsoleted.Version.getAsString() |
| 1117 | << 1 << Deprecated.Version.getAsString(); |
| 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | d->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context, |
| 1122 | Platform, |
| 1123 | Introduced.Version, |
| 1124 | Deprecated.Version, |
| 1125 | Obsoleted.Version)); |
| 1126 | } |
| 1127 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1128 | static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1129 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1130 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1131 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1132 | return; |
| 1133 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1134 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1135 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1136 | Arg = Arg->IgnoreParenCasts(); |
| 1137 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1139 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1140 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1141 | << "visibility" << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1142 | return; |
| 1143 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1144 | |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1145 | llvm::StringRef TypeStr = Str->getString(); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1146 | VisibilityAttr::VisibilityType type; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1147 | |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1148 | if (TypeStr == "default") |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1149 | type = VisibilityAttr::Default; |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1150 | else if (TypeStr == "hidden") |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1151 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1152 | else if (TypeStr == "internal") |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1153 | type = VisibilityAttr::Hidden; // FIXME |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1154 | else if (TypeStr == "protected") |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1155 | type = VisibilityAttr::Protected; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1156 | else { |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1157 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1158 | return; |
| 1159 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1160 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1161 | d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1164 | static void HandleObjCMethodFamilyAttr(Decl *decl, const AttributeList &attr, |
| 1165 | Sema &S) { |
| 1166 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 1167 | if (!method) { |
| 1168 | S.Diag(attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1169 | << ExpectedMethod; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1170 | return; |
| 1171 | } |
| 1172 | |
| 1173 | if (attr.getNumArgs() != 0 || !attr.getParameterName()) { |
| 1174 | if (!attr.getParameterName() && attr.getNumArgs() == 1) { |
| 1175 | S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1176 | << "objc_method_family" << 1; |
| 1177 | } else { |
| 1178 | S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1179 | } |
| 1180 | attr.setInvalid(); |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | llvm::StringRef param = attr.getParameterName()->getName(); |
| 1185 | ObjCMethodFamilyAttr::FamilyKind family; |
| 1186 | if (param == "none") |
| 1187 | family = ObjCMethodFamilyAttr::OMF_None; |
| 1188 | else if (param == "alloc") |
| 1189 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 1190 | else if (param == "copy") |
| 1191 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 1192 | else if (param == "init") |
| 1193 | family = ObjCMethodFamilyAttr::OMF_init; |
| 1194 | else if (param == "mutableCopy") |
| 1195 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 1196 | else if (param == "new") |
| 1197 | family = ObjCMethodFamilyAttr::OMF_new; |
| 1198 | else { |
| 1199 | // Just warn and ignore it. This is future-proof against new |
| 1200 | // families being used in system headers. |
| 1201 | S.Diag(attr.getParameterLoc(), diag::warn_unknown_method_family); |
| 1202 | return; |
| 1203 | } |
| 1204 | |
| 1205 | decl->addAttr(new (S.Context) ObjCMethodFamilyAttr(attr.getLoc(), |
| 1206 | S.Context, family)); |
| 1207 | } |
| 1208 | |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1209 | static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr, |
| 1210 | Sema &S) { |
| 1211 | if (Attr.getNumArgs() != 0) { |
| 1212 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1213 | return; |
| 1214 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1215 | |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1216 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 1217 | if (OCI == 0) { |
| 1218 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 1219 | return; |
| 1220 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1221 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1222 | D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1226 | if (Attr.getNumArgs() != 0) { |
John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1227 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1228 | return; |
| 1229 | } |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1230 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1231 | QualType T = TD->getUnderlyingType(); |
| 1232 | if (!T->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1233 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1234 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 1235 | return; |
| 1236 | } |
| 1237 | } |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1238 | D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context)); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1241 | static void |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1242 | HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1243 | if (Attr.getNumArgs() != 0) { |
John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1244 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1245 | return; |
| 1246 | } |
| 1247 | |
| 1248 | if (!isa<FunctionDecl>(D)) { |
| 1249 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 1250 | return; |
| 1251 | } |
| 1252 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1253 | D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context)); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1256 | static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1257 | if (!Attr.getParameterName()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1258 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1259 | << "blocks" << 1; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1260 | return; |
| 1261 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1262 | |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1263 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1264 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1265 | return; |
| 1266 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1267 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1268 | BlocksAttr::BlockType type; |
Chris Lattner | 68e4868 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 1269 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1270 | type = BlocksAttr::ByRef; |
| 1271 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1272 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1273 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1274 | return; |
| 1275 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1276 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1277 | d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type)); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1280 | static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1281 | // check the attribute arguments. |
| 1282 | if (Attr.getNumArgs() > 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1283 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1284 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1287 | int sentinel = 0; |
| 1288 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1289 | Expr *E = Attr.getArg(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1290 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1291 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1292 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1293 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1294 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1295 | return; |
| 1296 | } |
| 1297 | sentinel = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1298 | |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1299 | if (sentinel < 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1300 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 1301 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1302 | return; |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | int nullPos = 0; |
| 1307 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1308 | Expr *E = Attr.getArg(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1309 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1310 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1311 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1312 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1313 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1314 | return; |
| 1315 | } |
| 1316 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1317 | |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1318 | if (nullPos > 1 || nullPos < 0) { |
| 1319 | // FIXME: This error message could be improved, it would be nice |
| 1320 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1321 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 1322 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1323 | return; |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1328 | const FunctionType *FT = FD->getType()->getAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1329 | assert(FT && "FunctionDecl has non-function type?"); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1330 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1331 | if (isa<FunctionNoProtoType>(FT)) { |
| 1332 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 1333 | return; |
| 1334 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1336 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1337 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1338 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1339 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1340 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) { |
| 1341 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1342 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1343 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1344 | } |
| 1345 | } else if (isa<BlockDecl>(d)) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1346 | // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the |
| 1347 | // caller. |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1348 | ; |
| 1349 | } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1350 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 1351 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1352 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d) |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1353 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1354 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1355 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 1356 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1357 | return; |
| 1358 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1359 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1360 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1361 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1362 | return; |
| 1363 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1364 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1365 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1366 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1367 | return; |
| 1368 | } |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1369 | d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, |
| 1370 | nullPos)); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1373 | static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1374 | // check the attribute arguments. |
| 1375 | if (Attr.getNumArgs() != 0) { |
| 1376 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1377 | return; |
| 1378 | } |
| 1379 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1380 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1381 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1382 | << Attr.getName() << ExpectedFunctionOrMethod; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1383 | return; |
| 1384 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1385 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1386 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 1387 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1388 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 1389 | return; |
| 1390 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1391 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 1392 | if (MD->getResultType()->isVoidType()) { |
| 1393 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1394 | << Attr.getName() << 1; |
| 1395 | return; |
| 1396 | } |
| 1397 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1398 | D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1401 | static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1402 | // check the attribute arguments. |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1403 | if (attr.getNumArgs() != 0) { |
| 1404 | S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1405 | return; |
| 1406 | } |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1407 | |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1408 | if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) { |
| 1409 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1410 | << attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | 41136ee | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 1411 | return; |
| 1412 | } |
| 1413 | |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1414 | NamedDecl *nd = cast<NamedDecl>(d); |
| 1415 | |
| 1416 | // 'weak' only applies to declarations with external linkage. |
| 1417 | if (hasEffectivelyInternalLinkage(nd)) { |
| 1418 | S.Diag(attr.getLoc(), diag::err_attribute_weak_static); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1419 | return; |
| 1420 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1421 | |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1422 | nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1425 | static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1426 | // check the attribute arguments. |
| 1427 | if (Attr.getNumArgs() != 0) { |
| 1428 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1429 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1430 | } |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1431 | |
| 1432 | // weak_import only applies to variable & function declarations. |
| 1433 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1434 | if (!D->canBeWeakImported(isDef)) { |
| 1435 | if (isDef) |
| 1436 | S.Diag(Attr.getLoc(), |
| 1437 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 1438 | << "weak_import" << 2 /*variable and function*/; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 1439 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
| 1440 | (S.Context.Target.getTriple().getOS() == llvm::Triple::Darwin && |
| 1441 | isa<ObjCInterfaceDecl>(D))) { |
| 1442 | // Nothing to warn about here. |
| 1443 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 1444 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1445 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1446 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1447 | return; |
| 1448 | } |
| 1449 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1450 | D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1453 | static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr, |
| 1454 | Sema &S) { |
| 1455 | // Attribute has 3 arguments. |
| 1456 | if (Attr.getNumArgs() != 3) { |
John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1457 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1458 | return; |
| 1459 | } |
| 1460 | |
| 1461 | unsigned WGSize[3]; |
| 1462 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1463 | Expr *E = Attr.getArg(i); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1464 | llvm::APSInt ArgNum(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1465 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1466 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1467 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1468 | << "reqd_work_group_size" << E->getSourceRange(); |
| 1469 | return; |
| 1470 | } |
| 1471 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 1472 | } |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1473 | D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context, |
| 1474 | WGSize[0], WGSize[1], |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1475 | WGSize[2])); |
| 1476 | } |
| 1477 | |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1478 | static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1479 | // Attribute has no arguments. |
| 1480 | if (Attr.getNumArgs() != 1) { |
| 1481 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1482 | return; |
| 1483 | } |
| 1484 | |
| 1485 | // Make sure that there is a string literal as the sections's single |
| 1486 | // argument. |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1487 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1488 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1489 | if (!SE) { |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1490 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1491 | return; |
| 1492 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1493 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1494 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 5f08912 | 2009-11-30 17:08:26 +0000 | [diff] [blame] | 1495 | std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString()); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1496 | if (!Error.empty()) { |
| 1497 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 1498 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1499 | return; |
| 1500 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1501 | |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1502 | // This attribute cannot be applied to local variables. |
| 1503 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 1504 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 1505 | return; |
| 1506 | } |
| 1507 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1508 | D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, |
| 1509 | SE->getString())); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1512 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1513 | static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1514 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1515 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1516 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1517 | return; |
| 1518 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1519 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1520 | d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1521 | } |
| 1522 | |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1523 | static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1524 | // check the attribute arguments. |
| 1525 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1526 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1527 | return; |
| 1528 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1529 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1530 | d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1534 | // check the attribute arguments. |
| 1535 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1536 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1537 | return; |
| 1538 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1539 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1540 | d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1543 | static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1544 | if (!Attr.getParameterName()) { |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1545 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1546 | return; |
| 1547 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1548 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1549 | if (Attr.getNumArgs() != 0) { |
| 1550 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1551 | return; |
| 1552 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1553 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1554 | VarDecl *VD = dyn_cast<VarDecl>(d); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1555 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1556 | if (!VD || !VD->hasLocalStorage()) { |
| 1557 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 1558 | return; |
| 1559 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1560 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1561 | // Look up the function |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1562 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1563 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1564 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 1565 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1566 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1567 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1568 | Attr.getParameterName(); |
| 1569 | return; |
| 1570 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1571 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1572 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 1573 | if (!FD) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1574 | S.Diag(Attr.getParameterLoc(), |
| 1575 | diag::err_attribute_cleanup_arg_not_function) |
| 1576 | << Attr.getParameterName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1577 | return; |
| 1578 | } |
| 1579 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1580 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1581 | S.Diag(Attr.getParameterLoc(), |
| 1582 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 1583 | << Attr.getParameterName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1584 | return; |
| 1585 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1586 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1587 | // We're currently more strict than GCC about what function types we accept. |
| 1588 | // If this ever proves to be a problem it should be easy to fix. |
| 1589 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 1590 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 1591 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 1592 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1593 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1594 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 1595 | Attr.getParameterName() << ParamTy << Ty; |
| 1596 | return; |
| 1597 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1598 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1599 | d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD)); |
Argyrios Kyrtzidis | e88168a | 2010-12-06 17:51:53 +0000 | [diff] [blame] | 1600 | S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1603 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
| 1604 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
| 1605 | static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1606 | if (Attr.getNumArgs() != 1) { |
| 1607 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1608 | return; |
| 1609 | } |
| 1610 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
| 1611 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1612 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1613 | return; |
| 1614 | } |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1615 | |
| 1616 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1617 | // counted from one. |
| 1618 | bool HasImplicitThisParam = isInstanceMethod(d); |
| 1619 | unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1620 | unsigned FirstIdx = 1; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1621 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1622 | // checks for the 2nd argument |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1623 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1624 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1625 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 1626 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1627 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 1628 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1629 | return; |
| 1630 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1631 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1632 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 1633 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1634 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1635 | return; |
| 1636 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1637 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1638 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1639 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1640 | if (HasImplicitThisParam) { |
| 1641 | if (ArgIdx == 0) { |
| 1642 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1643 | << "format_arg" << IdxExpr->getSourceRange(); |
| 1644 | return; |
| 1645 | } |
| 1646 | ArgIdx--; |
| 1647 | } |
| 1648 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1649 | // make sure the format string is really a string |
| 1650 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1651 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1652 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 1653 | if (not_nsstring_type && |
| 1654 | !isCFStringType(Ty, S.Context) && |
| 1655 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1656 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1657 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1658 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1659 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1660 | << IdxExpr->getSourceRange(); |
| 1661 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1662 | } |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1663 | Ty = getFunctionOrMethodResultType(d); |
| 1664 | if (!isNSStringType(Ty, S.Context) && |
| 1665 | !isCFStringType(Ty, S.Context) && |
| 1666 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1667 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1668 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1669 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1670 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1671 | << IdxExpr->getSourceRange(); |
| 1672 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1675 | d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, |
| 1676 | Idx.getZExtValue())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1679 | enum FormatAttrKind { |
| 1680 | CFStringFormat, |
| 1681 | NSStringFormat, |
| 1682 | StrftimeFormat, |
| 1683 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1684 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1685 | InvalidFormat |
| 1686 | }; |
| 1687 | |
| 1688 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 1689 | /// types. |
| 1690 | static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) { |
| 1691 | // Check for formats that get handled specially. |
| 1692 | if (Format == "NSString") |
| 1693 | return NSStringFormat; |
| 1694 | if (Format == "CFString") |
| 1695 | return CFStringFormat; |
| 1696 | if (Format == "strftime") |
| 1697 | return StrftimeFormat; |
| 1698 | |
| 1699 | // Otherwise, check for supported formats. |
| 1700 | if (Format == "scanf" || Format == "printf" || Format == "printf0" || |
| 1701 | Format == "strfmon" || Format == "cmn_err" || Format == "strftime" || |
| 1702 | Format == "NSString" || Format == "CFString" || Format == "vcmn_err" || |
Chris Lattner | 0ddd0ae | 2011-02-18 17:05:55 +0000 | [diff] [blame] | 1703 | Format == "zcmn_err" || |
| 1704 | Format == "kprintf") // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1705 | return SupportedFormat; |
| 1706 | |
Duncan Sands | de4fe35 | 2010-03-23 14:44:19 +0000 | [diff] [blame] | 1707 | if (Format == "gcc_diag" || Format == "gcc_cdiag" || |
| 1708 | Format == "gcc_cxxdiag" || Format == "gcc_tdiag") |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1709 | return IgnoredFormat; |
| 1710 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1711 | return InvalidFormat; |
| 1712 | } |
| 1713 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1714 | /// Handle __attribute__((init_priority(priority))) attributes based on |
| 1715 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
| 1716 | static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr, |
| 1717 | Sema &S) { |
| 1718 | if (!S.getLangOptions().CPlusPlus) { |
| 1719 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1720 | return; |
| 1721 | } |
| 1722 | |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 1723 | if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) { |
| 1724 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 1725 | Attr.setInvalid(); |
| 1726 | return; |
| 1727 | } |
| 1728 | QualType T = dyn_cast<VarDecl>(d)->getType(); |
| 1729 | if (S.Context.getAsArrayType(T)) |
| 1730 | T = S.Context.getBaseElementType(T); |
| 1731 | if (!T->getAs<RecordType>()) { |
| 1732 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 1733 | Attr.setInvalid(); |
| 1734 | return; |
| 1735 | } |
| 1736 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1737 | if (Attr.getNumArgs() != 1) { |
| 1738 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1739 | Attr.setInvalid(); |
| 1740 | return; |
| 1741 | } |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1742 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 1743 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1744 | llvm::APSInt priority(32); |
| 1745 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 1746 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 1747 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1748 | << "init_priority" << priorityExpr->getSourceRange(); |
| 1749 | Attr.setInvalid(); |
| 1750 | return; |
| 1751 | } |
Fariborz Jahanian | 9f2a4ee | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 1752 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1753 | if (prioritynum < 101 || prioritynum > 65535) { |
| 1754 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 1755 | << priorityExpr->getSourceRange(); |
| 1756 | Attr.setInvalid(); |
| 1757 | return; |
| 1758 | } |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1759 | d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, |
| 1760 | prioritynum)); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1761 | } |
| 1762 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1763 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
| 1764 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1765 | static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1766 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1767 | if (!Attr.getParameterName()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1768 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1769 | << "format" << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1770 | return; |
| 1771 | } |
| 1772 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1773 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1774 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1775 | return; |
| 1776 | } |
| 1777 | |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 1778 | if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1779 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1780 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1781 | return; |
| 1782 | } |
| 1783 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1784 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1785 | // counted from one. |
| 1786 | bool HasImplicitThisParam = isInstanceMethod(d); |
| 1787 | unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1788 | unsigned FirstIdx = 1; |
| 1789 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1790 | llvm::StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1791 | |
| 1792 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1793 | if (Format.startswith("__") && Format.endswith("__")) |
| 1794 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1795 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1796 | // Check for supported formats. |
| 1797 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1798 | |
| 1799 | if (Kind == IgnoredFormat) |
| 1800 | return; |
| 1801 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1802 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1803 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1804 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1805 | return; |
| 1806 | } |
| 1807 | |
| 1808 | // checks for the 2nd argument |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1809 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1810 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1811 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 1812 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1813 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1814 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1815 | return; |
| 1816 | } |
| 1817 | |
| 1818 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1819 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1820 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1821 | return; |
| 1822 | } |
| 1823 | |
| 1824 | // FIXME: Do we need to bounds check? |
| 1825 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1826 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1827 | if (HasImplicitThisParam) { |
| 1828 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1829 | S.Diag(Attr.getLoc(), |
| 1830 | diag::err_format_attribute_implicit_this_format_string) |
| 1831 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1832 | return; |
| 1833 | } |
| 1834 | ArgIdx--; |
| 1835 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1837 | // make sure the format string is really a string |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1838 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1839 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1840 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1841 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1842 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1843 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1844 | return; |
| 1845 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1846 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1847 | // FIXME: do we need to check if the type is NSString*? What are the |
| 1848 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1849 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1850 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1851 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1852 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1853 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1854 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1855 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1856 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1857 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1858 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1859 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1860 | return; |
| 1861 | } |
| 1862 | |
| 1863 | // check the 3rd argument |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1864 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1865 | llvm::APSInt FirstArg(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1866 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 1867 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1868 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1869 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1870 | return; |
| 1871 | } |
| 1872 | |
| 1873 | // check if the function is variadic if the 3rd argument non-zero |
| 1874 | if (FirstArg != 0) { |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1875 | if (isFunctionOrMethodVariadic(d)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1876 | ++NumArgs; // +1 for ... |
| 1877 | } else { |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1878 | S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1879 | return; |
| 1880 | } |
| 1881 | } |
| 1882 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1883 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 1884 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1885 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1886 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1887 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 1888 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1889 | return; |
| 1890 | } |
| 1891 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 1892 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1893 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1894 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1895 | return; |
| 1896 | } |
| 1897 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1898 | d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format, |
| 1899 | Idx.getZExtValue(), |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1900 | FirstArg.getZExtValue())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1903 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 1904 | Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1905 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1906 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1907 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1908 | return; |
| 1909 | } |
| 1910 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1911 | // Try to find the underlying union declaration. |
| 1912 | RecordDecl *RD = 0; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1913 | TypedefDecl *TD = dyn_cast<TypedefDecl>(d); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1914 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 1915 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 1916 | else |
| 1917 | RD = dyn_cast<RecordDecl>(d); |
| 1918 | |
| 1919 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1920 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1921 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1922 | return; |
| 1923 | } |
| 1924 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1925 | if (!RD->isDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1926 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1927 | diag::warn_transparent_union_attribute_not_definition); |
| 1928 | return; |
| 1929 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1930 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1931 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 1932 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1933 | if (Field == FieldEnd) { |
| 1934 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 1935 | return; |
| 1936 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1937 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1938 | FieldDecl *FirstField = *Field; |
| 1939 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 1940 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1941 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 1942 | diag::warn_transparent_union_attribute_floating) |
| 1943 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1944 | return; |
| 1945 | } |
| 1946 | |
| 1947 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 1948 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 1949 | for (; Field != FieldEnd; ++Field) { |
| 1950 | QualType FieldType = Field->getType(); |
| 1951 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 1952 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 1953 | // Warn if we drop the attribute. |
| 1954 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1955 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1956 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1957 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1958 | diag::warn_transparent_union_attribute_field_size_align) |
| 1959 | << isSize << Field->getDeclName() << FieldBits; |
| 1960 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1961 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1962 | diag::note_transparent_union_first_field_size_align) |
| 1963 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1964 | return; |
| 1965 | } |
| 1966 | } |
| 1967 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1968 | RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1971 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1972 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1973 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1974 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1975 | return; |
| 1976 | } |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1977 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1978 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1979 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1980 | // Make sure that there is a string literal as the annotation's single |
| 1981 | // argument. |
| 1982 | if (!SE) { |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1983 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1984 | return; |
| 1985 | } |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1986 | d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, |
| 1987 | SE->getString())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 1990 | static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1991 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1992 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1993 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1994 | return; |
| 1995 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1996 | |
| 1997 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 1998 | // than GNU's, and should error out when it is used to specify a |
| 1999 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2000 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2001 | if (Attr.getNumArgs() == 0) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2002 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2003 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2004 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2005 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2006 | S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0)); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) { |
| 2010 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2011 | // Save dependent expressions in the AST to be instantiated. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2012 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2013 | return; |
| 2014 | } |
| 2015 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2016 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2017 | llvm::APSInt Alignment(32); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2018 | if (!E->isIntegerConstantExpr(Alignment, Context)) { |
| 2019 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
| 2020 | << "aligned" << E->getSourceRange(); |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2021 | return; |
| 2022 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2023 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2024 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2025 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2026 | return; |
| 2027 | } |
| 2028 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2029 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); |
| 2030 | } |
| 2031 | |
| 2032 | void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) { |
| 2033 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2034 | // FIXME: Perform checking of type validity |
| 2035 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS)); |
| 2036 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2037 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2038 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2039 | /// HandleModeAttr - This attribute modifies the width of a decl with primitive |
| 2040 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2041 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2042 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2043 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2044 | /// HImode, not an intermediate pointer. |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2045 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2046 | // This attribute isn't documented, but glibc uses it. It changes |
| 2047 | // the width of an int or unsigned int to the specified size. |
| 2048 | |
| 2049 | // Check that there aren't any arguments |
| 2050 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2051 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2052 | return; |
| 2053 | } |
| 2054 | |
| 2055 | IdentifierInfo *Name = Attr.getParameterName(); |
| 2056 | if (!Name) { |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2057 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2058 | return; |
| 2059 | } |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2060 | |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2061 | llvm::StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2062 | |
| 2063 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2064 | if (Str.startswith("__") && Str.endswith("__")) |
| 2065 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2066 | |
| 2067 | unsigned DestWidth = 0; |
| 2068 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2069 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2070 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2071 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2072 | switch (Str[0]) { |
| 2073 | case 'Q': DestWidth = 8; break; |
| 2074 | case 'H': DestWidth = 16; break; |
| 2075 | case 'S': DestWidth = 32; break; |
| 2076 | case 'D': DestWidth = 64; break; |
| 2077 | case 'X': DestWidth = 96; break; |
| 2078 | case 'T': DestWidth = 128; break; |
| 2079 | } |
| 2080 | if (Str[1] == 'F') { |
| 2081 | IntegerMode = false; |
| 2082 | } else if (Str[1] == 'C') { |
| 2083 | IntegerMode = false; |
| 2084 | ComplexMode = true; |
| 2085 | } else if (Str[1] != 'I') { |
| 2086 | DestWidth = 0; |
| 2087 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2088 | break; |
| 2089 | case 4: |
| 2090 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2091 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2092 | if (Str == "word") |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2093 | DestWidth = S.Context.Target.getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2094 | else if (Str == "byte") |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2095 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2096 | break; |
| 2097 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2098 | if (Str == "pointer") |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2099 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2100 | break; |
| 2101 | } |
| 2102 | |
| 2103 | QualType OldTy; |
| 2104 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 2105 | OldTy = TD->getUnderlyingType(); |
| 2106 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2107 | OldTy = VD->getType(); |
| 2108 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2109 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 2110 | << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2111 | return; |
| 2112 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2113 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2114 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2115 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2116 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2117 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2118 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2119 | } else if (ComplexMode) { |
| 2120 | if (!OldTy->isComplexType()) |
| 2121 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2122 | } else { |
| 2123 | if (!OldTy->isFloatingType()) |
| 2124 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2125 | } |
| 2126 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2127 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 2128 | // and friends, at least with glibc. |
| 2129 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 2130 | // width on unusual platforms. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2131 | // FIXME: Make sure floating-point mappings are accurate |
| 2132 | // FIXME: Support XF and TF types |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2133 | QualType NewTy; |
| 2134 | switch (DestWidth) { |
| 2135 | case 0: |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2136 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2137 | return; |
| 2138 | default: |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2139 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2140 | return; |
| 2141 | case 8: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2142 | if (!IntegerMode) { |
| 2143 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2144 | return; |
| 2145 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2146 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2147 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2148 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2149 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2150 | break; |
| 2151 | case 16: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2152 | if (!IntegerMode) { |
| 2153 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2154 | return; |
| 2155 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2156 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2157 | NewTy = S.Context.ShortTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2158 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2159 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2160 | break; |
| 2161 | case 32: |
| 2162 | if (!IntegerMode) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2163 | NewTy = S.Context.FloatTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2164 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2165 | NewTy = S.Context.IntTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2166 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2167 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2168 | break; |
| 2169 | case 64: |
| 2170 | if (!IntegerMode) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2171 | NewTy = S.Context.DoubleTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2172 | else if (OldTy->isSignedIntegerType()) |
Chandler Carruth | 7234370 | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2173 | if (S.Context.Target.getLongWidth() == 64) |
| 2174 | NewTy = S.Context.LongTy; |
| 2175 | else |
| 2176 | NewTy = S.Context.LongLongTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2177 | else |
Chandler Carruth | 7234370 | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2178 | if (S.Context.Target.getLongWidth() == 64) |
| 2179 | NewTy = S.Context.UnsignedLongTy; |
| 2180 | else |
| 2181 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2182 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2183 | case 96: |
| 2184 | NewTy = S.Context.LongDoubleTy; |
| 2185 | break; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2186 | case 128: |
| 2187 | if (!IntegerMode) { |
| 2188 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2189 | return; |
| 2190 | } |
Anders Carlsson | 88ea245 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 2191 | if (OldTy->isSignedIntegerType()) |
| 2192 | NewTy = S.Context.Int128Ty; |
| 2193 | else |
| 2194 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2195 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2198 | if (ComplexMode) { |
| 2199 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2200 | } |
| 2201 | |
| 2202 | // Install the new type. |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2203 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 2204 | // FIXME: preserve existing source info. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2205 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2206 | } else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2207 | cast<ValueDecl>(D)->setType(NewTy); |
| 2208 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2209 | |
Mike Stump | 3722f58 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 2210 | static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2211 | // check the attribute arguments. |
| 2212 | if (Attr.getNumArgs() > 0) { |
| 2213 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2214 | return; |
| 2215 | } |
Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 2216 | |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2217 | if (!isFunctionOrMethod(d)) { |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2218 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2219 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2220 | return; |
| 2221 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2222 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2223 | d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2224 | } |
| 2225 | |
Mike Stump | 3722f58 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 2226 | static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2227 | // check the attribute arguments. |
| 2228 | if (Attr.getNumArgs() != 0) { |
| 2229 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2230 | return; |
| 2231 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2232 | |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2233 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2234 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2235 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2236 | return; |
| 2237 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2238 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2239 | d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2242 | static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr, |
| 2243 | Sema &S) { |
| 2244 | // check the attribute arguments. |
| 2245 | if (Attr.getNumArgs() != 0) { |
| 2246 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2247 | return; |
| 2248 | } |
| 2249 | |
| 2250 | if (!isa<FunctionDecl>(d)) { |
| 2251 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2252 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2253 | return; |
| 2254 | } |
| 2255 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2256 | d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), |
| 2257 | S.Context)); |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2260 | static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2261 | if (S.LangOpts.CUDA) { |
| 2262 | // check the attribute arguments. |
| 2263 | if (Attr.getNumArgs() != 0) { |
| 2264 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2265 | return; |
| 2266 | } |
| 2267 | |
| 2268 | if (!isa<VarDecl>(d)) { |
| 2269 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2270 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2271 | return; |
| 2272 | } |
| 2273 | |
| 2274 | d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context)); |
| 2275 | } else { |
| 2276 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 2277 | } |
| 2278 | } |
| 2279 | |
| 2280 | static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2281 | if (S.LangOpts.CUDA) { |
| 2282 | // check the attribute arguments. |
| 2283 | if (Attr.getNumArgs() != 0) { |
| 2284 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2285 | return; |
| 2286 | } |
| 2287 | |
| 2288 | if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) { |
| 2289 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2290 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2291 | return; |
| 2292 | } |
| 2293 | |
| 2294 | d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context)); |
| 2295 | } else { |
| 2296 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2301 | if (S.LangOpts.CUDA) { |
| 2302 | // check the attribute arguments. |
| 2303 | if (Attr.getNumArgs() != 0) { |
| 2304 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2305 | return; |
| 2306 | } |
| 2307 | |
| 2308 | if (!isa<FunctionDecl>(d)) { |
| 2309 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2310 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2311 | return; |
| 2312 | } |
| 2313 | |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2314 | FunctionDecl *FD = cast<FunctionDecl>(d); |
| 2315 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 2316 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2317 | if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { |
| 2318 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2319 | << FD->getType() |
| 2320 | << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), |
| 2321 | "void"); |
| 2322 | } else { |
| 2323 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2324 | << FD->getType(); |
| 2325 | } |
| 2326 | return; |
| 2327 | } |
| 2328 | |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2329 | d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context)); |
| 2330 | } else { |
| 2331 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2336 | if (S.LangOpts.CUDA) { |
| 2337 | // check the attribute arguments. |
| 2338 | if (Attr.getNumArgs() != 0) { |
| 2339 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2340 | return; |
| 2341 | } |
| 2342 | |
| 2343 | if (!isa<FunctionDecl>(d)) { |
| 2344 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2345 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2346 | return; |
| 2347 | } |
| 2348 | |
| 2349 | d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context)); |
| 2350 | } else { |
| 2351 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 2352 | } |
| 2353 | } |
| 2354 | |
| 2355 | static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2356 | if (S.LangOpts.CUDA) { |
| 2357 | // check the attribute arguments. |
| 2358 | if (Attr.getNumArgs() != 0) { |
| 2359 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2360 | return; |
| 2361 | } |
| 2362 | |
| 2363 | if (!isa<VarDecl>(d)) { |
| 2364 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2365 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2366 | return; |
| 2367 | } |
| 2368 | |
| 2369 | d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context)); |
| 2370 | } else { |
| 2371 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 2372 | } |
| 2373 | } |
| 2374 | |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 2375 | static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2376 | // check the attribute arguments. |
| 2377 | if (Attr.getNumArgs() != 0) { |
| 2378 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2379 | return; |
| 2380 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2381 | |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2382 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
| 2383 | if (Fn == 0) { |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2384 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2385 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2386 | return; |
| 2387 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2388 | |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 2389 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 2390 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2391 | return; |
| 2392 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2393 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2394 | d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2395 | } |
| 2396 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2397 | static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) { |
| 2398 | if (hasDeclarator(d)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2399 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2400 | // Diagnostic is emitted elsewhere: here we store the (valid) attr |
| 2401 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 2402 | CallingConv CC; |
| 2403 | if (S.CheckCallingConvAttr(attr, CC)) |
| 2404 | return; |
| 2405 | |
| 2406 | if (!isa<ObjCMethodDecl>(d)) { |
| 2407 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2408 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2409 | return; |
| 2410 | } |
| 2411 | |
| 2412 | switch (attr.getKind()) { |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2413 | case AttributeList::AT_fastcall: |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2414 | d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context)); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2415 | return; |
| 2416 | case AttributeList::AT_stdcall: |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2417 | d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context)); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2418 | return; |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2419 | case AttributeList::AT_thiscall: |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2420 | d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context)); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 2421 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2422 | case AttributeList::AT_cdecl: |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2423 | d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context)); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2424 | return; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2425 | case AttributeList::AT_pascal: |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2426 | d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context)); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2427 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2428 | default: |
| 2429 | llvm_unreachable("unexpected attribute kind"); |
| 2430 | return; |
| 2431 | } |
| 2432 | } |
| 2433 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2434 | static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){ |
| 2435 | assert(Attr.isInvalid() == false); |
| 2436 | d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context)); |
| 2437 | } |
| 2438 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2439 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) { |
| 2440 | if (attr.isInvalid()) |
| 2441 | return true; |
| 2442 | |
| 2443 | if (attr.getNumArgs() != 0) { |
| 2444 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2445 | attr.setInvalid(); |
| 2446 | return true; |
| 2447 | } |
| 2448 | |
| 2449 | // TODO: diagnose uses of these conventions on the wrong target. |
| 2450 | switch (attr.getKind()) { |
| 2451 | case AttributeList::AT_cdecl: CC = CC_C; break; |
| 2452 | case AttributeList::AT_fastcall: CC = CC_X86FastCall; break; |
| 2453 | case AttributeList::AT_stdcall: CC = CC_X86StdCall; break; |
| 2454 | case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break; |
| 2455 | case AttributeList::AT_pascal: CC = CC_X86Pascal; break; |
| 2456 | default: llvm_unreachable("unexpected attribute kind"); return true; |
| 2457 | } |
| 2458 | |
| 2459 | return false; |
| 2460 | } |
| 2461 | |
| 2462 | static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) { |
| 2463 | if (hasDeclarator(d)) return; |
| 2464 | |
| 2465 | unsigned numParams; |
| 2466 | if (S.CheckRegparmAttr(attr, numParams)) |
| 2467 | return; |
| 2468 | |
| 2469 | if (!isa<ObjCMethodDecl>(d)) { |
| 2470 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2471 | << attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2472 | return; |
| 2473 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2474 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2475 | d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams)); |
| 2476 | } |
| 2477 | |
| 2478 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 2479 | /// otherwise setting numParams to the appropriate value. |
| 2480 | bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) { |
| 2481 | if (attr.isInvalid()) |
| 2482 | return true; |
| 2483 | |
| 2484 | if (attr.getNumArgs() != 1) { |
| 2485 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2486 | attr.setInvalid(); |
| 2487 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2488 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2489 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2490 | Expr *NumParamsExpr = attr.getArg(0); |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2491 | llvm::APSInt NumParams(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2492 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2493 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
| 2494 | Diag(attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2495 | << "regparm" << NumParamsExpr->getSourceRange(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2496 | attr.setInvalid(); |
| 2497 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2500 | if (Context.Target.getRegParmMax() == 0) { |
| 2501 | Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2502 | << NumParamsExpr->getSourceRange(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2503 | attr.setInvalid(); |
| 2504 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2507 | numParams = NumParams.getZExtValue(); |
| 2508 | if (numParams > Context.Target.getRegParmMax()) { |
| 2509 | Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
| 2510 | << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); |
| 2511 | attr.setInvalid(); |
| 2512 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2515 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2518 | static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){ |
| 2519 | if (S.LangOpts.CUDA) { |
| 2520 | // check the attribute arguments. |
| 2521 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2522 | // FIXME: 0 is not okay. |
| 2523 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2524 | return; |
| 2525 | } |
| 2526 | |
| 2527 | if (!isFunctionOrMethod(d)) { |
| 2528 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2529 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2530 | return; |
| 2531 | } |
| 2532 | |
| 2533 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 2534 | llvm::APSInt MaxThreads(32); |
| 2535 | if (MaxThreadsExpr->isTypeDependent() || |
| 2536 | MaxThreadsExpr->isValueDependent() || |
| 2537 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 2538 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2539 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 2540 | return; |
| 2541 | } |
| 2542 | |
| 2543 | llvm::APSInt MinBlocks(32); |
| 2544 | if (Attr.getNumArgs() > 1) { |
| 2545 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 2546 | if (MinBlocksExpr->isTypeDependent() || |
| 2547 | MinBlocksExpr->isValueDependent() || |
| 2548 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 2549 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2550 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 2551 | return; |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context, |
| 2556 | MaxThreads.getZExtValue(), |
| 2557 | MinBlocks.getZExtValue())); |
| 2558 | } else { |
| 2559 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 2560 | } |
| 2561 | } |
| 2562 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2563 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2564 | // Checker-specific attribute handlers. |
| 2565 | //===----------------------------------------------------------------------===// |
| 2566 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2567 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
| 2568 | return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type); |
| 2569 | } |
| 2570 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
| 2571 | return type->isPointerType() || isValidSubjectOfNSAttribute(S, type); |
| 2572 | } |
| 2573 | |
| 2574 | static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) { |
| 2575 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(d); |
| 2576 | if (!param) { |
| 2577 | S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2578 | << SourceRange(attr.getLoc()) << attr.getName() << ExpectedParameter; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | } |
| 2581 | |
| 2582 | bool typeOK, cf; |
| 2583 | if (attr.getKind() == AttributeList::AT_ns_consumed) { |
| 2584 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 2585 | cf = false; |
| 2586 | } else { |
| 2587 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 2588 | cf = true; |
| 2589 | } |
| 2590 | |
| 2591 | if (!typeOK) { |
| 2592 | S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 2593 | << SourceRange(attr.getLoc()) << attr.getName() << cf; |
| 2594 | return; |
| 2595 | } |
| 2596 | |
| 2597 | if (cf) |
| 2598 | param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context)); |
| 2599 | else |
| 2600 | param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context)); |
| 2601 | } |
| 2602 | |
| 2603 | static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr, |
| 2604 | Sema &S) { |
| 2605 | if (!isa<ObjCMethodDecl>(d)) { |
| 2606 | S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2607 | << SourceRange(attr.getLoc()) << attr.getName() << ExpectedMethod; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2608 | return; |
| 2609 | } |
| 2610 | |
| 2611 | d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context)); |
| 2612 | } |
| 2613 | |
| 2614 | static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr, |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2615 | Sema &S) { |
| 2616 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2617 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2618 | |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2619 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2620 | returnType = MD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2621 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2622 | returnType = FD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2623 | else { |
Ted Kremenek | eebcf57 | 2009-08-19 23:56:48 +0000 | [diff] [blame] | 2624 | S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2625 | << SourceRange(attr.getLoc()) << attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2626 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2627 | return; |
| 2628 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2629 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2630 | bool typeOK; |
| 2631 | bool cf; |
| 2632 | switch (attr.getKind()) { |
| 2633 | default: llvm_unreachable("invalid ownership attribute"); return; |
| 2634 | case AttributeList::AT_ns_returns_autoreleased: |
| 2635 | case AttributeList::AT_ns_returns_retained: |
| 2636 | case AttributeList::AT_ns_returns_not_retained: |
| 2637 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 2638 | cf = false; |
| 2639 | break; |
| 2640 | |
| 2641 | case AttributeList::AT_cf_returns_retained: |
| 2642 | case AttributeList::AT_cf_returns_not_retained: |
| 2643 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 2644 | cf = true; |
| 2645 | break; |
| 2646 | } |
| 2647 | |
| 2648 | if (!typeOK) { |
Ted Kremenek | eebcf57 | 2009-08-19 23:56:48 +0000 | [diff] [blame] | 2649 | S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2650 | << SourceRange(attr.getLoc()) |
| 2651 | << attr.getName() << isa<ObjCMethodDecl>(d) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2652 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2653 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2654 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2655 | switch (attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2656 | default: |
| 2657 | assert(0 && "invalid ownership attribute"); |
| 2658 | return; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2659 | case AttributeList::AT_ns_returns_autoreleased: |
| 2660 | d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(), |
| 2661 | S.Context)); |
| 2662 | return; |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2663 | case AttributeList::AT_cf_returns_not_retained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2664 | d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(), |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2665 | S.Context)); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2666 | return; |
| 2667 | case AttributeList::AT_ns_returns_not_retained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2668 | d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(), |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2669 | S.Context)); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2670 | return; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2671 | case AttributeList::AT_cf_returns_retained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2672 | d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(), |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2673 | S.Context)); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2674 | return; |
| 2675 | case AttributeList::AT_ns_returns_retained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2676 | d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(), |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2677 | S.Context)); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2678 | return; |
| 2679 | }; |
| 2680 | } |
| 2681 | |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 2682 | static bool isKnownDeclSpecAttr(const AttributeList &Attr) { |
| 2683 | return Attr.getKind() == AttributeList::AT_dllimport || |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2684 | Attr.getKind() == AttributeList::AT_dllexport || |
| 2685 | Attr.getKind() == AttributeList::AT_uuid; |
| 2686 | } |
| 2687 | |
| 2688 | //===----------------------------------------------------------------------===// |
| 2689 | // Microsoft specific attribute handlers. |
| 2690 | //===----------------------------------------------------------------------===// |
| 2691 | |
| 2692 | static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2693 | if (S.LangOpts.Microsoft || S.LangOpts.Borland) { |
| 2694 | // check the attribute arguments. |
| 2695 | if (Attr.getNumArgs() != 1) { |
| 2696 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2697 | return; |
| 2698 | } |
| 2699 | Expr *Arg = Attr.getArg(0); |
| 2700 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2701 | if (Str == 0 || Str->isWide()) { |
| 2702 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 2703 | << "uuid" << 1; |
| 2704 | return; |
| 2705 | } |
| 2706 | |
| 2707 | llvm::StringRef StrRef = Str->getString(); |
| 2708 | |
| 2709 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 2710 | StrRef.back() == '}'; |
| 2711 | |
| 2712 | // Validate GUID length. |
| 2713 | if (IsCurly && StrRef.size() != 38) { |
| 2714 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2715 | return; |
| 2716 | } |
| 2717 | if (!IsCurly && StrRef.size() != 36) { |
| 2718 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2719 | return; |
| 2720 | } |
| 2721 | |
| 2722 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 2723 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 2724 | llvm::StringRef::iterator I = StrRef.begin(); |
| 2725 | if (IsCurly) // Skip the optional '{' |
| 2726 | ++I; |
| 2727 | |
| 2728 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2729 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 2730 | if (*I != '-') { |
| 2731 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2732 | return; |
| 2733 | } |
| 2734 | } else if (!isxdigit(*I)) { |
| 2735 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2736 | return; |
| 2737 | } |
| 2738 | I++; |
| 2739 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2740 | |
| 2741 | d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context, |
| 2742 | Str->getString())); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2743 | } else |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2744 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2747 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2748 | // Top Level Sema Entry Points |
| 2749 | //===----------------------------------------------------------------------===// |
| 2750 | |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2751 | static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D, |
| 2752 | const AttributeList &Attr, Sema &S) { |
| 2753 | switch (Attr.getKind()) { |
| 2754 | case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break; |
| 2755 | case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break; |
| 2756 | case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break; |
| 2757 | default: |
| 2758 | break; |
| 2759 | } |
| 2760 | } |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2761 | |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2762 | static void ProcessInheritableDeclAttr(Scope *scope, Decl *D, |
| 2763 | const AttributeList &Attr, Sema &S) { |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2764 | switch (Attr.getKind()) { |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 2765 | case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 2766 | case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break; |
| 2767 | case AttributeList::AT_IBOutletCollection: |
| 2768 | HandleIBOutletCollection(D, Attr, S); break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2769 | case AttributeList::AT_address_space: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2770 | case AttributeList::AT_opencl_image_access: |
Fariborz Jahanian | 257eac6 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 2771 | case AttributeList::AT_objc_gc: |
John Thompson | 4798122 | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 2772 | case AttributeList::AT_vector_size: |
Bob Wilson | 118baf7 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 2773 | case AttributeList::AT_neon_vector_type: |
| 2774 | case AttributeList::AT_neon_polyvector_type: |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2775 | // Ignore these, these are type attributes, handled by |
| 2776 | // ProcessTypeAttributes. |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2777 | break; |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2778 | case AttributeList::AT_device: |
| 2779 | case AttributeList::AT_host: |
| 2780 | case AttributeList::AT_overloadable: |
| 2781 | // Ignore, this is a non-inheritable attribute, handled |
| 2782 | // by ProcessNonInheritableDeclAttr. |
| 2783 | break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2784 | case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; |
| 2785 | case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2786 | case AttributeList::AT_always_inline: |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 2787 | HandleAlwaysInlineAttr (D, Attr, S); break; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 2788 | case AttributeList::AT_analyzer_noreturn: |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2789 | HandleAnalyzerNoReturnAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2790 | case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2791 | case AttributeList::AT_availability:HandleAvailabilityAttr(D, Attr, S); break; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2792 | case AttributeList::AT_carries_dependency: |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2793 | HandleDependencyAttr (D, Attr, S); break; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 2794 | case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2795 | case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2796 | case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break; |
| 2797 | case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break; |
| 2798 | case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2799 | case AttributeList::AT_ext_vector_type: |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2800 | HandleExtVectorTypeAttr(scope, D, Attr, S); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2801 | break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2802 | case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; |
| 2803 | case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2804 | case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2805 | case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2806 | case AttributeList::AT_launch_bounds: |
| 2807 | HandleLaunchBoundsAttr(D, Attr, S); |
| 2808 | break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2809 | case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; |
| 2810 | case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break; |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 2811 | case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 2812 | case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2813 | case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 2814 | case AttributeList::AT_ownership_returns: |
| 2815 | case AttributeList::AT_ownership_takes: |
| 2816 | case AttributeList::AT_ownership_holds: |
| 2817 | HandleOwnershipAttr (D, Attr, S); break; |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 2818 | case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2819 | case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; |
| 2820 | case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2821 | case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2822 | case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2823 | |
| 2824 | // Checker-specific. |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2825 | case AttributeList::AT_cf_consumed: |
| 2826 | case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break; |
| 2827 | case AttributeList::AT_ns_consumes_self: |
| 2828 | HandleNSConsumesSelfAttr(D, Attr, S); break; |
| 2829 | |
| 2830 | case AttributeList::AT_ns_returns_autoreleased: |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2831 | case AttributeList::AT_ns_returns_not_retained: |
| 2832 | case AttributeList::AT_cf_returns_not_retained: |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2833 | case AttributeList::AT_ns_returns_retained: |
| 2834 | case AttributeList::AT_cf_returns_retained: |
| 2835 | HandleNSReturnsRetainedAttr(D, Attr, S); break; |
| 2836 | |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2837 | case AttributeList::AT_reqd_wg_size: |
| 2838 | HandleReqdWorkGroupSize(D, Attr, S); break; |
| 2839 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2840 | case AttributeList::AT_init_priority: |
| 2841 | HandleInitPriorityAttr(D, Attr, S); break; |
| 2842 | |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2843 | case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; |
| 2844 | case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2845 | case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break; |
| 2846 | case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; |
| 2847 | case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2848 | case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2849 | case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S); |
| 2850 | break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2851 | case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2852 | case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2853 | case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2854 | case AttributeList::AT_transparent_union: |
| 2855 | HandleTransparentUnionAttr(D, Attr, S); |
| 2856 | break; |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2857 | case AttributeList::AT_objc_exception: |
| 2858 | HandleObjCExceptionAttr(D, Attr, S); |
| 2859 | break; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2860 | case AttributeList::AT_objc_method_family: |
| 2861 | HandleObjCMethodFamilyAttr(D, Attr, S); |
| 2862 | break; |
Alexis Hunt | 54a0254 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2863 | case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break; |
| 2864 | case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break; |
| 2865 | case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break; |
| 2866 | case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break; |
| 2867 | case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break; |
| 2868 | case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break; |
| 2869 | case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break; |
| 2870 | case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break; |
| 2871 | case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2872 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | b4f3134 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 2873 | // Just ignore |
| 2874 | break; |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2875 | case AttributeList::AT_no_instrument_function: // Interacts with -pg. |
| 2876 | HandleNoInstrumentFunctionAttr(D, Attr, S); |
| 2877 | break; |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2878 | case AttributeList::AT_stdcall: |
| 2879 | case AttributeList::AT_cdecl: |
| 2880 | case AttributeList::AT_fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2881 | case AttributeList::AT_thiscall: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2882 | case AttributeList::AT_pascal: |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2883 | HandleCallConvAttr(D, Attr, S); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2884 | break; |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2885 | case AttributeList::AT_opencl_kernel_function: |
| 2886 | HandleOpenCLKernelAttr(D, Attr, S); |
| 2887 | break; |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2888 | case AttributeList::AT_uuid: |
| 2889 | HandleUuidAttr(D, Attr, S); |
| 2890 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2891 | default: |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2892 | // Ask target about the attribute. |
| 2893 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 2894 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Chandler Carruth | dd1bc0f | 2010-07-08 09:42:26 +0000 | [diff] [blame] | 2895 | S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) |
| 2896 | << Attr.getName(); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2897 | break; |
| 2898 | } |
| 2899 | } |
| 2900 | |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2901 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 2902 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 2903 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 2904 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
| 2905 | static void ProcessDeclAttribute(Scope *scope, Decl *D, |
| 2906 | const AttributeList &Attr, Sema &S, |
| 2907 | bool NonInheritable, bool Inheritable) { |
| 2908 | if (Attr.isInvalid()) |
| 2909 | return; |
| 2910 | |
| 2911 | if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) |
| 2912 | // FIXME: Try to deal with other __declspec attributes! |
| 2913 | return; |
| 2914 | |
| 2915 | if (NonInheritable) |
| 2916 | ProcessNonInheritableDeclAttr(scope, D, Attr, S); |
| 2917 | |
| 2918 | if (Inheritable) |
| 2919 | ProcessInheritableDeclAttr(scope, D, Attr, S); |
| 2920 | } |
| 2921 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2922 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 2923 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2924 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2925 | const AttributeList *AttrList, |
| 2926 | bool NonInheritable, bool Inheritable) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2927 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2928 | ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2929 | } |
| 2930 | |
| 2931 | // GCC accepts |
| 2932 | // static int a9 __attribute__((weakref)); |
| 2933 | // but that looks really pointless. We reject it. |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2934 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2935 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 2936 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2937 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2938 | } |
| 2939 | } |
| 2940 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2941 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
| 2942 | /// #pragma weak needs a non-definition decl and source may not have one |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2943 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 2944 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2945 | NamedDecl *NewD = 0; |
| 2946 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 2947 | NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2948 | FD->getInnerLocStart(), |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2949 | FD->getLocation(), DeclarationName(II), |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2950 | FD->getType(), FD->getTypeSourceInfo()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2951 | if (FD->getQualifier()) { |
| 2952 | FunctionDecl *NewFD = cast<FunctionDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2953 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2954 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2955 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 2956 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2957 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2958 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2959 | VD->getStorageClass(), |
| 2960 | VD->getStorageClassAsWritten()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2961 | if (VD->getQualifier()) { |
| 2962 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2963 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2964 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2965 | } |
| 2966 | return NewD; |
| 2967 | } |
| 2968 | |
| 2969 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak |
| 2970 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 2971 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 2972 | if (W.getUsed()) return; // only do this once |
| 2973 | W.setUsed(true); |
| 2974 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 2975 | IdentifierInfo *NDId = ND->getIdentifier(); |
| 2976 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias()); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2977 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 2978 | NDId->getName())); |
| 2979 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 2980 | WeakTopLevelDecl.push_back(NewD); |
| 2981 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 2982 | // to insert Decl at TU scope, sorry. |
| 2983 | DeclContext *SavedContext = CurContext; |
| 2984 | CurContext = Context.getTranslationUnitDecl(); |
| 2985 | PushOnScopeChains(NewD, S); |
| 2986 | CurContext = SavedContext; |
| 2987 | } else { // just add weak to existing |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2988 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2989 | } |
| 2990 | } |
| 2991 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2992 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 2993 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 2994 | /// specified in many different places, and we need to find and apply them all. |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2995 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 2996 | bool NonInheritable, bool Inheritable) { |
John McCall | 6fe0240 | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 2997 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 2998 | // have to do this. |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2999 | if (Inheritable && !WeakUndeclaredIdentifiers.empty()) { |
John McCall | 6fe0240 | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 3000 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 3001 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 3002 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 3003 | = WeakUndeclaredIdentifiers.find(Id); |
| 3004 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 3005 | WeakInfo W = I->second; |
| 3006 | DeclApplyPragmaWeak(S, ND, W); |
| 3007 | WeakUndeclaredIdentifiers[Id] = W; |
| 3008 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3009 | } |
| 3010 | } |
| 3011 | } |
| 3012 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3013 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3014 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3015 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3016 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3017 | // Walk the declarator structure, applying decl attributes that were in a type |
| 3018 | // position to the decl itself. This handles cases like: |
| 3019 | // int *__attr__(x)** D; |
| 3020 | // when X is a decl attribute. |
| 3021 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 3022 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3023 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3024 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3025 | // Finally, apply any attributes on the decl itself. |
| 3026 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3027 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3028 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3029 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3030 | // This duplicates a vector push_back but hides the need to know the |
| 3031 | // size of the type. |
| 3032 | void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) { |
| 3033 | assert(StackSize <= StackCapacity); |
| 3034 | |
| 3035 | // Grow the stack if necessary. |
| 3036 | if (StackSize == StackCapacity) { |
| 3037 | unsigned newCapacity = 2 * StackCapacity + 2; |
| 3038 | char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)]; |
| 3039 | const char *oldBuffer = (const char*) Stack; |
| 3040 | |
| 3041 | if (StackCapacity) |
| 3042 | memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic)); |
| 3043 | |
| 3044 | delete[] oldBuffer; |
| 3045 | Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer); |
| 3046 | StackCapacity = newCapacity; |
| 3047 | } |
| 3048 | |
| 3049 | assert(StackSize < StackCapacity); |
| 3050 | new (&Stack[StackSize++]) DelayedDiagnostic(diag); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3051 | } |
| 3052 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3053 | void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state, |
| 3054 | Decl *decl) { |
| 3055 | DelayedDiagnostics &DD = S.DelayedDiagnostics; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3056 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3057 | // Check the invariants. |
| 3058 | assert(DD.StackSize >= state.SavedStackSize); |
| 3059 | assert(state.SavedStackSize >= DD.ActiveStackBase); |
| 3060 | assert(DD.ParsingDepth > 0); |
| 3061 | |
| 3062 | // Drop the parsing depth. |
| 3063 | DD.ParsingDepth--; |
| 3064 | |
| 3065 | // If there are no active diagnostics, we're done. |
| 3066 | if (DD.StackSize == DD.ActiveStackBase) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3067 | return; |
| 3068 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3069 | // We only want to actually emit delayed diagnostics when we |
| 3070 | // successfully parsed a decl. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3071 | if (decl) { |
| 3072 | // We emit all the active diagnostics, not just those starting |
| 3073 | // from the saved state. The idea is this: we get one push for a |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3074 | // decl spec and another for each declarator; in a decl group like: |
| 3075 | // deprecated_typedef foo, *bar, baz(); |
| 3076 | // only the declarator pops will be passed decls. This is correct; |
| 3077 | // we really do need to consider delayed diagnostics from the decl spec |
| 3078 | // for each of the different declarations. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3079 | for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) { |
| 3080 | DelayedDiagnostic &diag = DD.Stack[i]; |
| 3081 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3082 | continue; |
| 3083 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3084 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3085 | case DelayedDiagnostic::Deprecation: |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3086 | S.HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3087 | break; |
| 3088 | |
| 3089 | case DelayedDiagnostic::Access: |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3090 | S.HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3091 | break; |
| 3092 | } |
| 3093 | } |
| 3094 | } |
| 3095 | |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3096 | // Destroy all the delayed diagnostics we're about to pop off. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3097 | for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i) |
Douglas Gregor | 899b68f | 2011-03-23 15:13:44 +0000 | [diff] [blame^] | 3098 | DD.Stack[i].Destroy(); |
John McCall | 1064d7e | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3099 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3100 | DD.StackSize = state.SavedStackSize; |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | static bool isDeclDeprecated(Decl *D) { |
| 3104 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 3105 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3106 | return true; |
| 3107 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 3108 | return false; |
| 3109 | } |
| 3110 | |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 3111 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3112 | Decl *Ctx) { |
| 3113 | if (isDeclDeprecated(Ctx)) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3114 | return; |
| 3115 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3116 | DD.Triggered = true; |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3117 | if (!DD.getDeprecationMessage().empty()) |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3118 | Diag(DD.Loc, diag::warn_deprecated_message) |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3119 | << DD.getDeprecationDecl()->getDeclName() |
| 3120 | << DD.getDeprecationMessage(); |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3121 | else |
| 3122 | Diag(DD.Loc, diag::warn_deprecated) |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3123 | << DD.getDeprecationDecl()->getDeclName(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3124 | } |
| 3125 | |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3126 | void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message, |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3127 | SourceLocation Loc, |
Peter Collingbourne | ed12ffb | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 3128 | bool UnknownObjCClass) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3129 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3130 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 3131 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3132 | return; |
| 3133 | } |
| 3134 | |
| 3135 | // Otherwise, don't warn if our current context is deprecated. |
| 3136 | if (isDeclDeprecated(cast<Decl>(CurContext))) |
| 3137 | return; |
Benjamin Kramer | bfac7dc | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3138 | if (!Message.empty()) |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3139 | Diag(Loc, diag::warn_deprecated_message) << D->getDeclName() |
| 3140 | << Message; |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3141 | else { |
Peter Collingbourne | ed12ffb | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 3142 | if (!UnknownObjCClass) |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3143 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 3144 | else |
| 3145 | Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName(); |
| 3146 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3147 | } |