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