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