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(); |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 58 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(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) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 104 | // In some sense, TypedefNameDecl really *ought* to be a DeclaratorDecl. |
| 105 | return isa<DeclaratorDecl>(d) || isa<BlockDecl>(d) || isa<TypedefNameDecl>(d); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 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) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 205 | TypedefNameDecl *tDecl = dyn_cast<TypedefNameDecl>(d); |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 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. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 755 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 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. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 771 | if (Attr.hasParameterOrArguments()) { |
Ryan Flynn | 76168e2 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 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) { |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 830 | if (attr.hasParameterOrArguments()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 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. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 938 | if (Attr.hasParameterOrArguments()) { |
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. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 955 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | b805dad | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 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(); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1094 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1095 | |
| 1096 | // Ensure that Introduced < Deprecated < Obsoleted (although not all |
| 1097 | // of these steps are needed). |
| 1098 | if (Introduced.isValid() && Deprecated.isValid() && |
| 1099 | !(Introduced.Version < Deprecated.Version)) { |
| 1100 | S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) |
| 1101 | << 1 << PlatformName << Deprecated.Version.getAsString() |
| 1102 | << 0 << Introduced.Version.getAsString(); |
| 1103 | return; |
| 1104 | } |
| 1105 | |
| 1106 | if (Introduced.isValid() && Obsoleted.isValid() && |
| 1107 | !(Introduced.Version < Obsoleted.Version)) { |
| 1108 | S.Diag(Introduced.KeywordLoc, diag::warn_availability_version_ordering) |
| 1109 | << 2 << PlatformName << Obsoleted.Version.getAsString() |
| 1110 | << 0 << Introduced.Version.getAsString(); |
| 1111 | return; |
| 1112 | } |
| 1113 | |
| 1114 | if (Deprecated.isValid() && Obsoleted.isValid() && |
| 1115 | !(Deprecated.Version < Obsoleted.Version)) { |
| 1116 | S.Diag(Deprecated.KeywordLoc, diag::warn_availability_version_ordering) |
| 1117 | << 2 << PlatformName << Obsoleted.Version.getAsString() |
| 1118 | << 1 << Deprecated.Version.getAsString(); |
| 1119 | return; |
| 1120 | } |
| 1121 | |
| 1122 | d->addAttr(::new (S.Context) AvailabilityAttr(Attr.getLoc(), S.Context, |
| 1123 | Platform, |
| 1124 | Introduced.Version, |
| 1125 | Deprecated.Version, |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1126 | Obsoleted.Version, |
| 1127 | IsUnavailable)); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1130 | static void HandleVisibilityAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1131 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1132 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1133 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1134 | return; |
| 1135 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1136 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1137 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1138 | Arg = Arg->IgnoreParenCasts(); |
| 1139 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1140 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1141 | if (Str == 0 || Str->isWide()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1142 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1143 | << "visibility" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1144 | return; |
| 1145 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1146 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1147 | llvm::StringRef TypeStr = Str->getString(); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1148 | VisibilityAttr::VisibilityType type; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1149 | |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1150 | if (TypeStr == "default") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1151 | type = VisibilityAttr::Default; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1152 | else if (TypeStr == "hidden") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1153 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1154 | else if (TypeStr == "internal") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1155 | type = VisibilityAttr::Hidden; // FIXME |
Benjamin Kramer | c96f494 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 1156 | else if (TypeStr == "protected") |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1157 | type = VisibilityAttr::Protected; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1158 | else { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1159 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1160 | return; |
| 1161 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1162 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1163 | d->addAttr(::new (S.Context) VisibilityAttr(Attr.getLoc(), S.Context, type)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1166 | static void HandleObjCMethodFamilyAttr(Decl *decl, const AttributeList &attr, |
| 1167 | Sema &S) { |
| 1168 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 1169 | if (!method) { |
| 1170 | S.Diag(attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1171 | << ExpectedMethod; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 1172 | return; |
| 1173 | } |
| 1174 | |
| 1175 | if (attr.getNumArgs() != 0 || !attr.getParameterName()) { |
| 1176 | if (!attr.getParameterName() && attr.getNumArgs() == 1) { |
| 1177 | S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1178 | << "objc_method_family" << 1; |
| 1179 | } else { |
| 1180 | S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1181 | } |
| 1182 | attr.setInvalid(); |
| 1183 | return; |
| 1184 | } |
| 1185 | |
| 1186 | llvm::StringRef param = attr.getParameterName()->getName(); |
| 1187 | ObjCMethodFamilyAttr::FamilyKind family; |
| 1188 | if (param == "none") |
| 1189 | family = ObjCMethodFamilyAttr::OMF_None; |
| 1190 | else if (param == "alloc") |
| 1191 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 1192 | else if (param == "copy") |
| 1193 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 1194 | else if (param == "init") |
| 1195 | family = ObjCMethodFamilyAttr::OMF_init; |
| 1196 | else if (param == "mutableCopy") |
| 1197 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 1198 | else if (param == "new") |
| 1199 | family = ObjCMethodFamilyAttr::OMF_new; |
| 1200 | else { |
| 1201 | // Just warn and ignore it. This is future-proof against new |
| 1202 | // families being used in system headers. |
| 1203 | S.Diag(attr.getParameterLoc(), diag::warn_unknown_method_family); |
| 1204 | return; |
| 1205 | } |
| 1206 | |
| 1207 | decl->addAttr(new (S.Context) ObjCMethodFamilyAttr(attr.getLoc(), |
| 1208 | S.Context, family)); |
| 1209 | } |
| 1210 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1211 | static void HandleObjCExceptionAttr(Decl *D, const AttributeList &Attr, |
| 1212 | Sema &S) { |
| 1213 | if (Attr.getNumArgs() != 0) { |
| 1214 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1215 | return; |
| 1216 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1218 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 1219 | if (OCI == 0) { |
| 1220 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 1221 | return; |
| 1222 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1223 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1224 | D->addAttr(::new (S.Context) ObjCExceptionAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 1225 | } |
| 1226 | |
| 1227 | static void HandleObjCNSObject(Decl *D, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1228 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1229 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1230 | return; |
| 1231 | } |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1232 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1233 | QualType T = TD->getUnderlyingType(); |
| 1234 | if (!T->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1235 | !T->getAs<PointerType>()->getPointeeType()->isRecordType()) { |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1236 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 1237 | return; |
| 1238 | } |
| 1239 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1240 | D->addAttr(::new (S.Context) ObjCNSObjectAttr(Attr.getLoc(), S.Context)); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1243 | static void |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1244 | HandleOverloadableAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1245 | if (Attr.getNumArgs() != 0) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1246 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1247 | return; |
| 1248 | } |
| 1249 | |
| 1250 | if (!isa<FunctionDecl>(D)) { |
| 1251 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 1252 | return; |
| 1253 | } |
| 1254 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1255 | D->addAttr(::new (S.Context) OverloadableAttr(Attr.getLoc(), S.Context)); |
Douglas Gregor | f9201e0 | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1258 | static void HandleBlocksAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1259 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1260 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1261 | << "blocks" << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1262 | return; |
| 1263 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1264 | |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1265 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1266 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1267 | return; |
| 1268 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1269 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1270 | BlocksAttr::BlockType type; |
Chris Lattner | 92e62b0 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 1271 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1272 | type = BlocksAttr::ByRef; |
| 1273 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1274 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1275 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1276 | return; |
| 1277 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1278 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1279 | d->addAttr(::new (S.Context) BlocksAttr(Attr.getLoc(), S.Context, type)); |
Steve Naroff | 9eae576 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1282 | static void HandleSentinelAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1283 | // check the attribute arguments. |
| 1284 | if (Attr.getNumArgs() > 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1285 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1286 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1289 | int sentinel = 0; |
| 1290 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1291 | Expr *E = Attr.getArg(0); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1292 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1293 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1294 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1295 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1296 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1297 | return; |
| 1298 | } |
| 1299 | sentinel = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1300 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1301 | if (sentinel < 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1302 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 1303 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1304 | return; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | int nullPos = 0; |
| 1309 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1310 | Expr *E = Attr.getArg(1); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1311 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1312 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1313 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1314 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1315 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1316 | return; |
| 1317 | } |
| 1318 | nullPos = Idx.getZExtValue(); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1319 | |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1320 | if (nullPos > 1 || nullPos < 0) { |
| 1321 | // FIXME: This error message could be improved, it would be nice |
| 1322 | // to say what the bounds actually are. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1323 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 1324 | << E->getSourceRange(); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1325 | return; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1330 | const FunctionType *FT = FD->getType()->getAs<FunctionType>(); |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1331 | assert(FT && "FunctionDecl has non-function type?"); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1332 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1333 | if (isa<FunctionNoProtoType>(FT)) { |
| 1334 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 1335 | return; |
| 1336 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1337 | |
Chris Lattner | 897cd90 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 1338 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1339 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1340 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1341 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1342 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) { |
| 1343 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1344 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1345 | return; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1346 | } |
| 1347 | } else if (isa<BlockDecl>(d)) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1348 | // Note! BlockDecl is typeless. Variadic diagnostics will be issued by the |
| 1349 | // caller. |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1350 | ; |
| 1351 | } else if (const VarDecl *V = dyn_cast<VarDecl>(d)) { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1352 | QualType Ty = V->getType(); |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 1353 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1354 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(d) |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1355 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1356 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 3bba33d | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 1357 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 1358 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1359 | return; |
| 1360 | } |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 1361 | } else { |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1362 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1363 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 2f7c392 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 1364 | return; |
| 1365 | } |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1366 | } else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1367 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1368 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1369 | return; |
| 1370 | } |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1371 | d->addAttr(::new (S.Context) SentinelAttr(Attr.getLoc(), S.Context, sentinel, |
| 1372 | nullPos)); |
Anders Carlsson | 7709182 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1375 | static void HandleWarnUnusedResult(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1376 | // check the attribute arguments. |
| 1377 | if (Attr.getNumArgs() != 0) { |
| 1378 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1379 | return; |
| 1380 | } |
| 1381 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1382 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D)) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1383 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1384 | << Attr.getName() << ExpectedFunctionOrMethod; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1385 | return; |
| 1386 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1387 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1388 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 1389 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1390 | << Attr.getName() << 0; |
Nuno Lopes | f857798 | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 1391 | return; |
| 1392 | } |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1393 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 1394 | if (MD->getResultType()->isVoidType()) { |
| 1395 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 1396 | << Attr.getName() << 1; |
| 1397 | return; |
| 1398 | } |
| 1399 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1400 | D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1403 | static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1404 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1405 | if (attr.hasParameterOrArguments()) { |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1406 | S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1407 | return; |
| 1408 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1409 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1410 | if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) { |
| 1411 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1412 | << attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | f23ecd9 | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 1413 | return; |
| 1414 | } |
| 1415 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1416 | NamedDecl *nd = cast<NamedDecl>(d); |
| 1417 | |
| 1418 | // 'weak' only applies to declarations with external linkage. |
| 1419 | if (hasEffectivelyInternalLinkage(nd)) { |
| 1420 | S.Diag(attr.getLoc(), diag::err_attribute_weak_static); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1421 | return; |
| 1422 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1423 | |
John McCall | 332bb2a | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1424 | nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1427 | static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
| 1428 | // check the attribute arguments. |
| 1429 | if (Attr.getNumArgs() != 0) { |
| 1430 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1431 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1432 | } |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1433 | |
| 1434 | // weak_import only applies to variable & function declarations. |
| 1435 | bool isDef = false; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1436 | if (!D->canBeWeakImported(isDef)) { |
| 1437 | if (isDef) |
| 1438 | S.Diag(Attr.getLoc(), |
| 1439 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 1440 | << "weak_import" << 2 /*variable and function*/; |
Douglas Gregor | def8631 | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 1441 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
| 1442 | (S.Context.Target.getTriple().getOS() == llvm::Triple::Darwin && |
| 1443 | isa<ObjCInterfaceDecl>(D))) { |
| 1444 | // Nothing to warn about here. |
| 1445 | } else |
Fariborz Jahanian | c034974 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 1446 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1447 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1448 | |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1449 | return; |
| 1450 | } |
| 1451 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1452 | D->addAttr(::new (S.Context) WeakImportAttr(Attr.getLoc(), S.Context)); |
Daniel Dunbar | 6e775db | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1455 | static void HandleReqdWorkGroupSize(Decl *D, const AttributeList &Attr, |
| 1456 | Sema &S) { |
| 1457 | // Attribute has 3 arguments. |
| 1458 | if (Attr.getNumArgs() != 3) { |
John McCall | 2b7baf0 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 1459 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1460 | return; |
| 1461 | } |
| 1462 | |
| 1463 | unsigned WGSize[3]; |
| 1464 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1465 | Expr *E = Attr.getArg(i); |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1466 | llvm::APSInt ArgNum(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1467 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1468 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1469 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1470 | << "reqd_work_group_size" << E->getSourceRange(); |
| 1471 | return; |
| 1472 | } |
| 1473 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 1474 | } |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1475 | D->addAttr(::new (S.Context) ReqdWorkGroupSizeAttr(Attr.getLoc(), S.Context, |
| 1476 | WGSize[0], WGSize[1], |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 1477 | WGSize[2])); |
| 1478 | } |
| 1479 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1480 | static void HandleSectionAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1481 | // Attribute has no arguments. |
| 1482 | if (Attr.getNumArgs() != 1) { |
| 1483 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1484 | return; |
| 1485 | } |
| 1486 | |
| 1487 | // Make sure that there is a string literal as the sections's single |
| 1488 | // argument. |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1489 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1490 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1491 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1492 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1493 | return; |
| 1494 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1495 | |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1496 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | bb377ed | 2009-11-30 17:08:26 +0000 | [diff] [blame] | 1497 | std::string Error = S.Context.Target.isValidSectionSpecifier(SE->getString()); |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1498 | if (!Error.empty()) { |
| 1499 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 1500 | << Error; |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1501 | return; |
| 1502 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1503 | |
Chris Lattner | a1e1dc7 | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 1504 | // This attribute cannot be applied to local variables. |
| 1505 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 1506 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 1507 | return; |
| 1508 | } |
| 1509 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1510 | D->addAttr(::new (S.Context) SectionAttr(Attr.getLoc(), S.Context, |
| 1511 | SE->getString())); |
Daniel Dunbar | 17f194f | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1514 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1515 | static void HandleNothrowAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1516 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1517 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1518 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1519 | return; |
| 1520 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1521 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1522 | d->addAttr(::new (S.Context) NoThrowAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1525 | static void HandleConstAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1526 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1527 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1528 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1529 | return; |
| 1530 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1531 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1532 | d->addAttr(::new (S.Context) ConstAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | static void HandlePureAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 1536 | // check the attribute arguments. |
| 1537 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1538 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1539 | return; |
| 1540 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1541 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1542 | d->addAttr(::new (S.Context) PureAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 232eb7d | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1545 | static void HandleCleanupAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1546 | if (!Attr.getParameterName()) { |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1547 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1548 | return; |
| 1549 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1550 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1551 | if (Attr.getNumArgs() != 0) { |
| 1552 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1553 | return; |
| 1554 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1555 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1556 | VarDecl *VD = dyn_cast<VarDecl>(d); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1557 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1558 | if (!VD || !VD->hasLocalStorage()) { |
| 1559 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 1560 | return; |
| 1561 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1562 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1563 | // Look up the function |
Douglas Gregor | c83c687 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 1564 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | f36e02d | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 1565 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1566 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 1567 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1568 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1569 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1570 | Attr.getParameterName(); |
| 1571 | return; |
| 1572 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1573 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1574 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 1575 | if (!FD) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1576 | S.Diag(Attr.getParameterLoc(), |
| 1577 | diag::err_attribute_cleanup_arg_not_function) |
| 1578 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1579 | return; |
| 1580 | } |
| 1581 | |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1582 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1583 | S.Diag(Attr.getParameterLoc(), |
| 1584 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 1585 | << Attr.getParameterName(); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1586 | return; |
| 1587 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1588 | |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1589 | // We're currently more strict than GCC about what function types we accept. |
| 1590 | // If this ever proves to be a problem it should be easy to fix. |
| 1591 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 1592 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 1593 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 1594 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | f0b0ccc | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 1595 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 89941c1 | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 1596 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 1597 | Attr.getParameterName() << ParamTy << Ty; |
| 1598 | return; |
| 1599 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1600 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1601 | d->addAttr(::new (S.Context) CleanupAttr(Attr.getLoc(), S.Context, FD)); |
Argyrios Kyrtzidis | 223ae5c | 2010-12-06 17:51:53 +0000 | [diff] [blame] | 1602 | S.MarkDeclarationReferenced(Attr.getParameterLoc(), FD); |
Anders Carlsson | f6e35d0 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1605 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
| 1606 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
| 1607 | static void HandleFormatArgAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1608 | if (Attr.getNumArgs() != 1) { |
| 1609 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1610 | return; |
| 1611 | } |
| 1612 | if (!isFunctionOrMethod(d) || !hasFunctionProto(d)) { |
| 1613 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1614 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1615 | return; |
| 1616 | } |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1617 | |
| 1618 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1619 | // counted from one. |
| 1620 | bool HasImplicitThisParam = isInstanceMethod(d); |
| 1621 | unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1622 | unsigned FirstIdx = 1; |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1623 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1624 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1625 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1626 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1627 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 1628 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1629 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 1630 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1631 | return; |
| 1632 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1633 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1634 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 1635 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1636 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 1637 | return; |
| 1638 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1639 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1640 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1641 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1642 | if (HasImplicitThisParam) { |
| 1643 | if (ArgIdx == 0) { |
| 1644 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1645 | << "format_arg" << IdxExpr->getSourceRange(); |
| 1646 | return; |
| 1647 | } |
| 1648 | ArgIdx--; |
| 1649 | } |
| 1650 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1651 | // make sure the format string is really a string |
| 1652 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1653 | |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1654 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 1655 | if (not_nsstring_type && |
| 1656 | !isCFStringType(Ty, S.Context) && |
| 1657 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1658 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1659 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1660 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1661 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1662 | << IdxExpr->getSourceRange(); |
| 1663 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1664 | } |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1665 | Ty = getFunctionOrMethodResultType(d); |
| 1666 | if (!isNSStringType(Ty, S.Context) && |
| 1667 | !isCFStringType(Ty, S.Context) && |
| 1668 | (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1669 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1670 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 1671 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1672 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1673 | << IdxExpr->getSourceRange(); |
| 1674 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1677 | d->addAttr(::new (S.Context) FormatArgAttr(Attr.getLoc(), S.Context, |
| 1678 | Idx.getZExtValue())); |
Fariborz Jahanian | 5b16092 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 1679 | } |
| 1680 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1681 | enum FormatAttrKind { |
| 1682 | CFStringFormat, |
| 1683 | NSStringFormat, |
| 1684 | StrftimeFormat, |
| 1685 | SupportedFormat, |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1686 | IgnoredFormat, |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1687 | InvalidFormat |
| 1688 | }; |
| 1689 | |
| 1690 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 1691 | /// types. |
| 1692 | static FormatAttrKind getFormatAttrKind(llvm::StringRef Format) { |
| 1693 | // Check for formats that get handled specially. |
| 1694 | if (Format == "NSString") |
| 1695 | return NSStringFormat; |
| 1696 | if (Format == "CFString") |
| 1697 | return CFStringFormat; |
| 1698 | if (Format == "strftime") |
| 1699 | return StrftimeFormat; |
| 1700 | |
| 1701 | // Otherwise, check for supported formats. |
| 1702 | if (Format == "scanf" || Format == "printf" || Format == "printf0" || |
| 1703 | Format == "strfmon" || Format == "cmn_err" || Format == "strftime" || |
| 1704 | Format == "NSString" || Format == "CFString" || Format == "vcmn_err" || |
Chris Lattner | cd5b306 | 2011-02-18 17:05:55 +0000 | [diff] [blame] | 1705 | Format == "zcmn_err" || |
| 1706 | Format == "kprintf") // OpenBSD. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1707 | return SupportedFormat; |
| 1708 | |
Duncan Sands | bc52595 | 2010-03-23 14:44:19 +0000 | [diff] [blame] | 1709 | if (Format == "gcc_diag" || Format == "gcc_cdiag" || |
| 1710 | Format == "gcc_cxxdiag" || Format == "gcc_tdiag") |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1711 | return IgnoredFormat; |
| 1712 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1713 | return InvalidFormat; |
| 1714 | } |
| 1715 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1716 | /// Handle __attribute__((init_priority(priority))) attributes based on |
| 1717 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
| 1718 | static void HandleInitPriorityAttr(Decl *d, const AttributeList &Attr, |
| 1719 | Sema &S) { |
| 1720 | if (!S.getLangOptions().CPlusPlus) { |
| 1721 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1722 | return; |
| 1723 | } |
| 1724 | |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 1725 | if (!isa<VarDecl>(d) || S.getCurFunctionOrMethodDecl()) { |
| 1726 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 1727 | Attr.setInvalid(); |
| 1728 | return; |
| 1729 | } |
| 1730 | QualType T = dyn_cast<VarDecl>(d)->getType(); |
| 1731 | if (S.Context.getAsArrayType(T)) |
| 1732 | T = S.Context.getBaseElementType(T); |
| 1733 | if (!T->getAs<RecordType>()) { |
| 1734 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 1735 | Attr.setInvalid(); |
| 1736 | return; |
| 1737 | } |
| 1738 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1739 | if (Attr.getNumArgs() != 1) { |
| 1740 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1741 | Attr.setInvalid(); |
| 1742 | return; |
| 1743 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1744 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | b9d5c22 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 1745 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1746 | llvm::APSInt priority(32); |
| 1747 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 1748 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 1749 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1750 | << "init_priority" << priorityExpr->getSourceRange(); |
| 1751 | Attr.setInvalid(); |
| 1752 | return; |
| 1753 | } |
Fariborz Jahanian | 9f967c5 | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 1754 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1755 | if (prioritynum < 101 || prioritynum > 65535) { |
| 1756 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 1757 | << priorityExpr->getSourceRange(); |
| 1758 | Attr.setInvalid(); |
| 1759 | return; |
| 1760 | } |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1761 | d->addAttr(::new (S.Context) InitPriorityAttr(Attr.getLoc(), S.Context, |
| 1762 | prioritynum)); |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1765 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
| 1766 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1767 | static void HandleFormatAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1768 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1769 | if (!Attr.getParameterName()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1770 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1771 | << "format" << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1772 | return; |
| 1773 | } |
| 1774 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1775 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1776 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1777 | return; |
| 1778 | } |
| 1779 | |
Fariborz Jahanian | 620d89c | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 1780 | if (!isFunctionOrMethodOrBlock(d) || !hasFunctionProto(d)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1781 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1782 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1783 | return; |
| 1784 | } |
| 1785 | |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1786 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1787 | // counted from one. |
| 1788 | bool HasImplicitThisParam = isInstanceMethod(d); |
| 1789 | unsigned NumArgs = getFunctionOrMethodNumArgs(d) + HasImplicitThisParam; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1790 | unsigned FirstIdx = 1; |
| 1791 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1792 | llvm::StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1793 | |
| 1794 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1795 | if (Format.startswith("__") && Format.endswith("__")) |
| 1796 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1797 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1798 | // Check for supported formats. |
| 1799 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 3c98902 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 1800 | |
| 1801 | if (Kind == IgnoredFormat) |
| 1802 | return; |
| 1803 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1804 | if (Kind == InvalidFormat) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1805 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1806 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1807 | return; |
| 1808 | } |
| 1809 | |
| 1810 | // checks for the 2nd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1811 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1812 | llvm::APSInt Idx(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1813 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 1814 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1815 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1816 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1817 | return; |
| 1818 | } |
| 1819 | |
| 1820 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1821 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1822 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1823 | return; |
| 1824 | } |
| 1825 | |
| 1826 | // FIXME: Do we need to bounds check? |
| 1827 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1828 | |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1829 | if (HasImplicitThisParam) { |
| 1830 | if (ArgIdx == 0) { |
Chandler Carruth | 07d7e7a | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1831 | S.Diag(Attr.getLoc(), |
| 1832 | diag::err_format_attribute_implicit_this_format_string) |
| 1833 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 4a2614e | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 1834 | return; |
| 1835 | } |
| 1836 | ArgIdx--; |
| 1837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1839 | // make sure the format string is really a string |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1840 | QualType Ty = getFunctionOrMethodArgType(d, ArgIdx); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1841 | |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1842 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1843 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1844 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1845 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 085e8f7 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 1846 | return; |
| 1847 | } |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1848 | } else if (Kind == NSStringFormat) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1849 | // FIXME: do we need to check if the type is NSString*? What are the |
| 1850 | // semantics? |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1851 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1852 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1853 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1854 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1855 | return; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1856 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1857 | } else if (!Ty->isPointerType() || |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1858 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 1859 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1860 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 1861 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1862 | return; |
| 1863 | } |
| 1864 | |
| 1865 | // check the 3rd argument |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1866 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1867 | llvm::APSInt FirstArg(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1868 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 1869 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1870 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1871 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1872 | return; |
| 1873 | } |
| 1874 | |
| 1875 | // check if the function is variadic if the 3rd argument non-zero |
| 1876 | if (FirstArg != 0) { |
Daniel Dunbar | 3568249 | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 1877 | if (isFunctionOrMethodVariadic(d)) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1878 | ++NumArgs; // +1 for ... |
| 1879 | } else { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1880 | S.Diag(d->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1881 | return; |
| 1882 | } |
| 1883 | } |
| 1884 | |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1885 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 1886 | // variable the input is just the current time + the format string. |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1887 | if (Kind == StrftimeFormat) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1888 | if (FirstArg != 0) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1889 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 1890 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1891 | return; |
| 1892 | } |
| 1893 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 1894 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1895 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1896 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1897 | return; |
| 1898 | } |
| 1899 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1900 | d->addAttr(::new (S.Context) FormatAttr(Attr.getLoc(), S.Context, Format, |
| 1901 | Idx.getZExtValue(), |
Daniel Dunbar | 2b0d9a2 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 1902 | FirstArg.getZExtValue())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1905 | static void HandleTransparentUnionAttr(Decl *d, const AttributeList &Attr, |
| 1906 | Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1907 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1908 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1909 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1910 | return; |
| 1911 | } |
| 1912 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1913 | // Try to find the underlying union declaration. |
| 1914 | RecordDecl *RD = 0; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 1915 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(d); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1916 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 1917 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 1918 | else |
| 1919 | RD = dyn_cast<RecordDecl>(d); |
| 1920 | |
| 1921 | if (!RD || !RD->isUnion()) { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1922 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1923 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1924 | return; |
| 1925 | } |
| 1926 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1927 | if (!RD->isDefinition()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1928 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1929 | diag::warn_transparent_union_attribute_not_definition); |
| 1930 | return; |
| 1931 | } |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1932 | |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 1933 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 1934 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1935 | if (Field == FieldEnd) { |
| 1936 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 1937 | return; |
| 1938 | } |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1939 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1940 | FieldDecl *FirstField = *Field; |
| 1941 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 1942 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1943 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 90cd672 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 1944 | diag::warn_transparent_union_attribute_floating) |
| 1945 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1946 | return; |
| 1947 | } |
| 1948 | |
| 1949 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 1950 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 1951 | for (; Field != FieldEnd; ++Field) { |
| 1952 | QualType FieldType = Field->getType(); |
| 1953 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 1954 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 1955 | // Warn if we drop the attribute. |
| 1956 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1957 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1958 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1959 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1960 | diag::warn_transparent_union_attribute_field_size_align) |
| 1961 | << isSize << Field->getDeclName() << FieldBits; |
| 1962 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1963 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 1964 | diag::note_transparent_union_first_field_size_align) |
| 1965 | << isSize << FirstBits; |
Eli Friedman | bc88745 | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 1966 | return; |
| 1967 | } |
| 1968 | } |
| 1969 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1970 | RD->addAttr(::new (S.Context) TransparentUnionAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 1973 | static void HandleAnnotateAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1974 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1975 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1976 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1977 | return; |
| 1978 | } |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1979 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1980 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1981 | |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1982 | // Make sure that there is a string literal as the annotation's single |
| 1983 | // argument. |
| 1984 | if (!SE) { |
Chris Lattner | 797c3c4 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 1985 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1986 | return; |
| 1987 | } |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1988 | d->addAttr(::new (S.Context) AnnotateAttr(Attr.getLoc(), S.Context, |
| 1989 | SE->getString())); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 1992 | static void HandleAlignedAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1993 | // check the attribute arguments. |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1994 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1995 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1996 | return; |
| 1997 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1998 | |
| 1999 | //FIXME: The C++0x version of this attribute has more limited applicabilty |
| 2000 | // than GNU's, and should error out when it is used to specify a |
| 2001 | // weaker alignment, rather than being silently ignored. |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2002 | |
Chris Lattner | 545dd34 | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2003 | if (Attr.getNumArgs() == 0) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2004 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getLoc(), S.Context, true, 0)); |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2005 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2006 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2007 | |
Peter Collingbourne | 7a73002 | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2008 | S.AddAlignedAttr(Attr.getLoc(), D, Attr.getArg(0)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, Expr *E) { |
| 2012 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2013 | // Save dependent expressions in the AST to be instantiated. |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2014 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2015 | return; |
| 2016 | } |
| 2017 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2018 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2019 | llvm::APSInt Alignment(32); |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2020 | if (!E->isIntegerConstantExpr(Alignment, Context)) { |
| 2021 | Diag(AttrLoc, diag::err_attribute_argument_not_int) |
| 2022 | << "aligned" << E->getSourceRange(); |
Chris Lattner | 49e2d34 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2023 | return; |
| 2024 | } |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2025 | if (!llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | 4ced79f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2026 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2027 | << E->getSourceRange(); |
Daniel Dunbar | 396b2a2 | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2028 | return; |
| 2029 | } |
| 2030 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2031 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, true, E)); |
| 2032 | } |
| 2033 | |
| 2034 | void Sema::AddAlignedAttr(SourceLocation AttrLoc, Decl *D, TypeSourceInfo *TS) { |
| 2035 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2036 | // FIXME: Perform checking of type validity |
| 2037 | D->addAttr(::new (Context) AlignedAttr(AttrLoc, Context, false, TS)); |
| 2038 | return; |
Chris Lattner | 6b6b537 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2039 | } |
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 | /// HandleModeAttr - This attribute modifies the width of a decl with primitive |
| 2042 | /// type. |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2043 | /// |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2044 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2045 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2046 | /// HImode, not an intermediate pointer. |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2047 | static void HandleModeAttr(Decl *D, const AttributeList &Attr, Sema &S) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2048 | // This attribute isn't documented, but glibc uses it. It changes |
| 2049 | // the width of an int or unsigned int to the specified size. |
| 2050 | |
| 2051 | // Check that there aren't any arguments |
| 2052 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2053 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2054 | return; |
| 2055 | } |
| 2056 | |
| 2057 | IdentifierInfo *Name = Attr.getParameterName(); |
| 2058 | if (!Name) { |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2059 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2060 | return; |
| 2061 | } |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2062 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2063 | llvm::StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2064 | |
| 2065 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2066 | if (Str.startswith("__") && Str.endswith("__")) |
| 2067 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2068 | |
| 2069 | unsigned DestWidth = 0; |
| 2070 | bool IntegerMode = true; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2071 | bool ComplexMode = false; |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2072 | switch (Str.size()) { |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2073 | case 2: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2074 | switch (Str[0]) { |
| 2075 | case 'Q': DestWidth = 8; break; |
| 2076 | case 'H': DestWidth = 16; break; |
| 2077 | case 'S': DestWidth = 32; break; |
| 2078 | case 'D': DestWidth = 64; break; |
| 2079 | case 'X': DestWidth = 96; break; |
| 2080 | case 'T': DestWidth = 128; break; |
| 2081 | } |
| 2082 | if (Str[1] == 'F') { |
| 2083 | IntegerMode = false; |
| 2084 | } else if (Str[1] == 'C') { |
| 2085 | IntegerMode = false; |
| 2086 | ComplexMode = true; |
| 2087 | } else if (Str[1] != 'I') { |
| 2088 | DestWidth = 0; |
| 2089 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2090 | break; |
| 2091 | case 4: |
| 2092 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2093 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2094 | if (Str == "word") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2095 | DestWidth = S.Context.Target.getPointerWidth(0); |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2096 | else if (Str == "byte") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2097 | DestWidth = S.Context.Target.getCharWidth(); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2098 | break; |
| 2099 | case 7: |
Daniel Dunbar | 210ae98 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2100 | if (Str == "pointer") |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2101 | DestWidth = S.Context.Target.getPointerWidth(0); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2102 | break; |
| 2103 | } |
| 2104 | |
| 2105 | QualType OldTy; |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2106 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2107 | OldTy = TD->getUnderlyingType(); |
| 2108 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2109 | OldTy = VD->getType(); |
| 2110 | else { |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2111 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
| 2112 | << "mode" << SourceRange(Attr.getLoc(), Attr.getLoc()); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2113 | return; |
| 2114 | } |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2115 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2116 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2117 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2118 | else if (IntegerMode) { |
Douglas Gregor | 2ade35e | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2119 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2120 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2121 | } else if (ComplexMode) { |
| 2122 | if (!OldTy->isComplexType()) |
| 2123 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2124 | } else { |
| 2125 | if (!OldTy->isFloatingType()) |
| 2126 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 2127 | } |
| 2128 | |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2129 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 2130 | // and friends, at least with glibc. |
| 2131 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 2132 | // width on unusual platforms. |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2133 | // FIXME: Make sure floating-point mappings are accurate |
| 2134 | // FIXME: Support XF and TF types |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2135 | QualType NewTy; |
| 2136 | switch (DestWidth) { |
| 2137 | case 0: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2138 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2139 | return; |
| 2140 | default: |
Chris Lattner | 3c73c41 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2141 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2142 | return; |
| 2143 | case 8: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2144 | if (!IntegerMode) { |
| 2145 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2146 | return; |
| 2147 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2148 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2149 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2150 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2151 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2152 | break; |
| 2153 | case 16: |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2154 | if (!IntegerMode) { |
| 2155 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2156 | return; |
| 2157 | } |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2158 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2159 | NewTy = S.Context.ShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2160 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2161 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2162 | break; |
| 2163 | case 32: |
| 2164 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2165 | NewTy = S.Context.FloatTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2166 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2167 | NewTy = S.Context.IntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2168 | else |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2169 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2170 | break; |
| 2171 | case 64: |
| 2172 | if (!IntegerMode) |
Chris Lattner | 0b2f4da | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 2173 | NewTy = S.Context.DoubleTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2174 | else if (OldTy->isSignedIntegerType()) |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2175 | if (S.Context.Target.getLongWidth() == 64) |
| 2176 | NewTy = S.Context.LongTy; |
| 2177 | else |
| 2178 | NewTy = S.Context.LongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2179 | else |
Chandler Carruth | aec7caa | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 2180 | if (S.Context.Target.getLongWidth() == 64) |
| 2181 | NewTy = S.Context.UnsignedLongTy; |
| 2182 | else |
| 2183 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2184 | break; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2185 | case 96: |
| 2186 | NewTy = S.Context.LongDoubleTy; |
| 2187 | break; |
Eli Friedman | f98aba3 | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 2188 | case 128: |
| 2189 | if (!IntegerMode) { |
| 2190 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 2191 | return; |
| 2192 | } |
Anders Carlsson | f5f7d86 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 2193 | if (OldTy->isSignedIntegerType()) |
| 2194 | NewTy = S.Context.Int128Ty; |
| 2195 | else |
| 2196 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2197 | break; |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2198 | } |
| 2199 | |
Eli Friedman | 7339749 | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2200 | if (ComplexMode) { |
| 2201 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2202 | } |
| 2203 | |
| 2204 | // Install the new type. |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2205 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2206 | // FIXME: preserve existing source info. |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2207 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | ba6a9bd | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 2208 | } else |
Chris Lattner | fbf1347 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2209 | cast<ValueDecl>(D)->setType(NewTy); |
| 2210 | } |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2211 | |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 2212 | static void HandleNoDebugAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2213 | // check the attribute arguments. |
| 2214 | if (Attr.getNumArgs() > 0) { |
| 2215 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2216 | return; |
| 2217 | } |
Anders Carlsson | e896d98 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 2218 | |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2219 | if (!isFunctionOrMethod(d)) { |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2220 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2221 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2222 | return; |
| 2223 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2224 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2225 | d->addAttr(::new (S.Context) NoDebugAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | d87df37 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Mike Stump | 1feade8 | 2009-08-26 22:31:08 +0000 | [diff] [blame] | 2228 | static void HandleNoInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2229 | // check the attribute arguments. |
| 2230 | if (Attr.getNumArgs() != 0) { |
| 2231 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2232 | return; |
| 2233 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2234 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2235 | if (!isa<FunctionDecl>(d)) { |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2236 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2237 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2238 | return; |
| 2239 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2240 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2241 | d->addAttr(::new (S.Context) NoInlineAttr(Attr.getLoc(), S.Context)); |
Anders Carlsson | 5bab788 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2244 | static void HandleNoInstrumentFunctionAttr(Decl *d, const AttributeList &Attr, |
| 2245 | Sema &S) { |
| 2246 | // check the attribute arguments. |
| 2247 | if (Attr.getNumArgs() != 0) { |
| 2248 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2249 | return; |
| 2250 | } |
| 2251 | |
| 2252 | if (!isa<FunctionDecl>(d)) { |
| 2253 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2254 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2255 | return; |
| 2256 | } |
| 2257 | |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2258 | d->addAttr(::new (S.Context) NoInstrumentFunctionAttr(Attr.getLoc(), |
| 2259 | S.Context)); |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2260 | } |
| 2261 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2262 | static void HandleConstantAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2263 | if (S.LangOpts.CUDA) { |
| 2264 | // check the attribute arguments. |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2265 | if (Attr.hasParameterOrArguments()) { |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2266 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2267 | return; |
| 2268 | } |
| 2269 | |
| 2270 | if (!isa<VarDecl>(d)) { |
| 2271 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2272 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2273 | return; |
| 2274 | } |
| 2275 | |
| 2276 | d->addAttr(::new (S.Context) CUDAConstantAttr(Attr.getLoc(), S.Context)); |
| 2277 | } else { |
| 2278 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 2279 | } |
| 2280 | } |
| 2281 | |
| 2282 | static void HandleDeviceAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2283 | if (S.LangOpts.CUDA) { |
| 2284 | // check the attribute arguments. |
| 2285 | if (Attr.getNumArgs() != 0) { |
| 2286 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2287 | return; |
| 2288 | } |
| 2289 | |
| 2290 | if (!isa<FunctionDecl>(d) && !isa<VarDecl>(d)) { |
| 2291 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2292 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2293 | return; |
| 2294 | } |
| 2295 | |
| 2296 | d->addAttr(::new (S.Context) CUDADeviceAttr(Attr.getLoc(), S.Context)); |
| 2297 | } else { |
| 2298 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | static void HandleGlobalAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2303 | if (S.LangOpts.CUDA) { |
| 2304 | // check the attribute arguments. |
| 2305 | if (Attr.getNumArgs() != 0) { |
| 2306 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2307 | return; |
| 2308 | } |
| 2309 | |
| 2310 | if (!isa<FunctionDecl>(d)) { |
| 2311 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2312 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2313 | return; |
| 2314 | } |
| 2315 | |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2316 | FunctionDecl *FD = cast<FunctionDecl>(d); |
| 2317 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 723df24 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 2318 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
Peter Collingbourne | 2c2c8dd | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 2319 | if (FunctionTypeLoc* FTL = dyn_cast<FunctionTypeLoc>(&TL)) { |
| 2320 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2321 | << FD->getType() |
| 2322 | << FixItHint::CreateReplacement(FTL->getResultLoc().getSourceRange(), |
| 2323 | "void"); |
| 2324 | } else { |
| 2325 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 2326 | << FD->getType(); |
| 2327 | } |
| 2328 | return; |
| 2329 | } |
| 2330 | |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2331 | d->addAttr(::new (S.Context) CUDAGlobalAttr(Attr.getLoc(), S.Context)); |
| 2332 | } else { |
| 2333 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 2334 | } |
| 2335 | } |
| 2336 | |
| 2337 | static void HandleHostAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2338 | if (S.LangOpts.CUDA) { |
| 2339 | // check the attribute arguments. |
| 2340 | if (Attr.getNumArgs() != 0) { |
| 2341 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2342 | return; |
| 2343 | } |
| 2344 | |
| 2345 | if (!isa<FunctionDecl>(d)) { |
| 2346 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2347 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2348 | return; |
| 2349 | } |
| 2350 | |
| 2351 | d->addAttr(::new (S.Context) CUDAHostAttr(Attr.getLoc(), S.Context)); |
| 2352 | } else { |
| 2353 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | static void HandleSharedAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2358 | if (S.LangOpts.CUDA) { |
| 2359 | // check the attribute arguments. |
| 2360 | if (Attr.getNumArgs() != 0) { |
| 2361 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2362 | return; |
| 2363 | } |
| 2364 | |
| 2365 | if (!isa<VarDecl>(d)) { |
| 2366 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2367 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2368 | return; |
| 2369 | } |
| 2370 | |
| 2371 | d->addAttr(::new (S.Context) CUDASharedAttr(Attr.getLoc(), S.Context)); |
| 2372 | } else { |
| 2373 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 2374 | } |
| 2375 | } |
| 2376 | |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 2377 | static void HandleGNUInlineAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2378 | // check the attribute arguments. |
| 2379 | if (Attr.getNumArgs() != 0) { |
| 2380 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2381 | return; |
| 2382 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2383 | |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2384 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(d); |
| 2385 | if (Fn == 0) { |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2386 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2387 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2388 | return; |
| 2389 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2390 | |
Douglas Gregor | 0130f3c | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 2391 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | cf2a721 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 2392 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | c519743 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 2393 | return; |
| 2394 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2395 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2396 | d->addAttr(::new (S.Context) GNUInlineAttr(Attr.getLoc(), S.Context)); |
Chris Lattner | 26e2554 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2399 | static void HandleCallConvAttr(Decl *d, const AttributeList &attr, Sema &S) { |
| 2400 | if (hasDeclarator(d)) return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2401 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2402 | // Diagnostic is emitted elsewhere: here we store the (valid) attr |
| 2403 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 2404 | CallingConv CC; |
| 2405 | if (S.CheckCallingConvAttr(attr, CC)) |
| 2406 | return; |
| 2407 | |
| 2408 | if (!isa<ObjCMethodDecl>(d)) { |
| 2409 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2410 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2411 | return; |
| 2412 | } |
| 2413 | |
| 2414 | switch (attr.getKind()) { |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2415 | case AttributeList::AT_fastcall: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2416 | d->addAttr(::new (S.Context) FastCallAttr(attr.getLoc(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2417 | return; |
| 2418 | case AttributeList::AT_stdcall: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2419 | d->addAttr(::new (S.Context) StdCallAttr(attr.getLoc(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2420 | return; |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2421 | case AttributeList::AT_thiscall: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2422 | d->addAttr(::new (S.Context) ThisCallAttr(attr.getLoc(), S.Context)); |
Douglas Gregor | 04633eb | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 2423 | return; |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2424 | case AttributeList::AT_cdecl: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2425 | d->addAttr(::new (S.Context) CDeclAttr(attr.getLoc(), S.Context)); |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2426 | return; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2427 | case AttributeList::AT_pascal: |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2428 | d->addAttr(::new (S.Context) PascalAttr(attr.getLoc(), S.Context)); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2429 | return; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2430 | case AttributeList::AT_pcs: { |
| 2431 | Expr *Arg = attr.getArg(0); |
| 2432 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 2433 | if (Str == 0 || Str->isWide()) { |
| 2434 | S.Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 2435 | << "pcs" << 1; |
| 2436 | attr.setInvalid(); |
| 2437 | return; |
| 2438 | } |
| 2439 | |
| 2440 | llvm::StringRef StrRef = Str->getString(); |
| 2441 | PcsAttr::PCSType PCS; |
| 2442 | if (StrRef == "aapcs") |
| 2443 | PCS = PcsAttr::AAPCS; |
| 2444 | else if (StrRef == "aapcs-vfp") |
| 2445 | PCS = PcsAttr::AAPCS_VFP; |
| 2446 | else { |
| 2447 | S.Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 2448 | attr.setInvalid(); |
| 2449 | return; |
| 2450 | } |
| 2451 | |
| 2452 | d->addAttr(::new (S.Context) PcsAttr(attr.getLoc(), S.Context, PCS)); |
| 2453 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2454 | default: |
| 2455 | llvm_unreachable("unexpected attribute kind"); |
| 2456 | return; |
| 2457 | } |
| 2458 | } |
| 2459 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2460 | static void HandleOpenCLKernelAttr(Decl *d, const AttributeList &Attr, Sema &S){ |
| 2461 | assert(Attr.isInvalid() == false); |
| 2462 | d->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getLoc(), S.Context)); |
| 2463 | } |
| 2464 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2465 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC) { |
| 2466 | if (attr.isInvalid()) |
| 2467 | return true; |
| 2468 | |
Ted Kremenek | 831efae | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2469 | if ((attr.getNumArgs() != 0 && |
| 2470 | !(attr.getKind() == AttributeList::AT_pcs && attr.getNumArgs() == 1)) || |
| 2471 | attr.getParameterName()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2472 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 2473 | attr.setInvalid(); |
| 2474 | return true; |
| 2475 | } |
| 2476 | |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2477 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 2478 | // move to TargetAttributesSema one day. |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2479 | switch (attr.getKind()) { |
| 2480 | case AttributeList::AT_cdecl: CC = CC_C; break; |
| 2481 | case AttributeList::AT_fastcall: CC = CC_X86FastCall; break; |
| 2482 | case AttributeList::AT_stdcall: CC = CC_X86StdCall; break; |
| 2483 | case AttributeList::AT_thiscall: CC = CC_X86ThisCall; break; |
| 2484 | case AttributeList::AT_pascal: CC = CC_X86Pascal; break; |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2485 | case AttributeList::AT_pcs: { |
| 2486 | Expr *Arg = attr.getArg(0); |
| 2487 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 2488 | if (Str == 0 || Str->isWide()) { |
| 2489 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 2490 | << "pcs" << 1; |
| 2491 | attr.setInvalid(); |
| 2492 | return true; |
| 2493 | } |
| 2494 | |
| 2495 | llvm::StringRef StrRef = Str->getString(); |
| 2496 | if (StrRef == "aapcs") { |
| 2497 | CC = CC_AAPCS; |
| 2498 | break; |
| 2499 | } else if (StrRef == "aapcs-vfp") { |
| 2500 | CC = CC_AAPCS_VFP; |
| 2501 | break; |
| 2502 | } |
| 2503 | // FALLS THROUGH |
| 2504 | } |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2505 | default: llvm_unreachable("unexpected attribute kind"); return true; |
| 2506 | } |
| 2507 | |
| 2508 | return false; |
| 2509 | } |
| 2510 | |
| 2511 | static void HandleRegparmAttr(Decl *d, const AttributeList &attr, Sema &S) { |
| 2512 | if (hasDeclarator(d)) return; |
| 2513 | |
| 2514 | unsigned numParams; |
| 2515 | if (S.CheckRegparmAttr(attr, numParams)) |
| 2516 | return; |
| 2517 | |
| 2518 | if (!isa<ObjCMethodDecl>(d)) { |
| 2519 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2520 | << attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2521 | return; |
| 2522 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2523 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2524 | d->addAttr(::new (S.Context) RegparmAttr(attr.getLoc(), S.Context, numParams)); |
| 2525 | } |
| 2526 | |
| 2527 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 2528 | /// otherwise setting numParams to the appropriate value. |
| 2529 | bool Sema::CheckRegparmAttr(const AttributeList &attr, unsigned &numParams) { |
| 2530 | if (attr.isInvalid()) |
| 2531 | return true; |
| 2532 | |
| 2533 | if (attr.getNumArgs() != 1) { |
| 2534 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2535 | attr.setInvalid(); |
| 2536 | return true; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2537 | } |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2538 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2539 | Expr *NumParamsExpr = attr.getArg(0); |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2540 | llvm::APSInt NumParams(32); |
Douglas Gregor | ac06a0e | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2541 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2542 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
| 2543 | Diag(attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2544 | << "regparm" << NumParamsExpr->getSourceRange(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2545 | attr.setInvalid(); |
| 2546 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2547 | } |
| 2548 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2549 | if (Context.Target.getRegParmMax() == 0) { |
| 2550 | Diag(attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2551 | << NumParamsExpr->getSourceRange(); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2552 | attr.setInvalid(); |
| 2553 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2554 | } |
| 2555 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2556 | numParams = NumParams.getZExtValue(); |
| 2557 | if (numParams > Context.Target.getRegParmMax()) { |
| 2558 | Diag(attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
| 2559 | << Context.Target.getRegParmMax() << NumParamsExpr->getSourceRange(); |
| 2560 | attr.setInvalid(); |
| 2561 | return true; |
Eli Friedman | 55d3aaf | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 2562 | } |
| 2563 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 2564 | return false; |
Fariborz Jahanian | ee76033 | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2567 | static void HandleLaunchBoundsAttr(Decl *d, const AttributeList &Attr, Sema &S){ |
| 2568 | if (S.LangOpts.CUDA) { |
| 2569 | // check the attribute arguments. |
| 2570 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | bdc49d3 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2571 | // FIXME: 0 is not okay. |
| 2572 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2573 | return; |
| 2574 | } |
| 2575 | |
| 2576 | if (!isFunctionOrMethod(d)) { |
| 2577 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2578 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2579 | return; |
| 2580 | } |
| 2581 | |
| 2582 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 2583 | llvm::APSInt MaxThreads(32); |
| 2584 | if (MaxThreadsExpr->isTypeDependent() || |
| 2585 | MaxThreadsExpr->isValueDependent() || |
| 2586 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 2587 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2588 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 2589 | return; |
| 2590 | } |
| 2591 | |
| 2592 | llvm::APSInt MinBlocks(32); |
| 2593 | if (Attr.getNumArgs() > 1) { |
| 2594 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 2595 | if (MinBlocksExpr->isTypeDependent() || |
| 2596 | MinBlocksExpr->isValueDependent() || |
| 2597 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 2598 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2599 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 2600 | return; |
| 2601 | } |
| 2602 | } |
| 2603 | |
| 2604 | d->addAttr(::new (S.Context) CUDALaunchBoundsAttr(Attr.getLoc(), S.Context, |
| 2605 | MaxThreads.getZExtValue(), |
| 2606 | MinBlocks.getZExtValue())); |
| 2607 | } else { |
| 2608 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 2609 | } |
| 2610 | } |
| 2611 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2612 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2613 | // Checker-specific attribute handlers. |
| 2614 | //===----------------------------------------------------------------------===// |
| 2615 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2616 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
| 2617 | return type->isObjCObjectPointerType() || S.Context.isObjCNSObjectType(type); |
| 2618 | } |
| 2619 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
| 2620 | return type->isPointerType() || isValidSubjectOfNSAttribute(S, type); |
| 2621 | } |
| 2622 | |
| 2623 | static void HandleNSConsumedAttr(Decl *d, const AttributeList &attr, Sema &S) { |
| 2624 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(d); |
| 2625 | if (!param) { |
| 2626 | S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2627 | << SourceRange(attr.getLoc()) << attr.getName() << ExpectedParameter; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2628 | return; |
| 2629 | } |
| 2630 | |
| 2631 | bool typeOK, cf; |
| 2632 | if (attr.getKind() == AttributeList::AT_ns_consumed) { |
| 2633 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 2634 | cf = false; |
| 2635 | } else { |
| 2636 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 2637 | cf = true; |
| 2638 | } |
| 2639 | |
| 2640 | if (!typeOK) { |
| 2641 | S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 2642 | << SourceRange(attr.getLoc()) << attr.getName() << cf; |
| 2643 | return; |
| 2644 | } |
| 2645 | |
| 2646 | if (cf) |
| 2647 | param->addAttr(::new (S.Context) CFConsumedAttr(attr.getLoc(), S.Context)); |
| 2648 | else |
| 2649 | param->addAttr(::new (S.Context) NSConsumedAttr(attr.getLoc(), S.Context)); |
| 2650 | } |
| 2651 | |
| 2652 | static void HandleNSConsumesSelfAttr(Decl *d, const AttributeList &attr, |
| 2653 | Sema &S) { |
| 2654 | if (!isa<ObjCMethodDecl>(d)) { |
| 2655 | S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2656 | << SourceRange(attr.getLoc()) << attr.getName() << ExpectedMethod; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2657 | return; |
| 2658 | } |
| 2659 | |
| 2660 | d->addAttr(::new (S.Context) NSConsumesSelfAttr(attr.getLoc(), S.Context)); |
| 2661 | } |
| 2662 | |
| 2663 | static void HandleNSReturnsRetainedAttr(Decl *d, const AttributeList &attr, |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2664 | Sema &S) { |
| 2665 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2666 | QualType returnType; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2667 | |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2668 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(d)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2669 | returnType = MD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2670 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(d)) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2671 | returnType = FD->getResultType(); |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2672 | else { |
Ted Kremenek | 21531fa | 2009-08-19 23:56:48 +0000 | [diff] [blame] | 2673 | S.Diag(d->getLocStart(), diag::warn_attribute_wrong_decl_type) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2674 | << SourceRange(attr.getLoc()) << attr.getName() |
John McCall | 883cc2c | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2675 | << ExpectedFunctionOrMethod; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2676 | return; |
| 2677 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2678 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2679 | bool typeOK; |
| 2680 | bool cf; |
| 2681 | switch (attr.getKind()) { |
| 2682 | default: llvm_unreachable("invalid ownership attribute"); return; |
| 2683 | case AttributeList::AT_ns_returns_autoreleased: |
| 2684 | case AttributeList::AT_ns_returns_retained: |
| 2685 | case AttributeList::AT_ns_returns_not_retained: |
| 2686 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 2687 | cf = false; |
| 2688 | break; |
| 2689 | |
| 2690 | case AttributeList::AT_cf_returns_retained: |
| 2691 | case AttributeList::AT_cf_returns_not_retained: |
| 2692 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 2693 | cf = true; |
| 2694 | break; |
| 2695 | } |
| 2696 | |
| 2697 | if (!typeOK) { |
Ted Kremenek | 21531fa | 2009-08-19 23:56:48 +0000 | [diff] [blame] | 2698 | S.Diag(d->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2699 | << SourceRange(attr.getLoc()) |
| 2700 | << attr.getName() << isa<ObjCMethodDecl>(d) << cf; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2701 | return; |
Ted Kremenek | 5dc53c9 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 2702 | } |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2703 | |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2704 | switch (attr.getKind()) { |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2705 | default: |
| 2706 | assert(0 && "invalid ownership attribute"); |
| 2707 | return; |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2708 | case AttributeList::AT_ns_returns_autoreleased: |
| 2709 | d->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr(attr.getLoc(), |
| 2710 | S.Context)); |
| 2711 | return; |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2712 | case AttributeList::AT_cf_returns_not_retained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2713 | d->addAttr(::new (S.Context) CFReturnsNotRetainedAttr(attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2714 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2715 | return; |
| 2716 | case AttributeList::AT_ns_returns_not_retained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2717 | d->addAttr(::new (S.Context) NSReturnsNotRetainedAttr(attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2718 | S.Context)); |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2719 | return; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2720 | case AttributeList::AT_cf_returns_retained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2721 | d->addAttr(::new (S.Context) CFReturnsRetainedAttr(attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2722 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2723 | return; |
| 2724 | case AttributeList::AT_ns_returns_retained: |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2725 | d->addAttr(::new (S.Context) NSReturnsRetainedAttr(attr.getLoc(), |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2726 | S.Context)); |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2727 | return; |
| 2728 | }; |
| 2729 | } |
| 2730 | |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 2731 | static bool isKnownDeclSpecAttr(const AttributeList &Attr) { |
| 2732 | return Attr.getKind() == AttributeList::AT_dllimport || |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2733 | Attr.getKind() == AttributeList::AT_dllexport || |
| 2734 | Attr.getKind() == AttributeList::AT_uuid; |
| 2735 | } |
| 2736 | |
| 2737 | //===----------------------------------------------------------------------===// |
| 2738 | // Microsoft specific attribute handlers. |
| 2739 | //===----------------------------------------------------------------------===// |
| 2740 | |
| 2741 | static void HandleUuidAttr(Decl *d, const AttributeList &Attr, Sema &S) { |
| 2742 | if (S.LangOpts.Microsoft || S.LangOpts.Borland) { |
| 2743 | // check the attribute arguments. |
| 2744 | if (Attr.getNumArgs() != 1) { |
| 2745 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2746 | return; |
| 2747 | } |
| 2748 | Expr *Arg = Attr.getArg(0); |
| 2749 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2750 | if (Str == 0 || Str->isWide()) { |
| 2751 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 2752 | << "uuid" << 1; |
| 2753 | return; |
| 2754 | } |
| 2755 | |
| 2756 | llvm::StringRef StrRef = Str->getString(); |
| 2757 | |
| 2758 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 2759 | StrRef.back() == '}'; |
| 2760 | |
| 2761 | // Validate GUID length. |
| 2762 | if (IsCurly && StrRef.size() != 38) { |
| 2763 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2764 | return; |
| 2765 | } |
| 2766 | if (!IsCurly && StrRef.size() != 36) { |
| 2767 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2768 | return; |
| 2769 | } |
| 2770 | |
| 2771 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 2772 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Anders Carlsson | f89e042 | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 2773 | llvm::StringRef::iterator I = StrRef.begin(); |
| 2774 | if (IsCurly) // Skip the optional '{' |
| 2775 | ++I; |
| 2776 | |
| 2777 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2778 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 2779 | if (*I != '-') { |
| 2780 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2781 | return; |
| 2782 | } |
| 2783 | } else if (!isxdigit(*I)) { |
| 2784 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 2785 | return; |
| 2786 | } |
| 2787 | I++; |
| 2788 | } |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2789 | |
| 2790 | d->addAttr(::new (S.Context) UuidAttr(Attr.getLoc(), S.Context, |
| 2791 | Str->getString())); |
Francois Pichet | d3d3be9 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 2792 | } else |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2793 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | f0122fe | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2796 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 2797 | // Top Level Sema Entry Points |
| 2798 | //===----------------------------------------------------------------------===// |
| 2799 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2800 | static void ProcessNonInheritableDeclAttr(Scope *scope, Decl *D, |
| 2801 | const AttributeList &Attr, Sema &S) { |
| 2802 | switch (Attr.getKind()) { |
| 2803 | case AttributeList::AT_device: HandleDeviceAttr (D, Attr, S); break; |
| 2804 | case AttributeList::AT_host: HandleHostAttr (D, Attr, S); break; |
| 2805 | case AttributeList::AT_overloadable:HandleOverloadableAttr(D, Attr, S); break; |
| 2806 | default: |
| 2807 | break; |
| 2808 | } |
| 2809 | } |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2810 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2811 | static void ProcessInheritableDeclAttr(Scope *scope, Decl *D, |
| 2812 | const AttributeList &Attr, Sema &S) { |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2813 | switch (Attr.getKind()) { |
Ted Kremenek | 63e5d7c | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 2814 | case AttributeList::AT_IBAction: HandleIBAction(D, Attr, S); break; |
Ted Kremenek | 857e918 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 2815 | case AttributeList::AT_IBOutlet: HandleIBOutlet(D, Attr, S); break; |
| 2816 | case AttributeList::AT_IBOutletCollection: |
| 2817 | HandleIBOutletCollection(D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2818 | case AttributeList::AT_address_space: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2819 | case AttributeList::AT_opencl_image_access: |
Fariborz Jahanian | ba372b8 | 2009-02-18 17:52:36 +0000 | [diff] [blame] | 2820 | case AttributeList::AT_objc_gc: |
John Thompson | 6e132aa | 2009-12-04 21:51:28 +0000 | [diff] [blame] | 2821 | case AttributeList::AT_vector_size: |
Bob Wilson | 4211bb6 | 2010-11-16 00:32:24 +0000 | [diff] [blame] | 2822 | case AttributeList::AT_neon_vector_type: |
| 2823 | case AttributeList::AT_neon_polyvector_type: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2824 | // Ignore these, these are type attributes, handled by |
| 2825 | // ProcessTypeAttributes. |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2826 | break; |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2827 | case AttributeList::AT_device: |
| 2828 | case AttributeList::AT_host: |
| 2829 | case AttributeList::AT_overloadable: |
| 2830 | // Ignore, this is a non-inheritable attribute, handled |
| 2831 | // by ProcessNonInheritableDeclAttr. |
| 2832 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2833 | case AttributeList::AT_alias: HandleAliasAttr (D, Attr, S); break; |
| 2834 | case AttributeList::AT_aligned: HandleAlignedAttr (D, Attr, S); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2835 | case AttributeList::AT_always_inline: |
Daniel Dunbar | af668b0 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 2836 | HandleAlwaysInlineAttr (D, Attr, S); break; |
Ted Kremenek | b725232 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 2837 | case AttributeList::AT_analyzer_noreturn: |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2838 | HandleAnalyzerNoReturnAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2839 | case AttributeList::AT_annotate: HandleAnnotateAttr (D, Attr, S); break; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2840 | case AttributeList::AT_availability:HandleAvailabilityAttr(D, Attr, S); break; |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2841 | case AttributeList::AT_carries_dependency: |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2842 | HandleDependencyAttr (D, Attr, S); break; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 2843 | case AttributeList::AT_common: HandleCommonAttr (D, Attr, S); break; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2844 | case AttributeList::AT_constant: HandleConstantAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2845 | case AttributeList::AT_constructor: HandleConstructorAttr (D, Attr, S); break; |
| 2846 | case AttributeList::AT_deprecated: HandleDeprecatedAttr (D, Attr, S); break; |
| 2847 | case AttributeList::AT_destructor: HandleDestructorAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2848 | case AttributeList::AT_ext_vector_type: |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 2849 | HandleExtVectorTypeAttr(scope, D, Attr, S); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2850 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2851 | case AttributeList::AT_format: HandleFormatAttr (D, Attr, S); break; |
| 2852 | case AttributeList::AT_format_arg: HandleFormatArgAttr (D, Attr, S); break; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2853 | case AttributeList::AT_global: HandleGlobalAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2854 | case AttributeList::AT_gnu_inline: HandleGNUInlineAttr (D, Attr, S); break; |
Peter Collingbourne | 7b38198 | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 2855 | case AttributeList::AT_launch_bounds: |
| 2856 | HandleLaunchBoundsAttr(D, Attr, S); |
| 2857 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2858 | case AttributeList::AT_mode: HandleModeAttr (D, Attr, S); break; |
| 2859 | case AttributeList::AT_malloc: HandleMallocAttr (D, Attr, S); break; |
Dan Gohman | 34c2630 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 2860 | case AttributeList::AT_may_alias: HandleMayAliasAttr (D, Attr, S); break; |
Eric Christopher | a6cf1e7 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 2861 | case AttributeList::AT_nocommon: HandleNoCommonAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2862 | case AttributeList::AT_nonnull: HandleNonNullAttr (D, Attr, S); break; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 2863 | case AttributeList::AT_ownership_returns: |
| 2864 | case AttributeList::AT_ownership_takes: |
| 2865 | case AttributeList::AT_ownership_holds: |
| 2866 | HandleOwnershipAttr (D, Attr, S); break; |
Daniel Dunbar | dd0cb22 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 2867 | case AttributeList::AT_naked: HandleNakedAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2868 | case AttributeList::AT_noreturn: HandleNoReturnAttr (D, Attr, S); break; |
| 2869 | case AttributeList::AT_nothrow: HandleNothrowAttr (D, Attr, S); break; |
Peter Collingbourne | ced7671 | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 2870 | case AttributeList::AT_shared: HandleSharedAttr (D, Attr, S); break; |
John Thompson | 35cc962 | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2871 | case AttributeList::AT_vecreturn: HandleVecReturnAttr (D, Attr, S); break; |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2872 | |
| 2873 | // Checker-specific. |
John McCall | c7ad381 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 2874 | case AttributeList::AT_cf_consumed: |
| 2875 | case AttributeList::AT_ns_consumed: HandleNSConsumedAttr (D, Attr, S); break; |
| 2876 | case AttributeList::AT_ns_consumes_self: |
| 2877 | HandleNSConsumesSelfAttr(D, Attr, S); break; |
| 2878 | |
| 2879 | case AttributeList::AT_ns_returns_autoreleased: |
Ted Kremenek | 31c780d | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 2880 | case AttributeList::AT_ns_returns_not_retained: |
| 2881 | case AttributeList::AT_cf_returns_not_retained: |
Ted Kremenek | b71368d | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 2882 | case AttributeList::AT_ns_returns_retained: |
| 2883 | case AttributeList::AT_cf_returns_retained: |
| 2884 | HandleNSReturnsRetainedAttr(D, Attr, S); break; |
| 2885 | |
Nate Begeman | 6f3d838 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2886 | case AttributeList::AT_reqd_wg_size: |
| 2887 | HandleReqdWorkGroupSize(D, Attr, S); break; |
| 2888 | |
Fariborz Jahanian | 521f12d | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2889 | case AttributeList::AT_init_priority: |
| 2890 | HandleInitPriorityAttr(D, Attr, S); break; |
| 2891 | |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2892 | case AttributeList::AT_packed: HandlePackedAttr (D, Attr, S); break; |
| 2893 | case AttributeList::AT_section: HandleSectionAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2894 | case AttributeList::AT_unavailable: HandleUnavailableAttr (D, Attr, S); break; |
| 2895 | case AttributeList::AT_unused: HandleUnusedAttr (D, Attr, S); break; |
| 2896 | case AttributeList::AT_used: HandleUsedAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2897 | case AttributeList::AT_visibility: HandleVisibilityAttr (D, Attr, S); break; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2898 | case AttributeList::AT_warn_unused_result: HandleWarnUnusedResult(D,Attr,S); |
| 2899 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2900 | case AttributeList::AT_weak: HandleWeakAttr (D, Attr, S); break; |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2901 | case AttributeList::AT_weakref: HandleWeakRefAttr (D, Attr, S); break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2902 | case AttributeList::AT_weak_import: HandleWeakImportAttr (D, Attr, S); break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2903 | case AttributeList::AT_transparent_union: |
| 2904 | HandleTransparentUnionAttr(D, Attr, S); |
| 2905 | break; |
Chris Lattner | 0db29ec | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2906 | case AttributeList::AT_objc_exception: |
| 2907 | HandleObjCExceptionAttr(D, Attr, S); |
| 2908 | break; |
John McCall | d5313b0 | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2909 | case AttributeList::AT_objc_method_family: |
| 2910 | HandleObjCMethodFamilyAttr(D, Attr, S); |
| 2911 | break; |
Sean Hunt | 7725e67 | 2009-11-25 04:20:27 +0000 | [diff] [blame] | 2912 | case AttributeList::AT_nsobject: HandleObjCNSObject (D, Attr, S); break; |
| 2913 | case AttributeList::AT_blocks: HandleBlocksAttr (D, Attr, S); break; |
| 2914 | case AttributeList::AT_sentinel: HandleSentinelAttr (D, Attr, S); break; |
| 2915 | case AttributeList::AT_const: HandleConstAttr (D, Attr, S); break; |
| 2916 | case AttributeList::AT_pure: HandlePureAttr (D, Attr, S); break; |
| 2917 | case AttributeList::AT_cleanup: HandleCleanupAttr (D, Attr, S); break; |
| 2918 | case AttributeList::AT_nodebug: HandleNoDebugAttr (D, Attr, S); break; |
| 2919 | case AttributeList::AT_noinline: HandleNoInlineAttr (D, Attr, S); break; |
| 2920 | case AttributeList::AT_regparm: HandleRegparmAttr (D, Attr, S); break; |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2921 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | 05f8e47 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 2922 | // Just ignore |
| 2923 | break; |
Chris Lattner | 7255a2d | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 2924 | case AttributeList::AT_no_instrument_function: // Interacts with -pg. |
| 2925 | HandleNoInstrumentFunctionAttr(D, Attr, S); |
| 2926 | break; |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2927 | case AttributeList::AT_stdcall: |
| 2928 | case AttributeList::AT_cdecl: |
| 2929 | case AttributeList::AT_fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2930 | case AttributeList::AT_thiscall: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2931 | case AttributeList::AT_pascal: |
Anton Korobeynikov | 414d896 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 2932 | case AttributeList::AT_pcs: |
Abramo Bagnara | e215f72 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 2933 | HandleCallConvAttr(D, Attr, S); |
John McCall | 04a67a6 | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 2934 | break; |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2935 | case AttributeList::AT_opencl_kernel_function: |
| 2936 | HandleOpenCLKernelAttr(D, Attr, S); |
| 2937 | break; |
Francois Pichet | 1154214 | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 2938 | case AttributeList::AT_uuid: |
| 2939 | HandleUuidAttr(D, Attr, S); |
| 2940 | break; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2941 | default: |
Anton Korobeynikov | 82d0a41 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 2942 | // Ask target about the attribute. |
| 2943 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 2944 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Chandler Carruth | 7d5c45e | 2010-07-08 09:42:26 +0000 | [diff] [blame] | 2945 | S.Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) |
| 2946 | << Attr.getName(); |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2947 | break; |
| 2948 | } |
| 2949 | } |
| 2950 | |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2951 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 2952 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 2953 | /// silently ignore it if a GNU attribute. FIXME: Applying a C++0x attribute to |
| 2954 | /// the wrong thing is illegal (C++0x [dcl.attr.grammar]/4). |
| 2955 | static void ProcessDeclAttribute(Scope *scope, Decl *D, |
| 2956 | const AttributeList &Attr, Sema &S, |
| 2957 | bool NonInheritable, bool Inheritable) { |
| 2958 | if (Attr.isInvalid()) |
| 2959 | return; |
| 2960 | |
| 2961 | if (Attr.isDeclspecAttribute() && !isKnownDeclSpecAttr(Attr)) |
| 2962 | // FIXME: Try to deal with other __declspec attributes! |
| 2963 | return; |
| 2964 | |
| 2965 | if (NonInheritable) |
| 2966 | ProcessNonInheritableDeclAttr(scope, D, Attr, S); |
| 2967 | |
| 2968 | if (Inheritable) |
| 2969 | ProcessInheritableDeclAttr(scope, D, Attr, S); |
| 2970 | } |
| 2971 | |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2972 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 2973 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | f48f367 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2974 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2975 | const AttributeList *AttrList, |
| 2976 | bool NonInheritable, bool Inheritable) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2977 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2978 | ProcessDeclAttribute(S, D, *l, *this, NonInheritable, Inheritable); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2979 | } |
| 2980 | |
| 2981 | // GCC accepts |
| 2982 | // static int a9 __attribute__((weakref)); |
| 2983 | // but that looks really pointless. We reject it. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 2984 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2985 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 2986 | dyn_cast<NamedDecl>(D)->getNameAsString(); |
Rafael Espindola | 11e8ce7 | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 2987 | return; |
Chris Lattner | 803d080 | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2988 | } |
| 2989 | } |
| 2990 | |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2991 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
| 2992 | /// #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] | 2993 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II) { |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 2994 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2995 | NamedDecl *NewD = 0; |
| 2996 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
| 2997 | NewD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 2998 | FD->getInnerLocStart(), |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 2999 | FD->getLocation(), DeclarationName(II), |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3000 | FD->getType(), FD->getTypeSourceInfo()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3001 | if (FD->getQualifier()) { |
| 3002 | FunctionDecl *NewFD = cast<FunctionDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3003 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3004 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3005 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 3006 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 3007 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3008 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | 16573fa | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 3009 | VD->getStorageClass(), |
| 3010 | VD->getStorageClassAsWritten()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3011 | if (VD->getQualifier()) { |
| 3012 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | c22b5ff | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 3013 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | b621766 | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 3014 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3015 | } |
| 3016 | return NewD; |
| 3017 | } |
| 3018 | |
| 3019 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs #pragma weak |
| 3020 | /// applied to it, possibly with an alias. |
Ryan Flynn | 7b1fdbd | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 3021 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 3022 | if (W.getUsed()) return; // only do this once |
| 3023 | W.setUsed(true); |
| 3024 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 3025 | IdentifierInfo *NDId = ND->getIdentifier(); |
| 3026 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias()); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3027 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 3028 | NDId->getName())); |
| 3029 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | c4f1fb1 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 3030 | WeakTopLevelDecl.push_back(NewD); |
| 3031 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 3032 | // to insert Decl at TU scope, sorry. |
| 3033 | DeclContext *SavedContext = CurContext; |
| 3034 | CurContext = Context.getTranslationUnitDecl(); |
| 3035 | PushOnScopeChains(NewD, S); |
| 3036 | CurContext = SavedContext; |
| 3037 | } else { // just add weak to existing |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3038 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3039 | } |
| 3040 | } |
| 3041 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3042 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 3043 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 3044 | /// 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] | 3045 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 3046 | bool NonInheritable, bool Inheritable) { |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 3047 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 3048 | // have to do this. |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3049 | if (Inheritable && !WeakUndeclaredIdentifiers.empty()) { |
John McCall | d4aff0e | 2010-10-27 00:59:00 +0000 | [diff] [blame] | 3050 | if (NamedDecl *ND = dyn_cast<NamedDecl>(D)) { |
| 3051 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 3052 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 3053 | = WeakUndeclaredIdentifiers.find(Id); |
| 3054 | if (I != WeakUndeclaredIdentifiers.end() && ND->hasLinkage()) { |
| 3055 | WeakInfo W = I->second; |
| 3056 | DeclApplyPragmaWeak(S, ND, W); |
| 3057 | WeakUndeclaredIdentifiers[Id] = W; |
| 3058 | } |
Ryan Flynn | e25ff83 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 3059 | } |
| 3060 | } |
| 3061 | } |
| 3062 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3063 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3064 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3065 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3066 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3067 | // Walk the declarator structure, applying decl attributes that were in a type |
| 3068 | // position to the decl itself. This handles cases like: |
| 3069 | // int *__attr__(x)** D; |
| 3070 | // when X is a decl attribute. |
| 3071 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 3072 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3073 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | bf91650 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3074 | |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3075 | // Finally, apply any attributes on the decl itself. |
| 3076 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | 6070039 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 3077 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 0744e5f | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3078 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3079 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3080 | // This duplicates a vector push_back but hides the need to know the |
| 3081 | // size of the type. |
| 3082 | void Sema::DelayedDiagnostics::add(const DelayedDiagnostic &diag) { |
| 3083 | assert(StackSize <= StackCapacity); |
| 3084 | |
| 3085 | // Grow the stack if necessary. |
| 3086 | if (StackSize == StackCapacity) { |
| 3087 | unsigned newCapacity = 2 * StackCapacity + 2; |
| 3088 | char *newBuffer = new char[newCapacity * sizeof(DelayedDiagnostic)]; |
| 3089 | const char *oldBuffer = (const char*) Stack; |
| 3090 | |
| 3091 | if (StackCapacity) |
| 3092 | memcpy(newBuffer, oldBuffer, StackCapacity * sizeof(DelayedDiagnostic)); |
| 3093 | |
| 3094 | delete[] oldBuffer; |
| 3095 | Stack = reinterpret_cast<sema::DelayedDiagnostic*>(newBuffer); |
| 3096 | StackCapacity = newCapacity; |
| 3097 | } |
| 3098 | |
| 3099 | assert(StackSize < StackCapacity); |
| 3100 | new (&Stack[StackSize++]) DelayedDiagnostic(diag); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3103 | void Sema::DelayedDiagnostics::popParsingDecl(Sema &S, ParsingDeclState state, |
| 3104 | Decl *decl) { |
| 3105 | DelayedDiagnostics &DD = S.DelayedDiagnostics; |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3106 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3107 | // Check the invariants. |
| 3108 | assert(DD.StackSize >= state.SavedStackSize); |
| 3109 | assert(state.SavedStackSize >= DD.ActiveStackBase); |
| 3110 | assert(DD.ParsingDepth > 0); |
| 3111 | |
| 3112 | // Drop the parsing depth. |
| 3113 | DD.ParsingDepth--; |
| 3114 | |
| 3115 | // If there are no active diagnostics, we're done. |
| 3116 | if (DD.StackSize == DD.ActiveStackBase) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3117 | return; |
| 3118 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3119 | // We only want to actually emit delayed diagnostics when we |
| 3120 | // successfully parsed a decl. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3121 | if (decl) { |
| 3122 | // We emit all the active diagnostics, not just those starting |
| 3123 | // 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] | 3124 | // decl spec and another for each declarator; in a decl group like: |
| 3125 | // deprecated_typedef foo, *bar, baz(); |
| 3126 | // only the declarator pops will be passed decls. This is correct; |
| 3127 | // we really do need to consider delayed diagnostics from the decl spec |
| 3128 | // for each of the different declarations. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3129 | for (unsigned i = DD.ActiveStackBase, e = DD.StackSize; i != e; ++i) { |
| 3130 | DelayedDiagnostic &diag = DD.Stack[i]; |
| 3131 | if (diag.Triggered) |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3132 | continue; |
| 3133 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3134 | switch (diag.Kind) { |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3135 | case DelayedDiagnostic::Deprecation: |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3136 | S.HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3137 | break; |
| 3138 | |
| 3139 | case DelayedDiagnostic::Access: |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3140 | S.HandleDelayedAccessCheck(diag, decl); |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3141 | break; |
| 3142 | } |
| 3143 | } |
| 3144 | } |
| 3145 | |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3146 | // Destroy all the delayed diagnostics we're about to pop off. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3147 | for (unsigned i = state.SavedStackSize, e = DD.StackSize; i != e; ++i) |
Douglas Gregor | 2923380 | 2011-03-23 15:13:44 +0000 | [diff] [blame] | 3148 | DD.Stack[i].Destroy(); |
John McCall | 58e6f34 | 2010-03-16 05:22:47 +0000 | [diff] [blame] | 3149 | |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3150 | DD.StackSize = state.SavedStackSize; |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3151 | } |
| 3152 | |
| 3153 | static bool isDeclDeprecated(Decl *D) { |
| 3154 | do { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 3155 | if (D->isDeprecated()) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3156 | return true; |
| 3157 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 3158 | return false; |
| 3159 | } |
| 3160 | |
John McCall | 9c3087b | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 3161 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3162 | Decl *Ctx) { |
| 3163 | if (isDeclDeprecated(Ctx)) |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3164 | return; |
| 3165 | |
John McCall | 2f51448 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 3166 | DD.Triggered = true; |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3167 | if (!DD.getDeprecationMessage().empty()) |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3168 | Diag(DD.Loc, diag::warn_deprecated_message) |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3169 | << DD.getDeprecationDecl()->getDeclName() |
| 3170 | << DD.getDeprecationMessage(); |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3171 | else |
| 3172 | Diag(DD.Loc, diag::warn_deprecated) |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3173 | << DD.getDeprecationDecl()->getDeclName(); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3176 | void Sema::EmitDeprecationWarning(NamedDecl *D, llvm::StringRef Message, |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3177 | SourceLocation Loc, |
Peter Collingbourne | 743b82b | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 3178 | bool UnknownObjCClass) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3179 | // Delay if we're currently parsing a declaration. |
John McCall | eee1d54 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 3180 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 3181 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, Message)); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3182 | return; |
| 3183 | } |
| 3184 | |
| 3185 | // Otherwise, don't warn if our current context is deprecated. |
| 3186 | if (isDeclDeprecated(cast<Decl>(CurContext))) |
| 3187 | return; |
Benjamin Kramer | ce2d186 | 2010-10-09 15:49:00 +0000 | [diff] [blame] | 3188 | if (!Message.empty()) |
Fariborz Jahanian | c4b35cf | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 3189 | Diag(Loc, diag::warn_deprecated_message) << D->getDeclName() |
| 3190 | << Message; |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3191 | else { |
Peter Collingbourne | 743b82b | 2011-01-02 19:53:12 +0000 | [diff] [blame] | 3192 | if (!UnknownObjCClass) |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 3193 | Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 3194 | else |
| 3195 | Diag(Loc, diag::warn_deprecated_fwdclass_message) << D->getDeclName(); |
| 3196 | } |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 3197 | } |