Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetAttributesSema.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
John McCall | 384aff8 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | acc5f3e | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
| 19 | #include "clang/AST/Expr.h" |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 20 | #include "clang/Basic/TargetInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 21 | #include "clang/Sema/DeclSpec.h" |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 22 | #include "clang/Sema/DelayedDiagnostic.h" |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 24 | using namespace clang; |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 25 | using namespace sema; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 26 | |
John McCall | 883cc2c | 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 | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // Helper functions |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 51 | static const FunctionType *getFunctionType(const Decl *d, |
| 52 | bool blocksToo = true) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 53 | QualType Ty; |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 54 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(d)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 55 | Ty = decl->getType(); |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 56 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(d)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 57 | Ty = decl->getType(); |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 58 | else if (const TypedefDecl* decl = dyn_cast<TypedefDecl>(d)) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 59 | Ty = decl->getUnderlyingType(); |
| 60 | else |
| 61 | return 0; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 63 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 64 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 755f9d2 | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 65 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 66 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 67 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 68 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Daniel Dunbar | 3568249 | 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 | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 74 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | a18d7d8 | 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 | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 81 | /// type (function or function-typed variable) or an Objective-C |
| 82 | /// method. |
Ted Kremenek | a18d7d8 | 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 | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Fariborz Jahanian | 620d89c | 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 | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 90 | static bool isFunctionOrMethodOrBlock(const Decl *d) { |
Fariborz Jahanian | 620d89c | 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 | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 98 | return isa<BlockDecl>(d); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 99 | } |
| 100 | |
John McCall | 711c52b | 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 | d3f2c10 | 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 | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 110 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 111 | static bool hasFunctionProto(const Decl *d) { |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 112 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 113 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 114 | else { |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 115 | assert(isa<ObjCMethodDecl>(d) || isa<BlockDecl>(d)); |
Daniel Dunbar | d3f2c10 | 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 | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 123 | static unsigned getFunctionOrMethodNumArgs(const Decl *d) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 124 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 125 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 126 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) |
| 127 | return BD->getNumParams(); |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 128 | return cast<ObjCMethodDecl>(d)->param_size(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 131 | static QualType getFunctionOrMethodArgType(const Decl *d, unsigned Idx) { |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 132 | if (const FunctionType *FnTy = getFunctionType(d)) |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 133 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Fariborz Jahanian | d66f22d | 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 | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 89951a8 | 2009-02-20 18:43:26 +0000 | [diff] [blame] | 137 | return cast<ObjCMethodDecl>(d)->param_begin()[Idx]->getType(); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Ted Kremenek | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 140 | static QualType getFunctionOrMethodResultType(const Decl *d) { |
Fariborz Jahanian | 5b16092 | 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 | a18d7d8 | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 146 | static bool isFunctionOrMethodVariadic(const Decl *d) { |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 147 | if (const FunctionType *FnTy = getFunctionType(d)) { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 148 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 149 | return proto->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 150 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(d)) |
Ted Kremenek | db9a0ae | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 151 | return BD->isVariadic(); |
Fariborz Jahanian | d66f22d | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 152 | else { |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 153 | return cast<ObjCMethodDecl>(d)->isVariadic(); |
| 154 | } |
| 155 | } |
| 156 | |
Chandler Carruth | 07d7e7a | 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 | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 163 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 164 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 165 | if (!PT) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 166 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 167 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 168 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 169 | if (!Cls) |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 170 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 171 | |
John McCall | 506b57e | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 172 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 174 | // 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 | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 179 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 180 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 181 | if (!PT) |
| 182 | return false; |
| 183 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 184 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 185 | if (!RT) |
| 186 | return false; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 187 | |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 188 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 465d41b | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 189 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 085e8f7 | 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 | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 195 | //===----------------------------------------------------------------------===// |
Chris Lattner | e5c5ee1 | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 196 | // Attribute Implementations |
| 197 | //===----------------------------------------------------------------------===// |
| 198 | |
Daniel Dunbar | 3068ae0 | 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 | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 203 | static void HandleExtVectorTypeAttr(Scope *scope, Decl *d, |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 204 | const AttributeList &Attr, Sema &S) { |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 205 | TypedefDecl *tDecl = dyn_cast<TypedefDecl>(d); |
| 206 | if (tDecl == 0) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 207 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 208 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 209 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 211 | QualType curType = tDecl->getUnderlyingType(); |
Douglas Gregor | 9cdda0c | 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 | f7a1a74 | 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 | 9cdda0c | 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 | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 227 | sizeExpr = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 228 | } |
Douglas Gregor | 9cdda0c | 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 | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 232 | QualType T = S.BuildExtVectorType(curType, sizeExpr, Attr.getLoc()); |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 233 | if (!T.isNull()) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 234 | // FIXME: preserve the old source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 235 | tDecl->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(T)); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | 9cdda0c | 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 | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 239 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 242 | static void HandlePackedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 243 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 244 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 245 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 246 | return; |
| 247 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 248 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 249 | if (TagDecl *TD = dyn_cast<TagDecl>(d)) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 250 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 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 | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 255 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 256 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 257 | << Attr.getName() << FD->getType(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 258 | else |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 259 | FD->addAttr(::new (S.Context) PackedAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 260 | } else |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 261 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 264 | static void HandleIBAction(Decl *d, const AttributeList &Attr, Sema &S) { |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 265 | // check the attribute arguments. |
| 266 | if (Attr.getNumArgs() > 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 267 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 268 | return; |
| 269 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | 63e5d7c | 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()) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 274 | d->addAttr(::new (S.Context) IBActionAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 278 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 63e5d7c | 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 | efbddd2 | 2010-02-17 02:37:45 +0000 | [diff] [blame] | 289 | // Objective-C classes. |
| 290 | if (isa<ObjCIvarDecl>(d) || isa<ObjCPropertyDecl>(d)) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 291 | d->addAttr(::new (S.Context) IBOutletAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 292 | return; |
Ted Kremenek | efbddd2 | 2010-02-17 02:37:45 +0000 | [diff] [blame] | 293 | } |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 294 | |
Ted Kremenek | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 295 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
Ted Kremenek | 96329d4 | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Ted Kremenek | 857e918 | 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 | a8fb24f | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 302 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 857e918 | 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 | 4ee2bb1 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 310 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 311 | return; |
| 312 | } |
Fariborz Jahanian | 3a3400b | 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 | a8fb24f | 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 | 3a3400b | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 329 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 330 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Fariborz Jahanian | a8fb24f | 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 | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 336 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | a8fb24f | 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 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 346 | d->addAttr(::new (S.Context) IBOutletCollectionAttr(Attr.getLoc(), S.Context, |
| 347 | QT)); |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 350 | static void HandleNonNullAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | bf91650 | 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 | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 353 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 354 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 355 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 356 | return; |
| 357 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 358 | |
Chandler Carruth | 07d7e7a | 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 | eb2b2a3 | 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 | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 366 | |
Ted Kremenek | eb2b2a3 | 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 | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 369 | |
| 370 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 371 | // The argument must be an integer constant expression. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 372 | Expr *Ex = *I; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 373 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 374 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 375 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | fa25bbb | 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 | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 378 | return; |
| 379 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 380 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 381 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 382 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 383 | if (x < 1 || x > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 384 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 385 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 386 | return; |
| 387 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | 465172f | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 389 | --x; |
Chandler Carruth | 07d7e7a | 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 | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 399 | |
| 400 | // Is the function argument a pointer type? |
Douglas Gregor | de43632 | 2010-12-15 15:41:46 +0000 | [diff] [blame] | 401 | QualType T = getFunctionOrMethodArgType(d, x).getNonReferenceType(); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 402 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 403 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | c9ef405 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 404 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 405 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 406 | continue; |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 407 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 408 | |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 409 | NonNullArgs.push_back(x); |
| 410 | } |
Mike Stump | bf91650 | 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 | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 414 | if (NonNullArgs.empty()) { |
Ted Kremenek | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 415 | for (unsigned I = 0, E = getFunctionOrMethodNumArgs(d); I != E; ++I) { |
Douglas Gregor | de43632 | 2010-12-15 15:41:46 +0000 | [diff] [blame] | 416 | QualType T = getFunctionOrMethodArgType(d, I).getNonReferenceType(); |
Ted Kremenek | dbfe99e | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 417 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Daniel Dunbar | d3f2c10 | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 418 | NonNullArgs.push_back(I); |
Fariborz Jahanian | ff3a078 | 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 | 46bbaca | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 432 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 433 | |
Ted Kremenek | ee1c08c | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 434 | // No pointer arguments? |
Fariborz Jahanian | 60acea4 | 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 | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 440 | return; |
Fariborz Jahanian | 60acea4 | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 441 | } |
Ted Kremenek | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 442 | } |
Ted Kremenek | 7fb43c1 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 443 | |
| 444 | unsigned* start = &NonNullArgs[0]; |
| 445 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 446 | llvm::array_pod_sort(start, start + size); |
Sean Hunt | cf807c4 | 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 | eb2b2a3 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Ted Kremenek | dd0e490 | 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 | 2a47992 | 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 | dd0e490 | 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. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 467 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 468 | switch (AL.getKind()) { |
| 469 | case AttributeList::AT_ownership_takes: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 470 | K = OwnershipAttr::Takes; |
Ted Kremenek | dd0e490 | 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 | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 475 | break; |
| 476 | case AttributeList::AT_ownership_holds: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 477 | K = OwnershipAttr::Holds; |
Ted Kremenek | dd0e490 | 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 | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 482 | break; |
| 483 | case AttributeList::AT_ownership_returns: |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 484 | K = OwnershipAttr::Returns; |
Ted Kremenek | dd0e490 | 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 | 2a47992 | 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 | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | if (!isFunction(d) || !hasFunctionProto(d)) { |
John McCall | 883cc2c | 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 | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | |
Chandler Carruth | 07d7e7a | 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 | dd0e490 | 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 | 2a47992 | 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 | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 517 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 518 | Expr *IdxExpr = *I; |
Ted Kremenek | dd0e490 | 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 | 07d7e7a | 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 | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 544 | switch (K) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 545 | case OwnershipAttr::Takes: |
| 546 | case OwnershipAttr::Holds: { |
Ted Kremenek | dd0e490 | 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) |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 552 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 553 | << "pointer" |
| 554 | << IdxExpr->getSourceRange(); |
| 555 | continue; |
| 556 | } |
| 557 | break; |
| 558 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 559 | case OwnershipAttr::Returns: { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 560 | if (AL.getNumArgs() > 1) { |
| 561 | // Is the function argument an integer type? |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 562 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | dd0e490 | 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 | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 574 | default: |
| 575 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 576 | } // switch |
| 577 | |
| 578 | // Check we don't have a conflict with another ownership attribute. |
Sean Hunt | cf807c4 | 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 | dd0e490 | 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); |
Sean Hunt | cf807c4 | 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 | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 603 | } |
Sean Hunt | cf807c4 | 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 | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 607 | } |
| 608 | |
John McCall | 332bb2a | 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 | 11e8ce7 | 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 | 332bb2a | 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 | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 638 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 639 | return; |
| 640 | } |
| 641 | |
| 642 | NamedDecl *nd = cast<NamedDecl>(d); |
| 643 | |
Rafael Espindola | 11e8ce7 | 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 | 7a126a4 | 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 | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 657 | nd->getNameAsString(); |
Sebastian Redl | 7a126a4 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 658 | return; |
Rafael Espindola | 11e8ce7 | 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 | 332bb2a | 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 | 11e8ce7 | 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 | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 689 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | 11e8ce7 | 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 | f48f367 | 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 | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 704 | d->addAttr(::new (S.Context) WeakRefAttr(Attr.getLoc(), S.Context)); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 707 | static void HandleAliasAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 708 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 709 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 710 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 711 | return; |
| 712 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 713 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 714 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 715 | Arg = Arg->IgnoreParenCasts(); |
| 716 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 717 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 718 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 719 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 720 | << "alias" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 721 | return; |
| 722 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 723 | |
Rafael Espindola | f5fe292 | 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 | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 729 | // FIXME: check if target symbol exists in current file |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 730 | |
Eric Christopher | f48f367 | 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 | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 735 | static void HandleNakedAttr(Decl *d, const AttributeList &Attr, |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 736 | Sema &S) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 737 | // Check the attribute arguments. |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 738 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 739 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 740 | return; |
| 741 | } |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 742 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 743 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 744 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 745 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | dd0cb22 | 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 | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 762 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 763 | return; |
| 764 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 765 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 766 | d->addAttr(::new (S.Context) AlwaysInlineAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 769 | static void HandleMallocAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 770 | // Check the attribute arguments. |
Ryan Flynn | 76168e2 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 775 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 776 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 778 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 779 | d->addAttr(::new (S.Context) MallocAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 780 | return; |
| 781 | } |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Ted Kremenek | 2cff7d1 | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 784 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Dan Gohman | 34c2630 | 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 | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 794 | d->addAttr(::new (S.Context) MayAliasAttr(Attr.getLoc(), S.Context)); |
| 795 | } |
| 796 | |
Eric Christopher | a6cf1e7 | 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 | 722109c | 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 | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 803 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 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 | 722109c | 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 | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 812 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 813 | } |
| 814 | |
John McCall | 711c52b | 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 | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 822 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 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 | b725232 | 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 | b56c1cc | 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 | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 857 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | b56c1cc | 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 | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 863 | } |
| 864 | |
John Thompson | 35cc962 | 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 | f48f367 | 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 | 35cc962 | 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 | 01add59 | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 891 | if (!isa<RecordDecl>(d)) { |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 892 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 893 | << Attr.getName() << ExpectedClass; |
John Thompson | 35cc962 | 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 | 01add59 | 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 | f48f367 | 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 | 01add59 | 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 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 924 | d->addAttr(::new (S.Context) VecReturnAttr(Attr.getLoc(), S.Context)); |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 925 | } |
| 926 | |
Sean Hunt | bbd37c6 | 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 | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 930 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Sean Hunt | bbd37c6 | 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 | 7379889 | 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 | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 939 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 940 | return; |
| 941 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 942 | |
John McCall | aec5860 | 2010-03-31 02:47:45 +0000 | [diff] [blame] | 943 | if (!isa<VarDecl>(d) && !isa<ObjCIvarDecl>(d) && !isFunctionOrMethod(d) && |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 944 | !isa<TypeDecl>(d) && !isa<LabelDecl>(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 945 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 946 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 947 | return; |
| 948 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 949 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 950 | d->addAttr(::new (S.Context) UnusedAttr(Attr.getLoc(), S.Context)); |
Ted Kremenek | 7379889 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Daniel Dunbar | b805dad | 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 | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 959 | |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 960 | if (const VarDecl *VD = dyn_cast<VarDecl>(d)) { |
Daniel Dunbar | 186204b | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 961 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | b805dad | 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 | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 967 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 968 | return; |
| 969 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 970 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 971 | d->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Daniel Dunbar | 3068ae0 | 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 | bdc49d3 | 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 | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 978 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 979 | } |
Daniel Dunbar | 3068ae0 | 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 | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 983 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 984 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 985 | if (E->isTypeDependent() || E->isValueDependent() || |
| 986 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 987 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 988 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 989 | return; |
| 990 | } |
| 991 | priority = Idx.getZExtValue(); |
| 992 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 993 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 994 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 995 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 996 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 997 | return; |
| 998 | } |
| 999 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1000 | d->addAttr(::new (S.Context) ConstructorAttr(Attr.getLoc(), S.Context, |
| 1001 | priority)); |
Daniel Dunbar | 3068ae0 | 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 | bdc49d3 | 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 | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1008 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1009 | } |
Daniel Dunbar | 3068ae0 | 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 | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1013 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1014 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1015 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1016 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1017 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1018 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1019 | return; |
| 1020 | } |
| 1021 | priority = Idx.getZExtValue(); |
| 1022 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1023 | |
Anders Carlsson | 6782fc6 | 2008-08-22 22:10:48 +0000 | [diff] [blame] | 1024 | if (!isa<FunctionDecl>(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1025 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1026 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1027 | return; |
| 1028 | } |
| 1029 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1030 | d->addAttr(::new (S.Context) DestructorAttr(Attr.getLoc(), S.Context, |
| 1031 | priority)); |
Daniel Dunbar | 3068ae0 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1034 | static void HandleDeprecatedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1035 | unsigned NumArgs = Attr.getNumArgs(); |
| 1036 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1037 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1038 | return; |
| 1039 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1040 | |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1041 | // Handle the case where deprecated attribute has a text message. |
Chris Lattner | 951bbb2 | 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 | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1045 | if (!SE) { |
Chris Lattner | 951bbb2 | 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 | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1048 | return; |
| 1049 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1050 | Str = SE->getString(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 1051 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1053 | d->addAttr(::new (S.Context) DeprecatedAttr(Attr.getLoc(), S.Context, Str)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1054 | } |
| 1055 | |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1056 | static void HandleUnavailableAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1057 | unsigned NumArgs = Attr.getNumArgs(); |
| 1058 | if (NumArgs > 1) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1059 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1060 | return; |
| 1061 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1062 | |
Fariborz Jahanian | c784dc1 | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1063 | // Handle the case where unavailable attribute has a text message. |
Chris Lattner | 951bbb2 | 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 | c784dc1 | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1067 | if (!SE) { |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1068 | S.Diag(Attr.getArg(0)->getLocStart(), |
Fariborz Jahanian | c784dc1 | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1069 | diag::err_attribute_not_string) << "unavailable"; |
| 1070 | return; |
| 1071 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1072 | Str = SE->getString(); |
Fariborz Jahanian | c784dc1 | 2010-10-06 23:12:32 +0000 | [diff] [blame] | 1073 | } |
Chris Lattner | 951bbb2 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1074 | d->addAttr(::new (S.Context) UnavailableAttr(Attr.getLoc(), S.Context, Str)); |
Fariborz Jahanian | bc1c877 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Douglas Gregor | 0a0d2b1 | 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 | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1128 | static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1129 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1130 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1131 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1132 | return; |
| 1133 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1134 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1135 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1136 | Arg = Arg->IgnoreParenCasts(); |
| 1137 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1138 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1139 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1140 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1141 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1142 | return; |
| 1143 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1144 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1145 | llvm::StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1146 | VisibilityAttr::VisibilityType type; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1147 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1148 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1149 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1150 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1151 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1152 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1153 | type = VisibilityAttr::Hidden; // FIXME |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1154 | else if (TypeStr == "protected") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1155 | type = VisibilityAttr::Protected; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1156 | else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1157 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1158 | return; |
| 1159 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1160 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1161 | d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
John McCall | d5313b0 | 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 | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1169 | << ExpectedMethod; |
John McCall | d5313b0 | 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 | 0db29ec | 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 | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1215 | |
Chris Lattner | 0db29ec | 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 | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1221 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1222 | D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1226 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1227 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1228 | return; |
| 1229 | } |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1230 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1231 | QualType T = TD->getUnderlyingType(); |
| 1232 | if (!T->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1233 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1234 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 1235 | return; |
| 1236 | } |
| 1237 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1238 | D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context)); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1241 | static void |
Douglas Gregor | f9201e0 | 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 | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1244 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | f9201e0 | 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 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1253 | D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context)); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1256 | static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1257 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1258 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1259 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1260 | return; |
| 1261 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1262 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1263 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1264 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1265 | return; |
| 1266 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1267 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1268 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 1269 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1270 | type = BlocksAttr::ByRef; |
| 1271 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1272 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1273 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1274 | return; |
| 1275 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1276 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1277 | d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type)); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Anders Carlsson | 7709182 | 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 | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1283 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1284 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1287 | int sentinel = 0; |
| 1288 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1289 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1290 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1291 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1292 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1293 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1294 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1295 | return; |
| 1296 | } |
| 1297 | sentinel = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1298 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1299 | if (sentinel < 0) { |
Chris Lattner | fa25bbb | 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 | 7709182 | 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 | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1308 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1309 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1310 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1311 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1312 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1313 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1314 | return; |
| 1315 | } |
| 1316 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1317 | |
Anders Carlsson | 7709182 | 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 | fa25bbb | 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 | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1323 | return; |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1328 | const FunctionType *FT = FD->getType()->getAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1329 | assert(FT && "FunctionDecl has non-function type?"); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1330 | |
Chris Lattner | 897cd90 | 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 | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1335 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1336 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1337 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1338 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1339 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1340 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) { |
| 1341 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1342 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1343 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1344 | } |
| 1345 | } else if (isa<BlockDecl>(d)) { |
Mike Stump | bf91650 | 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 | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1348 | ; |
| 1349 | } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1350 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 1351 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1352 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1353 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1354 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 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 | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1357 | return; |
| 1358 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1359 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1360 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1361 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1362 | return; |
| 1363 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1364 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1365 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1366 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1367 | return; |
| 1368 | } |
Eric Christopher | f48f367 | 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 | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Chris Lattner | 026dc96 | 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 | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1380 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1381 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1382 | << Attr.getName() << ExpectedFunctionOrMethod; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1383 | return; |
| 1384 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1385 | |
Fariborz Jahanian | f031774 | 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 | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 1389 | return; |
| 1390 | } |
Fariborz Jahanian | f031774 | 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 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1398 | D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1401 | static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1402 | // check the attribute arguments. |
John McCall | 332bb2a | 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 | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1405 | return; |
| 1406 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1407 | |
John McCall | 332bb2a | 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 | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1410 | << attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 1411 | return; |
| 1412 | } |
| 1413 | |
John McCall | 332bb2a | 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 | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1419 | return; |
| 1420 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1421 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1422 | nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Daniel Dunbar | 6e775db | 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 | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1430 | } |
Daniel Dunbar | 6e775db | 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 | 0a0d2b1 | 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*/; |
| 1439 | else if (S.Context.Target.getTriple().getOS() != llvm::Triple::Darwin || |
| 1440 | (!isa<ObjCInterfaceDecl>(D) && |
| 1441 | !isa<ObjCPropertyDecl>(D) && |
| 1442 | !isa<ObjCMethodDecl>(D))) |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 1443 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1444 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1445 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1446 | return; |
| 1447 | } |
| 1448 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1449 | D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1452 | static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr, |
| 1453 | Sema &S) { |
| 1454 | // Attribute has 3 arguments. |
| 1455 | if (Attr.getNumArgs() != 3) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1456 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1457 | return; |
| 1458 | } |
| 1459 | |
| 1460 | unsigned WGSize[3]; |
| 1461 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1462 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1463 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1464 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1465 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1466 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1467 | << "reqd_work_group_size" << E->getSourceRange(); |
| 1468 | return; |
| 1469 | } |
| 1470 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 1471 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1472 | D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context, |
| 1473 | WGSize[0], WGSize[1], |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1474 | WGSize[2])); |
| 1475 | } |
| 1476 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1477 | static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1478 | // Attribute has no arguments. |
| 1479 | if (Attr.getNumArgs() != 1) { |
| 1480 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1481 | return; |
| 1482 | } |
| 1483 | |
| 1484 | // Make sure that there is a string literal as the sections's single |
| 1485 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1486 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1487 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1488 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1489 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1490 | return; |
| 1491 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1493 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | bb377ed | 2009-11-30 17:08:26 +0000 | [diff] [blame] | 1494 | std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1495 | if (!Error.empty()) { |
| 1496 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 1497 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1498 | return; |
| 1499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1500 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1501 | // This attribute cannot be applied to local variables. |
| 1502 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 1503 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 1504 | return; |
| 1505 | } |
| 1506 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1507 | D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, |
| 1508 | SE->getString())); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1511 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1512 | static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1513 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1514 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1515 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1516 | return; |
| 1517 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1518 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1519 | d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1522 | static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1523 | // check the attribute arguments. |
| 1524 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1525 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1526 | return; |
| 1527 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1528 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1529 | d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1533 | // check the attribute arguments. |
| 1534 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1535 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1536 | return; |
| 1537 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1538 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1539 | d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1542 | static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1543 | if (!Attr.getParameterName()) { |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1544 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1545 | return; |
| 1546 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1547 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1548 | if (Attr.getNumArgs() != 0) { |
| 1549 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1550 | return; |
| 1551 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1552 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1553 | VarDecl *VD = dyn_cast<VarDecl>(d); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1554 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1555 | if (!VD || !VD->hasLocalStorage()) { |
| 1556 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 1557 | return; |
| 1558 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1559 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1560 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1561 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1562 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1563 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 1564 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1565 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1566 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1567 | Attr.getParameterName(); |
| 1568 | return; |
| 1569 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1570 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1571 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 1572 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1573 | S.Diag(Attr.getParameterLoc(), |
| 1574 | diag::err_attribute_cleanup_arg_not_function) |
| 1575 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1576 | return; |
| 1577 | } |
| 1578 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1579 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1580 | S.Diag(Attr.getParameterLoc(), |
| 1581 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 1582 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1583 | return; |
| 1584 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1585 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1586 | // We're currently more strict than GCC about what function types we accept. |
| 1587 | // If this ever proves to be a problem it should be easy to fix. |
| 1588 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 1589 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 1590 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 1591 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1592 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1593 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 1594 | Attr.getParameterName() << ParamTy << Ty; |
| 1595 | return; |
| 1596 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1597 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1598 | d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD)); |
Argyrios Kyrtzidis | 223ae5c | 2010-12-06 17:51:53 +0000 | [diff] [blame] | 1599 | S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1602 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
| 1603 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
| 1604 | static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1605 | if (Attr.getNumArgs() != 1) { |
| 1606 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1607 | return; |
| 1608 | } |
| 1609 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
| 1610 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1611 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1612 | return; |
| 1613 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1614 | |
| 1615 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1616 | // counted from one. |
| 1617 | bool HasImplicitThisParam = isInstanceMethod(d); |
| 1618 | unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1619 | unsigned FirstIdx = 1; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1620 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1621 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1622 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1623 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1624 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 1625 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1626 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 1627 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1628 | return; |
| 1629 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1630 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1631 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 1632 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1633 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1634 | return; |
| 1635 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1636 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1637 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1638 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1639 | if (HasImplicitThisParam) { |
| 1640 | if (ArgIdx == 0) { |
| 1641 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1642 | << "format_arg" << IdxExpr->getSourceRange(); |
| 1643 | return; |
| 1644 | } |
| 1645 | ArgIdx--; |
| 1646 | } |
| 1647 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1648 | // make sure the format string is really a string |
| 1649 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1650 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1651 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 1652 | if (not_nsstring_type && |
| 1653 | !isCFStringType(Ty, S.Context) && |
| 1654 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1655 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1656 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1657 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1658 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1659 | << IdxExpr->getSourceRange(); |
| 1660 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1661 | } |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1662 | Ty = getFunctionOrMethodResultType(d); |
| 1663 | if (!isNSStringType(Ty, S.Context) && |
| 1664 | !isCFStringType(Ty, S.Context) && |
| 1665 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1666 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1667 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1668 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1669 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1670 | << IdxExpr->getSourceRange(); |
| 1671 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1674 | d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, |
| 1675 | Idx.getZExtValue())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1678 | enum FormatAttrKind { |
| 1679 | CFStringFormat, |
| 1680 | NSStringFormat, |
| 1681 | StrftimeFormat, |
| 1682 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1683 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1684 | InvalidFormat |
| 1685 | }; |
| 1686 | |
| 1687 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 1688 | /// types. |
| 1689 | static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) { |
| 1690 | // Check for formats that get handled specially. |
| 1691 | if (Format == "NSString") |
| 1692 | return NSStringFormat; |
| 1693 | if (Format == "CFString") |
| 1694 | return CFStringFormat; |
| 1695 | if (Format == "strftime") |
| 1696 | return StrftimeFormat; |
| 1697 | |
| 1698 | // Otherwise, check for supported formats. |
| 1699 | if (Format == "scanf" || Format == "printf" || Format == "printf0" || |
| 1700 | Format == "strfmon" || Format == "cmn_err" || Format == "strftime" || |
| 1701 | Format == "NSString" || Format == "CFString" || Format == "vcmn_err" || |
Chris Lattner | cd5b306 | 2011-02-18 17:05:55 +0000 | [diff] [blame] | 1702 | Format == "zcmn_err" || |
| 1703 | Format == "kprintf") // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1704 | return SupportedFormat; |
| 1705 | |
Duncan Sands | bc52595 | 2010-03-23 14:44:19 +0000 | [diff] [blame] | 1706 | if (Format == "gcc_diag" || Format == "gcc_cdiag" || |
| 1707 | Format == "gcc_cxxdiag" || Format == "gcc_tdiag") |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1708 | return IgnoredFormat; |
| 1709 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1710 | return InvalidFormat; |
| 1711 | } |
| 1712 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1713 | /// Handle __attribute__((init_priority(priority))) attributes based on |
| 1714 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
| 1715 | static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr, |
| 1716 | Sema &S) { |
| 1717 | if (!S.getLangOptions().CPlusPlus) { |
| 1718 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1719 | return; |
| 1720 | } |
| 1721 | |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 1722 | if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) { |
| 1723 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 1724 | Attr.setInvalid(); |
| 1725 | return; |
| 1726 | } |
| 1727 | QualType T = dyn_cast<VarDecl>(d)->getType(); |
| 1728 | if (S.Context.getAsArrayType(T)) |
| 1729 | T = S.Context.getBaseElementType(T); |
| 1730 | if (!T->getAs<RecordType>()) { |
| 1731 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 1732 | Attr.setInvalid(); |
| 1733 | return; |
| 1734 | } |
| 1735 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1736 | if (Attr.getNumArgs() != 1) { |
| 1737 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1738 | Attr.setInvalid(); |
| 1739 | return; |
| 1740 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1741 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 1742 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1743 | llvm::APSInt priority(32); |
| 1744 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 1745 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 1746 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1747 | << "init_priority" << priorityExpr->getSourceRange(); |
| 1748 | Attr.setInvalid(); |
| 1749 | return; |
| 1750 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 1751 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1752 | if (prioritynum < 101 || prioritynum > 65535) { |
| 1753 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 1754 | << priorityExpr->getSourceRange(); |
| 1755 | Attr.setInvalid(); |
| 1756 | return; |
| 1757 | } |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1758 | d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, |
| 1759 | prioritynum)); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1762 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
| 1763 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1764 | static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1765 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1766 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1767 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1768 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1769 | return; |
| 1770 | } |
| 1771 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1772 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1773 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1774 | return; |
| 1775 | } |
| 1776 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 1777 | if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1778 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1779 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1780 | return; |
| 1781 | } |
| 1782 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1783 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1784 | // counted from one. |
| 1785 | bool HasImplicitThisParam = isInstanceMethod(d); |
| 1786 | unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1787 | unsigned FirstIdx = 1; |
| 1788 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1789 | llvm::StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1790 | |
| 1791 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1792 | if (Format.startswith("__") && Format.endswith("__")) |
| 1793 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1794 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1795 | // Check for supported formats. |
| 1796 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1797 | |
| 1798 | if (Kind == IgnoredFormat) |
| 1799 | return; |
| 1800 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1801 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1802 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1803 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1804 | return; |
| 1805 | } |
| 1806 | |
| 1807 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1808 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1809 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1810 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 1811 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1812 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1813 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1814 | return; |
| 1815 | } |
| 1816 | |
| 1817 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1818 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1819 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1820 | return; |
| 1821 | } |
| 1822 | |
| 1823 | // FIXME: Do we need to bounds check? |
| 1824 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1825 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1826 | if (HasImplicitThisParam) { |
| 1827 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1828 | S.Diag(Attr.getLoc(), |
| 1829 | diag::err_format_attribute_implicit_this_format_string) |
| 1830 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1831 | return; |
| 1832 | } |
| 1833 | ArgIdx--; |
| 1834 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1835 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1836 | // make sure the format string is really a string |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1837 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1838 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1839 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1840 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1841 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1842 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1843 | return; |
| 1844 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1845 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1846 | // FIXME: do we need to check if the type is NSString*? What are the |
| 1847 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1848 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1849 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1850 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1851 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1852 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1853 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1854 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1855 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1856 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1857 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1858 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1859 | return; |
| 1860 | } |
| 1861 | |
| 1862 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1863 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1864 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1865 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 1866 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1867 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1868 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1869 | return; |
| 1870 | } |
| 1871 | |
| 1872 | // check if the function is variadic if the 3rd argument non-zero |
| 1873 | if (FirstArg != 0) { |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1874 | if (isFunctionOrMethodVariadic(d)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1875 | ++NumArgs; // +1 for ... |
| 1876 | } else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1877 | S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1878 | return; |
| 1879 | } |
| 1880 | } |
| 1881 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1882 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 1883 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1884 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1885 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1886 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 1887 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1888 | return; |
| 1889 | } |
| 1890 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 1891 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1892 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1893 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1894 | return; |
| 1895 | } |
| 1896 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1897 | d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format, |
| 1898 | Idx.getZExtValue(), |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1899 | FirstArg.getZExtValue())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1902 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 1903 | Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1904 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1905 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1906 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1907 | return; |
| 1908 | } |
| 1909 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1910 | // Try to find the underlying union declaration. |
| 1911 | RecordDecl *RD = 0; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1912 | TypedefDecl *TD = dyn_cast<TypedefDecl>(d); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1913 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 1914 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 1915 | else |
| 1916 | RD = dyn_cast<RecordDecl>(d); |
| 1917 | |
| 1918 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1919 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1920 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1921 | return; |
| 1922 | } |
| 1923 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1924 | if (!RD->isDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1925 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1926 | diag::warn_transparent_union_attribute_not_definition); |
| 1927 | return; |
| 1928 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1929 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1930 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 1931 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1932 | if (Field == FieldEnd) { |
| 1933 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 1934 | return; |
| 1935 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1936 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1937 | FieldDecl *FirstField = *Field; |
| 1938 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 1939 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1940 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 1941 | diag::warn_transparent_union_attribute_floating) |
| 1942 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1943 | return; |
| 1944 | } |
| 1945 | |
| 1946 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 1947 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 1948 | for (; Field != FieldEnd; ++Field) { |
| 1949 | QualType FieldType = Field->getType(); |
| 1950 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 1951 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 1952 | // Warn if we drop the attribute. |
| 1953 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1954 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1955 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1956 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1957 | diag::warn_transparent_union_attribute_field_size_align) |
| 1958 | << isSize << Field->getDeclName() << FieldBits; |
| 1959 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1960 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1961 | diag::note_transparent_union_first_field_size_align) |
| 1962 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1963 | return; |
| 1964 | } |
| 1965 | } |
| 1966 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1967 | RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1968 | } |
| 1969 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1970 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1971 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1972 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1973 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1974 | return; |
| 1975 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1976 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1977 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1978 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1979 | // Make sure that there is a string literal as the annotation's single |
| 1980 | // argument. |
| 1981 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1982 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1983 | return; |
| 1984 | } |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1985 | d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, |
| 1986 | SE->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 1989 | static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1990 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1991 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1992 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1993 | return; |
| 1994 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1995 | |
| 1996 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 1997 | // than GNU's, and should error out when it is used to specify a |
| 1998 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1999 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2000 | if (Attr.getNumArgs() == 0) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2001 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2002 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2003 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2004 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2005 | S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) { |
| 2009 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2010 | // Save dependent expressions in the AST to be instantiated. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2011 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2012 | return; |
| 2013 | } |
| 2014 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2015 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2016 | llvm::APSInt Alignment(32); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2017 | if (!E->isIntegerConstantExpr(Alignment, Context)) { |
| 2018 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
| 2019 | << "aligned" << E->getSourceRange(); |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2020 | return; |
| 2021 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2022 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2023 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2024 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2025 | return; |
| 2026 | } |
| 2027 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2028 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); |
| 2029 | } |
| 2030 | |
| 2031 | void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) { |
| 2032 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2033 | // FIXME: Perform checking of type validity |
| 2034 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS)); |
| 2035 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2036 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2037 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2038 | /// HandleModeAttr - This attribute modifies the width of a decl with primitive |
| 2039 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2040 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2041 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2042 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2043 | /// HImode, not an intermediate pointer. |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2044 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2045 | // This attribute isn't documented, but glibc uses it. It changes |
| 2046 | // the width of an int or unsigned int to the specified size. |
| 2047 | |
| 2048 | // Check that there aren't any arguments |
| 2049 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2050 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2051 | return; |
| 2052 | } |
| 2053 | |
| 2054 | IdentifierInfo *Name = Attr.getParameterName(); |
| 2055 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2056 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2057 | return; |
| 2058 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2059 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2060 | llvm::StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2061 | |
| 2062 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2063 | if (Str.startswith("__") && Str.endswith("__")) |
| 2064 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2065 | |
| 2066 | unsigned DestWidth = 0; |
| 2067 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2068 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2069 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2070 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2071 | switch (Str[0]) { |
| 2072 | case 'Q': DestWidth = 8; break; |
| 2073 | case 'H': DestWidth = 16; break; |
| 2074 | case 'S': DestWidth = 32; break; |
| 2075 | case 'D': DestWidth = 64; break; |
| 2076 | case 'X': DestWidth = 96; break; |
| 2077 | case 'T': DestWidth = 128; break; |
| 2078 | } |
| 2079 | if (Str[1] == 'F') { |
| 2080 | IntegerMode = false; |
| 2081 | } else if (Str[1] == 'C') { |
| 2082 | IntegerMode = false; |
| 2083 | ComplexMode = true; |
| 2084 | } else if (Str[1] != 'I') { |
| 2085 | DestWidth = 0; |
| 2086 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2087 | break; |
| 2088 | case 4: |
| 2089 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2090 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2091 | if (Str == "word") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2092 | DestWidth = S.Context.Target.getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2093 | else if (Str == "byte") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2094 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2095 | break; |
| 2096 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2097 | if (Str == "pointer") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2098 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2099 | break; |
| 2100 | } |
| 2101 | |
| 2102 | QualType OldTy; |
| 2103 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) |
| 2104 | OldTy = TD->getUnderlyingType(); |
| 2105 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2106 | OldTy = VD->getType(); |
| 2107 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2108 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 2109 | << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2110 | return; |
| 2111 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2112 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2113 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2114 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2115 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2116 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2117 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2118 | } else if (ComplexMode) { |
| 2119 | if (!OldTy->isComplexType()) |
| 2120 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2121 | } else { |
| 2122 | if (!OldTy->isFloatingType()) |
| 2123 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2124 | } |
| 2125 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2126 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 2127 | // and friends, at least with glibc. |
| 2128 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 2129 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2130 | // FIXME: Make sure floating-point mappings are accurate |
| 2131 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2132 | QualType NewTy; |
| 2133 | switch (DestWidth) { |
| 2134 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2135 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2136 | return; |
| 2137 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2138 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2139 | return; |
| 2140 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2141 | if (!IntegerMode) { |
| 2142 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2143 | return; |
| 2144 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2145 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2146 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2147 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2148 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2149 | break; |
| 2150 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2151 | if (!IntegerMode) { |
| 2152 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2153 | return; |
| 2154 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2155 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2156 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2157 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2158 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2159 | break; |
| 2160 | case 32: |
| 2161 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2162 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2163 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2164 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2165 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2166 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2167 | break; |
| 2168 | case 64: |
| 2169 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2170 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2171 | else if (OldTy->isSignedIntegerType()) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2172 | if (S.Context.Target.getLongWidth() == 64) |
| 2173 | NewTy = S.Context.LongTy; |
| 2174 | else |
| 2175 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2176 | else |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2177 | if (S.Context.Target.getLongWidth() == 64) |
| 2178 | NewTy = S.Context.UnsignedLongTy; |
| 2179 | else |
| 2180 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2181 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2182 | case 96: |
| 2183 | NewTy = S.Context.LongDoubleTy; |
| 2184 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2185 | case 128: |
| 2186 | if (!IntegerMode) { |
| 2187 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2188 | return; |
| 2189 | } |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 2190 | if (OldTy->isSignedIntegerType()) |
| 2191 | NewTy = S.Context.Int128Ty; |
| 2192 | else |
| 2193 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2194 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2195 | } |
| 2196 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2197 | if (ComplexMode) { |
| 2198 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | // Install the new type. |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2202 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { |
| 2203 | // FIXME: preserve existing source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2204 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2205 | } else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2206 | cast<ValueDecl>(D)->setType(NewTy); |
| 2207 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2208 | |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 2209 | static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2210 | // check the attribute arguments. |
| 2211 | if (Attr.getNumArgs() > 0) { |
| 2212 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2213 | return; |
| 2214 | } |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 2215 | |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2216 | if (!isFunctionOrMethod(d)) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2217 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2218 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2219 | return; |
| 2220 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2221 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2222 | d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 2225 | static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2226 | // check the attribute arguments. |
| 2227 | if (Attr.getNumArgs() != 0) { |
| 2228 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2229 | return; |
| 2230 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2231 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2232 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2233 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2234 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2235 | return; |
| 2236 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2237 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2238 | d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2241 | static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr, |
| 2242 | Sema &S) { |
| 2243 | // check the attribute arguments. |
| 2244 | if (Attr.getNumArgs() != 0) { |
| 2245 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2246 | return; |
| 2247 | } |
| 2248 | |
| 2249 | if (!isa<FunctionDecl>(d)) { |
| 2250 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2251 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2252 | return; |
| 2253 | } |
| 2254 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2255 | d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), |
| 2256 | S.Context)); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2259 | static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2260 | if (S.LangOpts.CUDA) { |
| 2261 | // check the attribute arguments. |
| 2262 | if (Attr.getNumArgs() != 0) { |
| 2263 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2264 | return; |
| 2265 | } |
| 2266 | |
| 2267 | if (!isa<VarDecl>(d)) { |
| 2268 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2269 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2270 | return; |
| 2271 | } |
| 2272 | |
| 2273 | d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context)); |
| 2274 | } else { |
| 2275 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2280 | if (S.LangOpts.CUDA) { |
| 2281 | // check the attribute arguments. |
| 2282 | if (Attr.getNumArgs() != 0) { |
| 2283 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2284 | return; |
| 2285 | } |
| 2286 | |
| 2287 | if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) { |
| 2288 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2289 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2290 | return; |
| 2291 | } |
| 2292 | |
| 2293 | d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context)); |
| 2294 | } else { |
| 2295 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2300 | if (S.LangOpts.CUDA) { |
| 2301 | // check the attribute arguments. |
| 2302 | if (Attr.getNumArgs() != 0) { |
| 2303 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2304 | return; |
| 2305 | } |
| 2306 | |
| 2307 | if (!isa<FunctionDecl>(d)) { |
| 2308 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2309 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2310 | return; |
| 2311 | } |
| 2312 | |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2313 | FunctionDecl *FD = cast<FunctionDecl>(d); |
| 2314 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 2315 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2316 | if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { |
| 2317 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2318 | << FD->getType() |
| 2319 | << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), |
| 2320 | "void"); |
| 2321 | } else { |
| 2322 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2323 | << FD->getType(); |
| 2324 | } |
| 2325 | return; |
| 2326 | } |
| 2327 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2328 | d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context)); |
| 2329 | } else { |
| 2330 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 2331 | } |
| 2332 | } |
| 2333 | |
| 2334 | static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2335 | if (S.LangOpts.CUDA) { |
| 2336 | // check the attribute arguments. |
| 2337 | if (Attr.getNumArgs() != 0) { |
| 2338 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2339 | return; |
| 2340 | } |
| 2341 | |
| 2342 | if (!isa<FunctionDecl>(d)) { |
| 2343 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2344 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2345 | return; |
| 2346 | } |
| 2347 | |
| 2348 | d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context)); |
| 2349 | } else { |
| 2350 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2355 | if (S.LangOpts.CUDA) { |
| 2356 | // check the attribute arguments. |
| 2357 | if (Attr.getNumArgs() != 0) { |
| 2358 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2359 | return; |
| 2360 | } |
| 2361 | |
| 2362 | if (!isa<VarDecl>(d)) { |
| 2363 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2364 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2365 | return; |
| 2366 | } |
| 2367 | |
| 2368 | d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context)); |
| 2369 | } else { |
| 2370 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 2371 | } |
| 2372 | } |
| 2373 | |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 2374 | static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2375 | // check the attribute arguments. |
| 2376 | if (Attr.getNumArgs() != 0) { |
| 2377 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2378 | return; |
| 2379 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2380 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2381 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
| 2382 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2383 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2384 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2385 | return; |
| 2386 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2387 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 2388 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 2389 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2390 | return; |
| 2391 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2392 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2393 | d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2396 | static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) { |
| 2397 | if (hasDeclarator(d)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2398 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2399 | // Diagnostic is emitted elsewhere: here we store the (valid) attr |
| 2400 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 2401 | CallingConv CC; |
| 2402 | if (S.CheckCallingConvAttr(attr, CC)) |
| 2403 | return; |
| 2404 | |
| 2405 | if (!isa<ObjCMethodDecl>(d)) { |
| 2406 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2407 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2408 | return; |
| 2409 | } |
| 2410 | |
| 2411 | switch (attr.getKind()) { |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2412 | case AttributeList::AT_fastcall: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2413 | d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2414 | return; |
| 2415 | case AttributeList::AT_stdcall: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2416 | d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2417 | return; |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2418 | case AttributeList::AT_thiscall: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2419 | d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context)); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 2420 | return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2421 | case AttributeList::AT_cdecl: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2422 | d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2423 | return; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2424 | case AttributeList::AT_pascal: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2425 | d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context)); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2426 | return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2427 | default: |
| 2428 | llvm_unreachable("unexpected attribute kind"); |
| 2429 | return; |
| 2430 | } |
| 2431 | } |
| 2432 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2433 | static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){ |
| 2434 | assert(Attr.isInvalid() == false); |
| 2435 | d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context)); |
| 2436 | } |
| 2437 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2438 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) { |
| 2439 | if (attr.isInvalid()) |
| 2440 | return true; |
| 2441 | |
| 2442 | if (attr.getNumArgs() != 0) { |
| 2443 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2444 | attr.setInvalid(); |
| 2445 | return true; |
| 2446 | } |
| 2447 | |
| 2448 | // TODO: diagnose uses of these conventions on the wrong target. |
| 2449 | switch (attr.getKind()) { |
| 2450 | case AttributeList::AT_cdecl: CC = CC_C; break; |
| 2451 | case AttributeList::AT_fastcall: CC = CC_X86FastCall; break; |
| 2452 | case AttributeList::AT_stdcall: CC = CC_X86StdCall; break; |
| 2453 | case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break; |
| 2454 | case AttributeList::AT_pascal: CC = CC_X86Pascal; break; |
| 2455 | default: llvm_unreachable("unexpected attribute kind"); return true; |
| 2456 | } |
| 2457 | |
| 2458 | return false; |
| 2459 | } |
| 2460 | |
| 2461 | static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) { |
| 2462 | if (hasDeclarator(d)) return; |
| 2463 | |
| 2464 | unsigned numParams; |
| 2465 | if (S.CheckRegparmAttr(attr, numParams)) |
| 2466 | return; |
| 2467 | |
| 2468 | if (!isa<ObjCMethodDecl>(d)) { |
| 2469 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2470 | << attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2471 | return; |
| 2472 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2473 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2474 | d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams)); |
| 2475 | } |
| 2476 | |
| 2477 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 2478 | /// otherwise setting numParams to the appropriate value. |
| 2479 | bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) { |
| 2480 | if (attr.isInvalid()) |
| 2481 | return true; |
| 2482 | |
| 2483 | if (attr.getNumArgs() != 1) { |
| 2484 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2485 | attr.setInvalid(); |
| 2486 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2487 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2488 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2489 | Expr *NumParamsExpr = attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2490 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2491 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2492 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
| 2493 | Diag(attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2494 | << "regparm" << NumParamsExpr->getSourceRange(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2495 | attr.setInvalid(); |
| 2496 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2499 | if (Context.Target.getRegParmMax() == 0) { |
| 2500 | Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2501 | << NumParamsExpr->getSourceRange(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2502 | attr.setInvalid(); |
| 2503 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2506 | numParams = NumParams.getZExtValue(); |
| 2507 | if (numParams > Context.Target.getRegParmMax()) { |
| 2508 | Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
| 2509 | << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); |
| 2510 | attr.setInvalid(); |
| 2511 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2514 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2517 | static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){ |
| 2518 | if (S.LangOpts.CUDA) { |
| 2519 | // check the attribute arguments. |
| 2520 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2521 | // FIXME: 0 is not okay. |
| 2522 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2523 | return; |
| 2524 | } |
| 2525 | |
| 2526 | if (!isFunctionOrMethod(d)) { |
| 2527 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2528 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2529 | return; |
| 2530 | } |
| 2531 | |
| 2532 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 2533 | llvm::APSInt MaxThreads(32); |
| 2534 | if (MaxThreadsExpr->isTypeDependent() || |
| 2535 | MaxThreadsExpr->isValueDependent() || |
| 2536 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 2537 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2538 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 2539 | return; |
| 2540 | } |
| 2541 | |
| 2542 | llvm::APSInt MinBlocks(32); |
| 2543 | if (Attr.getNumArgs() > 1) { |
| 2544 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 2545 | if (MinBlocksExpr->isTypeDependent() || |
| 2546 | MinBlocksExpr->isValueDependent() || |
| 2547 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 2548 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2549 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 2550 | return; |
| 2551 | } |
| 2552 | } |
| 2553 | |
| 2554 | d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context, |
| 2555 | MaxThreads.getZExtValue(), |
| 2556 | MinBlocks.getZExtValue())); |
| 2557 | } else { |
| 2558 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 2559 | } |
| 2560 | } |
| 2561 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2562 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2563 | // Checker-specific attribute handlers. |
| 2564 | //===----------------------------------------------------------------------===// |
| 2565 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2566 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
| 2567 | return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type); |
| 2568 | } |
| 2569 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
| 2570 | return type->isPointerType() || isValidSubjectOfNSAttribute(S, type); |
| 2571 | } |
| 2572 | |
| 2573 | static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) { |
| 2574 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(d); |
| 2575 | if (!param) { |
| 2576 | S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2577 | << SourceRange(attr.getLoc()) << attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2578 | return; |
| 2579 | } |
| 2580 | |
| 2581 | bool typeOK, cf; |
| 2582 | if (attr.getKind() == AttributeList::AT_ns_consumed) { |
| 2583 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 2584 | cf = false; |
| 2585 | } else { |
| 2586 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 2587 | cf = true; |
| 2588 | } |
| 2589 | |
| 2590 | if (!typeOK) { |
| 2591 | S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 2592 | << SourceRange(attr.getLoc()) << attr.getName() << cf; |
| 2593 | return; |
| 2594 | } |
| 2595 | |
| 2596 | if (cf) |
| 2597 | param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context)); |
| 2598 | else |
| 2599 | param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context)); |
| 2600 | } |
| 2601 | |
| 2602 | static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr, |
| 2603 | Sema &S) { |
| 2604 | if (!isa<ObjCMethodDecl>(d)) { |
| 2605 | S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2606 | << SourceRange(attr.getLoc()) << attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2607 | return; |
| 2608 | } |
| 2609 | |
| 2610 | d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context)); |
| 2611 | } |
| 2612 | |
| 2613 | static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr, |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2614 | Sema &S) { |
| 2615 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2616 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2617 | |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2618 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2619 | returnType = MD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2620 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2621 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2622 | else { |
Ted Kremenek | 21531fa | 2009-08-19 23:56:48 +0000 | [diff] [blame] | 2623 | S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2624 | << SourceRange(attr.getLoc()) << attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2625 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2626 | return; |
| 2627 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2628 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2629 | bool typeOK; |
| 2630 | bool cf; |
| 2631 | switch (attr.getKind()) { |
| 2632 | default: llvm_unreachable("invalid ownership attribute"); return; |
| 2633 | case AttributeList::AT_ns_returns_autoreleased: |
| 2634 | case AttributeList::AT_ns_returns_retained: |
| 2635 | case AttributeList::AT_ns_returns_not_retained: |
| 2636 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 2637 | cf = false; |
| 2638 | break; |
| 2639 | |
| 2640 | case AttributeList::AT_cf_returns_retained: |
| 2641 | case AttributeList::AT_cf_returns_not_retained: |
| 2642 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 2643 | cf = true; |
| 2644 | break; |
| 2645 | } |
| 2646 | |
| 2647 | if (!typeOK) { |
Ted Kremenek | 21531fa | 2009-08-19 23:56:48 +0000 | [diff] [blame] | 2648 | S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2649 | << SourceRange(attr.getLoc()) |
| 2650 | << attr.getName() << isa<ObjCMethodDecl>(d) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2651 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2652 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2653 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2654 | switch (attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2655 | default: |
| 2656 | assert(0 && "invalid ownership attribute"); |
| 2657 | return; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2658 | case AttributeList::AT_ns_returns_autoreleased: |
| 2659 | d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(), |
| 2660 | S.Context)); |
| 2661 | return; |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2662 | case AttributeList::AT_cf_returns_not_retained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2663 | d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2664 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2665 | return; |
| 2666 | case AttributeList::AT_ns_returns_not_retained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2667 | d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2668 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2669 | return; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2670 | case AttributeList::AT_cf_returns_retained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2671 | d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2672 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2673 | return; |
| 2674 | case AttributeList::AT_ns_returns_retained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2675 | d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2676 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2677 | return; |
| 2678 | }; |
| 2679 | } |
| 2680 | |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 2681 | static bool isKnownDeclSpecAttr(const AttributeList &Attr) { |
| 2682 | return Attr.getKind() == AttributeList::AT_dllimport || |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2683 | Attr.getKind() == AttributeList::AT_dllexport || |
| 2684 | Attr.getKind() == AttributeList::AT_uuid; |
| 2685 | } |
| 2686 | |
| 2687 | //===----------------------------------------------------------------------===// |
| 2688 | // Microsoft specific attribute handlers. |
| 2689 | //===----------------------------------------------------------------------===// |
| 2690 | |
| 2691 | static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2692 | if (S.LangOpts.Microsoft || S.LangOpts.Borland) { |
| 2693 | // check the attribute arguments. |
| 2694 | if (Attr.getNumArgs() != 1) { |
| 2695 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2696 | return; |
| 2697 | } |
| 2698 | Expr *Arg = Attr.getArg(0); |
| 2699 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2700 | if (Str == 0 || Str->isWide()) { |
| 2701 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 2702 | << "uuid" << 1; |
| 2703 | return; |
| 2704 | } |
| 2705 | |
| 2706 | llvm::StringRef StrRef = Str->getString(); |
| 2707 | |
| 2708 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 2709 | StrRef.back() == '}'; |
| 2710 | |
| 2711 | // Validate GUID length. |
| 2712 | if (IsCurly && StrRef.size() != 38) { |
| 2713 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2714 | return; |
| 2715 | } |
| 2716 | if (!IsCurly && StrRef.size() != 36) { |
| 2717 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2718 | return; |
| 2719 | } |
| 2720 | |
| 2721 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 2722 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 2723 | llvm::StringRef::iterator I = StrRef.begin(); |
| 2724 | if (IsCurly) // Skip the optional '{' |
| 2725 | ++I; |
| 2726 | |
| 2727 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2728 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 2729 | if (*I != '-') { |
| 2730 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2731 | return; |
| 2732 | } |
| 2733 | } else if (!isxdigit(*I)) { |
| 2734 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2735 | return; |
| 2736 | } |
| 2737 | I++; |
| 2738 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2739 | |
| 2740 | d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context, |
| 2741 | Str->getString())); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2742 | } else |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2743 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 2744 | } |
| 2745 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2746 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2747 | // Top Level Sema Entry Points |
| 2748 | //===----------------------------------------------------------------------===// |
| 2749 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2750 | static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D, |
| 2751 | const AttributeList &Attr, Sema &S) { |
| 2752 | switch (Attr.getKind()) { |
| 2753 | case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break; |
| 2754 | case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break; |
| 2755 | case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break; |
| 2756 | default: |
| 2757 | break; |
| 2758 | } |
| 2759 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2760 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2761 | static void ProcessInheritableDeclAttr(Scope *scope, Decl *D, |
| 2762 | const AttributeList &Attr, Sema &S) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2763 | switch (Attr.getKind()) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 2764 | case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break; |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 2765 | case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break; |
| 2766 | case AttributeList::AT_IBOutletCollection: |
| 2767 | HandleIBOutletCollection(D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2768 | case AttributeList::AT_address_space: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2769 | case AttributeList::AT_opencl_image_access: |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 2770 | case AttributeList::AT_objc_gc: |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 2771 | case AttributeList::AT_vector_size: |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 2772 | case AttributeList::AT_neon_vector_type: |
| 2773 | case AttributeList::AT_neon_polyvector_type: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2774 | // Ignore these, these are type attributes, handled by |
| 2775 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2776 | break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2777 | case AttributeList::AT_device: |
| 2778 | case AttributeList::AT_host: |
| 2779 | case AttributeList::AT_overloadable: |
| 2780 | // Ignore, this is a non-inheritable attribute, handled |
| 2781 | // by ProcessNonInheritableDeclAttr. |
| 2782 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2783 | case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; |
| 2784 | case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2785 | case AttributeList::AT_always_inline: |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 2786 | HandleAlwaysInlineAttr (D, Attr, S); break; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 2787 | case AttributeList::AT_analyzer_noreturn: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2788 | HandleAnalyzerNoReturnAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2789 | case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame^] | 2790 | case AttributeList::AT_availability:HandleAvailabilityAttr(D, Attr, S); break; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2791 | case AttributeList::AT_carries_dependency: |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2792 | HandleDependencyAttr (D, Attr, S); break; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 2793 | case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2794 | case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2795 | case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break; |
| 2796 | case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break; |
| 2797 | case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2798 | case AttributeList::AT_ext_vector_type: |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2799 | HandleExtVectorTypeAttr(scope, D, Attr, S); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2800 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2801 | case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; |
| 2802 | case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2803 | case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2804 | case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2805 | case AttributeList::AT_launch_bounds: |
| 2806 | HandleLaunchBoundsAttr(D, Attr, S); |
| 2807 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2808 | case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; |
| 2809 | case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 2810 | case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 2811 | case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2812 | case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 2813 | case AttributeList::AT_ownership_returns: |
| 2814 | case AttributeList::AT_ownership_takes: |
| 2815 | case AttributeList::AT_ownership_holds: |
| 2816 | HandleOwnershipAttr (D, Attr, S); break; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 2817 | case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2818 | case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; |
| 2819 | case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2820 | case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2821 | case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2822 | |
| 2823 | // Checker-specific. |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2824 | case AttributeList::AT_cf_consumed: |
| 2825 | case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break; |
| 2826 | case AttributeList::AT_ns_consumes_self: |
| 2827 | HandleNSConsumesSelfAttr(D, Attr, S); break; |
| 2828 | |
| 2829 | case AttributeList::AT_ns_returns_autoreleased: |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2830 | case AttributeList::AT_ns_returns_not_retained: |
| 2831 | case AttributeList::AT_cf_returns_not_retained: |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2832 | case AttributeList::AT_ns_returns_retained: |
| 2833 | case AttributeList::AT_cf_returns_retained: |
| 2834 | HandleNSReturnsRetainedAttr(D, Attr, S); break; |
| 2835 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2836 | case AttributeList::AT_reqd_wg_size: |
| 2837 | HandleReqdWorkGroupSize(D, Attr, S); break; |
| 2838 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2839 | case AttributeList::AT_init_priority: |
| 2840 | HandleInitPriorityAttr(D, Attr, S); break; |
| 2841 | |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2842 | case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; |
| 2843 | case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2844 | case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break; |
| 2845 | case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; |
| 2846 | case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2847 | case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2848 | case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S); |
| 2849 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2850 | case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2851 | case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2852 | case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2853 | case AttributeList::AT_transparent_union: |
| 2854 | HandleTransparentUnionAttr(D, Attr, S); |
| 2855 | break; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2856 | case AttributeList::AT_objc_exception: |
| 2857 | HandleObjCExceptionAttr(D, Attr, S); |
| 2858 | break; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2859 | case AttributeList::AT_objc_method_family: |
| 2860 | HandleObjCMethodFamilyAttr(D, Attr, S); |
| 2861 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2862 | case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break; |
| 2863 | case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break; |
| 2864 | case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break; |
| 2865 | case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break; |
| 2866 | case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break; |
| 2867 | case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break; |
| 2868 | case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break; |
| 2869 | case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break; |
| 2870 | case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2871 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 2872 | // Just ignore |
| 2873 | break; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2874 | case AttributeList::AT_no_instrument_function: // Interacts with -pg. |
| 2875 | HandleNoInstrumentFunctionAttr(D, Attr, S); |
| 2876 | break; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2877 | case AttributeList::AT_stdcall: |
| 2878 | case AttributeList::AT_cdecl: |
| 2879 | case AttributeList::AT_fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2880 | case AttributeList::AT_thiscall: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2881 | case AttributeList::AT_pascal: |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2882 | HandleCallConvAttr(D, Attr, S); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2883 | break; |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2884 | case AttributeList::AT_opencl_kernel_function: |
| 2885 | HandleOpenCLKernelAttr(D, Attr, S); |
| 2886 | break; |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2887 | case AttributeList::AT_uuid: |
| 2888 | HandleUuidAttr(D, Attr, S); |
| 2889 | break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2890 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2891 | // Ask target about the attribute. |
| 2892 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 2893 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Chandler Carruth | 7d5c45e | 2010-07-08 09:42:26 +0000 | [diff] [blame] | 2894 | S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) |
| 2895 | << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2896 | break; |
| 2897 | } |
| 2898 | } |
| 2899 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2900 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 2901 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 2902 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 2903 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
| 2904 | static void ProcessDeclAttribute(Scope *scope, Decl *D, |
| 2905 | const AttributeList &Attr, Sema &S, |
| 2906 | bool NonInheritable, bool Inheritable) { |
| 2907 | if (Attr.isInvalid()) |
| 2908 | return; |
| 2909 | |
| 2910 | if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) |
| 2911 | // FIXME: Try to deal with other __declspec attributes! |
| 2912 | return; |
| 2913 | |
| 2914 | if (NonInheritable) |
| 2915 | ProcessNonInheritableDeclAttr(scope, D, Attr, S); |
| 2916 | |
| 2917 | if (Inheritable) |
| 2918 | ProcessInheritableDeclAttr(scope, D, Attr, S); |
| 2919 | } |
| 2920 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2921 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 2922 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2923 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2924 | const AttributeList *AttrList, |
| 2925 | bool NonInheritable, bool Inheritable) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2926 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2927 | ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | // GCC accepts |
| 2931 | // static int a9 __attribute__((weakref)); |
| 2932 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2933 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2934 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 2935 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2936 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2937 | } |
| 2938 | } |
| 2939 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2940 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
| 2941 | /// #pragma weak needs a non-definition decl and source may not have one |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2942 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 2943 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2944 | NamedDecl *NewD = 0; |
| 2945 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 2946 | NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2947 | FD->getInnerLocStart(), |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2948 | FD->getLocation(), DeclarationName(II), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2949 | FD->getType(), FD->getTypeSourceInfo()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2950 | if (FD->getQualifier()) { |
| 2951 | FunctionDecl *NewFD = cast<FunctionDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2952 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2953 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2954 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 2955 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2956 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2957 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 2958 | VD->getStorageClass(), |
| 2959 | VD->getStorageClassAsWritten()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2960 | if (VD->getQualifier()) { |
| 2961 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 2962 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 2963 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2964 | } |
| 2965 | return NewD; |
| 2966 | } |
| 2967 | |
| 2968 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak |
| 2969 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 2970 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 2971 | if (W.getUsed()) return; // only do this once |
| 2972 | W.setUsed(true); |
| 2973 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 2974 | IdentifierInfo *NDId = ND->getIdentifier(); |
| 2975 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2976 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 2977 | NDId->getName())); |
| 2978 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 2979 | WeakTopLevelDecl.push_back(NewD); |
| 2980 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 2981 | // to insert Decl at TU scope, sorry. |
| 2982 | DeclContext *SavedContext = CurContext; |
| 2983 | CurContext = Context.getTranslationUnitDecl(); |
| 2984 | PushOnScopeChains(NewD, S); |
| 2985 | CurContext = SavedContext; |
| 2986 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2987 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2988 | } |
| 2989 | } |
| 2990 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2991 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 2992 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 2993 | /// specified in many different places, and we need to find and apply them all. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2994 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 2995 | bool NonInheritable, bool Inheritable) { |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 2996 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 2997 | // have to do this. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2998 | if (Inheritable && !WeakUndeclaredIdentifiers.empty()) { |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 2999 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 3000 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 3001 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 3002 | = WeakUndeclaredIdentifiers.find(Id); |
| 3003 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 3004 | WeakInfo W = I->second; |
| 3005 | DeclApplyPragmaWeak(S, ND, W); |
| 3006 | WeakUndeclaredIdentifiers[Id] = W; |
| 3007 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3008 | } |
| 3009 | } |
| 3010 | } |
| 3011 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3012 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3013 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3014 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3015 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3016 | // Walk the declarator structure, applying decl attributes that were in a type |
| 3017 | // position to the decl itself. This handles cases like: |
| 3018 | // int *__attr__(x)** D; |
| 3019 | // when X is a decl attribute. |
| 3020 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 3021 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3022 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3023 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3024 | // Finally, apply any attributes on the decl itself. |
| 3025 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3026 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3027 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3028 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3029 | // This duplicates a vector push_back but hides the need to know the |
| 3030 | // size of the type. |
| 3031 | void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) { |
| 3032 | assert(StackSize <= StackCapacity); |
| 3033 | |
| 3034 | // Grow the stack if necessary. |
| 3035 | if (StackSize == StackCapacity) { |
| 3036 | unsigned newCapacity = 2 * StackCapacity + 2; |
| 3037 | char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)]; |
| 3038 | const char *oldBuffer = (const char*) Stack; |
| 3039 | |
| 3040 | if (StackCapacity) |
| 3041 | memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic)); |
| 3042 | |
| 3043 | delete[] oldBuffer; |
| 3044 | Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer); |
| 3045 | StackCapacity = newCapacity; |
| 3046 | } |
| 3047 | |
| 3048 | assert(StackSize < StackCapacity); |
| 3049 | new (&Stack[StackSize++]) DelayedDiagnostic(diag); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3052 | void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state, |
| 3053 | Decl *decl) { |
| 3054 | DelayedDiagnostics &DD = S.DelayedDiagnostics; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3055 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3056 | // Check the invariants. |
| 3057 | assert(DD.StackSize >= state.SavedStackSize); |
| 3058 | assert(state.SavedStackSize >= DD.ActiveStackBase); |
| 3059 | assert(DD.ParsingDepth > 0); |
| 3060 | |
| 3061 | // Drop the parsing depth. |
| 3062 | DD.ParsingDepth--; |
| 3063 | |
| 3064 | // If there are no active diagnostics, we're done. |
| 3065 | if (DD.StackSize == DD.ActiveStackBase) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3066 | return; |
| 3067 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3068 | // We only want to actually emit delayed diagnostics when we |
| 3069 | // successfully parsed a decl. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3070 | if (decl) { |
| 3071 | // We emit all the active diagnostics, not just those starting |
| 3072 | // from the saved state. The idea is this: we get one push for a |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3073 | // decl spec and another for each declarator; in a decl group like: |
| 3074 | // deprecated_typedef foo, *bar, baz(); |
| 3075 | // only the declarator pops will be passed decls. This is correct; |
| 3076 | // we really do need to consider delayed diagnostics from the decl spec |
| 3077 | // for each of the different declarations. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3078 | for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) { |
| 3079 | DelayedDiagnostic &diag = DD.Stack[i]; |
| 3080 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3081 | continue; |
| 3082 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3083 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3084 | case DelayedDiagnostic::Deprecation: |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3085 | S.HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3086 | break; |
| 3087 | |
| 3088 | case DelayedDiagnostic::Access: |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3089 | S.HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3090 | break; |
| 3091 | } |
| 3092 | } |
| 3093 | } |
| 3094 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3095 | // Destroy all the delayed diagnostics we're about to pop off. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3096 | for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i) |
| 3097 | DD.Stack[i].destroy(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3098 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3099 | DD.StackSize = state.SavedStackSize; |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | static bool isDeclDeprecated(Decl *D) { |
| 3103 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame^] | 3104 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3105 | return true; |
| 3106 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 3107 | return false; |
| 3108 | } |
| 3109 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 3110 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3111 | Decl *Ctx) { |
| 3112 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3113 | return; |
| 3114 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3115 | DD.Triggered = true; |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3116 | if (!DD.getDeprecationMessage().empty()) |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3117 | Diag(DD.Loc, diag::warn_deprecated_message) |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3118 | << DD.getDeprecationDecl()->getDeclName() |
| 3119 | << DD.getDeprecationMessage(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3120 | else |
| 3121 | Diag(DD.Loc, diag::warn_deprecated) |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3122 | << DD.getDeprecationDecl()->getDeclName(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3125 | void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3126 | SourceLocation Loc, |
Peter Collingbourne | 743b82b | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 3127 | bool UnknownObjCClass) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3128 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3129 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 3130 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3131 | return; |
| 3132 | } |
| 3133 | |
| 3134 | // Otherwise, don't warn if our current context is deprecated. |
| 3135 | if (isDeclDeprecated(cast<Decl>(CurContext))) |
| 3136 | return; |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3137 | if (!Message.empty()) |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3138 | Diag(Loc, diag::warn_deprecated_message) << D->getDeclName() |
| 3139 | << Message; |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3140 | else { |
Peter Collingbourne | 743b82b | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 3141 | if (!UnknownObjCClass) |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3142 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 3143 | else |
| 3144 | Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName(); |
| 3145 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3146 | } |