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