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