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" |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 22 | #include "clang/AST/Mangle.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 23 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 30 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 32 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 33 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 34 | |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 35 | /// These constants match the enumerated choices of |
| 36 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 37 | enum AttributeDeclKind { |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 38 | ExpectedFunction, |
| 39 | ExpectedUnion, |
| 40 | ExpectedVariableOrFunction, |
| 41 | ExpectedFunctionOrMethod, |
| 42 | ExpectedParameter, |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 43 | ExpectedFunctionMethodOrBlock, |
Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 44 | ExpectedFunctionMethodOrClass, |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 45 | ExpectedFunctionMethodOrParameter, |
| 46 | ExpectedClass, |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 47 | ExpectedVariable, |
| 48 | ExpectedMethod, |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 49 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 50 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 51 | ExpectedStruct, |
Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 52 | ExpectedVariableFunctionOrTag, |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 53 | ExpectedTLSVar, |
| 54 | ExpectedVariableOrField, |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 55 | ExpectedVariableFieldOrTag, |
Aaron Ballman | f90ccb0 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 56 | ExpectedTypeOrNamespace, |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 57 | ExpectedObjectiveCInterface, |
Fariborz Jahanian | f720f86 | 2013-11-19 17:42:25 +0000 | [diff] [blame] | 58 | ExpectedMethodOrProperty, |
| 59 | ExpectedStructOrUnion, |
Aaron Ballman | dbb634f | 2013-11-20 01:35:23 +0000 | [diff] [blame] | 60 | ExpectedStructOrUnionOrClass, |
| 61 | ExpectedType |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 64 | //===----------------------------------------------------------------------===// |
| 65 | // Helper functions |
| 66 | //===----------------------------------------------------------------------===// |
| 67 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 68 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 69 | bool blocksToo = true) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 70 | QualType Ty; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 71 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 72 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 73 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 74 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 75 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 76 | Ty = decl->getUnderlyingType(); |
| 77 | else |
| 78 | return 0; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 80 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 81 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 28c433d | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 82 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 83 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 84 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 85 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 88 | // FIXME: We should provide an abstraction around a method or function |
| 89 | // to provide the following bits of information. |
| 90 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 91 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 92 | /// type (function or function-typed variable). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 93 | static bool isFunction(const Decl *D) { |
| 94 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 98 | /// type (function or function-typed variable) or an Objective-C |
| 99 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 100 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 101 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 104 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 105 | /// type (function or function-typed variable) or an Objective-C |
| 106 | /// method or a block. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 107 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 108 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 109 | return true; |
| 110 | // check for block is more involved. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 111 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 112 | QualType Ty = V->getType(); |
| 113 | return Ty->isBlockPointerType(); |
| 114 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 116 | } |
| 117 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 118 | /// Return true if the given decl has a declarator that should have |
| 119 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 120 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 121 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 122 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 123 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 126 | /// hasFunctionProto - Return true if the given decl has a argument |
| 127 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 128 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 129 | static bool hasFunctionProto(const Decl *D) { |
| 130 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 131 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 132 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 133 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 139 | /// arguments. It is an error to call this on a K&R function (use |
| 140 | /// hasFunctionProto first). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 141 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 142 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 143 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 144 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 145 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 146 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 149 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 150 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 151 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 152 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 153 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 154 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 155 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 158 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 159 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 160 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 161 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 164 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 165 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 166 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 167 | return proto->isVariadic(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 168 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 169 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 170 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 171 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 175 | static bool isInstanceMethod(const Decl *D) { |
| 176 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 177 | return MethodDecl->isInstance(); |
| 178 | return false; |
| 179 | } |
| 180 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 181 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 182 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 183 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 184 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 185 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 186 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 187 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 188 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 189 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 190 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 192 | // FIXME: Should we walk the chain of classes? |
| 193 | return ClsName == &Ctx.Idents.get("NSString") || |
| 194 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 195 | } |
| 196 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 197 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 198 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 199 | if (!PT) |
| 200 | return false; |
| 201 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 202 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 203 | if (!RT) |
| 204 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 205 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 206 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 207 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 208 | return false; |
| 209 | |
| 210 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 211 | } |
| 212 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 213 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 214 | // FIXME: Include the type in the argument list. |
| 215 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 216 | } |
| 217 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 218 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 219 | /// output an error. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 220 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 221 | unsigned Num) { |
| 222 | if (getNumAttributeArgs(Attr) != Num) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 223 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 224 | << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | |
| 228 | return true; |
| 229 | } |
| 230 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 231 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 232 | /// \brief Check if the attribute has at least as many args as Num. May |
| 233 | /// output an error. |
| 234 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 235 | unsigned Num) { |
| 236 | if (getNumAttributeArgs(Attr) < Num) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 237 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 238 | return false; |
| 239 | } |
| 240 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 244 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 245 | /// instance method D. May output an error. |
| 246 | /// |
| 247 | /// \returns true if IdxExpr is a valid index. |
| 248 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 249 | StringRef AttrName, |
| 250 | SourceLocation AttrLoc, |
| 251 | unsigned AttrArgNum, |
| 252 | const Expr *IdxExpr, |
| 253 | uint64_t &Idx) |
| 254 | { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 255 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 256 | |
| 257 | // In C++ the implicit 'this' function parameter also counts. |
| 258 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 259 | bool HP = hasFunctionProto(D); |
| 260 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 261 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 262 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 263 | HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 264 | |
| 265 | llvm::APSInt IdxInt; |
| 266 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 267 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 268 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 269 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 270 | << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 271 | return false; |
| 272 | } |
| 273 | |
| 274 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 275 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 276 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 277 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 278 | return false; |
| 279 | } |
| 280 | Idx--; // Convert to zero-based. |
| 281 | if (HasImplicitThisParam) { |
| 282 | if (Idx == 0) { |
| 283 | S.Diag(AttrLoc, |
| 284 | diag::err_attribute_invalid_implicit_this_argument) |
| 285 | << AttrName << IdxExpr->getSourceRange(); |
| 286 | return false; |
| 287 | } |
| 288 | --Idx; |
| 289 | } |
| 290 | |
| 291 | return true; |
| 292 | } |
| 293 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 294 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 295 | /// If not emit an error and return false. If the argument is an identifier it |
| 296 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 297 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 298 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 299 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 300 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 301 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 302 | if (Attr.isArgIdent(ArgNum)) { |
| 303 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 304 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 305 | << Attr.getName() << AANT_ArgumentString |
| 306 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 307 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 308 | Str = Loc->Ident->getName(); |
| 309 | if (ArgLocation) |
| 310 | *ArgLocation = Loc->Loc; |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | // Now check for an actual string literal. |
| 315 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 316 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 317 | if (ArgLocation) |
| 318 | *ArgLocation = ArgExpr->getLocStart(); |
| 319 | |
| 320 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 321 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 322 | << Attr.getName() << AANT_ArgumentString; |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | Str = Literal->getString(); |
| 327 | return true; |
| 328 | } |
| 329 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 330 | /// |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 331 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 332 | /// \return true if the Decl is a field or potentially shared global variable |
| 333 | /// |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 334 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 335 | if (isa<FieldDecl>(D)) |
| 336 | return true; |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 337 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 338 | return vd->hasGlobalStorage() && !vd->getTLSKind(); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 339 | |
| 340 | return false; |
| 341 | } |
| 342 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 343 | /// \brief Check if the passed-in expression is of type int or bool. |
| 344 | static bool isIntOrBool(Expr *Exp) { |
| 345 | QualType QT = Exp->getType(); |
| 346 | return QT->isBooleanType() || QT->isIntegerType(); |
| 347 | } |
| 348 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 349 | |
| 350 | // Check to see if the type is a smart pointer of some kind. We assume |
| 351 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 352 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 353 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 354 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 355 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 356 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 357 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 358 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 359 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 360 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 361 | return false; |
| 362 | |
| 363 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 366 | /// \brief Check if passed in Decl is a pointer type. |
| 367 | /// Note that this function may produce an error message. |
| 368 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 369 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 370 | const AttributeList &Attr) { |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 371 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 372 | QualType QT = vd->getType(); |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 373 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 374 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 375 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 376 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 377 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 378 | // (We don't want to force template instantiation if we can avoid it, |
| 379 | // since that would alter the order in which templates are instantiated.) |
| 380 | if (RT->isIncompleteType()) |
| 381 | return true; |
| 382 | |
| 383 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 384 | return true; |
| 385 | } |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 386 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 387 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 388 | << Attr.getName()->getName() << QT; |
| 389 | } else { |
| 390 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 391 | << Attr.getName(); |
| 392 | } |
| 393 | return false; |
| 394 | } |
| 395 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 396 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 397 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 398 | static const RecordType *getRecordType(QualType QT) { |
| 399 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 400 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 401 | |
| 402 | // Now check if we point to record type. |
| 403 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 404 | return PT->getPointeeType()->getAs<RecordType>(); |
| 405 | |
| 406 | return 0; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 407 | } |
| 408 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 409 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 410 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 411 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 412 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 413 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 414 | return true; |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 419 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 420 | /// resolves to a lockable object. |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 421 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 422 | QualType Ty) { |
| 423 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 424 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 425 | // Warn if could not get record type for this argument. |
Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 426 | if (!RT) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 427 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 428 | << Attr.getName() << Ty.getAsString(); |
| 429 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 430 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 431 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 432 | // Don't check for lockable if the class hasn't been defined yet. |
DeLesley Hutchins | 3509f29 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 433 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 434 | return; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 435 | |
| 436 | // Allow smart pointers to be used as lockable objects. |
| 437 | // FIXME -- Check the type that the smart pointer points to. |
| 438 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 439 | return; |
| 440 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 441 | // Check if the type is lockable. |
| 442 | RecordDecl *RD = RT->getDecl(); |
| 443 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 444 | return; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 445 | |
| 446 | // Else check if any base classes are lockable. |
| 447 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 448 | CXXBasePaths BPaths(false, false); |
| 449 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 450 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 451 | } |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 452 | |
| 453 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 454 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 457 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 458 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 459 | /// \param Sidx The attribute argument index to start checking with. |
| 460 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 461 | /// parameter list. |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 462 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 463 | const AttributeList &Attr, |
| 464 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 465 | int Sidx = 0, |
| 466 | bool ParamIdxOk = false) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 467 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 468 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 469 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 470 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 471 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 472 | Args.push_back(ArgExp); |
| 473 | continue; |
| 474 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 475 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 476 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 477 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 478 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 479 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 480 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 481 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 482 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 483 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 484 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 485 | // We allow constant strings to be used as a placeholder for expressions |
| 486 | // that are not valid C++ syntax, but warn that they are ignored. |
| 487 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 488 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 489 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 490 | continue; |
| 491 | } |
| 492 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 493 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 494 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 495 | // A pointer to member expression of the form &MyClass::mu is treated |
| 496 | // specially -- we need to look at the type of the member. |
| 497 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 498 | if (UOp->getOpcode() == UO_AddrOf) |
| 499 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 500 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 501 | ArgTy = DRE->getDecl()->getType(); |
| 502 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 503 | // First see if we can just cast to record type, or point to record type. |
| 504 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 505 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 506 | // Now check if we index into a record type function param. |
| 507 | if(!RT && ParamIdxOk) { |
| 508 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 509 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 510 | if(FD && IL) { |
| 511 | unsigned int NumParams = FD->getNumParams(); |
| 512 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 513 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 514 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 515 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 516 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 517 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 518 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 519 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 520 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 524 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 525 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 526 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 527 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 530 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 531 | // Attribute Implementations |
| 532 | //===----------------------------------------------------------------------===// |
| 533 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 534 | // FIXME: All this manual attribute parsing code is gross. At the |
| 535 | // least add some helper functions to check most argument patterns (# |
| 536 | // and types of args). |
| 537 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 538 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 539 | const AttributeList &Attr) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 540 | // D must be either a member field or global (potentially shared) variable. |
| 541 | if (!mayBeSharedVariable(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 542 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 543 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 544 | return false; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 547 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 550 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 551 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 552 | return; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 553 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 554 | D->addAttr(::new (S.Context) |
| 555 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 556 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 559 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 560 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 561 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 562 | return; |
| 563 | |
| 564 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 565 | return; |
| 566 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 567 | D->addAttr(::new (S.Context) |
| 568 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 569 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 572 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 573 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 574 | Expr* &Arg) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 575 | // D must be either a member field or global (potentially shared) variable. |
| 576 | if (!mayBeSharedVariable(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 577 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 578 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 579 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 580 | } |
| 581 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 582 | SmallVector<Expr*, 1> Args; |
| 583 | // check that all arguments are lockable objects |
| 584 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 585 | unsigned Size = Args.size(); |
| 586 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 587 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 588 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 589 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 590 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 591 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 594 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 595 | Expr *Arg = 0; |
| 596 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 597 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 598 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 599 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 600 | } |
| 601 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 602 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 603 | const AttributeList &Attr) { |
| 604 | Expr *Arg = 0; |
| 605 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 606 | return; |
| 607 | |
| 608 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 609 | return; |
| 610 | |
| 611 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 612 | S.Context, Arg)); |
| 613 | } |
| 614 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 615 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 616 | const AttributeList &Attr) { |
Caitlin Sadowski | 086fb95 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 617 | // FIXME: Lockable structs for C code. |
David Blaikie | 021221d | 2013-07-29 18:24:03 +0000 | [diff] [blame] | 618 | if (!isa<RecordDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 619 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 620 | << Attr.getName() << ExpectedStructOrUnionOrClass; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 621 | return false; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 624 | return true; |
| 625 | } |
| 626 | |
| 627 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 628 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 629 | return; |
| 630 | |
| 631 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 632 | } |
| 633 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 634 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 635 | const AttributeList &Attr) { |
| 636 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 637 | return; |
| 638 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 639 | D->addAttr(::new (S.Context) |
| 640 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 641 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 644 | static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D, |
| 645 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 646 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 647 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 648 | << Attr.getName() << ExpectedFunctionOrMethod; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 649 | return; |
| 650 | } |
| 651 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 652 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 653 | S.Context)); |
| 654 | } |
| 655 | |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 656 | static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 657 | const AttributeList &Attr) { |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 658 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 659 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 660 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 661 | return; |
| 662 | } |
| 663 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 664 | D->addAttr(::new (S.Context) |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 665 | NoSanitizeAddressAttr(Attr.getRange(), S.Context, |
| 666 | Attr.getAttributeSpellingListIndex())); |
| 667 | } |
| 668 | |
| 669 | static void handleNoSanitizeMemory(Sema &S, Decl *D, |
| 670 | const AttributeList &Attr) { |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 671 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 672 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 673 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 674 | return; |
| 675 | } |
| 676 | |
| 677 | D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(), |
| 678 | S.Context)); |
| 679 | } |
| 680 | |
| 681 | static void handleNoSanitizeThread(Sema &S, Decl *D, |
| 682 | const AttributeList &Attr) { |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 683 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 684 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 685 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(), |
| 690 | S.Context)); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 693 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 694 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 695 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 696 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 697 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 698 | |
| 699 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 700 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 701 | if (!VD || !mayBeSharedVariable(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 702 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 703 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 704 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 705 | } |
| 706 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 707 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 708 | QualType QT = VD->getType(); |
| 709 | if (!QT->isDependentType()) { |
| 710 | const RecordType *RT = getRecordType(QT); |
| 711 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 712 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 713 | << Attr.getName(); |
| 714 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 718 | // Check that all arguments are lockable objects. |
| 719 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 720 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 721 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 722 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 723 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 726 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 727 | const AttributeList &Attr) { |
| 728 | SmallVector<Expr*, 1> Args; |
| 729 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 730 | return; |
| 731 | |
| 732 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 733 | D->addAttr(::new (S.Context) |
| 734 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 735 | StartArg, Args.size(), |
| 736 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 739 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 740 | const AttributeList &Attr) { |
| 741 | SmallVector<Expr*, 1> Args; |
| 742 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 743 | return; |
| 744 | |
| 745 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 746 | D->addAttr(::new (S.Context) |
| 747 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 748 | StartArg, Args.size(), |
| 749 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 752 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 753 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 754 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 755 | // zero or more arguments ok |
| 756 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 757 | // check that the attribute is applied to a function |
| 758 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 759 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 760 | << Attr.getName() << ExpectedFunctionOrMethod; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 761 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 764 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 765 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 766 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 767 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 770 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 771 | const AttributeList &Attr) { |
| 772 | SmallVector<Expr*, 1> Args; |
| 773 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 774 | return; |
| 775 | |
| 776 | unsigned Size = Args.size(); |
| 777 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 778 | D->addAttr(::new (S.Context) |
| 779 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 780 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 783 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 784 | const AttributeList &Attr) { |
| 785 | SmallVector<Expr*, 1> Args; |
| 786 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 787 | return; |
| 788 | |
| 789 | unsigned Size = Args.size(); |
| 790 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 791 | D->addAttr(::new (S.Context) |
| 792 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 793 | StartArg, Size, |
| 794 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 795 | } |
| 796 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 797 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 798 | const AttributeList &Attr) { |
| 799 | SmallVector<Expr*, 1> Args; |
| 800 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 801 | return; |
| 802 | |
| 803 | unsigned Size = Args.size(); |
| 804 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 805 | D->addAttr(::new (S.Context) |
| 806 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 807 | Attr.getAttributeSpellingListIndex())); |
| 808 | } |
| 809 | |
| 810 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 811 | const AttributeList &Attr) { |
| 812 | SmallVector<Expr*, 1> Args; |
| 813 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 814 | return; |
| 815 | |
| 816 | unsigned Size = Args.size(); |
| 817 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 818 | D->addAttr(::new (S.Context) |
| 819 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 820 | StartArg, Size, |
| 821 | Attr.getAttributeSpellingListIndex())); |
| 822 | } |
| 823 | |
| 824 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 825 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 826 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 827 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 828 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 829 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 830 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 831 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 832 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 833 | << Attr.getName() << ExpectedFunctionOrMethod; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 834 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 837 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 838 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 839 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 840 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 844 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 845 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 846 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 849 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 850 | const AttributeList &Attr) { |
| 851 | SmallVector<Expr*, 2> Args; |
| 852 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 853 | return; |
| 854 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 855 | D->addAttr(::new (S.Context) |
| 856 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 857 | Attr.getArgAsExpr(0), |
| 858 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 859 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 862 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 863 | const AttributeList &Attr) { |
| 864 | SmallVector<Expr*, 2> Args; |
| 865 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 866 | return; |
| 867 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 868 | D->addAttr(::new (S.Context) |
| 869 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 870 | Attr.getArgAsExpr(0), |
| 871 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 872 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 875 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 876 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 877 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 878 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 879 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 880 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 881 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 882 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 883 | << Attr.getName() << ExpectedFunctionOrMethod; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 884 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 887 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 888 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 889 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 890 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 891 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 892 | return true; |
| 893 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 894 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 895 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 896 | const AttributeList &Attr) { |
| 897 | SmallVector<Expr*, 1> Args; |
| 898 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 899 | return; |
| 900 | |
| 901 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 902 | D->addAttr(::new (S.Context) |
| 903 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 904 | StartArg, Args.size(), |
| 905 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 908 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 909 | const AttributeList &Attr) { |
| 910 | SmallVector<Expr*, 1> Args; |
| 911 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 912 | return; |
| 913 | |
| 914 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 915 | D->addAttr(::new (S.Context) |
| 916 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 917 | StartArg, Args.size(), |
| 918 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 921 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 922 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 923 | // zero or more arguments ok |
| 924 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 925 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 926 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 927 | << Attr.getName() << ExpectedFunctionOrMethod; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 928 | return; |
| 929 | } |
| 930 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 931 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 932 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 933 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 934 | unsigned Size = Args.size(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 935 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 936 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 937 | D->addAttr(::new (S.Context) |
| 938 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 939 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 943 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 944 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 945 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 946 | << Attr.getName() << ExpectedFunctionOrMethod; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 947 | return; |
| 948 | } |
| 949 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 950 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 951 | SmallVector<Expr*, 1> Args; |
| 952 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 953 | unsigned Size = Args.size(); |
| 954 | if (Size == 0) |
| 955 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 956 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 957 | D->addAttr(::new (S.Context) |
| 958 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 959 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 963 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 964 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 965 | return; |
| 966 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 967 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 968 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 969 | << Attr.getName() << ExpectedFunctionOrMethod; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 970 | return; |
| 971 | } |
| 972 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 973 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 974 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 975 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 976 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 977 | if (Size == 0) |
| 978 | return; |
| 979 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 980 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 981 | D->addAttr(::new (S.Context) |
| 982 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 983 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 984 | } |
| 985 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 986 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 987 | ConsumableAttr::ConsumedState DefaultState; |
| 988 | |
| 989 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 990 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 991 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 992 | DefaultState)) { |
| 993 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 994 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 995 | return; |
| 996 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 997 | } else { |
| 998 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 999 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 1000 | return; |
| 1001 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1002 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1003 | if (!isa<CXXRecordDecl>(D)) { |
| 1004 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1005 | Attr.getName() << ExpectedClass; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1006 | return; |
| 1007 | } |
| 1008 | |
| 1009 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1010 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1011 | Attr.getAttributeSpellingListIndex())); |
| 1012 | } |
| 1013 | |
| 1014 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 1015 | const AttributeList &Attr) { |
| 1016 | ASTContext &CurrContext = S.getASTContext(); |
| 1017 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 1018 | |
| 1019 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1020 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 1021 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 1022 | RD->getNameAsString(); |
| 1023 | |
| 1024 | return false; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | return true; |
| 1029 | } |
| 1030 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1031 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1032 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 1033 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1034 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1035 | return; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1036 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1037 | if (!isa<CXXMethodDecl>(D)) { |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1038 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1039 | Attr.getName() << ExpectedMethod; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1040 | return; |
| 1041 | } |
| 1042 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1043 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1044 | return; |
| 1045 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1046 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 1047 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 1048 | CallableWhenAttr::ConsumedState CallableState; |
| 1049 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1050 | StringRef StateString; |
| 1051 | SourceLocation Loc; |
| 1052 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 1053 | return; |
| 1054 | |
| 1055 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1056 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1057 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 1058 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1059 | return; |
| 1060 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1061 | |
| 1062 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1065 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1066 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 1067 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1070 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1071 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 1072 | const AttributeList &Attr) { |
| 1073 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 1074 | |
| 1075 | if (!isa<ParmVarDecl>(D)) { |
| 1076 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1077 | Attr.getName() << ExpectedParameter; |
| 1078 | return; |
| 1079 | } |
| 1080 | |
| 1081 | ParamTypestateAttr::ConsumedState ParamState; |
| 1082 | |
| 1083 | if (Attr.isArgIdent(0)) { |
| 1084 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1085 | StringRef StateString = Ident->Ident->getName(); |
| 1086 | |
| 1087 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 1088 | ParamState)) { |
| 1089 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1090 | << Attr.getName() << StateString; |
| 1091 | return; |
| 1092 | } |
| 1093 | } else { |
| 1094 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1095 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1096 | return; |
| 1097 | } |
| 1098 | |
| 1099 | // FIXME: This check is currently being done in the analysis. It can be |
| 1100 | // enabled here only after the parser propagates attributes at |
| 1101 | // template specialization definition, not declaration. |
| 1102 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 1103 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1104 | // |
| 1105 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1106 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1107 | // ReturnType.getAsString(); |
| 1108 | // return; |
| 1109 | //} |
| 1110 | |
| 1111 | D->addAttr(::new (S.Context) |
| 1112 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 1113 | Attr.getAttributeSpellingListIndex())); |
| 1114 | } |
| 1115 | |
| 1116 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1117 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 1118 | const AttributeList &Attr) { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1119 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 1120 | |
| 1121 | if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) { |
| 1122 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1123 | Attr.getName() << ExpectedFunctionMethodOrParameter; |
| 1124 | return; |
| 1125 | } |
| 1126 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1127 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 1128 | |
| 1129 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1130 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1131 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1132 | ReturnState)) { |
| 1133 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1134 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1135 | return; |
| 1136 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1137 | } else { |
| 1138 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1139 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1140 | return; |
| 1141 | } |
| 1142 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1143 | // FIXME: This check is currently being done in the analysis. It can be |
| 1144 | // enabled here only after the parser propagates attributes at |
| 1145 | // template specialization definition, not declaration. |
| 1146 | //QualType ReturnType; |
| 1147 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1148 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1149 | // ReturnType = Param->getType(); |
| 1150 | // |
| 1151 | //} else if (const CXXConstructorDecl *Constructor = |
| 1152 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1153 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1154 | // |
| 1155 | //} else { |
| 1156 | // |
| 1157 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1158 | //} |
| 1159 | // |
| 1160 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1161 | // |
| 1162 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1163 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1164 | // ReturnType.getAsString(); |
| 1165 | // return; |
| 1166 | //} |
| 1167 | |
| 1168 | D->addAttr(::new (S.Context) |
| 1169 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1170 | Attr.getAttributeSpellingListIndex())); |
| 1171 | } |
| 1172 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1173 | |
| 1174 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1175 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1176 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1177 | |
| 1178 | if (!isa<CXXMethodDecl>(D)) { |
| 1179 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1180 | Attr.getName() << ExpectedMethod; |
| 1181 | return; |
| 1182 | } |
| 1183 | |
| 1184 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1185 | return; |
| 1186 | |
| 1187 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1188 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1189 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1190 | StringRef Param = Ident->Ident->getName(); |
| 1191 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1192 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1193 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1194 | return; |
| 1195 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1196 | } else { |
| 1197 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1198 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1199 | return; |
| 1200 | } |
| 1201 | |
| 1202 | D->addAttr(::new (S.Context) |
| 1203 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1204 | Attr.getAttributeSpellingListIndex())); |
| 1205 | } |
| 1206 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1207 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1208 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1209 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1210 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1211 | |
| 1212 | if (!isa<CXXMethodDecl>(D)) { |
| 1213 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1214 | Attr.getName() << ExpectedMethod; |
| 1215 | return; |
| 1216 | } |
| 1217 | |
| 1218 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1219 | return; |
| 1220 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1221 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1222 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1223 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1224 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1225 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1226 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1227 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1228 | return; |
| 1229 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1230 | } else { |
| 1231 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1232 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1233 | return; |
| 1234 | } |
| 1235 | |
| 1236 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1237 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1238 | Attr.getAttributeSpellingListIndex())); |
| 1239 | } |
| 1240 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1241 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1242 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1243 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 1244 | if (TD == 0) { |
| 1245 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 1246 | // and type-ids. |
Aaron Ballman | dbb634f | 2013-11-20 01:35:23 +0000 | [diff] [blame] | 1247 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) << |
| 1248 | Attr.getName() << ExpectedType; |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1249 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1250 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1251 | |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1252 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1253 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1256 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1257 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1258 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1259 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1260 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1261 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1262 | if (!FD->getType()->isDependentType() && |
| 1263 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1264 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1265 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1266 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1267 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1268 | FD->addAttr(::new (S.Context) |
| 1269 | PackedAttr(Attr.getRange(), S.Context, |
| 1270 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1271 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1272 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1275 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1276 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1277 | RD->addAttr(::new (S.Context) |
| 1278 | MsStructAttr(Attr.getRange(), S.Context, |
| 1279 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1280 | else |
Aaron Ballman | b80f94b | 2013-11-20 22:22:04 +0000 | [diff] [blame] | 1281 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1282 | << Attr.getName() << ExpectedStructOrUnionOrClass; |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1283 | } |
| 1284 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1285 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1286 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1287 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1288 | if (MD->isInstanceMethod()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1289 | D->addAttr(::new (S.Context) |
| 1290 | IBActionAttr(Attr.getRange(), S.Context, |
| 1291 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1292 | return; |
| 1293 | } |
| 1294 | |
Ted Kremenek | d68ec81 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1295 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1298 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1299 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1300 | // variables or properties of Objective-C classes. The outlet must also |
| 1301 | // have an object reference type. |
| 1302 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1303 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1304 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1305 | << Attr.getName() << VD->getType() << 0; |
| 1306 | return false; |
| 1307 | } |
| 1308 | } |
| 1309 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1310 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1311 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1312 | << Attr.getName() << PD->getType() << 1; |
| 1313 | return false; |
| 1314 | } |
| 1315 | } |
| 1316 | else { |
| 1317 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1318 | return false; |
| 1319 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1320 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1321 | return true; |
| 1322 | } |
| 1323 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1324 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1325 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1326 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1327 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1328 | D->addAttr(::new (S.Context) |
| 1329 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1330 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1333 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1334 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1335 | |
| 1336 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1337 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1338 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1339 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1340 | return; |
| 1341 | } |
| 1342 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1343 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1344 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1345 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1346 | ParsedType PT; |
| 1347 | |
| 1348 | if (Attr.hasParsedType()) |
| 1349 | PT = Attr.getTypeArg(); |
| 1350 | else { |
| 1351 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1352 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1353 | if (!PT) { |
| 1354 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1355 | return; |
| 1356 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1357 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1358 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1359 | TypeSourceInfo *QTLoc = 0; |
| 1360 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1361 | if (!QTLoc) |
| 1362 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1363 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1364 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1365 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1366 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1367 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1368 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1369 | S.Diag(Attr.getLoc(), |
| 1370 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1371 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1372 | return; |
| 1373 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1374 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1375 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1376 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1377 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1380 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1381 | if (const RecordType *UT = T->getAsUnionType()) |
| 1382 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1383 | RecordDecl *UD = UT->getDecl(); |
| 1384 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1385 | itend = UD->field_end(); it != itend; ++it) { |
| 1386 | QualType QT = it->getType(); |
| 1387 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1388 | T = QT; |
| 1389 | return; |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1395 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | e881ce2 | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1396 | if (!isFunctionOrMethod(D)) { |
| 1397 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1398 | << Attr.getName() << ExpectedFunctionOrMethod; |
Nuno Lopes | e881ce2 | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1399 | return; |
| 1400 | } |
| 1401 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1402 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1403 | return; |
| 1404 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1405 | SmallVector<unsigned, 8> SizeArgs; |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1406 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1407 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1408 | uint64_t Idx; |
| 1409 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1410 | Attr.getLoc(), i + 1, Ex, Idx)) |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1411 | return; |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1412 | |
| 1413 | // check if the function argument is of an integer type |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1414 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1415 | if (!T->isIntegerType()) { |
Aaron Ballman | 9d69509 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 1416 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1417 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 1418 | << Ex->getSourceRange(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1419 | return; |
| 1420 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1421 | SizeArgs.push_back(Idx); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
| 1424 | // check if the function returns a pointer |
| 1425 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1426 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1427 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1430 | D->addAttr(::new (S.Context) |
| 1431 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1432 | SizeArgs.data(), SizeArgs.size(), |
| 1433 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1436 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1437 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1438 | // ignore it as well |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1439 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1440 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1441 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1442 | return; |
| 1443 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1444 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1445 | SmallVector<unsigned, 8> NonNullArgs; |
| 1446 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1447 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1448 | uint64_t Idx; |
| 1449 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1450 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1451 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1452 | |
| 1453 | // Is the function argument a pointer type? |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1454 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1455 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1456 | |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1457 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1458 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | 62157e5 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1459 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1460 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1461 | continue; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1462 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1463 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1464 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1465 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1466 | |
| 1467 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1468 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1469 | if (NonNullArgs.empty()) { |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1470 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1471 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1472 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1473 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1474 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1475 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1476 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1477 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1478 | if (NonNullArgs.empty()) { |
| 1479 | // Warn the trivial case only if attribute is not coming from a |
| 1480 | // macro instantiation. |
| 1481 | if (Attr.getLoc().isFileID()) |
| 1482 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1483 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1484 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1485 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1486 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1487 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1488 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1489 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1490 | D->addAttr(::new (S.Context) |
| 1491 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1492 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1495 | static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) { |
| 1496 | switch (K) { |
| 1497 | case OwnershipAttr::Holds: return "'ownership_holds'"; |
| 1498 | case OwnershipAttr::Takes: return "'ownership_takes'"; |
| 1499 | case OwnershipAttr::Returns: return "'ownership_returns'"; |
| 1500 | } |
| 1501 | llvm_unreachable("unknown ownership"); |
| 1502 | } |
| 1503 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1504 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1505 | // This attribute must be applied to a function declaration. The first |
| 1506 | // argument to the attribute must be an identifier, the name of the resource, |
| 1507 | // for example: malloc. The following arguments must be argument indexes, the |
| 1508 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1509 | // The difference between Holds and Takes is that a pointer may still be used |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1510 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1511 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1512 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1513 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1514 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1515 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1516 | return; |
| 1517 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1518 | |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1519 | // Figure out our Kind, and check arguments while we're at it. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1520 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1521 | switch (AL.getKind()) { |
| 1522 | case AttributeList::AT_ownership_takes: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1523 | K = OwnershipAttr::Takes; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1524 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1525 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1526 | return; |
| 1527 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1528 | break; |
| 1529 | case AttributeList::AT_ownership_holds: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1530 | K = OwnershipAttr::Holds; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1531 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1532 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1533 | return; |
| 1534 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1535 | break; |
| 1536 | case AttributeList::AT_ownership_returns: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1537 | K = OwnershipAttr::Returns; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1538 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1539 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1540 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1541 | return; |
| 1542 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1543 | break; |
| 1544 | default: |
| 1545 | // This should never happen given how we are called. |
| 1546 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1549 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1550 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1551 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1552 | return; |
| 1553 | } |
| 1554 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1555 | StringRef Module = AL.getArgAsIdent(0)->Ident->getName(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1556 | |
| 1557 | // Normalize the argument, __foo__ becomes foo. |
| 1558 | if (Module.startswith("__") && Module.endswith("__")) |
| 1559 | Module = Module.substr(2, Module.size() - 4); |
| 1560 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1561 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1562 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1563 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1564 | uint64_t Idx; |
| 1565 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1566 | AL.getLoc(), i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1567 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1568 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1569 | // Is the function argument a pointer type? |
| 1570 | QualType T = getFunctionOrMethodArgType(D, Idx); |
| 1571 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1572 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1573 | case OwnershipAttr::Takes: |
| 1574 | case OwnershipAttr::Holds: |
| 1575 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1576 | Err = 0; |
| 1577 | break; |
| 1578 | case OwnershipAttr::Returns: |
| 1579 | if (!T->isIntegerType()) |
| 1580 | Err = 1; |
| 1581 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1582 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1583 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1584 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1585 | << Ex->getSourceRange(); |
| 1586 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1587 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1588 | |
| 1589 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1590 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1591 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1592 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
| 1593 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1594 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1595 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1596 | << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind()); |
| 1597 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1598 | } |
| 1599 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1600 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | unsigned* start = OwnershipArgs.data(); |
| 1604 | unsigned size = OwnershipArgs.size(); |
| 1605 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1606 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1607 | D->addAttr(::new (S.Context) |
| 1608 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1609 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1612 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1613 | // Check the attribute arguments. |
| 1614 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1615 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1616 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1617 | return; |
| 1618 | } |
| 1619 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1620 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1621 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1622 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1623 | return; |
| 1624 | } |
| 1625 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1626 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1627 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1628 | // gcc rejects |
| 1629 | // class c { |
| 1630 | // static int a __attribute__((weakref ("v2"))); |
| 1631 | // static int b() __attribute__((weakref ("f3"))); |
| 1632 | // }; |
| 1633 | // and ignores the attributes of |
| 1634 | // void f(void) { |
| 1635 | // static int a __attribute__((weakref ("v2"))); |
| 1636 | // } |
| 1637 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1638 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1639 | if (!Ctx->isFileContext()) { |
| 1640 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1641 | nd->getNameAsString(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1642 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1643 | } |
| 1644 | |
| 1645 | // The GCC manual says |
| 1646 | // |
| 1647 | // At present, a declaration to which `weakref' is attached can only |
| 1648 | // be `static'. |
| 1649 | // |
| 1650 | // It also says |
| 1651 | // |
| 1652 | // Without a TARGET, |
| 1653 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1654 | // equivalent to `weak'. |
| 1655 | // |
| 1656 | // gcc 4.4.1 will accept |
| 1657 | // int a7 __attribute__((weakref)); |
| 1658 | // as |
| 1659 | // int a7 __attribute__((weak)); |
| 1660 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1661 | // it if this behaviour is actually used. |
| 1662 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1663 | // GCC rejects |
| 1664 | // static ((alias ("y"), weakref)). |
| 1665 | // Should we? How to check that weakref is before or after alias? |
| 1666 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1667 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1668 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1669 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1670 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1671 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1672 | // GCC will accept anything as the argument of weakref. Should we |
| 1673 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1674 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1675 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1676 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1677 | D->addAttr(::new (S.Context) |
| 1678 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1679 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1682 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1683 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1684 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1685 | return; |
| 1686 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1687 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1688 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1689 | return; |
| 1690 | } |
| 1691 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1692 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1693 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1694 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1695 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1698 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1699 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1700 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1701 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1702 | return; |
| 1703 | } |
| 1704 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1705 | D->addAttr(::new (S.Context) |
| 1706 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1707 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1708 | } |
| 1709 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1710 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1711 | if (!isa<FunctionDecl>(D)) { |
| 1712 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1713 | << Attr.getName() << ExpectedFunction; |
| 1714 | return; |
| 1715 | } |
| 1716 | |
| 1717 | if (D->hasAttr<HotAttr>()) { |
| 1718 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1719 | << Attr.getName() << "hot"; |
| 1720 | return; |
| 1721 | } |
| 1722 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1723 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1724 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1728 | if (!isa<FunctionDecl>(D)) { |
| 1729 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1730 | << Attr.getName() << ExpectedFunction; |
| 1731 | return; |
| 1732 | } |
| 1733 | |
| 1734 | if (D->hasAttr<ColdAttr>()) { |
| 1735 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1736 | << Attr.getName() << "cold"; |
| 1737 | return; |
| 1738 | } |
| 1739 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1740 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1741 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1744 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1745 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1746 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1747 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1748 | return; |
| 1749 | } |
| 1750 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1751 | D->addAttr(::new (S.Context) |
| 1752 | NakedAttr(Attr.getRange(), S.Context, |
| 1753 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1756 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1757 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1758 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1759 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1760 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1761 | return; |
| 1762 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1763 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1764 | D->addAttr(::new (S.Context) |
| 1765 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1766 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1769 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1770 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1771 | StringRef Model; |
| 1772 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1773 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1774 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1775 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1776 | |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1777 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1778 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1779 | << Attr.getName() << ExpectedTLSVar; |
| 1780 | return; |
| 1781 | } |
| 1782 | |
| 1783 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1784 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1785 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1786 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1787 | return; |
| 1788 | } |
| 1789 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1790 | D->addAttr(::new (S.Context) |
| 1791 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1792 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1795 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1796 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1797 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1798 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1799 | D->addAttr(::new (S.Context) |
| 1800 | MallocAttr(Attr.getRange(), S.Context, |
| 1801 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1802 | return; |
| 1803 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1806 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1809 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1810 | D->addAttr(::new (S.Context) |
| 1811 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1812 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1815 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1816 | if (isa<VarDecl>(D)) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1817 | D->addAttr(::new (S.Context) |
| 1818 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1819 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1820 | else |
| 1821 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1822 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1825 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1826 | if (S.LangOpts.CPlusPlus) { |
| 1827 | S.Diag(Attr.getLoc(), diag::err_common_not_supported_cplusplus); |
| 1828 | return; |
| 1829 | } |
| 1830 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1831 | if (isa<VarDecl>(D)) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1832 | D->addAttr(::new (S.Context) |
| 1833 | CommonAttr(Attr.getRange(), S.Context, |
| 1834 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1835 | else |
| 1836 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1837 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1838 | } |
| 1839 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1840 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1841 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1842 | |
| 1843 | if (S.CheckNoReturnAttr(attr)) return; |
| 1844 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1845 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1846 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1847 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1848 | return; |
| 1849 | } |
| 1850 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1851 | D->addAttr(::new (S.Context) |
| 1852 | NoReturnAttr(attr.getRange(), S.Context, |
| 1853 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1857 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1858 | attr.setInvalid(); |
| 1859 | return true; |
| 1860 | } |
| 1861 | |
| 1862 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1865 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1866 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1867 | |
| 1868 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1869 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1870 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1871 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1872 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1873 | && !VD->getType()->isFunctionPointerType())) { |
| 1874 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1875 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1876 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1877 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1878 | return; |
| 1879 | } |
| 1880 | } |
| 1881 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1882 | D->addAttr(::new (S.Context) |
| 1883 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1884 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1887 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1888 | const AttributeList &Attr) { |
| 1889 | // C++11 [dcl.attr.noreturn]p1: |
| 1890 | // The attribute may be applied to the declarator-id in a function |
| 1891 | // declaration. |
| 1892 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1893 | if (!FD) { |
| 1894 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1895 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1896 | return; |
| 1897 | } |
| 1898 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1899 | D->addAttr(::new (S.Context) |
| 1900 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1901 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1904 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1905 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1906 | /* |
| 1907 | Returning a Vector Class in Registers |
| 1908 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1909 | According to the PPU ABI specifications, a class with a single member of |
| 1910 | vector type is returned in memory when used as the return value of a function. |
| 1911 | This results in inefficient code when implementing vector classes. To return |
| 1912 | the value in a single vector register, add the vecreturn attribute to the |
| 1913 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1914 | |
| 1915 | Example: |
| 1916 | |
| 1917 | struct Vector |
| 1918 | { |
| 1919 | __vector float xyzw; |
| 1920 | } __attribute__((vecreturn)); |
| 1921 | |
| 1922 | Vector Add(Vector lhs, Vector rhs) |
| 1923 | { |
| 1924 | Vector result; |
| 1925 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1926 | return result; // This will be returned in a register |
| 1927 | } |
| 1928 | */ |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1929 | if (!isa<RecordDecl>(D)) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1930 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1931 | << Attr.getName() << ExpectedClass; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1932 | return; |
| 1933 | } |
| 1934 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1935 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1936 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1937 | return; |
| 1938 | } |
| 1939 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1940 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1941 | int count = 0; |
| 1942 | |
| 1943 | if (!isa<CXXRecordDecl>(record)) { |
| 1944 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1945 | return; |
| 1946 | } |
| 1947 | |
| 1948 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1949 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1950 | return; |
| 1951 | } |
| 1952 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1953 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1954 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1955 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1956 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1957 | return; |
| 1958 | } |
| 1959 | count++; |
| 1960 | } |
| 1961 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1962 | D->addAttr(::new (S.Context) |
| 1963 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1964 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1967 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1968 | const AttributeList &Attr) { |
| 1969 | if (isa<ParmVarDecl>(D)) { |
| 1970 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1971 | // parameter of a function declaration or lambda. |
| 1972 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1973 | S.Diag(Attr.getLoc(), |
| 1974 | diag::err_carries_dependency_param_not_function_decl); |
| 1975 | return; |
| 1976 | } |
| 1977 | } else if (!isa<FunctionDecl>(D)) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1978 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1979 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1980 | return; |
| 1981 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1982 | |
| 1983 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1984 | Attr.getRange(), S.Context, |
| 1985 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1988 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1989 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 429c134 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1990 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1991 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1992 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1993 | return; |
| 1994 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1995 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1996 | D->addAttr(::new (S.Context) |
| 1997 | UnusedAttr(Attr.getRange(), S.Context, |
| 1998 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 2001 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 2002 | const AttributeList &Attr) { |
Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 2003 | if (!isa<FunctionDecl>(D)) { |
| 2004 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2005 | << Attr.getName() << ExpectedFunction; |
| 2006 | return; |
| 2007 | } |
| 2008 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2009 | D->addAttr(::new (S.Context) |
| 2010 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 2011 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2014 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2015 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 2016 | if (VD->hasLocalStorage()) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2017 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 2018 | return; |
| 2019 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2020 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2021 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2022 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2023 | return; |
| 2024 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2025 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2026 | D->addAttr(::new (S.Context) |
| 2027 | UsedAttr(Attr.getRange(), S.Context, |
| 2028 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2031 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2032 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2033 | if (Attr.getNumArgs() > 1) { |
| 2034 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2035 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2036 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2037 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2038 | int priority = ConstructorAttr::DefaultPriority; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2039 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2040 | Expr *E = Attr.getArgAsExpr(0); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2041 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2042 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2043 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2044 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2045 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2046 | << E->getSourceRange(); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2047 | return; |
| 2048 | } |
| 2049 | priority = Idx.getZExtValue(); |
| 2050 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2051 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2052 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2053 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2054 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2055 | return; |
| 2056 | } |
| 2057 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2058 | D->addAttr(::new (S.Context) |
| 2059 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 2060 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2063 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2064 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2065 | if (Attr.getNumArgs() > 1) { |
| 2066 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2067 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2068 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2069 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2070 | int priority = DestructorAttr::DefaultPriority; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2071 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2072 | Expr *E = Attr.getArgAsExpr(0); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2073 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2074 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2075 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2076 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2077 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2078 | << E->getSourceRange(); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2079 | return; |
| 2080 | } |
| 2081 | priority = Idx.getZExtValue(); |
| 2082 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2083 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2084 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2085 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2086 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2087 | return; |
| 2088 | } |
| 2089 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2090 | D->addAttr(::new (S.Context) |
| 2091 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2092 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2095 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2096 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2097 | const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2098 | unsigned NumArgs = Attr.getNumArgs(); |
| 2099 | if (NumArgs > 1) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2100 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2101 | return; |
| 2102 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2103 | |
| 2104 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2105 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2106 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2107 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2108 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2109 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2110 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2111 | } |
| 2112 | |
Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2113 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 2114 | const AttributeList &Attr) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2115 | D->addAttr(::new (S.Context) |
| 2116 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 2117 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2118 | } |
| 2119 | |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2120 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 2121 | const AttributeList &Attr) { |
| 2122 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | f90ccb0 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2123 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2124 | << Attr.getName() << ExpectedObjectiveCInterface; |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2125 | return; |
| 2126 | } |
| 2127 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2128 | D->addAttr(::new (S.Context) |
| 2129 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2130 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2133 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2134 | const AttributeList &Attr) { |
Fariborz Jahanian | 7249e36 | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2135 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2136 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2137 | return; |
| 2138 | } |
| 2139 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2140 | D->addAttr(::new (S.Context) |
| 2141 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2142 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 9d4d20a | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2145 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2146 | IdentifierInfo *Platform, |
| 2147 | VersionTuple Introduced, |
| 2148 | VersionTuple Deprecated, |
| 2149 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2150 | StringRef PlatformName |
| 2151 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2152 | if (PlatformName.empty()) |
| 2153 | PlatformName = Platform->getName(); |
| 2154 | |
| 2155 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2156 | // of these steps are needed). |
| 2157 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2158 | !(Introduced <= Deprecated)) { |
| 2159 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2160 | << 1 << PlatformName << Deprecated.getAsString() |
| 2161 | << 0 << Introduced.getAsString(); |
| 2162 | return true; |
| 2163 | } |
| 2164 | |
| 2165 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2166 | !(Introduced <= Obsoleted)) { |
| 2167 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2168 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2169 | << 0 << Introduced.getAsString(); |
| 2170 | return true; |
| 2171 | } |
| 2172 | |
| 2173 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2174 | !(Deprecated <= Obsoleted)) { |
| 2175 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2176 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2177 | << 1 << Deprecated.getAsString(); |
| 2178 | return true; |
| 2179 | } |
| 2180 | |
| 2181 | return false; |
| 2182 | } |
| 2183 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2184 | /// \brief Check whether the two versions match. |
| 2185 | /// |
| 2186 | /// If either version tuple is empty, then they are assumed to match. If |
| 2187 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2188 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2189 | bool BeforeIsOkay) { |
| 2190 | if (X.empty() || Y.empty()) |
| 2191 | return true; |
| 2192 | |
| 2193 | if (X == Y) |
| 2194 | return true; |
| 2195 | |
| 2196 | if (BeforeIsOkay && X < Y) |
| 2197 | return true; |
| 2198 | |
| 2199 | return false; |
| 2200 | } |
| 2201 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2202 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2203 | IdentifierInfo *Platform, |
| 2204 | VersionTuple Introduced, |
| 2205 | VersionTuple Deprecated, |
| 2206 | VersionTuple Obsoleted, |
| 2207 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2208 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2209 | bool Override, |
| 2210 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2211 | VersionTuple MergedIntroduced = Introduced; |
| 2212 | VersionTuple MergedDeprecated = Deprecated; |
| 2213 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2214 | bool FoundAny = false; |
| 2215 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2216 | if (D->hasAttrs()) { |
| 2217 | AttrVec &Attrs = D->getAttrs(); |
| 2218 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2219 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2220 | if (!OldAA) { |
| 2221 | ++i; |
| 2222 | continue; |
| 2223 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2224 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2225 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2226 | if (OldPlatform != Platform) { |
| 2227 | ++i; |
| 2228 | continue; |
| 2229 | } |
| 2230 | |
| 2231 | FoundAny = true; |
| 2232 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2233 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2234 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2235 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2236 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2237 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2238 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2239 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2240 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2241 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2242 | if (Override) { |
| 2243 | int Which = -1; |
| 2244 | VersionTuple FirstVersion; |
| 2245 | VersionTuple SecondVersion; |
| 2246 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2247 | Which = 0; |
| 2248 | FirstVersion = OldIntroduced; |
| 2249 | SecondVersion = Introduced; |
| 2250 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2251 | Which = 1; |
| 2252 | FirstVersion = Deprecated; |
| 2253 | SecondVersion = OldDeprecated; |
| 2254 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2255 | Which = 2; |
| 2256 | FirstVersion = Obsoleted; |
| 2257 | SecondVersion = OldObsoleted; |
| 2258 | } |
| 2259 | |
| 2260 | if (Which == -1) { |
| 2261 | Diag(OldAA->getLocation(), |
| 2262 | diag::warn_mismatched_availability_override_unavail) |
| 2263 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2264 | } else { |
| 2265 | Diag(OldAA->getLocation(), |
| 2266 | diag::warn_mismatched_availability_override) |
| 2267 | << Which |
| 2268 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2269 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2270 | } |
| 2271 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2272 | } else { |
| 2273 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2274 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2275 | } |
| 2276 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2277 | Attrs.erase(Attrs.begin() + i); |
| 2278 | --e; |
| 2279 | continue; |
| 2280 | } |
| 2281 | |
| 2282 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2283 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2284 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2285 | |
| 2286 | if (MergedIntroduced2.empty()) |
| 2287 | MergedIntroduced2 = OldIntroduced; |
| 2288 | if (MergedDeprecated2.empty()) |
| 2289 | MergedDeprecated2 = OldDeprecated; |
| 2290 | if (MergedObsoleted2.empty()) |
| 2291 | MergedObsoleted2 = OldObsoleted; |
| 2292 | |
| 2293 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2294 | MergedIntroduced2, MergedDeprecated2, |
| 2295 | MergedObsoleted2)) { |
| 2296 | Attrs.erase(Attrs.begin() + i); |
| 2297 | --e; |
| 2298 | continue; |
| 2299 | } |
| 2300 | |
| 2301 | MergedIntroduced = MergedIntroduced2; |
| 2302 | MergedDeprecated = MergedDeprecated2; |
| 2303 | MergedObsoleted = MergedObsoleted2; |
| 2304 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2305 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | if (FoundAny && |
| 2309 | MergedIntroduced == Introduced && |
| 2310 | MergedDeprecated == Deprecated && |
| 2311 | MergedObsoleted == Obsoleted) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2312 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2313 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2314 | // Only create a new attribute if !Override, but we want to do |
| 2315 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2316 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2317 | MergedDeprecated, MergedObsoleted) && |
| 2318 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2319 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2320 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2321 | Obsoleted, IsUnavailable, Message, |
| 2322 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2323 | } |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2324 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2327 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2328 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2329 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2330 | return; |
| 2331 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2332 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2333 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2334 | IdentifierInfo *II = Platform->Ident; |
| 2335 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2336 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2337 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2338 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2339 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2340 | if (!ND) { |
| 2341 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2342 | return; |
| 2343 | } |
| 2344 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2345 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2346 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2347 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2348 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2349 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 2350 | if (const StringLiteral *SE = |
| 2351 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2352 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2353 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2354 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2355 | Introduced.Version, |
| 2356 | Deprecated.Version, |
| 2357 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2358 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2359 | /*Override=*/false, |
| 2360 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2361 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2362 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2365 | template <class T> |
| 2366 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2367 | typename T::VisibilityType value, |
| 2368 | unsigned attrSpellingListIndex) { |
| 2369 | T *existingAttr = D->getAttr<T>(); |
| 2370 | if (existingAttr) { |
| 2371 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2372 | if (existingValue == value) |
| 2373 | return NULL; |
| 2374 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2375 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2376 | D->dropAttr<T>(); |
| 2377 | } |
| 2378 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2379 | } |
| 2380 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2381 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2382 | VisibilityAttr::VisibilityType Vis, |
| 2383 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2384 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2385 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2388 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2389 | TypeVisibilityAttr::VisibilityType Vis, |
| 2390 | unsigned AttrSpellingListIndex) { |
| 2391 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2392 | AttrSpellingListIndex); |
| 2393 | } |
| 2394 | |
| 2395 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2396 | bool isTypeVisibility) { |
| 2397 | // Visibility attributes don't mean anything on a typedef. |
| 2398 | if (isa<TypedefNameDecl>(D)) { |
| 2399 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2400 | << Attr.getName(); |
| 2401 | return; |
| 2402 | } |
| 2403 | |
| 2404 | // 'type_visibility' can only go on a type or namespace. |
| 2405 | if (isTypeVisibility && |
| 2406 | !(isa<TagDecl>(D) || |
| 2407 | isa<ObjCInterfaceDecl>(D) || |
| 2408 | isa<NamespaceDecl>(D))) { |
| 2409 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2410 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2411 | return; |
| 2412 | } |
| 2413 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2414 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2415 | StringRef TypeStr; |
| 2416 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2417 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2418 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2419 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2420 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2421 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2422 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2423 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2424 | return; |
| 2425 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2426 | |
| 2427 | // Complain about attempts to use protected visibility on targets |
| 2428 | // (like Darwin) that don't support it. |
| 2429 | if (type == VisibilityAttr::Protected && |
| 2430 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2431 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2432 | type = VisibilityAttr::Default; |
| 2433 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2434 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2435 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2436 | clang::Attr *newAttr; |
| 2437 | if (isTypeVisibility) { |
| 2438 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2439 | (TypeVisibilityAttr::VisibilityType) type, |
| 2440 | Index); |
| 2441 | } else { |
| 2442 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2443 | } |
| 2444 | if (newAttr) |
| 2445 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2448 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2449 | const AttributeList &Attr) { |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2450 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2451 | if (!method) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2452 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2453 | << ExpectedMethod; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2454 | return; |
| 2455 | } |
| 2456 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2457 | if (!Attr.isArgIdent(0)) { |
| 2458 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2459 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2460 | return; |
| 2461 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2462 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2463 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2464 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2465 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2466 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2467 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2468 | return; |
| 2469 | } |
| 2470 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2471 | if (F == ObjCMethodFamilyAttr::OMF_init && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2472 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2473 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2474 | << method->getResultType(); |
| 2475 | // Ignore the attribute. |
| 2476 | return; |
| 2477 | } |
| 2478 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2479 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2480 | S.Context, F)); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2483 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2484 | const AttributeList &Attr) { |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2485 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2486 | if (OCI == 0) { |
Aaron Ballman | f90ccb0 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2487 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2488 | << Attr.getName() << ExpectedObjectiveCInterface; |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2489 | return; |
| 2490 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2491 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2492 | D->addAttr(::new (S.Context) |
| 2493 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2494 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2497 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2498 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2499 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2500 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2501 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2502 | return; |
| 2503 | } |
| 2504 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2505 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2506 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2507 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2508 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2509 | return; |
| 2510 | } |
| 2511 | } |
| 2512 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2513 | // It is okay to include this attribute on properties, e.g.: |
| 2514 | // |
| 2515 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2516 | // |
| 2517 | // In this case it follows tradition and suppresses an error in the above |
| 2518 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2519 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2520 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2521 | D->addAttr(::new (S.Context) |
| 2522 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2523 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2526 | static void |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2527 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2528 | if (!isa<FunctionDecl>(D)) { |
| 2529 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2530 | return; |
| 2531 | } |
| 2532 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2533 | D->addAttr(::new (S.Context) |
| 2534 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2535 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2536 | } |
| 2537 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2538 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2539 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2540 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2541 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2542 | return; |
| 2543 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2544 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2545 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2546 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2547 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2548 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2549 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2550 | return; |
| 2551 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2552 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2553 | D->addAttr(::new (S.Context) |
| 2554 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2555 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2558 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2559 | // check the attribute arguments. |
| 2560 | if (Attr.getNumArgs() > 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2561 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2562 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2565 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2566 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2567 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2568 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2569 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2570 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2571 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2572 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2573 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2574 | return; |
| 2575 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2576 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2577 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2578 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2579 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2580 | return; |
| 2581 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2582 | |
| 2583 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2586 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2587 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2588 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2589 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2590 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2591 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2592 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2593 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2594 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2595 | return; |
| 2596 | } |
| 2597 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2598 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2599 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2600 | // FIXME: This error message could be improved, it would be nice |
| 2601 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2602 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2603 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2604 | return; |
| 2605 | } |
| 2606 | } |
| 2607 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2608 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2609 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2610 | if (isa<FunctionNoProtoType>(FT)) { |
| 2611 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2612 | return; |
| 2613 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2614 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2615 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2616 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2617 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2618 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2619 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2620 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2621 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2622 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2623 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2624 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2625 | if (!BD->isVariadic()) { |
| 2626 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2627 | return; |
| 2628 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2629 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2630 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2631 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2632 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2633 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2634 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2635 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2636 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2637 | return; |
| 2638 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2639 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2640 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2641 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2642 | return; |
| 2643 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2644 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2645 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2646 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2647 | return; |
| 2648 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2649 | D->addAttr(::new (S.Context) |
| 2650 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2651 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2654 | static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2655 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 2656 | RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context)); |
| 2657 | else |
| 2658 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2659 | } |
| 2660 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2661 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2662 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2663 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | 3d699e0 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2664 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2665 | return; |
| 2666 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2667 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2668 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2669 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2670 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2671 | return; |
| 2672 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2673 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2674 | if (MD->getResultType()->isVoidType()) { |
| 2675 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2676 | << Attr.getName() << 1; |
| 2677 | return; |
| 2678 | } |
| 2679 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2680 | D->addAttr(::new (S.Context) |
| 2681 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2682 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2685 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2686 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 47f9a73 | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2687 | if (isa<CXXRecordDecl>(D)) { |
| 2688 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2689 | return; |
| 2690 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2691 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2692 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | 41136ee | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2693 | return; |
| 2694 | } |
| 2695 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2696 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2697 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2698 | nd->addAttr(::new (S.Context) |
| 2699 | WeakAttr(Attr.getRange(), S.Context, |
| 2700 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2703 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2704 | // weak_import only applies to variable & function declarations. |
| 2705 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2706 | if (!D->canBeWeakImported(isDef)) { |
| 2707 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2708 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2709 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2710 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2711 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2712 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2713 | // Nothing to warn about here. |
| 2714 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2715 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2716 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2717 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2718 | return; |
| 2719 | } |
| 2720 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2721 | D->addAttr(::new (S.Context) |
| 2722 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2723 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2726 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2727 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2728 | const AttributeList &Attr) { |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2729 | unsigned WGSize[3]; |
| 2730 | for (unsigned i = 0; i < 3; ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2731 | Expr *E = Attr.getArgAsExpr(i); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2732 | llvm::APSInt ArgNum(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2733 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2734 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9d69509 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 2735 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 2736 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 2737 | << E->getSourceRange(); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2738 | return; |
| 2739 | } |
| 2740 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2741 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2742 | |
| 2743 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2744 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2745 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2746 | if (!(A->getXDim() == WGSize[0] && |
| 2747 | A->getYDim() == WGSize[1] && |
| 2748 | A->getZDim() == WGSize[2])) { |
| 2749 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2750 | Attr.getName(); |
| 2751 | } |
| 2752 | } |
| 2753 | |
| 2754 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2755 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2756 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2757 | if (!(A->getXDim() == WGSize[0] && |
| 2758 | A->getYDim() == WGSize[1] && |
| 2759 | A->getZDim() == WGSize[2])) { |
| 2760 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2761 | Attr.getName(); |
| 2762 | } |
| 2763 | } |
| 2764 | |
| 2765 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2766 | D->addAttr(::new (S.Context) |
| 2767 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2768 | WGSize[0], WGSize[1], WGSize[2], |
| 2769 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2770 | else |
| 2771 | D->addAttr(::new (S.Context) |
| 2772 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2773 | WGSize[0], WGSize[1], WGSize[2], |
| 2774 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2775 | } |
| 2776 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2777 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2778 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2779 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2780 | if (!Attr.hasParsedType()) { |
| 2781 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2782 | << Attr.getName() << 1; |
| 2783 | return; |
| 2784 | } |
| 2785 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2786 | TypeSourceInfo *ParmTSI = 0; |
| 2787 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2788 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2789 | |
| 2790 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2791 | (ParmType->isBooleanType() || |
| 2792 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2793 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2794 | << ParmType; |
| 2795 | return; |
| 2796 | } |
| 2797 | |
| 2798 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2799 | D->hasAttr<VecTypeHintAttr>()) { |
| 2800 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2801 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2802 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2803 | return; |
| 2804 | } |
| 2805 | } |
| 2806 | |
| 2807 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2808 | ParmTSI)); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2809 | } |
| 2810 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2811 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2812 | StringRef Name, |
| 2813 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2814 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2815 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2816 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2817 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2818 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2819 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2820 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2821 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2822 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2825 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2826 | // Make sure that there is a string literal as the sections's single |
| 2827 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2828 | StringRef Str; |
| 2829 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2830 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2831 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2832 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2833 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2834 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2835 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2836 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2837 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2838 | return; |
| 2839 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2840 | |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2841 | // This attribute cannot be applied to local variables. |
| 2842 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2843 | S.Diag(LiteralLoc, diag::err_attribute_section_local_variable); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2844 | return; |
| 2845 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2846 | |
| 2847 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2848 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2849 | if (NewAttr) |
| 2850 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2853 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2854 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2855 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2856 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2857 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2858 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2859 | D->addAttr(::new (S.Context) |
| 2860 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2861 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2862 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2865 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2866 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2867 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2868 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2869 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2870 | D->addAttr(::new (S.Context) |
| 2871 | ConstAttr(Attr.getRange(), S.Context, |
| 2872 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2873 | } |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2874 | } |
| 2875 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2876 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2877 | D->addAttr(::new (S.Context) |
| 2878 | PureAttr(Attr.getRange(), S.Context, |
| 2879 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2882 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2883 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2884 | if (!VD || !VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2885 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2886 | return; |
| 2887 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2888 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2889 | Expr *E = Attr.getArgAsExpr(0); |
| 2890 | SourceLocation Loc = E->getExprLoc(); |
| 2891 | FunctionDecl *FD = 0; |
| 2892 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2893 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2894 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2895 | // will warn the user. |
| 2896 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2897 | if (DRE->hasQualifier()) |
| 2898 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2899 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2900 | NI = DRE->getNameInfo(); |
| 2901 | if (!FD) { |
| 2902 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2903 | << NI.getName(); |
| 2904 | return; |
| 2905 | } |
| 2906 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2907 | if (ULE->hasExplicitTemplateArgs()) |
| 2908 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2909 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2910 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2911 | if (!FD) { |
| 2912 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2913 | << NI.getName(); |
| 2914 | if (ULE->getType() == S.Context.OverloadTy) |
| 2915 | S.NoteAllOverloadCandidates(ULE); |
| 2916 | return; |
| 2917 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2918 | } else { |
| 2919 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2920 | return; |
| 2921 | } |
| 2922 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2923 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2924 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2925 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2926 | return; |
| 2927 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2928 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2929 | // We're currently more strict than GCC about what function types we accept. |
| 2930 | // If this ever proves to be a problem it should be easy to fix. |
| 2931 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2932 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2933 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2934 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2935 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2936 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2937 | return; |
| 2938 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2939 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2940 | D->addAttr(::new (S.Context) |
| 2941 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2942 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2943 | } |
| 2944 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2945 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2946 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2947 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2948 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2949 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2950 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2951 | return; |
| 2952 | } |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2953 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2954 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2955 | uint64_t ArgIdx; |
| 2956 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 2957 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2958 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2959 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2960 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2961 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2962 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2963 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2964 | if (not_nsstring_type && |
| 2965 | !isCFStringType(Ty, S.Context) && |
| 2966 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2967 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2968 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2969 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2970 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2971 | << IdxExpr->getSourceRange(); |
| 2972 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2973 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2974 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2975 | if (!isNSStringType(Ty, S.Context) && |
| 2976 | !isCFStringType(Ty, S.Context) && |
| 2977 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2978 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2979 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2980 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2981 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2982 | << IdxExpr->getSourceRange(); |
| 2983 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2986 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2987 | // because that has corrected for the implicit this parameter, and is zero- |
| 2988 | // based. The attribute expects what the user wrote explicitly. |
| 2989 | llvm::APSInt Val; |
| 2990 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2991 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2992 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2993 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2994 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2997 | enum FormatAttrKind { |
| 2998 | CFStringFormat, |
| 2999 | NSStringFormat, |
| 3000 | StrftimeFormat, |
| 3001 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3002 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3003 | InvalidFormat |
| 3004 | }; |
| 3005 | |
| 3006 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3007 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3008 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3009 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 3010 | // Check for formats that get handled specially. |
| 3011 | .Case("NSString", NSStringFormat) |
| 3012 | .Case("CFString", CFStringFormat) |
| 3013 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3014 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3015 | // Otherwise, check for supported formats. |
| 3016 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3017 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3018 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3019 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3020 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3021 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3022 | } |
| 3023 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3024 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3025 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3026 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3027 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3028 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3029 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3030 | return; |
| 3031 | } |
| 3032 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3033 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3034 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3035 | Attr.setInvalid(); |
| 3036 | return; |
| 3037 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3038 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3039 | if (S.Context.getAsArrayType(T)) |
| 3040 | T = S.Context.getBaseElementType(T); |
| 3041 | if (!T->getAs<RecordType>()) { |
| 3042 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3043 | Attr.setInvalid(); |
| 3044 | return; |
| 3045 | } |
| 3046 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3047 | Expr *priorityExpr = Attr.getArgAsExpr(0); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3048 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3049 | llvm::APSInt priority(32); |
| 3050 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 3051 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
Aaron Ballman | 9d69509 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3052 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3053 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3054 | << priorityExpr->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3055 | Attr.setInvalid(); |
| 3056 | return; |
| 3057 | } |
Fariborz Jahanian | 9f2a4ee | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 3058 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3059 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3060 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 3061 | << priorityExpr->getSourceRange(); |
| 3062 | Attr.setInvalid(); |
| 3063 | return; |
| 3064 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3065 | D->addAttr(::new (S.Context) |
| 3066 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3067 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3068 | } |
| 3069 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3070 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3071 | IdentifierInfo *Format, int FormatIdx, |
| 3072 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3073 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3074 | // Check whether we already have an equivalent format attribute. |
| 3075 | for (specific_attr_iterator<FormatAttr> |
| 3076 | i = D->specific_attr_begin<FormatAttr>(), |
| 3077 | e = D->specific_attr_end<FormatAttr>(); |
| 3078 | i != e ; ++i) { |
| 3079 | FormatAttr *f = *i; |
| 3080 | if (f->getType() == Format && |
| 3081 | f->getFormatIdx() == FormatIdx && |
| 3082 | f->getFirstArg() == FirstArg) { |
| 3083 | // If we don't have a valid location for this attribute, adopt the |
| 3084 | // location. |
| 3085 | if (f->getLocation().isInvalid()) |
| 3086 | f->setRange(Range); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3087 | return NULL; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3088 | } |
| 3089 | } |
| 3090 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3091 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3092 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3095 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3096 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3097 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3098 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3099 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3100 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3101 | return; |
| 3102 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3103 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3104 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3105 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3106 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3107 | return; |
| 3108 | } |
| 3109 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3110 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3111 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3112 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3113 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3114 | unsigned FirstIdx = 1; |
| 3115 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3116 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 3117 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3118 | |
| 3119 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3120 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3121 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3122 | // If we've modified the string name, we need a new identifier for it. |
| 3123 | II = &S.Context.Idents.get(Format); |
| 3124 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3125 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3126 | // Check for supported formats. |
| 3127 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3128 | |
| 3129 | if (Kind == IgnoredFormat) |
| 3130 | return; |
| 3131 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3132 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3133 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3134 | << "format" << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3135 | return; |
| 3136 | } |
| 3137 | |
| 3138 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3139 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3140 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3141 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3142 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3143 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3144 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3145 | << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3146 | return; |
| 3147 | } |
| 3148 | |
| 3149 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3150 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3151 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3152 | return; |
| 3153 | } |
| 3154 | |
| 3155 | // FIXME: Do we need to bounds check? |
| 3156 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3157 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3158 | if (HasImplicitThisParam) { |
| 3159 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3160 | S.Diag(Attr.getLoc(), |
| 3161 | diag::err_format_attribute_implicit_this_format_string) |
| 3162 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3163 | return; |
| 3164 | } |
| 3165 | ArgIdx--; |
| 3166 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3168 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3169 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3170 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3171 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3172 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3173 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3174 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3175 | return; |
| 3176 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3177 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3178 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3179 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3180 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3181 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3182 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3183 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3184 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3185 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3186 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3187 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3188 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3189 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3190 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3191 | return; |
| 3192 | } |
| 3193 | |
| 3194 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3195 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3196 | llvm::APSInt FirstArg(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3197 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3198 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3199 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3200 | << Attr.getName() << 3 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3201 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3202 | return; |
| 3203 | } |
| 3204 | |
| 3205 | // check if the function is variadic if the 3rd argument non-zero |
| 3206 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3207 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3208 | ++NumArgs; // +1 for ... |
| 3209 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3210 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3211 | return; |
| 3212 | } |
| 3213 | } |
| 3214 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3215 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3216 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3217 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3218 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3219 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3220 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3221 | return; |
| 3222 | } |
| 3223 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3224 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3225 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3226 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3227 | return; |
| 3228 | } |
| 3229 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3230 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3231 | Idx.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3232 | FirstArg.getZExtValue(), |
| 3233 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3234 | if (NewAttr) |
| 3235 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3238 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3239 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3240 | // Try to find the underlying union declaration. |
| 3241 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3242 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3243 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3244 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3245 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3246 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3247 | |
| 3248 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3249 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3250 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3251 | return; |
| 3252 | } |
| 3253 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3254 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3255 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3256 | diag::warn_transparent_union_attribute_not_definition); |
| 3257 | return; |
| 3258 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3259 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3260 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3261 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3262 | if (Field == FieldEnd) { |
| 3263 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3264 | return; |
| 3265 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3266 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3267 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3268 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3269 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3270 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3271 | diag::warn_transparent_union_attribute_floating) |
| 3272 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3273 | return; |
| 3274 | } |
| 3275 | |
| 3276 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3277 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3278 | for (; Field != FieldEnd; ++Field) { |
| 3279 | QualType FieldType = Field->getType(); |
| 3280 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3281 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3282 | // Warn if we drop the attribute. |
| 3283 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3284 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3285 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3286 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3287 | diag::warn_transparent_union_attribute_field_size_align) |
| 3288 | << isSize << Field->getDeclName() << FieldBits; |
| 3289 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3290 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3291 | diag::note_transparent_union_first_field_size_align) |
| 3292 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3293 | return; |
| 3294 | } |
| 3295 | } |
| 3296 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3297 | RD->addAttr(::new (S.Context) |
| 3298 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3299 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3302 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3303 | // Make sure that there is a string literal as the annotation's single |
| 3304 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3305 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3306 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3307 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3308 | |
| 3309 | // Don't duplicate annotations that are already set. |
| 3310 | for (specific_attr_iterator<AnnotateAttr> |
| 3311 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3312 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3313 | if ((*i)->getAnnotation() == Str) |
| 3314 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3315 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3316 | |
| 3317 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3318 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3319 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3322 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3323 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3324 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3325 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3326 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3327 | return; |
| 3328 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3329 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3330 | if (Attr.getNumArgs() == 0) { |
| 3331 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3332 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3333 | return; |
| 3334 | } |
| 3335 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3336 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3337 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3338 | S.Diag(Attr.getEllipsisLoc(), |
| 3339 | diag::err_pack_expansion_without_parameter_packs); |
| 3340 | return; |
| 3341 | } |
| 3342 | |
| 3343 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3344 | return; |
| 3345 | |
| 3346 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3347 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3351 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3352 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3353 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3354 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3355 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3356 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3357 | // C++11 [dcl.align]p1: |
| 3358 | // An alignment-specifier may be applied to a variable or to a class |
| 3359 | // data member, but it shall not be applied to a bit-field, a function |
| 3360 | // parameter, the formal parameter of a catch clause, or a variable |
| 3361 | // declared with the register storage class specifier. An |
| 3362 | // alignment-specifier may also be applied to the declaration of a class |
| 3363 | // or enumeration type. |
| 3364 | // C11 6.7.5/2: |
| 3365 | // An alignment attribute shall not be specified in a declaration of |
| 3366 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3367 | // object declared with the register storage-class specifier. |
| 3368 | int DiagKind = -1; |
| 3369 | if (isa<ParmVarDecl>(D)) { |
| 3370 | DiagKind = 0; |
| 3371 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3372 | if (VD->getStorageClass() == SC_Register) |
| 3373 | DiagKind = 1; |
| 3374 | if (VD->isExceptionVariable()) |
| 3375 | DiagKind = 2; |
| 3376 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3377 | if (FD->isBitField()) |
| 3378 | DiagKind = 3; |
| 3379 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3380 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3381 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3382 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3383 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3384 | return; |
| 3385 | } |
| 3386 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3387 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3388 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3389 | return; |
| 3390 | } |
| 3391 | } |
| 3392 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3393 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3394 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3395 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3396 | AA->setPackExpansion(IsPackExpansion); |
| 3397 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3398 | return; |
| 3399 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3400 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3401 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3402 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3403 | ExprResult ICE |
| 3404 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3405 | diag::err_aligned_attribute_argument_not_int, |
| 3406 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3407 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3408 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3409 | |
| 3410 | // C++11 [dcl.align]p2: |
| 3411 | // -- if the constant expression evaluates to zero, the alignment |
| 3412 | // specifier shall have no effect |
| 3413 | // C11 6.7.5p6: |
| 3414 | // An alignment specification of zero has no effect. |
| 3415 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3416 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3417 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3418 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3419 | return; |
| 3420 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3421 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3422 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3423 | // We've already verified it's a power of 2, now let's make sure it's |
| 3424 | // 8192 or less. |
| 3425 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3426 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3427 | << E->getSourceRange(); |
| 3428 | return; |
| 3429 | } |
| 3430 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3431 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3432 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3433 | ICE.take(), SpellingListIndex); |
| 3434 | AA->setPackExpansion(IsPackExpansion); |
| 3435 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3438 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3439 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3440 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3441 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3442 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3443 | SpellingListIndex); |
| 3444 | AA->setPackExpansion(IsPackExpansion); |
| 3445 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3446 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3447 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3448 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3449 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3450 | |
| 3451 | QualType Ty; |
| 3452 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3453 | Ty = VD->getType(); |
| 3454 | else |
| 3455 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 3653b7e | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3456 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3457 | return; |
| 3458 | |
| 3459 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3460 | // The combined effect of all alignment attributes in a declaration shall |
| 3461 | // not specify an alignment that is less strict than the alignment that |
| 3462 | // would otherwise be required for the entity being declared. |
| 3463 | AlignedAttr *AlignasAttr = 0; |
| 3464 | unsigned Align = 0; |
| 3465 | for (specific_attr_iterator<AlignedAttr> |
| 3466 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3467 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3468 | if (I->isAlignmentDependent()) |
| 3469 | return; |
| 3470 | if (I->isAlignas()) |
| 3471 | AlignasAttr = *I; |
| 3472 | Align = std::max(Align, I->getAlignment(Context)); |
| 3473 | } |
| 3474 | |
| 3475 | if (AlignasAttr && Align) { |
| 3476 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3477 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3478 | if (NaturalAlign > RequestedAlign) |
| 3479 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3480 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3481 | } |
| 3482 | } |
| 3483 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3484 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3485 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3486 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3487 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3488 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3489 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3490 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3491 | // This attribute isn't documented, but glibc uses it. It changes |
| 3492 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3493 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 3494 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3495 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3496 | return; |
| 3497 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3498 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3499 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 3500 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3501 | |
| 3502 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3503 | if (Str.startswith("__") && Str.endswith("__")) |
| 3504 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3505 | |
| 3506 | unsigned DestWidth = 0; |
| 3507 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3508 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3509 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3510 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3511 | switch (Str[0]) { |
| 3512 | case 'Q': DestWidth = 8; break; |
| 3513 | case 'H': DestWidth = 16; break; |
| 3514 | case 'S': DestWidth = 32; break; |
| 3515 | case 'D': DestWidth = 64; break; |
| 3516 | case 'X': DestWidth = 96; break; |
| 3517 | case 'T': DestWidth = 128; break; |
| 3518 | } |
| 3519 | if (Str[1] == 'F') { |
| 3520 | IntegerMode = false; |
| 3521 | } else if (Str[1] == 'C') { |
| 3522 | IntegerMode = false; |
| 3523 | ComplexMode = true; |
| 3524 | } else if (Str[1] != 'I') { |
| 3525 | DestWidth = 0; |
| 3526 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3527 | break; |
| 3528 | case 4: |
| 3529 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3530 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3531 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3532 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3533 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3534 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3535 | break; |
| 3536 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3537 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3538 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3539 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3540 | case 11: |
| 3541 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3542 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3543 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3544 | } |
| 3545 | |
| 3546 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3547 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3548 | OldTy = TD->getUnderlyingType(); |
| 3549 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3550 | OldTy = VD->getType(); |
| 3551 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3552 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3553 | << "mode" << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3554 | return; |
| 3555 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3556 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3557 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3558 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3559 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3560 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3561 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3562 | } else if (ComplexMode) { |
| 3563 | if (!OldTy->isComplexType()) |
| 3564 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3565 | } else { |
| 3566 | if (!OldTy->isFloatingType()) |
| 3567 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3568 | } |
| 3569 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3570 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3571 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3572 | // FIXME: Make sure floating-point mappings are accurate |
| 3573 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3574 | if (!DestWidth) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3575 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3576 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
| 3579 | QualType NewTy; |
| 3580 | |
| 3581 | if (IntegerMode) |
| 3582 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 3583 | OldTy->isSignedIntegerType()); |
| 3584 | else |
| 3585 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 3586 | |
| 3587 | if (NewTy.isNull()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3588 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3589 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3592 | if (ComplexMode) { |
| 3593 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3594 | } |
| 3595 | |
| 3596 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3597 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3598 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3599 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3600 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3601 | |
| 3602 | D->addAttr(::new (S.Context) |
| 3603 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3604 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3605 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3606 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3607 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3608 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3609 | if (!VD->hasGlobalStorage()) |
| 3610 | S.Diag(Attr.getLoc(), |
| 3611 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3612 | << Attr.getName(); |
| 3613 | } else if (!isFunctionOrMethod(D)) { |
| 3614 | S.Diag(Attr.getLoc(), |
| 3615 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3616 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3617 | return; |
| 3618 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3619 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3620 | D->addAttr(::new (S.Context) |
| 3621 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3622 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3623 | } |
| 3624 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3625 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3626 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3627 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3628 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3629 | return; |
| 3630 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3631 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3632 | D->addAttr(::new (S.Context) |
| 3633 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3634 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3635 | } |
| 3636 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3637 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3638 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3639 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3640 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3641 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3642 | return; |
| 3643 | } |
| 3644 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3645 | D->addAttr(::new (S.Context) |
| 3646 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3647 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3648 | } |
| 3649 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3650 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3651 | if (S.LangOpts.CUDA) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3652 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3653 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3654 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3655 | return; |
| 3656 | } |
| 3657 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3658 | D->addAttr(::new (S.Context) |
| 3659 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3660 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3661 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3662 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3663 | } |
| 3664 | } |
| 3665 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3666 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3667 | if (S.LangOpts.CUDA) { |
| 3668 | // check the attribute arguments. |
| 3669 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3670 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3671 | << Attr.getName() << 0; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3672 | return; |
| 3673 | } |
| 3674 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3675 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3676 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3677 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3678 | return; |
| 3679 | } |
| 3680 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3681 | D->addAttr(::new (S.Context) |
| 3682 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3683 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3684 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3685 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3686 | } |
| 3687 | } |
| 3688 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3689 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3690 | if (S.LangOpts.CUDA) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3691 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3692 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3693 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3694 | return; |
| 3695 | } |
| 3696 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3697 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3698 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3699 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3700 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3701 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3702 | << FD->getType() |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3703 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3704 | "void"); |
| 3705 | } else { |
| 3706 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3707 | << FD->getType(); |
| 3708 | } |
| 3709 | return; |
| 3710 | } |
| 3711 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3712 | D->addAttr(::new (S.Context) |
| 3713 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3714 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3715 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3716 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3717 | } |
| 3718 | } |
| 3719 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3720 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3721 | if (S.LangOpts.CUDA) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3722 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3723 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3724 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3725 | return; |
| 3726 | } |
| 3727 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3728 | D->addAttr(::new (S.Context) |
| 3729 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3730 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3731 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3732 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3733 | } |
| 3734 | } |
| 3735 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3736 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3737 | if (S.LangOpts.CUDA) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3738 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3739 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3740 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3741 | return; |
| 3742 | } |
| 3743 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3744 | D->addAttr(::new (S.Context) |
| 3745 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3746 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3747 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3748 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3749 | } |
| 3750 | } |
| 3751 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3752 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3753 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3754 | if (Fn == 0) { |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3755 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3756 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3757 | return; |
| 3758 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3759 | |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3760 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3761 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3762 | return; |
| 3763 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3764 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3765 | D->addAttr(::new (S.Context) |
| 3766 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3767 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3770 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3771 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3772 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3773 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3774 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3775 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3776 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3777 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3778 | return; |
| 3779 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3780 | if (!isa<ObjCMethodDecl>(D)) { |
| 3781 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3782 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3783 | return; |
| 3784 | } |
| 3785 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3786 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3787 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3788 | D->addAttr(::new (S.Context) |
| 3789 | FastCallAttr(Attr.getRange(), S.Context, |
| 3790 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3791 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3792 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3793 | D->addAttr(::new (S.Context) |
| 3794 | StdCallAttr(Attr.getRange(), S.Context, |
| 3795 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3796 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3797 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3798 | D->addAttr(::new (S.Context) |
| 3799 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3800 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3801 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3802 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3803 | D->addAttr(::new (S.Context) |
| 3804 | CDeclAttr(Attr.getRange(), S.Context, |
| 3805 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3806 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3807 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3808 | D->addAttr(::new (S.Context) |
| 3809 | PascalAttr(Attr.getRange(), S.Context, |
| 3810 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3811 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3812 | case AttributeList::AT_MSABI: |
| 3813 | D->addAttr(::new (S.Context) |
| 3814 | MSABIAttr(Attr.getRange(), S.Context, |
| 3815 | Attr.getAttributeSpellingListIndex())); |
| 3816 | return; |
| 3817 | case AttributeList::AT_SysVABI: |
| 3818 | D->addAttr(::new (S.Context) |
| 3819 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3820 | Attr.getAttributeSpellingListIndex())); |
| 3821 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3822 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3823 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3824 | switch (CC) { |
| 3825 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3826 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3827 | break; |
| 3828 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3829 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3830 | break; |
| 3831 | default: |
| 3832 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3833 | } |
| 3834 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3835 | D->addAttr(::new (S.Context) |
| 3836 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3837 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3838 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3839 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3840 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3841 | D->addAttr(::new (S.Context) |
| 3842 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3843 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3844 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3845 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3846 | D->addAttr(::new (S.Context) |
| 3847 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3848 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3849 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3850 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3851 | default: |
| 3852 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3853 | } |
| 3854 | } |
| 3855 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3856 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3857 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3860 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3861 | Expr *E = Attr.getArgAsExpr(0); |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3862 | llvm::APSInt ArgNum(32); |
| 3863 | if (E->isTypeDependent() || E->isValueDependent() || |
| 3864 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Aaron Ballman | 9d69509 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3865 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3866 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3867 | << E->getSourceRange(); |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3868 | return; |
| 3869 | } |
| 3870 | |
| 3871 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr( |
| 3872 | Attr.getRange(), S.Context, ArgNum.getZExtValue())); |
| 3873 | } |
| 3874 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3875 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3876 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3877 | if (attr.isInvalid()) |
| 3878 | return true; |
| 3879 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3880 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3881 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3882 | attr.setInvalid(); |
| 3883 | return true; |
| 3884 | } |
| 3885 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3886 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3887 | // move to TargetAttributesSema one day. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3888 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3889 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3890 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3891 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3892 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3893 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3894 | case AttributeList::AT_MSABI: |
| 3895 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3896 | CC_X86_64Win64; |
| 3897 | break; |
| 3898 | case AttributeList::AT_SysVABI: |
| 3899 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3900 | CC_C; |
| 3901 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3902 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3903 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3904 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3905 | attr.setInvalid(); |
| 3906 | return true; |
| 3907 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3908 | if (StrRef == "aapcs") { |
| 3909 | CC = CC_AAPCS; |
| 3910 | break; |
| 3911 | } else if (StrRef == "aapcs-vfp") { |
| 3912 | CC = CC_AAPCS_VFP; |
| 3913 | break; |
| 3914 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3915 | |
| 3916 | attr.setInvalid(); |
| 3917 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3918 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3919 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3920 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3921 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3922 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3925 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3926 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3927 | if (A == TargetInfo::CCCR_Warning) { |
| 3928 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3929 | |
| 3930 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3931 | if (FD) |
| 3932 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3933 | TargetInfo::CCMT_NonMember; |
| 3934 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3937 | return false; |
| 3938 | } |
| 3939 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3940 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3941 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3942 | |
| 3943 | unsigned numParams; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3944 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3945 | return; |
| 3946 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3947 | if (!isa<ObjCMethodDecl>(D)) { |
| 3948 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3949 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3950 | return; |
| 3951 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3952 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3953 | D->addAttr(::new (S.Context) |
| 3954 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 3955 | Attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
| 3958 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3959 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3960 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3961 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3962 | return true; |
| 3963 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3964 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3965 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3966 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3967 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3968 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3969 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3970 | llvm::APSInt NumParams(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3971 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3972 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Aaron Ballman | 9d69509 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 3973 | Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3974 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3975 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3976 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3977 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3978 | } |
| 3979 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3980 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3981 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3982 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3983 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3984 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3985 | } |
| 3986 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3987 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3988 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3989 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3990 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3991 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3992 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3993 | } |
| 3994 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3995 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3996 | } |
| 3997 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3998 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3999 | if (S.LangOpts.CUDA) { |
| 4000 | // check the attribute arguments. |
| 4001 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 4002 | // FIXME: 0 is not okay. |
| 4003 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4004 | return; |
| 4005 | } |
| 4006 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4007 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4008 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4009 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4010 | return; |
| 4011 | } |
| 4012 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4013 | Expr *MaxThreadsExpr = Attr.getArgAsExpr(0); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4014 | llvm::APSInt MaxThreads(32); |
| 4015 | if (MaxThreadsExpr->isTypeDependent() || |
| 4016 | MaxThreadsExpr->isValueDependent() || |
| 4017 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4018 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4019 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4020 | << MaxThreadsExpr->getSourceRange(); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4021 | return; |
| 4022 | } |
| 4023 | |
| 4024 | llvm::APSInt MinBlocks(32); |
| 4025 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4026 | Expr *MinBlocksExpr = Attr.getArgAsExpr(1); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4027 | if (MinBlocksExpr->isTypeDependent() || |
| 4028 | MinBlocksExpr->isValueDependent() || |
| 4029 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4030 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4031 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4032 | << MinBlocksExpr->getSourceRange(); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4033 | return; |
| 4034 | } |
| 4035 | } |
| 4036 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4037 | D->addAttr(::new (S.Context) |
| 4038 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 4039 | MaxThreads.getZExtValue(), |
| 4040 | MinBlocks.getZExtValue(), |
| 4041 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4042 | } else { |
| 4043 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4044 | } |
| 4045 | } |
| 4046 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4047 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4048 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4049 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4050 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4051 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4052 | return; |
| 4053 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4054 | |
| 4055 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4056 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4057 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4058 | StringRef AttrName = Attr.getName()->getName(); |
| 4059 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4060 | |
| 4061 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4062 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4063 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4064 | return; |
| 4065 | } |
| 4066 | |
| 4067 | uint64_t ArgumentIdx; |
| 4068 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4069 | Attr.getLoc(), 2, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4070 | Attr.getArgAsExpr(1), ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4071 | return; |
| 4072 | |
| 4073 | uint64_t TypeTagIdx; |
| 4074 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4075 | Attr.getLoc(), 3, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4076 | Attr.getArgAsExpr(2), TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4077 | return; |
| 4078 | |
| 4079 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4080 | if (IsPointer) { |
| 4081 | // Ensure that buffer has a pointer type. |
| 4082 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4083 | if (!BufferTy->isPointerType()) { |
| 4084 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4085 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4086 | } |
| 4087 | } |
| 4088 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4089 | D->addAttr(::new (S.Context) |
| 4090 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4091 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4092 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4096 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4097 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4098 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4099 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4100 | return; |
| 4101 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4102 | |
| 4103 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4104 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4105 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4106 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4107 | TypeSourceInfo *MatchingCTypeLoc = 0; |
| 4108 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 4109 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4110 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4111 | D->addAttr(::new (S.Context) |
| 4112 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4113 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4114 | Attr.getLayoutCompatible(), |
| 4115 | Attr.getMustBeNull(), |
| 4116 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4119 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4120 | // Checker-specific attribute handlers. |
| 4121 | //===----------------------------------------------------------------------===// |
| 4122 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4123 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4124 | return type->isDependentType() || |
| 4125 | type->isObjCObjectPointerType() || |
| 4126 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4127 | } |
| 4128 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4129 | return type->isDependentType() || |
| 4130 | type->isPointerType() || |
| 4131 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4134 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4135 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4136 | if (!param) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4137 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4138 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4139 | return; |
| 4140 | } |
| 4141 | |
| 4142 | bool typeOK, cf; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4143 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4144 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4145 | cf = false; |
| 4146 | } else { |
| 4147 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4148 | cf = true; |
| 4149 | } |
| 4150 | |
| 4151 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4152 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4153 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4154 | return; |
| 4155 | } |
| 4156 | |
| 4157 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4158 | param->addAttr(::new (S.Context) |
| 4159 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4160 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4161 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4162 | param->addAttr(::new (S.Context) |
| 4163 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4164 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4167 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4168 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4169 | if (!isa<ObjCMethodDecl>(D)) { |
| 4170 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4171 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4172 | return; |
| 4173 | } |
| 4174 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4175 | D->addAttr(::new (S.Context) |
| 4176 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4177 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4178 | } |
| 4179 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4180 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4181 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4182 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4183 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4184 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4185 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4186 | returnType = MD->getResultType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4187 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4188 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4189 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4190 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4191 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4192 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4193 | returnType = FD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4194 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4195 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4196 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4197 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4198 | return; |
| 4199 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4200 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4201 | bool typeOK; |
| 4202 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4203 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4204 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4205 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4206 | case AttributeList::AT_NSReturnsRetained: |
| 4207 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4208 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4209 | cf = false; |
| 4210 | break; |
| 4211 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4212 | case AttributeList::AT_CFReturnsRetained: |
| 4213 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4214 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4215 | cf = true; |
| 4216 | break; |
| 4217 | } |
| 4218 | |
| 4219 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4220 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4221 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4222 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4223 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4224 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4225 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4226 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4227 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4228 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4229 | D->addAttr(::new (S.Context) |
| 4230 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4231 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4232 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4233 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4234 | D->addAttr(::new (S.Context) |
| 4235 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4236 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4237 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4238 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4239 | D->addAttr(::new (S.Context) |
| 4240 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4241 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4242 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4243 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4244 | D->addAttr(::new (S.Context) |
| 4245 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4246 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4247 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4248 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4249 | D->addAttr(::new (S.Context) |
| 4250 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4251 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4252 | return; |
| 4253 | }; |
| 4254 | } |
| 4255 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4256 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4257 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4258 | const int EP_ObjCMethod = 1; |
| 4259 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4260 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4261 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4262 | QualType resultType; |
| 4263 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4264 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4265 | |
Fariborz Jahanian | a53e5d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4266 | if (!method) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4267 | ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D); |
| 4268 | if (!property) { |
| 4269 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4270 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty; |
| 4271 | return; |
| 4272 | } |
| 4273 | resultType = property->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4274 | } |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4275 | else |
| 4276 | // Check that the method returns a normal pointer. |
| 4277 | resultType = method->getResultType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4278 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4279 | if (!resultType->isReferenceType() && |
| 4280 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4281 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4282 | << SourceRange(loc) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4283 | << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty) |
| 4284 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4285 | |
| 4286 | // Drop the attribute. |
| 4287 | return; |
| 4288 | } |
| 4289 | |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4290 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4291 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4292 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4293 | } |
| 4294 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4295 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4296 | const AttributeList &attr) { |
| 4297 | SourceLocation loc = attr.getLoc(); |
| 4298 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4299 | |
| 4300 | if (!method) { |
| 4301 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4302 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4303 | return; |
| 4304 | } |
| 4305 | DeclContext *DC = method->getDeclContext(); |
| 4306 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4307 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4308 | << attr.getName() << 0; |
| 4309 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4310 | return; |
| 4311 | } |
| 4312 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4313 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4314 | << attr.getName() << 1; |
| 4315 | return; |
| 4316 | } |
| 4317 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4318 | method->addAttr(::new (S.Context) |
| 4319 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4320 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4323 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4324 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4325 | if (!isa<FunctionDecl>(D)) { |
| 4326 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4327 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4328 | return; |
| 4329 | } |
| 4330 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4331 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4332 | |
| 4333 | // Check whether there's a conflicting attribute already present. |
| 4334 | Attr *Existing; |
| 4335 | if (IsAudited) { |
| 4336 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4337 | } else { |
| 4338 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4339 | } |
| 4340 | if (Existing) { |
| 4341 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4342 | << A.getName() |
| 4343 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4344 | << A.getRange() << Existing->getRange(); |
| 4345 | return; |
| 4346 | } |
| 4347 | |
| 4348 | // All clear; add the attribute. |
| 4349 | if (IsAudited) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4350 | D->addAttr(::new (S.Context) |
| 4351 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4352 | A.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4353 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4354 | D->addAttr(::new (S.Context) |
| 4355 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4356 | A.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4357 | } |
| 4358 | } |
| 4359 | |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4360 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4361 | const AttributeList &Attr) { |
| 4362 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4363 | if (!RD || RD->isUnion()) { |
| 4364 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4365 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4368 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4369 | |
| 4370 | // In Objective-C, verify that the type names an Objective-C type. |
| 4371 | // We don't want to check this outside of ObjC because people sometimes |
| 4372 | // do crazy C declarations of Objective-C types. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4373 | if (Parm && S.getLangOpts().ObjC1) { |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4374 | // Check for an existing type with this name. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4375 | LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc, |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4376 | Sema::LookupOrdinaryName); |
| 4377 | if (S.LookupName(R, Sc)) { |
| 4378 | NamedDecl *Target = R.getFoundDecl(); |
| 4379 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4380 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4381 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4382 | } |
| 4383 | } |
| 4384 | } |
| 4385 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4386 | D->addAttr(::new (S.Context) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4387 | NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4388 | Attr.getAttributeSpellingListIndex())); |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4391 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 4392 | const AttributeList &Attr) { |
Fariborz Jahanian | db3d855 | 2013-11-19 00:09:48 +0000 | [diff] [blame] | 4393 | if (!isa<RecordDecl>(D)) { |
Fariborz Jahanian | f720f86 | 2013-11-19 17:42:25 +0000 | [diff] [blame] | 4394 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4395 | << Attr.getName() |
| 4396 | << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass |
| 4397 | : ExpectedStructOrUnion); |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4398 | return; |
| 4399 | } |
| 4400 | |
| 4401 | if (Attr.getNumArgs() != 1) { |
| 4402 | S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id); |
| 4403 | return; |
| 4404 | } |
| 4405 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
| 4406 | if (!Parm) { |
| 4407 | S.Diag(D->getLocStart(), diag::err_objc_bridge_not_id); |
| 4408 | return; |
| 4409 | } |
| 4410 | |
| 4411 | D->addAttr(::new (S.Context) |
| 4412 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0, |
| 4413 | Attr.getAttributeSpellingListIndex())); |
| 4414 | } |
| 4415 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4416 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4417 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4418 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4419 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4420 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4421 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4422 | } |
| 4423 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4424 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4425 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4426 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4427 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4428 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4429 | return; |
| 4430 | } |
| 4431 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4432 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4433 | QualType type = vd->getType(); |
| 4434 | |
| 4435 | if (!type->isDependentType() && |
| 4436 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4437 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4438 | << type; |
| 4439 | return; |
| 4440 | } |
| 4441 | |
| 4442 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4443 | |
| 4444 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4445 | // going to infer. |
| 4446 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4447 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4448 | |
| 4449 | switch (lifetime) { |
| 4450 | case Qualifiers::OCL_None: |
| 4451 | assert(type->isDependentType() && |
| 4452 | "didn't infer lifetime for non-dependent type?"); |
| 4453 | break; |
| 4454 | |
| 4455 | case Qualifiers::OCL_Weak: // meaningful |
| 4456 | case Qualifiers::OCL_Strong: // meaningful |
| 4457 | break; |
| 4458 | |
| 4459 | case Qualifiers::OCL_ExplicitNone: |
| 4460 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4461 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4462 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4463 | break; |
| 4464 | } |
| 4465 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4466 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4467 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4468 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4469 | } |
| 4470 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4471 | //===----------------------------------------------------------------------===// |
| 4472 | // Microsoft specific attribute handlers. |
| 4473 | //===----------------------------------------------------------------------===// |
| 4474 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4475 | // Check if MS extensions or some other language extensions are enabled. If |
| 4476 | // not, issue a diagnostic that the given attribute is unused. |
| 4477 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4478 | bool OtherExtension = false) { |
| 4479 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4480 | return true; |
| 4481 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4482 | return false; |
| 4483 | } |
| 4484 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4485 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4486 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4487 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4488 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4489 | StringRef StrRef; |
| 4490 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4491 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4492 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4493 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4494 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4495 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4496 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4497 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4498 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4499 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4500 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4501 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4502 | return; |
| 4503 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4504 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4505 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4506 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4507 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4508 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4509 | return; |
| 4510 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4511 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4512 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4513 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4514 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4515 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4516 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4517 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 4518 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4519 | } |
| 4520 | |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4521 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4522 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4523 | return; |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4524 | |
| 4525 | AttributeList::Kind Kind = Attr.getKind(); |
| 4526 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4527 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4528 | ::new (S.Context) |
| 4529 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4530 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4531 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4532 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4533 | ::new (S.Context) |
| 4534 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4535 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4536 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4537 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4538 | ::new (S.Context) |
| 4539 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4540 | Attr.getAttributeSpellingListIndex())); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4541 | } |
| 4542 | |
| 4543 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4544 | if (!checkMicrosoftExt(S, Attr)) |
| 4545 | return; |
| 4546 | |
| 4547 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4548 | if (Kind == AttributeList::AT_Win64) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4549 | D->addAttr( |
| 4550 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4551 | Attr.getAttributeSpellingListIndex())); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4552 | } |
| 4553 | |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4554 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4555 | if (!checkMicrosoftExt(S, Attr)) |
| 4556 | return; |
| 4557 | D->addAttr(::new (S.Context) |
| 4558 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4559 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4562 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4563 | if (!checkMicrosoftExt(S, Attr)) |
| 4564 | return; |
| 4565 | // Check linkage after possibly merging declaratinos. See |
| 4566 | // checkAttributesAfterMerging(). |
| 4567 | D->addAttr(::new (S.Context) |
| 4568 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4569 | Attr.getAttributeSpellingListIndex())); |
| 4570 | } |
| 4571 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4572 | /// Handles semantic checking for features that are common to all attributes, |
| 4573 | /// such as checking whether a parameter was properly specified, or the correct |
| 4574 | /// number of arguments were passed, etc. |
| 4575 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 4576 | const AttributeList &Attr) { |
| 4577 | // Several attributes carry different semantics than the parsing requires, so |
| 4578 | // those are opted out of the common handling. |
| 4579 | // |
| 4580 | // We also bail on unknown and ignored attributes because those are handled |
| 4581 | // as part of the target-specific handling logic. |
| 4582 | if (Attr.hasCustomParsing() || |
| 4583 | Attr.getKind() == AttributeList::UnknownAttribute || |
| 4584 | Attr.getKind() == AttributeList::IgnoredAttribute) |
| 4585 | return false; |
| 4586 | |
| 4587 | // If there are no optional arguments, then checking for the argument count |
| 4588 | // is trivial. |
| 4589 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 4590 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4591 | return true; |
| 4592 | return false; |
| 4593 | } |
| 4594 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4595 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4596 | // Top Level Sema Entry Points |
| 4597 | //===----------------------------------------------------------------------===// |
| 4598 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4599 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4600 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4601 | /// silently ignore it if a GNU attribute. |
| 4602 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4603 | const AttributeList &Attr, |
| 4604 | bool IncludeCXX11Attributes) { |
| 4605 | if (Attr.isInvalid()) |
| 4606 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4607 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4608 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4609 | // instead. |
| 4610 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4611 | return; |
| 4612 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4613 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4614 | return; |
| 4615 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4616 | switch (Attr.getKind()) { |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4617 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4618 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4619 | case AttributeList::AT_IBOutletCollection: |
| 4620 | handleIBOutletCollection(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4621 | case AttributeList::AT_AddressSpace: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4622 | case AttributeList::AT_ObjCGC: |
| 4623 | case AttributeList::AT_VectorSize: |
| 4624 | case AttributeList::AT_NeonVectorType: |
| 4625 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4626 | case AttributeList::AT_Ptr32: |
| 4627 | case AttributeList::AT_Ptr64: |
| 4628 | case AttributeList::AT_SPtr: |
| 4629 | case AttributeList::AT_UPtr: |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4630 | // Ignore these, these are type attributes, handled by |
| 4631 | // ProcessTypeAttributes. |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4632 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4633 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4634 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4635 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4636 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4637 | handleAlwaysInlineAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4638 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4639 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4640 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4641 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4642 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4643 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4644 | handleDependencyAttr(S, scope, D, Attr); |
| 4645 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4646 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4647 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4648 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4649 | case AttributeList::AT_CXX11NoReturn: |
| 4650 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4651 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4652 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4653 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4654 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4655 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4656 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4657 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4658 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4659 | case AttributeList::AT_MinSize: |
| 4660 | handleMinSizeAttr(S, D, Attr); |
| 4661 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4662 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4663 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4664 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | 9a22621 | 2013-08-28 23:13:26 +0000 | [diff] [blame] | 4665 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4666 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4667 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4668 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4669 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4670 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4671 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4672 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4673 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4674 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4675 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4676 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4677 | case AttributeList::AT_ownership_returns: |
| 4678 | case AttributeList::AT_ownership_takes: |
| 4679 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4680 | handleOwnershipAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4681 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4682 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4683 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4684 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4685 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4686 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4687 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4688 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4689 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4690 | handleObjCOwnershipAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4691 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4692 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4693 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4694 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4695 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4696 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4697 | case AttributeList::AT_ObjCRequiresSuper: |
| 4698 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4699 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4700 | case AttributeList::AT_NSBridged: |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4701 | handleNSBridgedAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4702 | |
| 4703 | case AttributeList::AT_ObjCBridge: |
| 4704 | handleObjCBridgeAttr(S, scope, D, Attr); break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4705 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4706 | case AttributeList::AT_CFAuditedTransfer: |
| 4707 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4708 | handleCFTransferAttr(S, D, Attr); break; |
| 4709 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4710 | // Checker-specific. |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4711 | case AttributeList::AT_CFConsumed: |
| 4712 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4713 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4714 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4715 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4716 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4717 | case AttributeList::AT_NSReturnsNotRetained: |
| 4718 | case AttributeList::AT_CFReturnsNotRetained: |
| 4719 | case AttributeList::AT_NSReturnsRetained: |
| 4720 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4721 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4722 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4723 | case AttributeList::AT_WorkGroupSizeHint: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4724 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4725 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4726 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4727 | case AttributeList::AT_VecTypeHint: |
| 4728 | handleVecTypeHint(S, D, Attr); break; |
| 4729 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4730 | case AttributeList::AT_InitPriority: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4731 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4732 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4733 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4734 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4735 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4736 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4737 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4738 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4739 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4740 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4741 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4742 | handleObjCRootClassAttr(S, D, Attr); |
| 4743 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4744 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 0c2c90b | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4745 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | 9d4d20a | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4746 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4747 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4748 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4749 | handleReturnsTwiceAttr(S, D, Attr); |
| 4750 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4751 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4752 | case AttributeList::AT_Visibility: |
| 4753 | handleVisibilityAttr(S, D, Attr, false); |
| 4754 | break; |
| 4755 | case AttributeList::AT_TypeVisibility: |
| 4756 | handleVisibilityAttr(S, D, Attr, true); |
| 4757 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4758 | case AttributeList::AT_WarnUnused: |
| 4759 | handleWarnUnusedAttr(S, D, Attr); |
| 4760 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4761 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4762 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4763 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4764 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4765 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4766 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4767 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4768 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4769 | case AttributeList::AT_ObjCException: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4770 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4771 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4772 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4773 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4774 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4775 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4776 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4777 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4778 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4779 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4780 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4781 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4782 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4783 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4784 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | b4f3134 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4785 | // Just ignore |
| 4786 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4787 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4788 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4789 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4790 | case AttributeList::AT_StdCall: |
| 4791 | case AttributeList::AT_CDecl: |
| 4792 | case AttributeList::AT_FastCall: |
| 4793 | case AttributeList::AT_ThisCall: |
| 4794 | case AttributeList::AT_Pascal: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4795 | case AttributeList::AT_MSABI: |
| 4796 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4797 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4798 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4799 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4800 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4801 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4802 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4803 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4804 | break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4805 | case AttributeList::AT_OpenCLImageAccess: |
| 4806 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4807 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4808 | |
| 4809 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4810 | case AttributeList::AT_MsStruct: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4811 | handleMsStructAttr(S, D, Attr); |
| 4812 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4813 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4814 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4815 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4816 | case AttributeList::AT_SingleInheritance: |
| 4817 | case AttributeList::AT_MultipleInheritance: |
| 4818 | case AttributeList::AT_VirtualInheritance: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4819 | handleInheritanceAttr(S, D, Attr); |
| 4820 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4821 | case AttributeList::AT_Win64: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4822 | handlePortabilityAttr(S, D, Attr); |
| 4823 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4824 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4825 | handleForceInlineAttr(S, D, Attr); |
| 4826 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4827 | case AttributeList::AT_SelectAny: |
| 4828 | handleSelectAnyAttr(S, D, Attr); |
| 4829 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4830 | |
| 4831 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4832 | case AttributeList::AT_AssertExclusiveLock: |
| 4833 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4834 | break; |
| 4835 | case AttributeList::AT_AssertSharedLock: |
| 4836 | handleAssertSharedLockAttr(S, D, Attr); |
| 4837 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4838 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4839 | handleGuardedVarAttr(S, D, Attr); |
| 4840 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4841 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4842 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4843 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4844 | case AttributeList::AT_ScopedLockable: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4845 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4846 | break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4847 | case AttributeList::AT_NoSanitizeAddress: |
| 4848 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4849 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4850 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4851 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 4852 | break; |
| 4853 | case AttributeList::AT_NoSanitizeThread: |
| 4854 | handleNoSanitizeThread(S, D, Attr); |
| 4855 | break; |
| 4856 | case AttributeList::AT_NoSanitizeMemory: |
| 4857 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4858 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4859 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4860 | handleLockableAttr(S, D, Attr); |
| 4861 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4862 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4863 | handleGuardedByAttr(S, D, Attr); |
| 4864 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4865 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4866 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4867 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4868 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4869 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4870 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4871 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4872 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4873 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4874 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4875 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4876 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4877 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4878 | handleLockReturnedAttr(S, D, Attr); |
| 4879 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4880 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4881 | handleLocksExcludedAttr(S, D, Attr); |
| 4882 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4883 | case AttributeList::AT_SharedLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4884 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4885 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4886 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4887 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4888 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4889 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4890 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4891 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4892 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4893 | handleUnlockFunAttr(S, D, Attr); |
| 4894 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4895 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4896 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4897 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4898 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4899 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4900 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4901 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4902 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4903 | case AttributeList::AT_Consumable: |
| 4904 | handleConsumableAttr(S, D, Attr); |
| 4905 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4906 | case AttributeList::AT_CallableWhen: |
| 4907 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4908 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4909 | case AttributeList::AT_ParamTypestate: |
| 4910 | handleParamTypestateAttr(S, D, Attr); |
| 4911 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4912 | case AttributeList::AT_ReturnTypestate: |
| 4913 | handleReturnTypestateAttr(S, D, Attr); |
| 4914 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4915 | case AttributeList::AT_SetTypestate: |
| 4916 | handleSetTypestateAttr(S, D, Attr); |
| 4917 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4918 | case AttributeList::AT_TestTypestate: |
| 4919 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4920 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4921 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4922 | // Type safety attributes. |
| 4923 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4924 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4925 | break; |
| 4926 | case AttributeList::AT_TypeTagForDatatype: |
| 4927 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4928 | break; |
| 4929 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4930 | default: |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4931 | // Ask target about the attribute. |
| 4932 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4933 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4934 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4935 | diag::warn_unhandled_ms_attribute_ignored : |
| 4936 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4937 | break; |
| 4938 | } |
| 4939 | } |
| 4940 | |
| 4941 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4942 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4943 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4944 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4945 | bool IncludeCXX11Attributes) { |
| 4946 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4947 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4948 | |
| 4949 | // GCC accepts |
| 4950 | // static int a9 __attribute__((weakref)); |
| 4951 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4952 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4953 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4954 | cast<NamedDecl>(D)->getNameAsString(); |
| 4955 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4956 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4957 | } |
| 4958 | } |
| 4959 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4960 | // Annotation attributes are the only attributes allowed after an access |
| 4961 | // specifier. |
| 4962 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4963 | const AttributeList *AttrList) { |
| 4964 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4965 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4966 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4967 | } else { |
| 4968 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4969 | return true; |
| 4970 | } |
| 4971 | } |
| 4972 | |
| 4973 | return false; |
| 4974 | } |
| 4975 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4976 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4977 | /// contains any decl attributes that we should warn about. |
| 4978 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4979 | for ( ; A; A = A->getNext()) { |
| 4980 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4981 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4982 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4983 | |
| 4984 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4985 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4986 | << A->getName() << A->getRange(); |
| 4987 | } else { |
| 4988 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4989 | << A->getName() << A->getRange(); |
| 4990 | } |
| 4991 | } |
| 4992 | } |
| 4993 | |
| 4994 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4995 | /// used to build a declaration, complain about any decl attributes |
| 4996 | /// which might be lying around on it. |
| 4997 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4998 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4999 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5000 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5001 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5002 | } |
| 5003 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5004 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5005 | /// \#pragma weak needs a non-definition decl and source may not have one. |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5006 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5007 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5008 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5009 | NamedDecl *NewD = 0; |
| 5010 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5011 | FunctionDecl *NewFD; |
| 5012 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 5013 | // FIXME: Mangling? |
| 5014 | // FIXME: Is the qualifier info correct? |
| 5015 | // FIXME: Is the DeclContext correct? |
| 5016 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 5017 | Loc, Loc, DeclarationName(II), |
| 5018 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5019 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5020 | FD->hasPrototype(), |
| 5021 | false/*isConstexprSpecified*/); |
| 5022 | NewD = NewFD; |
| 5023 | |
| 5024 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5025 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5026 | |
| 5027 | // Fake up parameter variables; they are declared as if this were |
| 5028 | // a typedef. |
| 5029 | QualType FDTy = FD->getType(); |
| 5030 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5031 | SmallVector<ParmVarDecl*, 16> Params; |
| 5032 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 5033 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 5034 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 5035 | Param->setScopeInfo(0, Params.size()); |
| 5036 | Params.push_back(Param); |
| 5037 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5038 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5039 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5040 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5041 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5042 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5043 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5044 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5045 | if (VD->getQualifier()) { |
| 5046 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5047 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5048 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5049 | } |
| 5050 | return NewD; |
| 5051 | } |
| 5052 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5053 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5054 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5055 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5056 | if (W.getUsed()) return; // only do this once |
| 5057 | W.setUsed(true); |
| 5058 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5059 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5060 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5061 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 5062 | NDId->getName())); |
| 5063 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5064 | WeakTopLevelDecl.push_back(NewD); |
| 5065 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5066 | // to insert Decl at TU scope, sorry. |
| 5067 | DeclContext *SavedContext = CurContext; |
| 5068 | CurContext = Context.getTranslationUnitDecl(); |
| 5069 | PushOnScopeChains(NewD, S); |
| 5070 | CurContext = SavedContext; |
| 5071 | } else { // just add weak to existing |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5072 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5073 | } |
| 5074 | } |
| 5075 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5076 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5077 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5078 | // have to do this. |
| 5079 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5080 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5081 | NamedDecl *ND = NULL; |
| 5082 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5083 | if (VD->isExternC()) |
| 5084 | ND = VD; |
| 5085 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5086 | if (FD->isExternC()) |
| 5087 | ND = FD; |
| 5088 | if (ND) { |
| 5089 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5090 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5091 | = WeakUndeclaredIdentifiers.find(Id); |
| 5092 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5093 | WeakInfo W = I->second; |
| 5094 | DeclApplyPragmaWeak(S, ND, W); |
| 5095 | WeakUndeclaredIdentifiers[Id] = W; |
| 5096 | } |
| 5097 | } |
| 5098 | } |
| 5099 | } |
| 5100 | } |
| 5101 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5102 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5103 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5104 | /// specified in many different places, and we need to find and apply them all. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5105 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5106 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5107 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5108 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5109 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5110 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5111 | // position to the decl itself. This handles cases like: |
| 5112 | // int *__attr__(x)** D; |
| 5113 | // when X is a decl attribute. |
| 5114 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5115 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5116 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5117 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5118 | // Finally, apply any attributes on the decl itself. |
| 5119 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5120 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5121 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5122 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5123 | /// Is the given declaration allowed to use a forbidden type? |
| 5124 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5125 | // Private ivars are always okay. Unfortunately, people don't |
| 5126 | // always properly make their ivars private, even in system headers. |
| 5127 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5128 | // Function declarations in sys headers will be marked unavailable. |
| 5129 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5130 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5131 | return false; |
| 5132 | |
| 5133 | // Require it to be declared in a system header. |
| 5134 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5135 | } |
| 5136 | |
| 5137 | /// Handle a delayed forbidden-type diagnostic. |
| 5138 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5139 | Decl *decl) { |
| 5140 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5141 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5142 | "this system declaration uses an unsupported type")); |
| 5143 | return; |
| 5144 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5145 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5146 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5147 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5148 | // kind of forbidden type messages on unavailable functions. |
| 5149 | if (FD->hasAttr<UnavailableAttr>() && |
| 5150 | diag.getForbiddenTypeDiagnostic() == |
| 5151 | diag::err_arc_array_param_no_ownership) { |
| 5152 | diag.Triggered = true; |
| 5153 | return; |
| 5154 | } |
| 5155 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5156 | |
| 5157 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5158 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5159 | diag.Triggered = true; |
| 5160 | } |
| 5161 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5162 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5163 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5164 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5165 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5166 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5167 | // When delaying diagnostics to run in the context of a parsed |
| 5168 | // declaration, we only want to actually emit anything if parsing |
| 5169 | // succeeds. |
| 5170 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5171 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5172 | // We emit all the active diagnostics in this pool or any of its |
| 5173 | // parents. In general, we'll get one pool for the decl spec |
| 5174 | // and a child pool for each declarator; in a decl group like: |
| 5175 | // deprecated_typedef foo, *bar, baz(); |
| 5176 | // only the declarator pops will be passed decls. This is correct; |
| 5177 | // we really do need to consider delayed diagnostics from the decl spec |
| 5178 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5179 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5180 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5181 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5182 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5183 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5184 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5185 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5186 | continue; |
| 5187 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5188 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5189 | case DelayedDiagnostic::Deprecation: |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5190 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5191 | if (!decl->isInvalidDecl()) |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5192 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5193 | break; |
| 5194 | |
| 5195 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5196 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5197 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5198 | |
| 5199 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5200 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5201 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5202 | } |
| 5203 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5204 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5205 | } |
| 5206 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5207 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5208 | /// been delayed in the current context instead of in the given pool. |
| 5209 | /// Essentially, this just moves them to the current pool. |
| 5210 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5211 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5212 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5213 | curPool->steal(pool); |
| 5214 | } |
| 5215 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5216 | static bool isDeclDeprecated(Decl *D) { |
| 5217 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5218 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5219 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5220 | // A category implicitly has the availability of the interface. |
| 5221 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5222 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5223 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5224 | return false; |
| 5225 | } |
| 5226 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5227 | static void |
| 5228 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5229 | SourceLocation Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5230 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5231 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5232 | DeclarationName Name = D->getDeclName(); |
| 5233 | if (!Message.empty()) { |
| 5234 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5235 | S.Diag(D->getLocation(), |
| 5236 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5237 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5238 | if (ObjCPropery) |
| 5239 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5240 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5241 | } else if (!UnknownObjCClass) { |
| 5242 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5243 | S.Diag(D->getLocation(), |
| 5244 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5245 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5246 | if (ObjCPropery) |
| 5247 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5248 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5249 | } else { |
| 5250 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5251 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5252 | } |
| 5253 | } |
| 5254 | |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5255 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5256 | Decl *Ctx) { |
| 5257 | if (isDeclDeprecated(Ctx)) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5258 | return; |
| 5259 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5260 | DD.Triggered = true; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5261 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5262 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5263 | DD.getUnknownObjCClass(), |
| 5264 | DD.getObjCProperty()); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5265 | } |
| 5266 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5267 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5268 | SourceLocation Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5269 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5270 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5271 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5272 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5273 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5274 | UnknownObjCClass, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5275 | ObjCProperty, |
Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5276 | Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5277 | return; |
| 5278 | } |
| 5279 | |
| 5280 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5281 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5282 | return; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5283 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5284 | } |