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