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 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 64 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame^] | 65 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 66 | C, |
| 67 | Cpp, |
| 68 | ObjC |
| 69 | }; |
| 70 | } |
| 71 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 72 | //===----------------------------------------------------------------------===// |
| 73 | // Helper functions |
| 74 | //===----------------------------------------------------------------------===// |
| 75 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 76 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 77 | bool blocksToo = true) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 78 | QualType Ty; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 79 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 80 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 81 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 82 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 83 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 84 | Ty = decl->getUnderlyingType(); |
| 85 | else |
| 86 | return 0; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 88 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 89 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 28c433d | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 90 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 91 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 92 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 93 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 96 | // FIXME: We should provide an abstraction around a method or function |
| 97 | // to provide the following bits of information. |
| 98 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 99 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 100 | /// type (function or function-typed variable). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 101 | static bool isFunction(const Decl *D) { |
| 102 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 106 | /// type (function or function-typed variable) or an Objective-C |
| 107 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 108 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 109 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 112 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 113 | /// type (function or function-typed variable) or an Objective-C |
| 114 | /// method or a block. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 115 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 116 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 117 | return true; |
| 118 | // check for block is more involved. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 119 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 120 | QualType Ty = V->getType(); |
| 121 | return Ty->isBlockPointerType(); |
| 122 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 123 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 124 | } |
| 125 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 126 | /// Return true if the given decl has a declarator that should have |
| 127 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 128 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 129 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 130 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 131 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 134 | /// hasFunctionProto - Return true if the given decl has a argument |
| 135 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 136 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 137 | static bool hasFunctionProto(const Decl *D) { |
| 138 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 139 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 140 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 141 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 142 | return true; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 147 | /// arguments. It is an error to call this on a K&R function (use |
| 148 | /// hasFunctionProto first). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 149 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 150 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 151 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
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->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 154 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 157 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 158 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 159 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 160 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 161 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 162 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 163 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 166 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 167 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 168 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 169 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 172 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 173 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 174 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 175 | return proto->isVariadic(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 176 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 177 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 178 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 179 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 183 | static bool isInstanceMethod(const Decl *D) { |
| 184 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 185 | return MethodDecl->isInstance(); |
| 186 | return false; |
| 187 | } |
| 188 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 189 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 190 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 191 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 192 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 193 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 194 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 195 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 196 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 197 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 198 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 200 | // FIXME: Should we walk the chain of classes? |
| 201 | return ClsName == &Ctx.Idents.get("NSString") || |
| 202 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 203 | } |
| 204 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 205 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 206 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 207 | if (!PT) |
| 208 | return false; |
| 209 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 210 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 211 | if (!RT) |
| 212 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 213 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 214 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 215 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 216 | return false; |
| 217 | |
| 218 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 219 | } |
| 220 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 221 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 222 | // FIXME: Include the type in the argument list. |
| 223 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 224 | } |
| 225 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 226 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 227 | /// output an error. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 228 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 229 | unsigned Num) { |
| 230 | if (getNumAttributeArgs(Attr) != Num) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 231 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 232 | << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
| 236 | return true; |
| 237 | } |
| 238 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 239 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 240 | /// \brief Check if the attribute has at least as many args as Num. May |
| 241 | /// output an error. |
| 242 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 243 | unsigned Num) { |
| 244 | if (getNumAttributeArgs(Attr) < Num) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 245 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 246 | return false; |
| 247 | } |
| 248 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 249 | return true; |
| 250 | } |
| 251 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 252 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 253 | /// expression and return success or failure. May output an error. |
| 254 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 255 | const Expr *Expr, uint32_t &Val, |
| 256 | unsigned Idx = UINT_MAX) { |
| 257 | llvm::APSInt I(32); |
| 258 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 259 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 260 | if (Idx != UINT_MAX) |
| 261 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 262 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 263 | << Expr->getSourceRange(); |
| 264 | else |
| 265 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 266 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 267 | << Expr->getSourceRange(); |
| 268 | return false; |
| 269 | } |
| 270 | Val = (uint32_t)I.getZExtValue(); |
| 271 | return true; |
| 272 | } |
| 273 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 274 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 275 | /// instance method D. May output an error. |
| 276 | /// |
| 277 | /// \returns true if IdxExpr is a valid index. |
| 278 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 279 | StringRef AttrName, |
| 280 | SourceLocation AttrLoc, |
| 281 | unsigned AttrArgNum, |
| 282 | const Expr *IdxExpr, |
| 283 | uint64_t &Idx) |
| 284 | { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 285 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 286 | |
| 287 | // In C++ the implicit 'this' function parameter also counts. |
| 288 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 289 | bool HP = hasFunctionProto(D); |
| 290 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 291 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 292 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 293 | HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 294 | |
| 295 | llvm::APSInt IdxInt; |
| 296 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 297 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 298 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 299 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 300 | << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 301 | return false; |
| 302 | } |
| 303 | |
| 304 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 305 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 306 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 307 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 308 | return false; |
| 309 | } |
| 310 | Idx--; // Convert to zero-based. |
| 311 | if (HasImplicitThisParam) { |
| 312 | if (Idx == 0) { |
| 313 | S.Diag(AttrLoc, |
| 314 | diag::err_attribute_invalid_implicit_this_argument) |
| 315 | << AttrName << IdxExpr->getSourceRange(); |
| 316 | return false; |
| 317 | } |
| 318 | --Idx; |
| 319 | } |
| 320 | |
| 321 | return true; |
| 322 | } |
| 323 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 324 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 325 | /// If not emit an error and return false. If the argument is an identifier it |
| 326 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 327 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 328 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 329 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 330 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 331 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 332 | if (Attr.isArgIdent(ArgNum)) { |
| 333 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 334 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 335 | << Attr.getName() << AANT_ArgumentString |
| 336 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 337 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 338 | Str = Loc->Ident->getName(); |
| 339 | if (ArgLocation) |
| 340 | *ArgLocation = Loc->Loc; |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | // Now check for an actual string literal. |
| 345 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 346 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 347 | if (ArgLocation) |
| 348 | *ArgLocation = ArgExpr->getLocStart(); |
| 349 | |
| 350 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 351 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 352 | << Attr.getName() << AANT_ArgumentString; |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | Str = Literal->getString(); |
| 357 | return true; |
| 358 | } |
| 359 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 360 | /// |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 361 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 362 | /// \return true if the Decl is a field or potentially shared global variable |
| 363 | /// |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 364 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 365 | if (isa<FieldDecl>(D)) |
| 366 | return true; |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 367 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 368 | return vd->hasGlobalStorage() && !vd->getTLSKind(); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 369 | |
| 370 | return false; |
| 371 | } |
| 372 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 373 | /// \brief Check if the passed-in expression is of type int or bool. |
| 374 | static bool isIntOrBool(Expr *Exp) { |
| 375 | QualType QT = Exp->getType(); |
| 376 | return QT->isBooleanType() || QT->isIntegerType(); |
| 377 | } |
| 378 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 379 | |
| 380 | // Check to see if the type is a smart pointer of some kind. We assume |
| 381 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 382 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 383 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 384 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 385 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 386 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 387 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 388 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 389 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 390 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 391 | return false; |
| 392 | |
| 393 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 396 | /// \brief Check if passed in Decl is a pointer type. |
| 397 | /// Note that this function may produce an error message. |
| 398 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 399 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 400 | const AttributeList &Attr) { |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 401 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 402 | QualType QT = vd->getType(); |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 403 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 404 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 405 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 406 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 407 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 408 | // (We don't want to force template instantiation if we can avoid it, |
| 409 | // since that would alter the order in which templates are instantiated.) |
| 410 | if (RT->isIncompleteType()) |
| 411 | return true; |
| 412 | |
| 413 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 414 | return true; |
| 415 | } |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 416 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 417 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 418 | << Attr.getName()->getName() << QT; |
| 419 | } else { |
| 420 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 421 | << Attr.getName(); |
| 422 | } |
| 423 | return false; |
| 424 | } |
| 425 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 426 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 427 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 428 | static const RecordType *getRecordType(QualType QT) { |
| 429 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 430 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 431 | |
| 432 | // Now check if we point to record type. |
| 433 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 434 | return PT->getPointeeType()->getAs<RecordType>(); |
| 435 | |
| 436 | return 0; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 437 | } |
| 438 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 439 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 440 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 441 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 442 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 443 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 444 | return true; |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 449 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 450 | /// resolves to a lockable object. |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 451 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 452 | QualType Ty) { |
| 453 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 454 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 455 | // Warn if could not get record type for this argument. |
Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 456 | if (!RT) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 457 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 458 | << Attr.getName() << Ty.getAsString(); |
| 459 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 460 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 461 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 462 | // 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] | 463 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 464 | return; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 465 | |
| 466 | // Allow smart pointers to be used as lockable objects. |
| 467 | // FIXME -- Check the type that the smart pointer points to. |
| 468 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 469 | return; |
| 470 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 471 | // Check if the type is lockable. |
| 472 | RecordDecl *RD = RT->getDecl(); |
| 473 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 474 | return; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 475 | |
| 476 | // Else check if any base classes are lockable. |
| 477 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 478 | CXXBasePaths BPaths(false, false); |
| 479 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 480 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 481 | } |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 482 | |
| 483 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 484 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 485 | } |
| 486 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 487 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 488 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 489 | /// \param Sidx The attribute argument index to start checking with. |
| 490 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 491 | /// parameter list. |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 492 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 493 | const AttributeList &Attr, |
| 494 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 495 | int Sidx = 0, |
| 496 | bool ParamIdxOk = false) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 497 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 498 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 499 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 500 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 501 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 502 | Args.push_back(ArgExp); |
| 503 | continue; |
| 504 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 505 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 506 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 507 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 508 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 509 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 510 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 511 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 512 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 513 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 514 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 515 | // We allow constant strings to be used as a placeholder for expressions |
| 516 | // that are not valid C++ syntax, but warn that they are ignored. |
| 517 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 518 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 519 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 520 | continue; |
| 521 | } |
| 522 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 523 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 524 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 525 | // A pointer to member expression of the form &MyClass::mu is treated |
| 526 | // specially -- we need to look at the type of the member. |
| 527 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 528 | if (UOp->getOpcode() == UO_AddrOf) |
| 529 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 530 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 531 | ArgTy = DRE->getDecl()->getType(); |
| 532 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 533 | // First see if we can just cast to record type, or point to record type. |
| 534 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 535 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 536 | // Now check if we index into a record type function param. |
| 537 | if(!RT && ParamIdxOk) { |
| 538 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 539 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 540 | if(FD && IL) { |
| 541 | unsigned int NumParams = FD->getNumParams(); |
| 542 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 543 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 544 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 545 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 546 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 547 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 548 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 549 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 550 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 554 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 555 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 556 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 557 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 560 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 561 | // Attribute Implementations |
| 562 | //===----------------------------------------------------------------------===// |
| 563 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 564 | // FIXME: All this manual attribute parsing code is gross. At the |
| 565 | // least add some helper functions to check most argument patterns (# |
| 566 | // and types of args). |
| 567 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 568 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 569 | const AttributeList &Attr) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 570 | // D must be either a member field or global (potentially shared) variable. |
| 571 | if (!mayBeSharedVariable(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 572 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 573 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 574 | return false; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 577 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 580 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 581 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 582 | return; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 583 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 584 | D->addAttr(::new (S.Context) |
| 585 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 586 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 589 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 590 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 591 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 592 | return; |
| 593 | |
| 594 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 595 | return; |
| 596 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 597 | D->addAttr(::new (S.Context) |
| 598 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 599 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 602 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 603 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 604 | Expr* &Arg) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 605 | // D must be either a member field or global (potentially shared) variable. |
| 606 | if (!mayBeSharedVariable(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 607 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 608 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 609 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 610 | } |
| 611 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 612 | SmallVector<Expr*, 1> Args; |
| 613 | // check that all arguments are lockable objects |
| 614 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 615 | unsigned Size = Args.size(); |
| 616 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 617 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 618 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 619 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 620 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 621 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 624 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 625 | Expr *Arg = 0; |
| 626 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 627 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 628 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 629 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 630 | } |
| 631 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 632 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 633 | const AttributeList &Attr) { |
| 634 | Expr *Arg = 0; |
| 635 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 636 | return; |
| 637 | |
| 638 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 639 | return; |
| 640 | |
| 641 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 642 | S.Context, Arg)); |
| 643 | } |
| 644 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 645 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 646 | const AttributeList &Attr) { |
Caitlin Sadowski | 086fb95 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 647 | // FIXME: Lockable structs for C code. |
David Blaikie | 021221d | 2013-07-29 18:24:03 +0000 | [diff] [blame] | 648 | if (!isa<RecordDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 649 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 650 | << Attr.getName() << ExpectedStructOrUnionOrClass; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 651 | return false; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 654 | return true; |
| 655 | } |
| 656 | |
| 657 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 658 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 659 | return; |
| 660 | |
| 661 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 662 | } |
| 663 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 664 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 665 | const AttributeList &Attr) { |
| 666 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 667 | return; |
| 668 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 669 | D->addAttr(::new (S.Context) |
| 670 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 671 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 674 | static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D, |
| 675 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 676 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 677 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 678 | << Attr.getName() << ExpectedFunctionOrMethod; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 679 | return; |
| 680 | } |
| 681 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 682 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 683 | S.Context)); |
| 684 | } |
| 685 | |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 686 | static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 687 | const AttributeList &Attr) { |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 688 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 689 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 690 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 691 | return; |
| 692 | } |
| 693 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 694 | D->addAttr(::new (S.Context) |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 695 | NoSanitizeAddressAttr(Attr.getRange(), S.Context, |
| 696 | Attr.getAttributeSpellingListIndex())); |
| 697 | } |
| 698 | |
| 699 | static void handleNoSanitizeMemory(Sema &S, Decl *D, |
| 700 | const AttributeList &Attr) { |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 701 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 702 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 703 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(), |
| 708 | S.Context)); |
| 709 | } |
| 710 | |
| 711 | static void handleNoSanitizeThread(Sema &S, Decl *D, |
| 712 | const AttributeList &Attr) { |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 713 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 714 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 715 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 716 | return; |
| 717 | } |
| 718 | |
| 719 | D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(), |
| 720 | S.Context)); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 721 | } |
| 722 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 723 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 724 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 725 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 726 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 727 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 728 | |
| 729 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 730 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 731 | if (!VD || !mayBeSharedVariable(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 732 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 733 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 734 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 735 | } |
| 736 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 737 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 738 | QualType QT = VD->getType(); |
| 739 | if (!QT->isDependentType()) { |
| 740 | const RecordType *RT = getRecordType(QT); |
| 741 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 742 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 743 | << Attr.getName(); |
| 744 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 748 | // Check that all arguments are lockable objects. |
| 749 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 750 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 751 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 752 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 753 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 754 | } |
| 755 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 756 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 757 | const AttributeList &Attr) { |
| 758 | SmallVector<Expr*, 1> Args; |
| 759 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 760 | return; |
| 761 | |
| 762 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 763 | D->addAttr(::new (S.Context) |
| 764 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 765 | StartArg, Args.size(), |
| 766 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 769 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 770 | const AttributeList &Attr) { |
| 771 | SmallVector<Expr*, 1> Args; |
| 772 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 773 | return; |
| 774 | |
| 775 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 776 | D->addAttr(::new (S.Context) |
| 777 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 778 | StartArg, Args.size(), |
| 779 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 782 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 783 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 784 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 785 | // zero or more arguments ok |
| 786 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 787 | // check that the attribute is applied to a function |
| 788 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 789 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 790 | << Attr.getName() << ExpectedFunctionOrMethod; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 791 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 794 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 795 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 796 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 797 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 798 | } |
| 799 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 800 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 801 | const AttributeList &Attr) { |
| 802 | SmallVector<Expr*, 1> Args; |
| 803 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 804 | return; |
| 805 | |
| 806 | unsigned Size = Args.size(); |
| 807 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 808 | D->addAttr(::new (S.Context) |
| 809 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 810 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 813 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 814 | const AttributeList &Attr) { |
| 815 | SmallVector<Expr*, 1> Args; |
| 816 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 817 | return; |
| 818 | |
| 819 | unsigned Size = Args.size(); |
| 820 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 821 | D->addAttr(::new (S.Context) |
| 822 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 823 | StartArg, Size, |
| 824 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 825 | } |
| 826 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 827 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 828 | const AttributeList &Attr) { |
| 829 | SmallVector<Expr*, 1> Args; |
| 830 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 831 | return; |
| 832 | |
| 833 | unsigned Size = Args.size(); |
| 834 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 835 | D->addAttr(::new (S.Context) |
| 836 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 837 | Attr.getAttributeSpellingListIndex())); |
| 838 | } |
| 839 | |
| 840 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 841 | const AttributeList &Attr) { |
| 842 | SmallVector<Expr*, 1> Args; |
| 843 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 844 | return; |
| 845 | |
| 846 | unsigned Size = Args.size(); |
| 847 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 848 | D->addAttr(::new (S.Context) |
| 849 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 850 | StartArg, Size, |
| 851 | Attr.getAttributeSpellingListIndex())); |
| 852 | } |
| 853 | |
| 854 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 855 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 856 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 857 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 858 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 859 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 860 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 861 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 862 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 863 | << Attr.getName() << ExpectedFunctionOrMethod; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 864 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 867 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 868 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 869 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 870 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 874 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 875 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 876 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 879 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 880 | const AttributeList &Attr) { |
| 881 | SmallVector<Expr*, 2> Args; |
| 882 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 883 | return; |
| 884 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 885 | D->addAttr(::new (S.Context) |
| 886 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 887 | Attr.getArgAsExpr(0), |
| 888 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 889 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 892 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 893 | const AttributeList &Attr) { |
| 894 | SmallVector<Expr*, 2> Args; |
| 895 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 896 | return; |
| 897 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 898 | D->addAttr(::new (S.Context) |
| 899 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 900 | Attr.getArgAsExpr(0), |
| 901 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 902 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 905 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 906 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 907 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 908 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 909 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 910 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 911 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 912 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 913 | << Attr.getName() << ExpectedFunctionOrMethod; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 914 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 917 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 918 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 919 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 920 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 921 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 922 | return true; |
| 923 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 924 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 925 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 926 | const AttributeList &Attr) { |
| 927 | SmallVector<Expr*, 1> Args; |
| 928 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 929 | return; |
| 930 | |
| 931 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 932 | D->addAttr(::new (S.Context) |
| 933 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 934 | StartArg, Args.size(), |
| 935 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 936 | } |
| 937 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 938 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 939 | const AttributeList &Attr) { |
| 940 | SmallVector<Expr*, 1> Args; |
| 941 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 942 | return; |
| 943 | |
| 944 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 945 | D->addAttr(::new (S.Context) |
| 946 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 947 | StartArg, Args.size(), |
| 948 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 951 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 952 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 953 | // zero or more arguments ok |
| 954 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 955 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 956 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 957 | << Attr.getName() << ExpectedFunctionOrMethod; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 958 | return; |
| 959 | } |
| 960 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 961 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 962 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 963 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 964 | unsigned Size = Args.size(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 965 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 966 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 967 | D->addAttr(::new (S.Context) |
| 968 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 969 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 973 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 974 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 975 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 976 | << Attr.getName() << ExpectedFunctionOrMethod; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 977 | return; |
| 978 | } |
| 979 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 980 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 981 | SmallVector<Expr*, 1> Args; |
| 982 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 983 | unsigned Size = Args.size(); |
| 984 | if (Size == 0) |
| 985 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 986 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 987 | D->addAttr(::new (S.Context) |
| 988 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 989 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 993 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 994 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 995 | return; |
| 996 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 997 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 998 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 999 | << Attr.getName() << ExpectedFunctionOrMethod; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 1000 | return; |
| 1001 | } |
| 1002 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1003 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1004 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 1005 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1006 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 1007 | if (Size == 0) |
| 1008 | return; |
| 1009 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 1010 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1011 | D->addAttr(::new (S.Context) |
| 1012 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 1013 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1016 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1017 | ConsumableAttr::ConsumedState DefaultState; |
| 1018 | |
| 1019 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1020 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1021 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1022 | DefaultState)) { |
| 1023 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1024 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1025 | return; |
| 1026 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1027 | } else { |
| 1028 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1029 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 1030 | return; |
| 1031 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1032 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1033 | if (!isa<CXXRecordDecl>(D)) { |
| 1034 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1035 | Attr.getName() << ExpectedClass; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1036 | return; |
| 1037 | } |
| 1038 | |
| 1039 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1040 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1041 | Attr.getAttributeSpellingListIndex())); |
| 1042 | } |
| 1043 | |
| 1044 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 1045 | const AttributeList &Attr) { |
| 1046 | ASTContext &CurrContext = S.getASTContext(); |
| 1047 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 1048 | |
| 1049 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1050 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 1051 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 1052 | RD->getNameAsString(); |
| 1053 | |
| 1054 | return false; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | return true; |
| 1059 | } |
| 1060 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1061 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1062 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 1063 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1064 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1065 | return; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1066 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1067 | if (!isa<CXXMethodDecl>(D)) { |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1068 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1069 | Attr.getName() << ExpectedMethod; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1070 | return; |
| 1071 | } |
| 1072 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1073 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1074 | return; |
| 1075 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1076 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 1077 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 1078 | CallableWhenAttr::ConsumedState CallableState; |
| 1079 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1080 | StringRef StateString; |
| 1081 | SourceLocation Loc; |
| 1082 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 1083 | return; |
| 1084 | |
| 1085 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1086 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1087 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 1088 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1089 | return; |
| 1090 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1091 | |
| 1092 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1095 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1096 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 1097 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1100 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1101 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 1102 | const AttributeList &Attr) { |
| 1103 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 1104 | |
| 1105 | if (!isa<ParmVarDecl>(D)) { |
| 1106 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1107 | Attr.getName() << ExpectedParameter; |
| 1108 | return; |
| 1109 | } |
| 1110 | |
| 1111 | ParamTypestateAttr::ConsumedState ParamState; |
| 1112 | |
| 1113 | if (Attr.isArgIdent(0)) { |
| 1114 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1115 | StringRef StateString = Ident->Ident->getName(); |
| 1116 | |
| 1117 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 1118 | ParamState)) { |
| 1119 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1120 | << Attr.getName() << StateString; |
| 1121 | return; |
| 1122 | } |
| 1123 | } else { |
| 1124 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1125 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1126 | return; |
| 1127 | } |
| 1128 | |
| 1129 | // FIXME: This check is currently being done in the analysis. It can be |
| 1130 | // enabled here only after the parser propagates attributes at |
| 1131 | // template specialization definition, not declaration. |
| 1132 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 1133 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1134 | // |
| 1135 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1136 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1137 | // ReturnType.getAsString(); |
| 1138 | // return; |
| 1139 | //} |
| 1140 | |
| 1141 | D->addAttr(::new (S.Context) |
| 1142 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 1143 | Attr.getAttributeSpellingListIndex())); |
| 1144 | } |
| 1145 | |
| 1146 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1147 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 1148 | const AttributeList &Attr) { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1149 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 1150 | |
| 1151 | if (!(isa<FunctionDecl>(D) || isa<ParmVarDecl>(D))) { |
| 1152 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1153 | Attr.getName() << ExpectedFunctionMethodOrParameter; |
| 1154 | return; |
| 1155 | } |
| 1156 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1157 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 1158 | |
| 1159 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1160 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1161 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1162 | ReturnState)) { |
| 1163 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1164 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1165 | return; |
| 1166 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1167 | } else { |
| 1168 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1169 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1170 | return; |
| 1171 | } |
| 1172 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1173 | // FIXME: This check is currently being done in the analysis. It can be |
| 1174 | // enabled here only after the parser propagates attributes at |
| 1175 | // template specialization definition, not declaration. |
| 1176 | //QualType ReturnType; |
| 1177 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1178 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1179 | // ReturnType = Param->getType(); |
| 1180 | // |
| 1181 | //} else if (const CXXConstructorDecl *Constructor = |
| 1182 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1183 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1184 | // |
| 1185 | //} else { |
| 1186 | // |
| 1187 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1188 | //} |
| 1189 | // |
| 1190 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1191 | // |
| 1192 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1193 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1194 | // ReturnType.getAsString(); |
| 1195 | // return; |
| 1196 | //} |
| 1197 | |
| 1198 | D->addAttr(::new (S.Context) |
| 1199 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1200 | Attr.getAttributeSpellingListIndex())); |
| 1201 | } |
| 1202 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1203 | |
| 1204 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1205 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1206 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1207 | |
| 1208 | if (!isa<CXXMethodDecl>(D)) { |
| 1209 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1210 | Attr.getName() << ExpectedMethod; |
| 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1215 | return; |
| 1216 | |
| 1217 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1218 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1219 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1220 | StringRef Param = Ident->Ident->getName(); |
| 1221 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1222 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1223 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1224 | return; |
| 1225 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1226 | } else { |
| 1227 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1228 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1229 | return; |
| 1230 | } |
| 1231 | |
| 1232 | D->addAttr(::new (S.Context) |
| 1233 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1234 | Attr.getAttributeSpellingListIndex())); |
| 1235 | } |
| 1236 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1237 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1238 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1239 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1240 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1241 | |
| 1242 | if (!isa<CXXMethodDecl>(D)) { |
| 1243 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << |
| 1244 | Attr.getName() << ExpectedMethod; |
| 1245 | return; |
| 1246 | } |
| 1247 | |
| 1248 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1249 | return; |
| 1250 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1251 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1252 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1253 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1254 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1255 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1256 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1257 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1258 | return; |
| 1259 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1260 | } else { |
| 1261 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1262 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1263 | return; |
| 1264 | } |
| 1265 | |
| 1266 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1267 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1268 | Attr.getAttributeSpellingListIndex())); |
| 1269 | } |
| 1270 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1271 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1272 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1273 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 1274 | if (TD == 0) { |
| 1275 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 1276 | // and type-ids. |
Aaron Ballman | dbb634f | 2013-11-20 01:35:23 +0000 | [diff] [blame] | 1277 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) << |
| 1278 | Attr.getName() << ExpectedType; |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1279 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1280 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1281 | |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1282 | // Remember this typedef decl, we will need it later for diagnostics. |
| 1283 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1284 | } |
| 1285 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1286 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1287 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1288 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1289 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1290 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1291 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1292 | if (!FD->getType()->isDependentType() && |
| 1293 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1294 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1295 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1296 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1297 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1298 | FD->addAttr(::new (S.Context) |
| 1299 | PackedAttr(Attr.getRange(), S.Context, |
| 1300 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1301 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1302 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1305 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1306 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1307 | RD->addAttr(::new (S.Context) |
| 1308 | MsStructAttr(Attr.getRange(), S.Context, |
| 1309 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1310 | else |
Aaron Ballman | b80f94b | 2013-11-20 22:22:04 +0000 | [diff] [blame] | 1311 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1312 | << Attr.getName() << ExpectedStructOrUnionOrClass; |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1315 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1316 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1317 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1318 | if (MD->isInstanceMethod()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1319 | D->addAttr(::new (S.Context) |
| 1320 | IBActionAttr(Attr.getRange(), S.Context, |
| 1321 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1322 | return; |
| 1323 | } |
| 1324 | |
Ted Kremenek | d68ec81 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1325 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1328 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1329 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1330 | // variables or properties of Objective-C classes. The outlet must also |
| 1331 | // have an object reference type. |
| 1332 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1333 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1334 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1335 | << Attr.getName() << VD->getType() << 0; |
| 1336 | return false; |
| 1337 | } |
| 1338 | } |
| 1339 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1340 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1341 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1342 | << Attr.getName() << PD->getType() << 1; |
| 1343 | return false; |
| 1344 | } |
| 1345 | } |
| 1346 | else { |
| 1347 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1348 | return false; |
| 1349 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1350 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1351 | return true; |
| 1352 | } |
| 1353 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1354 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1355 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1356 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1357 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1358 | D->addAttr(::new (S.Context) |
| 1359 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1360 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1363 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1364 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1365 | |
| 1366 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1367 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1368 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1369 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1370 | return; |
| 1371 | } |
| 1372 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1373 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1374 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1375 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1376 | ParsedType PT; |
| 1377 | |
| 1378 | if (Attr.hasParsedType()) |
| 1379 | PT = Attr.getTypeArg(); |
| 1380 | else { |
| 1381 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1382 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1383 | if (!PT) { |
| 1384 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1385 | return; |
| 1386 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1387 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1388 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1389 | TypeSourceInfo *QTLoc = 0; |
| 1390 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1391 | if (!QTLoc) |
| 1392 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1393 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1394 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1395 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1396 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1397 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1398 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1399 | S.Diag(Attr.getLoc(), |
| 1400 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1401 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1402 | return; |
| 1403 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1404 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1405 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1406 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1407 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1408 | } |
| 1409 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1410 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1411 | if (const RecordType *UT = T->getAsUnionType()) |
| 1412 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1413 | RecordDecl *UD = UT->getDecl(); |
| 1414 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1415 | itend = UD->field_end(); it != itend; ++it) { |
| 1416 | QualType QT = it->getType(); |
| 1417 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1418 | T = QT; |
| 1419 | return; |
| 1420 | } |
| 1421 | } |
| 1422 | } |
| 1423 | } |
| 1424 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1425 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | e881ce2 | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1426 | if (!isFunctionOrMethod(D)) { |
| 1427 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1428 | << Attr.getName() << ExpectedFunctionOrMethod; |
Nuno Lopes | e881ce2 | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1429 | return; |
| 1430 | } |
| 1431 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1432 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1433 | return; |
| 1434 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1435 | SmallVector<unsigned, 8> SizeArgs; |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1436 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1437 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1438 | uint64_t Idx; |
| 1439 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1440 | Attr.getLoc(), i + 1, Ex, Idx)) |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1441 | return; |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1442 | |
| 1443 | // check if the function argument is of an integer type |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1444 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1445 | if (!T->isIntegerType()) { |
Aaron Ballman | 9d69509 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 1446 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1447 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 1448 | << Ex->getSourceRange(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1449 | return; |
| 1450 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1451 | SizeArgs.push_back(Idx); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | // check if the function returns a pointer |
| 1455 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1456 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1457 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1460 | D->addAttr(::new (S.Context) |
| 1461 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1462 | SizeArgs.data(), SizeArgs.size(), |
| 1463 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1466 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1467 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1468 | // ignore it as well |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1469 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1470 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1471 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1472 | return; |
| 1473 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1474 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1475 | SmallVector<unsigned, 8> NonNullArgs; |
| 1476 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1477 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1478 | uint64_t Idx; |
| 1479 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1480 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1481 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1482 | |
| 1483 | // Is the function argument a pointer type? |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1484 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1485 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1486 | |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1487 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1488 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | 62157e5 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1489 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1490 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1491 | continue; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1492 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1493 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1494 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1495 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1496 | |
| 1497 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1498 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1499 | if (NonNullArgs.empty()) { |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1500 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1501 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1502 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1503 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1504 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1505 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1506 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1507 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1508 | if (NonNullArgs.empty()) { |
| 1509 | // Warn the trivial case only if attribute is not coming from a |
| 1510 | // macro instantiation. |
| 1511 | if (Attr.getLoc().isFileID()) |
| 1512 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1513 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1514 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1515 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1516 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1517 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1518 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1519 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1520 | D->addAttr(::new (S.Context) |
| 1521 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1522 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1525 | static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) { |
| 1526 | switch (K) { |
| 1527 | case OwnershipAttr::Holds: return "'ownership_holds'"; |
| 1528 | case OwnershipAttr::Takes: return "'ownership_takes'"; |
| 1529 | case OwnershipAttr::Returns: return "'ownership_returns'"; |
| 1530 | } |
| 1531 | llvm_unreachable("unknown ownership"); |
| 1532 | } |
| 1533 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1534 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1535 | // This attribute must be applied to a function declaration. The first |
| 1536 | // argument to the attribute must be an identifier, the name of the resource, |
| 1537 | // for example: malloc. The following arguments must be argument indexes, the |
| 1538 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1539 | // 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] | 1540 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1541 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1542 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1543 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1544 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1545 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1546 | return; |
| 1547 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1548 | |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1549 | // Figure out our Kind, and check arguments while we're at it. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1550 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1551 | switch (AL.getKind()) { |
| 1552 | case AttributeList::AT_ownership_takes: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1553 | K = OwnershipAttr::Takes; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1554 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1555 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1556 | return; |
| 1557 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1558 | break; |
| 1559 | case AttributeList::AT_ownership_holds: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1560 | K = OwnershipAttr::Holds; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1561 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1562 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1563 | return; |
| 1564 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1565 | break; |
| 1566 | case AttributeList::AT_ownership_returns: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1567 | K = OwnershipAttr::Returns; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1568 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1569 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1570 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1571 | return; |
| 1572 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1573 | break; |
| 1574 | default: |
| 1575 | // This should never happen given how we are called. |
| 1576 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1579 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1580 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1581 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1582 | return; |
| 1583 | } |
| 1584 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1585 | StringRef Module = AL.getArgAsIdent(0)->Ident->getName(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1586 | |
| 1587 | // Normalize the argument, __foo__ becomes foo. |
| 1588 | if (Module.startswith("__") && Module.endswith("__")) |
| 1589 | Module = Module.substr(2, Module.size() - 4); |
| 1590 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1591 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1592 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1593 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1594 | uint64_t Idx; |
| 1595 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1596 | AL.getLoc(), i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1597 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1598 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1599 | // Is the function argument a pointer type? |
| 1600 | QualType T = getFunctionOrMethodArgType(D, Idx); |
| 1601 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1602 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1603 | case OwnershipAttr::Takes: |
| 1604 | case OwnershipAttr::Holds: |
| 1605 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1606 | Err = 0; |
| 1607 | break; |
| 1608 | case OwnershipAttr::Returns: |
| 1609 | if (!T->isIntegerType()) |
| 1610 | Err = 1; |
| 1611 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1612 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1613 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1614 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1615 | << Ex->getSourceRange(); |
| 1616 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1617 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1618 | |
| 1619 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1620 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1621 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1622 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
| 1623 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1624 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1625 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1626 | << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind()); |
| 1627 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1628 | } |
| 1629 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1630 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | unsigned* start = OwnershipArgs.data(); |
| 1634 | unsigned size = OwnershipArgs.size(); |
| 1635 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1636 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1637 | D->addAttr(::new (S.Context) |
| 1638 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1639 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1642 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1643 | // Check the attribute arguments. |
| 1644 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1645 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1646 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1647 | return; |
| 1648 | } |
| 1649 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1650 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1651 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1652 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1653 | return; |
| 1654 | } |
| 1655 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1656 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1657 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1658 | // gcc rejects |
| 1659 | // class c { |
| 1660 | // static int a __attribute__((weakref ("v2"))); |
| 1661 | // static int b() __attribute__((weakref ("f3"))); |
| 1662 | // }; |
| 1663 | // and ignores the attributes of |
| 1664 | // void f(void) { |
| 1665 | // static int a __attribute__((weakref ("v2"))); |
| 1666 | // } |
| 1667 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1668 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1669 | if (!Ctx->isFileContext()) { |
| 1670 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1671 | nd->getNameAsString(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1672 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | // The GCC manual says |
| 1676 | // |
| 1677 | // At present, a declaration to which `weakref' is attached can only |
| 1678 | // be `static'. |
| 1679 | // |
| 1680 | // It also says |
| 1681 | // |
| 1682 | // Without a TARGET, |
| 1683 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1684 | // equivalent to `weak'. |
| 1685 | // |
| 1686 | // gcc 4.4.1 will accept |
| 1687 | // int a7 __attribute__((weakref)); |
| 1688 | // as |
| 1689 | // int a7 __attribute__((weak)); |
| 1690 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1691 | // it if this behaviour is actually used. |
| 1692 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1693 | // GCC rejects |
| 1694 | // static ((alias ("y"), weakref)). |
| 1695 | // Should we? How to check that weakref is before or after alias? |
| 1696 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1697 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1698 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1699 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1700 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1701 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1702 | // GCC will accept anything as the argument of weakref. Should we |
| 1703 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1704 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1705 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1706 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1707 | D->addAttr(::new (S.Context) |
| 1708 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1709 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1712 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1713 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1714 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1715 | return; |
| 1716 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1717 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1718 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1719 | return; |
| 1720 | } |
| 1721 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1722 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1723 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1724 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1725 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1728 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1729 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1730 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1731 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1732 | return; |
| 1733 | } |
| 1734 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1735 | D->addAttr(::new (S.Context) |
| 1736 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1737 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1740 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1741 | if (!isa<FunctionDecl>(D)) { |
| 1742 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1743 | << Attr.getName() << ExpectedFunction; |
| 1744 | return; |
| 1745 | } |
| 1746 | |
| 1747 | if (D->hasAttr<HotAttr>()) { |
| 1748 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1749 | << Attr.getName() << "hot"; |
| 1750 | return; |
| 1751 | } |
| 1752 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1753 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1754 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1758 | if (!isa<FunctionDecl>(D)) { |
| 1759 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1760 | << Attr.getName() << ExpectedFunction; |
| 1761 | return; |
| 1762 | } |
| 1763 | |
| 1764 | if (D->hasAttr<ColdAttr>()) { |
| 1765 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1766 | << Attr.getName() << "cold"; |
| 1767 | return; |
| 1768 | } |
| 1769 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1770 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1771 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1774 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1775 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1776 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1777 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1778 | return; |
| 1779 | } |
| 1780 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1781 | D->addAttr(::new (S.Context) |
| 1782 | NakedAttr(Attr.getRange(), S.Context, |
| 1783 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1786 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1787 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1788 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1789 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1790 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1791 | return; |
| 1792 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1793 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1794 | D->addAttr(::new (S.Context) |
| 1795 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1796 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1799 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1800 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1801 | StringRef Model; |
| 1802 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1803 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1804 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1805 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1806 | |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 1807 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1808 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1809 | << Attr.getName() << ExpectedTLSVar; |
| 1810 | return; |
| 1811 | } |
| 1812 | |
| 1813 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1814 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1815 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1816 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1817 | return; |
| 1818 | } |
| 1819 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1820 | D->addAttr(::new (S.Context) |
| 1821 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1822 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1825 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1826 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1827 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1828 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1829 | D->addAttr(::new (S.Context) |
| 1830 | MallocAttr(Attr.getRange(), S.Context, |
| 1831 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1832 | return; |
| 1833 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1836 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1839 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1840 | D->addAttr(::new (S.Context) |
| 1841 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1842 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1845 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1846 | if (isa<VarDecl>(D)) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1847 | D->addAttr(::new (S.Context) |
| 1848 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1849 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1850 | else |
| 1851 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1852 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1855 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1856 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1857 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1858 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1859 | return; |
| 1860 | } |
| 1861 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1862 | if (isa<VarDecl>(D)) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1863 | D->addAttr(::new (S.Context) |
| 1864 | CommonAttr(Attr.getRange(), S.Context, |
| 1865 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1866 | else |
| 1867 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1868 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1871 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1872 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1873 | |
| 1874 | if (S.CheckNoReturnAttr(attr)) return; |
| 1875 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1876 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1877 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1878 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1879 | return; |
| 1880 | } |
| 1881 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1882 | D->addAttr(::new (S.Context) |
| 1883 | NoReturnAttr(attr.getRange(), S.Context, |
| 1884 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1888 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1889 | attr.setInvalid(); |
| 1890 | return true; |
| 1891 | } |
| 1892 | |
| 1893 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1896 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1897 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1898 | |
| 1899 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1900 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1901 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1902 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1903 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1904 | && !VD->getType()->isFunctionPointerType())) { |
| 1905 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1906 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1907 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1908 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1909 | return; |
| 1910 | } |
| 1911 | } |
| 1912 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1913 | D->addAttr(::new (S.Context) |
| 1914 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1915 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1918 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1919 | const AttributeList &Attr) { |
| 1920 | // C++11 [dcl.attr.noreturn]p1: |
| 1921 | // The attribute may be applied to the declarator-id in a function |
| 1922 | // declaration. |
| 1923 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1924 | if (!FD) { |
| 1925 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1926 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1927 | return; |
| 1928 | } |
| 1929 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1930 | D->addAttr(::new (S.Context) |
| 1931 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1932 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1935 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1936 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1937 | /* |
| 1938 | Returning a Vector Class in Registers |
| 1939 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1940 | According to the PPU ABI specifications, a class with a single member of |
| 1941 | vector type is returned in memory when used as the return value of a function. |
| 1942 | This results in inefficient code when implementing vector classes. To return |
| 1943 | the value in a single vector register, add the vecreturn attribute to the |
| 1944 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1945 | |
| 1946 | Example: |
| 1947 | |
| 1948 | struct Vector |
| 1949 | { |
| 1950 | __vector float xyzw; |
| 1951 | } __attribute__((vecreturn)); |
| 1952 | |
| 1953 | Vector Add(Vector lhs, Vector rhs) |
| 1954 | { |
| 1955 | Vector result; |
| 1956 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1957 | return result; // This will be returned in a register |
| 1958 | } |
| 1959 | */ |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1960 | if (!isa<RecordDecl>(D)) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1961 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1962 | << Attr.getName() << ExpectedClass; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1963 | return; |
| 1964 | } |
| 1965 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1966 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1967 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1968 | return; |
| 1969 | } |
| 1970 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1971 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1972 | int count = 0; |
| 1973 | |
| 1974 | if (!isa<CXXRecordDecl>(record)) { |
| 1975 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1976 | return; |
| 1977 | } |
| 1978 | |
| 1979 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1980 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1981 | return; |
| 1982 | } |
| 1983 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1984 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1985 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1986 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1987 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1988 | return; |
| 1989 | } |
| 1990 | count++; |
| 1991 | } |
| 1992 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1993 | D->addAttr(::new (S.Context) |
| 1994 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1995 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1998 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1999 | const AttributeList &Attr) { |
| 2000 | if (isa<ParmVarDecl>(D)) { |
| 2001 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 2002 | // parameter of a function declaration or lambda. |
| 2003 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 2004 | S.Diag(Attr.getLoc(), |
| 2005 | diag::err_carries_dependency_param_not_function_decl); |
| 2006 | return; |
| 2007 | } |
| 2008 | } else if (!isa<FunctionDecl>(D)) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2009 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2010 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2011 | return; |
| 2012 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2013 | |
| 2014 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 2015 | Attr.getRange(), S.Context, |
| 2016 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2019 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2020 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 429c134 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 2021 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2022 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2023 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 2024 | return; |
| 2025 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2026 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2027 | D->addAttr(::new (S.Context) |
| 2028 | UnusedAttr(Attr.getRange(), S.Context, |
| 2029 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 2032 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 2033 | const AttributeList &Attr) { |
Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 2034 | if (!isa<FunctionDecl>(D)) { |
| 2035 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2036 | << Attr.getName() << ExpectedFunction; |
| 2037 | return; |
| 2038 | } |
| 2039 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2040 | D->addAttr(::new (S.Context) |
| 2041 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 2042 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 2043 | } |
| 2044 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2045 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2046 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 2047 | if (VD->hasLocalStorage()) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2048 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 2049 | return; |
| 2050 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2051 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2052 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2053 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2054 | return; |
| 2055 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2056 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2057 | D->addAttr(::new (S.Context) |
| 2058 | UsedAttr(Attr.getRange(), S.Context, |
| 2059 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2062 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2063 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2064 | if (Attr.getNumArgs() > 1) { |
| 2065 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2066 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2067 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2068 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2069 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 2070 | if (Attr.getNumArgs() > 0 && |
| 2071 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 2072 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2073 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2074 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2075 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2076 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2077 | return; |
| 2078 | } |
| 2079 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2080 | D->addAttr(::new (S.Context) |
| 2081 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 2082 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2085 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2086 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2087 | if (Attr.getNumArgs() > 1) { |
| 2088 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2089 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2090 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2091 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2092 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 2093 | if (Attr.getNumArgs() > 0 && |
| 2094 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 2095 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2096 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2097 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2098 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2099 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2100 | return; |
| 2101 | } |
| 2102 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2103 | D->addAttr(::new (S.Context) |
| 2104 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2105 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2106 | } |
| 2107 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2108 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2109 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2110 | const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2111 | unsigned NumArgs = Attr.getNumArgs(); |
| 2112 | if (NumArgs > 1) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2113 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2114 | return; |
| 2115 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2116 | |
| 2117 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2118 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2119 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2120 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2121 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2122 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2123 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2126 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 2127 | const AttributeList &Attr) { |
Aaron Ballman | f361453 | 2013-11-24 20:36:50 +0000 | [diff] [blame] | 2128 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2129 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2130 | << Attr.getName() << ExpectedObjectiveCInterface; |
| 2131 | return; |
| 2132 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2133 | D->addAttr(::new (S.Context) |
| 2134 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 2135 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2138 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 2139 | const AttributeList &Attr) { |
| 2140 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | f90ccb0 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2141 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2142 | << Attr.getName() << ExpectedObjectiveCInterface; |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2143 | return; |
| 2144 | } |
| 2145 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2146 | D->addAttr(::new (S.Context) |
| 2147 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2148 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2151 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
| 2152 | const AttributeList &Attr) { |
| 2153 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2154 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2155 | << Attr.getName() << ExpectedObjectiveCInterface; |
| 2156 | return; |
| 2157 | } |
| 2158 | |
Ted Kremenek | 7559b47 | 2013-11-23 22:29:11 +0000 | [diff] [blame] | 2159 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2160 | |
| 2161 | if (!Parm) { |
| 2162 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1; |
| 2163 | return; |
| 2164 | } |
| 2165 | |
| 2166 | D->addAttr(::new (S.Context) |
| 2167 | ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 2168 | Attr.getAttributeSpellingListIndex())); |
| 2169 | } |
| 2170 | |
| 2171 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2172 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2173 | const AttributeList &Attr) { |
Fariborz Jahanian | 7249e36 | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2174 | if (!isa<ObjCInterfaceDecl>(D)) { |
Aaron Ballman | f361453 | 2013-11-24 20:36:50 +0000 | [diff] [blame] | 2175 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2176 | << Attr.getName() << ExpectedObjectiveCInterface; |
Fariborz Jahanian | 7249e36 | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2177 | return; |
| 2178 | } |
| 2179 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2180 | D->addAttr(::new (S.Context) |
| 2181 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2182 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 9d4d20a | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2183 | } |
| 2184 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2185 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2186 | IdentifierInfo *Platform, |
| 2187 | VersionTuple Introduced, |
| 2188 | VersionTuple Deprecated, |
| 2189 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2190 | StringRef PlatformName |
| 2191 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2192 | if (PlatformName.empty()) |
| 2193 | PlatformName = Platform->getName(); |
| 2194 | |
| 2195 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2196 | // of these steps are needed). |
| 2197 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2198 | !(Introduced <= Deprecated)) { |
| 2199 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2200 | << 1 << PlatformName << Deprecated.getAsString() |
| 2201 | << 0 << Introduced.getAsString(); |
| 2202 | return true; |
| 2203 | } |
| 2204 | |
| 2205 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2206 | !(Introduced <= Obsoleted)) { |
| 2207 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2208 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2209 | << 0 << Introduced.getAsString(); |
| 2210 | return true; |
| 2211 | } |
| 2212 | |
| 2213 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2214 | !(Deprecated <= Obsoleted)) { |
| 2215 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2216 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2217 | << 1 << Deprecated.getAsString(); |
| 2218 | return true; |
| 2219 | } |
| 2220 | |
| 2221 | return false; |
| 2222 | } |
| 2223 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2224 | /// \brief Check whether the two versions match. |
| 2225 | /// |
| 2226 | /// If either version tuple is empty, then they are assumed to match. If |
| 2227 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2228 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2229 | bool BeforeIsOkay) { |
| 2230 | if (X.empty() || Y.empty()) |
| 2231 | return true; |
| 2232 | |
| 2233 | if (X == Y) |
| 2234 | return true; |
| 2235 | |
| 2236 | if (BeforeIsOkay && X < Y) |
| 2237 | return true; |
| 2238 | |
| 2239 | return false; |
| 2240 | } |
| 2241 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2242 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2243 | IdentifierInfo *Platform, |
| 2244 | VersionTuple Introduced, |
| 2245 | VersionTuple Deprecated, |
| 2246 | VersionTuple Obsoleted, |
| 2247 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2248 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2249 | bool Override, |
| 2250 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2251 | VersionTuple MergedIntroduced = Introduced; |
| 2252 | VersionTuple MergedDeprecated = Deprecated; |
| 2253 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2254 | bool FoundAny = false; |
| 2255 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2256 | if (D->hasAttrs()) { |
| 2257 | AttrVec &Attrs = D->getAttrs(); |
| 2258 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2259 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2260 | if (!OldAA) { |
| 2261 | ++i; |
| 2262 | continue; |
| 2263 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2264 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2265 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2266 | if (OldPlatform != Platform) { |
| 2267 | ++i; |
| 2268 | continue; |
| 2269 | } |
| 2270 | |
| 2271 | FoundAny = true; |
| 2272 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2273 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2274 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2275 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2276 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2277 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2278 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2279 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2280 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2281 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2282 | if (Override) { |
| 2283 | int Which = -1; |
| 2284 | VersionTuple FirstVersion; |
| 2285 | VersionTuple SecondVersion; |
| 2286 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2287 | Which = 0; |
| 2288 | FirstVersion = OldIntroduced; |
| 2289 | SecondVersion = Introduced; |
| 2290 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2291 | Which = 1; |
| 2292 | FirstVersion = Deprecated; |
| 2293 | SecondVersion = OldDeprecated; |
| 2294 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2295 | Which = 2; |
| 2296 | FirstVersion = Obsoleted; |
| 2297 | SecondVersion = OldObsoleted; |
| 2298 | } |
| 2299 | |
| 2300 | if (Which == -1) { |
| 2301 | Diag(OldAA->getLocation(), |
| 2302 | diag::warn_mismatched_availability_override_unavail) |
| 2303 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2304 | } else { |
| 2305 | Diag(OldAA->getLocation(), |
| 2306 | diag::warn_mismatched_availability_override) |
| 2307 | << Which |
| 2308 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2309 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2310 | } |
| 2311 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2312 | } else { |
| 2313 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2314 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2315 | } |
| 2316 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2317 | Attrs.erase(Attrs.begin() + i); |
| 2318 | --e; |
| 2319 | continue; |
| 2320 | } |
| 2321 | |
| 2322 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2323 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2324 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2325 | |
| 2326 | if (MergedIntroduced2.empty()) |
| 2327 | MergedIntroduced2 = OldIntroduced; |
| 2328 | if (MergedDeprecated2.empty()) |
| 2329 | MergedDeprecated2 = OldDeprecated; |
| 2330 | if (MergedObsoleted2.empty()) |
| 2331 | MergedObsoleted2 = OldObsoleted; |
| 2332 | |
| 2333 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2334 | MergedIntroduced2, MergedDeprecated2, |
| 2335 | MergedObsoleted2)) { |
| 2336 | Attrs.erase(Attrs.begin() + i); |
| 2337 | --e; |
| 2338 | continue; |
| 2339 | } |
| 2340 | |
| 2341 | MergedIntroduced = MergedIntroduced2; |
| 2342 | MergedDeprecated = MergedDeprecated2; |
| 2343 | MergedObsoleted = MergedObsoleted2; |
| 2344 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2345 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | if (FoundAny && |
| 2349 | MergedIntroduced == Introduced && |
| 2350 | MergedDeprecated == Deprecated && |
| 2351 | MergedObsoleted == Obsoleted) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2352 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2353 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2354 | // Only create a new attribute if !Override, but we want to do |
| 2355 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2356 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2357 | MergedDeprecated, MergedObsoleted) && |
| 2358 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2359 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2360 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2361 | Obsoleted, IsUnavailable, Message, |
| 2362 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2363 | } |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2364 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2367 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2368 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2369 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2370 | return; |
| 2371 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2372 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2373 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2374 | IdentifierInfo *II = Platform->Ident; |
| 2375 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2376 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2377 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2378 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2379 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2380 | if (!ND) { |
| 2381 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2382 | return; |
| 2383 | } |
| 2384 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2385 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2386 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2387 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2388 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2389 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 2390 | if (const StringLiteral *SE = |
| 2391 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2392 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2393 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2394 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2395 | Introduced.Version, |
| 2396 | Deprecated.Version, |
| 2397 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2398 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2399 | /*Override=*/false, |
| 2400 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2401 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2402 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2405 | template <class T> |
| 2406 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2407 | typename T::VisibilityType value, |
| 2408 | unsigned attrSpellingListIndex) { |
| 2409 | T *existingAttr = D->getAttr<T>(); |
| 2410 | if (existingAttr) { |
| 2411 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2412 | if (existingValue == value) |
| 2413 | return NULL; |
| 2414 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2415 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2416 | D->dropAttr<T>(); |
| 2417 | } |
| 2418 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2419 | } |
| 2420 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2421 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2422 | VisibilityAttr::VisibilityType Vis, |
| 2423 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2424 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2425 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2428 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2429 | TypeVisibilityAttr::VisibilityType Vis, |
| 2430 | unsigned AttrSpellingListIndex) { |
| 2431 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2432 | AttrSpellingListIndex); |
| 2433 | } |
| 2434 | |
| 2435 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2436 | bool isTypeVisibility) { |
| 2437 | // Visibility attributes don't mean anything on a typedef. |
| 2438 | if (isa<TypedefNameDecl>(D)) { |
| 2439 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2440 | << Attr.getName(); |
| 2441 | return; |
| 2442 | } |
| 2443 | |
| 2444 | // 'type_visibility' can only go on a type or namespace. |
| 2445 | if (isTypeVisibility && |
| 2446 | !(isa<TagDecl>(D) || |
| 2447 | isa<ObjCInterfaceDecl>(D) || |
| 2448 | isa<NamespaceDecl>(D))) { |
| 2449 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2450 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2451 | return; |
| 2452 | } |
| 2453 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2454 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2455 | StringRef TypeStr; |
| 2456 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2457 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2458 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2459 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2460 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2461 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2462 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2463 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2464 | return; |
| 2465 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2466 | |
| 2467 | // Complain about attempts to use protected visibility on targets |
| 2468 | // (like Darwin) that don't support it. |
| 2469 | if (type == VisibilityAttr::Protected && |
| 2470 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2471 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2472 | type = VisibilityAttr::Default; |
| 2473 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2474 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2475 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2476 | clang::Attr *newAttr; |
| 2477 | if (isTypeVisibility) { |
| 2478 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2479 | (TypeVisibilityAttr::VisibilityType) type, |
| 2480 | Index); |
| 2481 | } else { |
| 2482 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2483 | } |
| 2484 | if (newAttr) |
| 2485 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2488 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2489 | const AttributeList &Attr) { |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2490 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2491 | if (!method) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2492 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2493 | << ExpectedMethod; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2494 | return; |
| 2495 | } |
| 2496 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2497 | if (!Attr.isArgIdent(0)) { |
| 2498 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2499 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2500 | return; |
| 2501 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2502 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2503 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2504 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2505 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2506 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2507 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2508 | return; |
| 2509 | } |
| 2510 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2511 | if (F == ObjCMethodFamilyAttr::OMF_init && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2512 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2513 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2514 | << method->getResultType(); |
| 2515 | // Ignore the attribute. |
| 2516 | return; |
| 2517 | } |
| 2518 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2519 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2520 | S.Context, F)); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2523 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2524 | const AttributeList &Attr) { |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2525 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2526 | if (OCI == 0) { |
Aaron Ballman | f90ccb0 | 2013-07-18 14:56:42 +0000 | [diff] [blame] | 2527 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 2528 | << Attr.getName() << ExpectedObjectiveCInterface; |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2529 | return; |
| 2530 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2531 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2532 | D->addAttr(::new (S.Context) |
| 2533 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2534 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2537 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2538 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2539 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2540 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2541 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2542 | return; |
| 2543 | } |
| 2544 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2545 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2546 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2547 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2548 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2549 | return; |
| 2550 | } |
| 2551 | } |
| 2552 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2553 | // It is okay to include this attribute on properties, e.g.: |
| 2554 | // |
| 2555 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2556 | // |
| 2557 | // In this case it follows tradition and suppresses an error in the above |
| 2558 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2559 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2560 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2561 | D->addAttr(::new (S.Context) |
| 2562 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2563 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2564 | } |
| 2565 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2566 | static void |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2567 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2568 | if (!isa<FunctionDecl>(D)) { |
| 2569 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2570 | return; |
| 2571 | } |
| 2572 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2573 | D->addAttr(::new (S.Context) |
| 2574 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2575 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2576 | } |
| 2577 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2578 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2579 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2580 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2581 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2582 | return; |
| 2583 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2584 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2585 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2586 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2587 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2588 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2589 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2590 | return; |
| 2591 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2592 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2593 | D->addAttr(::new (S.Context) |
| 2594 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2595 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2596 | } |
| 2597 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2598 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2599 | // check the attribute arguments. |
| 2600 | if (Attr.getNumArgs() > 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2601 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2602 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2605 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2606 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2607 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2608 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2609 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2610 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2611 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2612 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2613 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2614 | return; |
| 2615 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2616 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2617 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2618 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2619 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2620 | return; |
| 2621 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2622 | |
| 2623 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2626 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2627 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2628 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2629 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2630 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2631 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2632 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2633 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2634 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2635 | return; |
| 2636 | } |
| 2637 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2638 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2639 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2640 | // FIXME: This error message could be improved, it would be nice |
| 2641 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2642 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2643 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2644 | return; |
| 2645 | } |
| 2646 | } |
| 2647 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2648 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2649 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2650 | if (isa<FunctionNoProtoType>(FT)) { |
| 2651 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2652 | return; |
| 2653 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2654 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2655 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2656 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2657 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2658 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2659 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2660 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2661 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2662 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2663 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2664 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2665 | if (!BD->isVariadic()) { |
| 2666 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2667 | return; |
| 2668 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2669 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2670 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2671 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2672 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2673 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2674 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2675 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2676 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2677 | return; |
| 2678 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2679 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2680 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2681 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2682 | return; |
| 2683 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2684 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2685 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2686 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2687 | return; |
| 2688 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2689 | D->addAttr(::new (S.Context) |
| 2690 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2691 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2694 | static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2695 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 2696 | RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context)); |
| 2697 | else |
| 2698 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2699 | } |
| 2700 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2701 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2702 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2703 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | 3d699e0 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2704 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2705 | return; |
| 2706 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2707 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2708 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2709 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2710 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2711 | return; |
| 2712 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2713 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2714 | if (MD->getResultType()->isVoidType()) { |
| 2715 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2716 | << Attr.getName() << 1; |
| 2717 | return; |
| 2718 | } |
| 2719 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2720 | D->addAttr(::new (S.Context) |
| 2721 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2722 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2725 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2726 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 47f9a73 | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2727 | if (isa<CXXRecordDecl>(D)) { |
| 2728 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2729 | return; |
| 2730 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2731 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2732 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | 41136ee | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2733 | return; |
| 2734 | } |
| 2735 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2736 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2737 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2738 | nd->addAttr(::new (S.Context) |
| 2739 | WeakAttr(Attr.getRange(), S.Context, |
| 2740 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2743 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2744 | // weak_import only applies to variable & function declarations. |
| 2745 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2746 | if (!D->canBeWeakImported(isDef)) { |
| 2747 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2748 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2749 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2750 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2751 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2752 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2753 | // Nothing to warn about here. |
| 2754 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2755 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2756 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2757 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2758 | return; |
| 2759 | } |
| 2760 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2761 | D->addAttr(::new (S.Context) |
| 2762 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2763 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2764 | } |
| 2765 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2766 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2767 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2768 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2769 | uint32_t WGSize[3]; |
| 2770 | for (unsigned i = 0; i < 3; ++i) |
| 2771 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2772 | return; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2773 | |
| 2774 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2775 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2776 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2777 | if (!(A->getXDim() == WGSize[0] && |
| 2778 | A->getYDim() == WGSize[1] && |
| 2779 | A->getZDim() == WGSize[2])) { |
| 2780 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2781 | Attr.getName(); |
| 2782 | } |
| 2783 | } |
| 2784 | |
| 2785 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2786 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2787 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2788 | if (!(A->getXDim() == WGSize[0] && |
| 2789 | A->getYDim() == WGSize[1] && |
| 2790 | A->getZDim() == WGSize[2])) { |
| 2791 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2792 | Attr.getName(); |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2797 | D->addAttr(::new (S.Context) |
| 2798 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2799 | WGSize[0], WGSize[1], WGSize[2], |
| 2800 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2801 | else |
| 2802 | D->addAttr(::new (S.Context) |
| 2803 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2804 | WGSize[0], WGSize[1], WGSize[2], |
| 2805 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2808 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2809 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2810 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2811 | if (!Attr.hasParsedType()) { |
| 2812 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2813 | << Attr.getName() << 1; |
| 2814 | return; |
| 2815 | } |
| 2816 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2817 | TypeSourceInfo *ParmTSI = 0; |
| 2818 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2819 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2820 | |
| 2821 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2822 | (ParmType->isBooleanType() || |
| 2823 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2824 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2825 | << ParmType; |
| 2826 | return; |
| 2827 | } |
| 2828 | |
| 2829 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2830 | D->hasAttr<VecTypeHintAttr>()) { |
| 2831 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2832 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2833 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2834 | return; |
| 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2839 | ParmTSI)); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2840 | } |
| 2841 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2842 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2843 | StringRef Name, |
| 2844 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2845 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2846 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2847 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2848 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2849 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2850 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2851 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2852 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2853 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2854 | } |
| 2855 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2856 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2857 | // Make sure that there is a string literal as the sections's single |
| 2858 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2859 | StringRef Str; |
| 2860 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2861 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2862 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2863 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2864 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2865 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2866 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2867 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2868 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2869 | return; |
| 2870 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2871 | |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2872 | // This attribute cannot be applied to local variables. |
| 2873 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2874 | S.Diag(LiteralLoc, diag::err_attribute_section_local_variable); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2875 | return; |
| 2876 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2877 | |
| 2878 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2879 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2880 | if (NewAttr) |
| 2881 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2884 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2885 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2886 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2887 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2888 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2889 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2890 | D->addAttr(::new (S.Context) |
| 2891 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2892 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2893 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2896 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2897 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2898 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2899 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2900 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2901 | D->addAttr(::new (S.Context) |
| 2902 | ConstAttr(Attr.getRange(), S.Context, |
| 2903 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2904 | } |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2907 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2908 | D->addAttr(::new (S.Context) |
| 2909 | PureAttr(Attr.getRange(), S.Context, |
| 2910 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2911 | } |
| 2912 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2913 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2914 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2915 | if (!VD || !VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2916 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2917 | return; |
| 2918 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2919 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2920 | Expr *E = Attr.getArgAsExpr(0); |
| 2921 | SourceLocation Loc = E->getExprLoc(); |
| 2922 | FunctionDecl *FD = 0; |
| 2923 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2924 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2925 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2926 | // will warn the user. |
| 2927 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2928 | if (DRE->hasQualifier()) |
| 2929 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2930 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2931 | NI = DRE->getNameInfo(); |
| 2932 | if (!FD) { |
| 2933 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2934 | << NI.getName(); |
| 2935 | return; |
| 2936 | } |
| 2937 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2938 | if (ULE->hasExplicitTemplateArgs()) |
| 2939 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2940 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2941 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2942 | if (!FD) { |
| 2943 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2944 | << NI.getName(); |
| 2945 | if (ULE->getType() == S.Context.OverloadTy) |
| 2946 | S.NoteAllOverloadCandidates(ULE); |
| 2947 | return; |
| 2948 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2949 | } else { |
| 2950 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2951 | return; |
| 2952 | } |
| 2953 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2954 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2955 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2956 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2957 | return; |
| 2958 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2959 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2960 | // We're currently more strict than GCC about what function types we accept. |
| 2961 | // If this ever proves to be a problem it should be easy to fix. |
| 2962 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2963 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2964 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2965 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2966 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2967 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2968 | return; |
| 2969 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2970 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2971 | D->addAttr(::new (S.Context) |
| 2972 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2973 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2976 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2977 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2978 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2979 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2980 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2981 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2982 | return; |
| 2983 | } |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2984 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2985 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2986 | uint64_t ArgIdx; |
| 2987 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 2988 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2989 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2990 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2991 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2992 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2993 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2994 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2995 | if (not_nsstring_type && |
| 2996 | !isCFStringType(Ty, S.Context) && |
| 2997 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2998 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2999 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 3000 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3001 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3002 | << IdxExpr->getSourceRange(); |
| 3003 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3004 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3005 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3006 | if (!isNSStringType(Ty, S.Context) && |
| 3007 | !isCFStringType(Ty, S.Context) && |
| 3008 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3009 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3010 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 3011 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3012 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3013 | << IdxExpr->getSourceRange(); |
| 3014 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 3017 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 3018 | // because that has corrected for the implicit this parameter, and is zero- |
| 3019 | // based. The attribute expects what the user wrote explicitly. |
| 3020 | llvm::APSInt Val; |
| 3021 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 3022 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3023 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 3024 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3025 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3026 | } |
| 3027 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3028 | enum FormatAttrKind { |
| 3029 | CFStringFormat, |
| 3030 | NSStringFormat, |
| 3031 | StrftimeFormat, |
| 3032 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3033 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3034 | InvalidFormat |
| 3035 | }; |
| 3036 | |
| 3037 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3038 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3039 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3040 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 3041 | // Check for formats that get handled specially. |
| 3042 | .Case("NSString", NSStringFormat) |
| 3043 | .Case("CFString", CFStringFormat) |
| 3044 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3045 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3046 | // Otherwise, check for supported formats. |
| 3047 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3048 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3049 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3050 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3051 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3052 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3055 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3056 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3057 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3058 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3059 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3060 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3061 | return; |
| 3062 | } |
| 3063 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3064 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3065 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3066 | Attr.setInvalid(); |
| 3067 | return; |
| 3068 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3069 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3070 | if (S.Context.getAsArrayType(T)) |
| 3071 | T = S.Context.getBaseElementType(T); |
| 3072 | if (!T->getAs<RecordType>()) { |
| 3073 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3074 | Attr.setInvalid(); |
| 3075 | return; |
| 3076 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3077 | |
| 3078 | Expr *E = Attr.getArgAsExpr(0); |
| 3079 | uint32_t prioritynum; |
| 3080 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3081 | Attr.setInvalid(); |
| 3082 | return; |
| 3083 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3084 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3085 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3086 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3087 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3088 | Attr.setInvalid(); |
| 3089 | return; |
| 3090 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3091 | D->addAttr(::new (S.Context) |
| 3092 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3093 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3094 | } |
| 3095 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3096 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3097 | IdentifierInfo *Format, int FormatIdx, |
| 3098 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3099 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3100 | // Check whether we already have an equivalent format attribute. |
| 3101 | for (specific_attr_iterator<FormatAttr> |
| 3102 | i = D->specific_attr_begin<FormatAttr>(), |
| 3103 | e = D->specific_attr_end<FormatAttr>(); |
| 3104 | i != e ; ++i) { |
| 3105 | FormatAttr *f = *i; |
| 3106 | if (f->getType() == Format && |
| 3107 | f->getFormatIdx() == FormatIdx && |
| 3108 | f->getFirstArg() == FirstArg) { |
| 3109 | // If we don't have a valid location for this attribute, adopt the |
| 3110 | // location. |
| 3111 | if (f->getLocation().isInvalid()) |
| 3112 | f->setRange(Range); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3113 | return NULL; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3114 | } |
| 3115 | } |
| 3116 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3117 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3118 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3121 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3122 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3123 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3124 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3125 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3126 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3127 | return; |
| 3128 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3129 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3130 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3131 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3132 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3133 | return; |
| 3134 | } |
| 3135 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3136 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3137 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3138 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3139 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3140 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3141 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 3142 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3143 | |
| 3144 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3145 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3146 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3147 | // If we've modified the string name, we need a new identifier for it. |
| 3148 | II = &S.Context.Idents.get(Format); |
| 3149 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3150 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3151 | // Check for supported formats. |
| 3152 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3153 | |
| 3154 | if (Kind == IgnoredFormat) |
| 3155 | return; |
| 3156 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3157 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3158 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3159 | << "format" << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3160 | return; |
| 3161 | } |
| 3162 | |
| 3163 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3164 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3165 | uint32_t Idx; |
| 3166 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3167 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3168 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3169 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3170 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3171 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3172 | return; |
| 3173 | } |
| 3174 | |
| 3175 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3176 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3177 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3178 | if (HasImplicitThisParam) { |
| 3179 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3180 | S.Diag(Attr.getLoc(), |
| 3181 | diag::err_format_attribute_implicit_this_format_string) |
| 3182 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3183 | return; |
| 3184 | } |
| 3185 | ArgIdx--; |
| 3186 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3187 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3188 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3189 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3190 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3191 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3192 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3193 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3194 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3195 | return; |
| 3196 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3197 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3198 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3199 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3200 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3201 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3202 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3203 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3204 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3205 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3206 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3207 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3208 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3209 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3210 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3211 | return; |
| 3212 | } |
| 3213 | |
| 3214 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3215 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3216 | uint32_t FirstArg; |
| 3217 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3218 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3219 | |
| 3220 | // check if the function is variadic if the 3rd argument non-zero |
| 3221 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3222 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3223 | ++NumArgs; // +1 for ... |
| 3224 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3225 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3226 | return; |
| 3227 | } |
| 3228 | } |
| 3229 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3230 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3231 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3232 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3233 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3234 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3235 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3236 | return; |
| 3237 | } |
| 3238 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3239 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3240 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3241 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3242 | return; |
| 3243 | } |
| 3244 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3245 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3246 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3247 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3248 | if (NewAttr) |
| 3249 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3252 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3253 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3254 | // Try to find the underlying union declaration. |
| 3255 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3256 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3257 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3258 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3259 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3260 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3261 | |
| 3262 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3263 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3264 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3265 | return; |
| 3266 | } |
| 3267 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3268 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3269 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3270 | diag::warn_transparent_union_attribute_not_definition); |
| 3271 | return; |
| 3272 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3273 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3274 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3275 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3276 | if (Field == FieldEnd) { |
| 3277 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3278 | return; |
| 3279 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3280 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3281 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3282 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3283 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3284 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3285 | diag::warn_transparent_union_attribute_floating) |
| 3286 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3287 | return; |
| 3288 | } |
| 3289 | |
| 3290 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3291 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3292 | for (; Field != FieldEnd; ++Field) { |
| 3293 | QualType FieldType = Field->getType(); |
| 3294 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3295 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3296 | // Warn if we drop the attribute. |
| 3297 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3298 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3299 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3300 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3301 | diag::warn_transparent_union_attribute_field_size_align) |
| 3302 | << isSize << Field->getDeclName() << FieldBits; |
| 3303 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3304 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3305 | diag::note_transparent_union_first_field_size_align) |
| 3306 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3307 | return; |
| 3308 | } |
| 3309 | } |
| 3310 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3311 | RD->addAttr(::new (S.Context) |
| 3312 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3313 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3314 | } |
| 3315 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3316 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3317 | // Make sure that there is a string literal as the annotation's single |
| 3318 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3319 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3320 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3321 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3322 | |
| 3323 | // Don't duplicate annotations that are already set. |
| 3324 | for (specific_attr_iterator<AnnotateAttr> |
| 3325 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3326 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3327 | if ((*i)->getAnnotation() == Str) |
| 3328 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3329 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3330 | |
| 3331 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3332 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3333 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3336 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3337 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3338 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3339 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3340 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3341 | return; |
| 3342 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3343 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3344 | if (Attr.getNumArgs() == 0) { |
| 3345 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3346 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3347 | return; |
| 3348 | } |
| 3349 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3350 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3351 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3352 | S.Diag(Attr.getEllipsisLoc(), |
| 3353 | diag::err_pack_expansion_without_parameter_packs); |
| 3354 | return; |
| 3355 | } |
| 3356 | |
| 3357 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3358 | return; |
| 3359 | |
| 3360 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3361 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3365 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3366 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3367 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3368 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3369 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3370 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3371 | // C++11 [dcl.align]p1: |
| 3372 | // An alignment-specifier may be applied to a variable or to a class |
| 3373 | // data member, but it shall not be applied to a bit-field, a function |
| 3374 | // parameter, the formal parameter of a catch clause, or a variable |
| 3375 | // declared with the register storage class specifier. An |
| 3376 | // alignment-specifier may also be applied to the declaration of a class |
| 3377 | // or enumeration type. |
| 3378 | // C11 6.7.5/2: |
| 3379 | // An alignment attribute shall not be specified in a declaration of |
| 3380 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3381 | // object declared with the register storage-class specifier. |
| 3382 | int DiagKind = -1; |
| 3383 | if (isa<ParmVarDecl>(D)) { |
| 3384 | DiagKind = 0; |
| 3385 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3386 | if (VD->getStorageClass() == SC_Register) |
| 3387 | DiagKind = 1; |
| 3388 | if (VD->isExceptionVariable()) |
| 3389 | DiagKind = 2; |
| 3390 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3391 | if (FD->isBitField()) |
| 3392 | DiagKind = 3; |
| 3393 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3394 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3395 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3396 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3397 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3398 | return; |
| 3399 | } |
| 3400 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3401 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3402 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3403 | return; |
| 3404 | } |
| 3405 | } |
| 3406 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3407 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3408 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3409 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3410 | AA->setPackExpansion(IsPackExpansion); |
| 3411 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3412 | return; |
| 3413 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3414 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3415 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3416 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3417 | ExprResult ICE |
| 3418 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3419 | diag::err_aligned_attribute_argument_not_int, |
| 3420 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3421 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3422 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3423 | |
| 3424 | // C++11 [dcl.align]p2: |
| 3425 | // -- if the constant expression evaluates to zero, the alignment |
| 3426 | // specifier shall have no effect |
| 3427 | // C11 6.7.5p6: |
| 3428 | // An alignment specification of zero has no effect. |
| 3429 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3430 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3431 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3432 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3433 | return; |
| 3434 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3435 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3436 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3437 | // We've already verified it's a power of 2, now let's make sure it's |
| 3438 | // 8192 or less. |
| 3439 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3440 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3441 | << E->getSourceRange(); |
| 3442 | return; |
| 3443 | } |
| 3444 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3445 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3446 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3447 | ICE.take(), SpellingListIndex); |
| 3448 | AA->setPackExpansion(IsPackExpansion); |
| 3449 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3450 | } |
| 3451 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3452 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3453 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3454 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3455 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3456 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3457 | SpellingListIndex); |
| 3458 | AA->setPackExpansion(IsPackExpansion); |
| 3459 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3460 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3461 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3462 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3463 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3464 | |
| 3465 | QualType Ty; |
| 3466 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3467 | Ty = VD->getType(); |
| 3468 | else |
| 3469 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 3653b7e | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3470 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3471 | return; |
| 3472 | |
| 3473 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3474 | // The combined effect of all alignment attributes in a declaration shall |
| 3475 | // not specify an alignment that is less strict than the alignment that |
| 3476 | // would otherwise be required for the entity being declared. |
| 3477 | AlignedAttr *AlignasAttr = 0; |
| 3478 | unsigned Align = 0; |
| 3479 | for (specific_attr_iterator<AlignedAttr> |
| 3480 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3481 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3482 | if (I->isAlignmentDependent()) |
| 3483 | return; |
| 3484 | if (I->isAlignas()) |
| 3485 | AlignasAttr = *I; |
| 3486 | Align = std::max(Align, I->getAlignment(Context)); |
| 3487 | } |
| 3488 | |
| 3489 | if (AlignasAttr && Align) { |
| 3490 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3491 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3492 | if (NaturalAlign > RequestedAlign) |
| 3493 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3494 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3495 | } |
| 3496 | } |
| 3497 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3498 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3499 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3500 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3501 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3502 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3503 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3504 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3505 | // This attribute isn't documented, but glibc uses it. It changes |
| 3506 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3507 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 3508 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3509 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3510 | return; |
| 3511 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3512 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3513 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 3514 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3515 | |
| 3516 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3517 | if (Str.startswith("__") && Str.endswith("__")) |
| 3518 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3519 | |
| 3520 | unsigned DestWidth = 0; |
| 3521 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3522 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3523 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3524 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3525 | switch (Str[0]) { |
| 3526 | case 'Q': DestWidth = 8; break; |
| 3527 | case 'H': DestWidth = 16; break; |
| 3528 | case 'S': DestWidth = 32; break; |
| 3529 | case 'D': DestWidth = 64; break; |
| 3530 | case 'X': DestWidth = 96; break; |
| 3531 | case 'T': DestWidth = 128; break; |
| 3532 | } |
| 3533 | if (Str[1] == 'F') { |
| 3534 | IntegerMode = false; |
| 3535 | } else if (Str[1] == 'C') { |
| 3536 | IntegerMode = false; |
| 3537 | ComplexMode = true; |
| 3538 | } else if (Str[1] != 'I') { |
| 3539 | DestWidth = 0; |
| 3540 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3541 | break; |
| 3542 | case 4: |
| 3543 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3544 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3545 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3546 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3547 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3548 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3549 | break; |
| 3550 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3551 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3552 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3553 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3554 | case 11: |
| 3555 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3556 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3557 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
| 3560 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3561 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3562 | OldTy = TD->getUnderlyingType(); |
| 3563 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3564 | OldTy = VD->getType(); |
| 3565 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3566 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3567 | << "mode" << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3568 | return; |
| 3569 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3570 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3571 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3572 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3573 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3574 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3575 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3576 | } else if (ComplexMode) { |
| 3577 | if (!OldTy->isComplexType()) |
| 3578 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3579 | } else { |
| 3580 | if (!OldTy->isFloatingType()) |
| 3581 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3582 | } |
| 3583 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3584 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3585 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3586 | // FIXME: Make sure floating-point mappings are accurate |
| 3587 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3588 | if (!DestWidth) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3589 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3590 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
| 3593 | QualType NewTy; |
| 3594 | |
| 3595 | if (IntegerMode) |
| 3596 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 3597 | OldTy->isSignedIntegerType()); |
| 3598 | else |
| 3599 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 3600 | |
| 3601 | if (NewTy.isNull()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3602 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3603 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3604 | } |
| 3605 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3606 | if (ComplexMode) { |
| 3607 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
| 3610 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3611 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3612 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3613 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3614 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3615 | |
| 3616 | D->addAttr(::new (S.Context) |
| 3617 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3618 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3619 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3620 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3621 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3622 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3623 | if (!VD->hasGlobalStorage()) |
| 3624 | S.Diag(Attr.getLoc(), |
| 3625 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3626 | << Attr.getName(); |
| 3627 | } else if (!isFunctionOrMethod(D)) { |
| 3628 | S.Diag(Attr.getLoc(), |
| 3629 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3630 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3631 | return; |
| 3632 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3633 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3634 | D->addAttr(::new (S.Context) |
| 3635 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3636 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3639 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3640 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3641 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3642 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3643 | return; |
| 3644 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3645 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3646 | D->addAttr(::new (S.Context) |
| 3647 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3648 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3649 | } |
| 3650 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3651 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3652 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3653 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3654 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3655 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3656 | return; |
| 3657 | } |
| 3658 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3659 | D->addAttr(::new (S.Context) |
| 3660 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3661 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3664 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3665 | if (S.LangOpts.CUDA) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3666 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3667 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3668 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3669 | return; |
| 3670 | } |
| 3671 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3672 | D->addAttr(::new (S.Context) |
| 3673 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3674 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3675 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3676 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3677 | } |
| 3678 | } |
| 3679 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3680 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3681 | if (S.LangOpts.CUDA) { |
| 3682 | // check the attribute arguments. |
| 3683 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3684 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3685 | << Attr.getName() << 0; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3686 | return; |
| 3687 | } |
| 3688 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3689 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3690 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3691 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3692 | return; |
| 3693 | } |
| 3694 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3695 | D->addAttr(::new (S.Context) |
| 3696 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3697 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3698 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3699 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3700 | } |
| 3701 | } |
| 3702 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3703 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3704 | if (S.LangOpts.CUDA) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3705 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3706 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3707 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3708 | return; |
| 3709 | } |
| 3710 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3711 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3712 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3713 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3714 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3715 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3716 | << FD->getType() |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3717 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3718 | "void"); |
| 3719 | } else { |
| 3720 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3721 | << FD->getType(); |
| 3722 | } |
| 3723 | return; |
| 3724 | } |
| 3725 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3726 | D->addAttr(::new (S.Context) |
| 3727 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3728 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3729 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3730 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3731 | } |
| 3732 | } |
| 3733 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3734 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3735 | if (S.LangOpts.CUDA) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3736 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3737 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3738 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3739 | return; |
| 3740 | } |
| 3741 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3742 | D->addAttr(::new (S.Context) |
| 3743 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3744 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3745 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3746 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3747 | } |
| 3748 | } |
| 3749 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3750 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3751 | if (S.LangOpts.CUDA) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3752 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3753 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3754 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3755 | return; |
| 3756 | } |
| 3757 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3758 | D->addAttr(::new (S.Context) |
| 3759 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3760 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3761 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3762 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3763 | } |
| 3764 | } |
| 3765 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3766 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3767 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3768 | if (Fn == 0) { |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3769 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3770 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3771 | return; |
| 3772 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3773 | |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3774 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3775 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3776 | return; |
| 3777 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3778 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3779 | D->addAttr(::new (S.Context) |
| 3780 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3781 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3784 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3785 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3786 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3787 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3788 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3789 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3790 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3791 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3792 | return; |
| 3793 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3794 | if (!isa<ObjCMethodDecl>(D)) { |
| 3795 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3796 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3797 | return; |
| 3798 | } |
| 3799 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3800 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3801 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3802 | D->addAttr(::new (S.Context) |
| 3803 | FastCallAttr(Attr.getRange(), S.Context, |
| 3804 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3805 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3806 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3807 | D->addAttr(::new (S.Context) |
| 3808 | StdCallAttr(Attr.getRange(), S.Context, |
| 3809 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3810 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3811 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3812 | D->addAttr(::new (S.Context) |
| 3813 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3814 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3815 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3816 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3817 | D->addAttr(::new (S.Context) |
| 3818 | CDeclAttr(Attr.getRange(), S.Context, |
| 3819 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3820 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3821 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3822 | D->addAttr(::new (S.Context) |
| 3823 | PascalAttr(Attr.getRange(), S.Context, |
| 3824 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3825 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3826 | case AttributeList::AT_MSABI: |
| 3827 | D->addAttr(::new (S.Context) |
| 3828 | MSABIAttr(Attr.getRange(), S.Context, |
| 3829 | Attr.getAttributeSpellingListIndex())); |
| 3830 | return; |
| 3831 | case AttributeList::AT_SysVABI: |
| 3832 | D->addAttr(::new (S.Context) |
| 3833 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3834 | Attr.getAttributeSpellingListIndex())); |
| 3835 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3836 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3837 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3838 | switch (CC) { |
| 3839 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3840 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3841 | break; |
| 3842 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3843 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3844 | break; |
| 3845 | default: |
| 3846 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3847 | } |
| 3848 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3849 | D->addAttr(::new (S.Context) |
| 3850 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3851 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3852 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3853 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3854 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3855 | D->addAttr(::new (S.Context) |
| 3856 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3857 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3858 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3859 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3860 | D->addAttr(::new (S.Context) |
| 3861 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3862 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3863 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3864 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3865 | default: |
| 3866 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3867 | } |
| 3868 | } |
| 3869 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3870 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, |
| 3871 | const AttributeList &Attr) { |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3872 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3873 | } |
| 3874 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3875 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, |
| 3876 | const AttributeList &Attr) { |
| 3877 | uint32_t ArgNum; |
| 3878 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum)) |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3879 | return; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3880 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3881 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(), |
| 3882 | S.Context, ArgNum)); |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3883 | } |
| 3884 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3885 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3886 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3887 | if (attr.isInvalid()) |
| 3888 | return true; |
| 3889 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3890 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3891 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3892 | attr.setInvalid(); |
| 3893 | return true; |
| 3894 | } |
| 3895 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3896 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3897 | // move to TargetAttributesSema one day. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3898 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3899 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3900 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3901 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3902 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3903 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3904 | case AttributeList::AT_MSABI: |
| 3905 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3906 | CC_X86_64Win64; |
| 3907 | break; |
| 3908 | case AttributeList::AT_SysVABI: |
| 3909 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3910 | CC_C; |
| 3911 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3912 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3913 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3914 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3915 | attr.setInvalid(); |
| 3916 | return true; |
| 3917 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3918 | if (StrRef == "aapcs") { |
| 3919 | CC = CC_AAPCS; |
| 3920 | break; |
| 3921 | } else if (StrRef == "aapcs-vfp") { |
| 3922 | CC = CC_AAPCS_VFP; |
| 3923 | break; |
| 3924 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3925 | |
| 3926 | attr.setInvalid(); |
| 3927 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3928 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3929 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3930 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3931 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3932 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3935 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3936 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3937 | if (A == TargetInfo::CCCR_Warning) { |
| 3938 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3939 | |
| 3940 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3941 | if (FD) |
| 3942 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3943 | TargetInfo::CCMT_NonMember; |
| 3944 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3947 | return false; |
| 3948 | } |
| 3949 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3950 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3951 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3952 | |
| 3953 | unsigned numParams; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3954 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3955 | return; |
| 3956 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3957 | if (!isa<ObjCMethodDecl>(D)) { |
| 3958 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3959 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3960 | return; |
| 3961 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3962 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3963 | D->addAttr(::new (S.Context) |
| 3964 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 3965 | Attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
| 3968 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3969 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3970 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3971 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3972 | return true; |
| 3973 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3974 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3975 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3976 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3977 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3978 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3979 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3980 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3981 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3982 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3983 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3984 | } |
| 3985 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3986 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3987 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3988 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3989 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3990 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3991 | } |
| 3992 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3993 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3994 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3995 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3996 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3997 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3998 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4001 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4004 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4005 | if (S.LangOpts.CUDA) { |
| 4006 | // check the attribute arguments. |
| 4007 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 4008 | // FIXME: 0 is not okay. |
| 4009 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4010 | return; |
| 4011 | } |
| 4012 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4013 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4014 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4015 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4016 | return; |
| 4017 | } |
| 4018 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4019 | uint32_t MaxThreads, MinBlocks; |
| 4020 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) || |
| 4021 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2)) |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4022 | return; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4023 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4024 | D->addAttr(::new (S.Context) |
| 4025 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4026 | MaxThreads, MinBlocks, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4027 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4028 | } else { |
| 4029 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4030 | } |
| 4031 | } |
| 4032 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4033 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4034 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4035 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4036 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4037 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4038 | return; |
| 4039 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4040 | |
| 4041 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4042 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4043 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4044 | StringRef AttrName = Attr.getName()->getName(); |
| 4045 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4046 | |
| 4047 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4048 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4049 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4050 | return; |
| 4051 | } |
| 4052 | |
| 4053 | uint64_t ArgumentIdx; |
| 4054 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4055 | Attr.getLoc(), 2, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4056 | Attr.getArgAsExpr(1), ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4057 | return; |
| 4058 | |
| 4059 | uint64_t TypeTagIdx; |
| 4060 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4061 | Attr.getLoc(), 3, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4062 | Attr.getArgAsExpr(2), TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4063 | return; |
| 4064 | |
| 4065 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4066 | if (IsPointer) { |
| 4067 | // Ensure that buffer has a pointer type. |
| 4068 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4069 | if (!BufferTy->isPointerType()) { |
| 4070 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4071 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4072 | } |
| 4073 | } |
| 4074 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4075 | D->addAttr(::new (S.Context) |
| 4076 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4077 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4078 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
| 4081 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4082 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4083 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4084 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4085 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4086 | return; |
| 4087 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4088 | |
| 4089 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4090 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4091 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4092 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4093 | TypeSourceInfo *MatchingCTypeLoc = 0; |
| 4094 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 4095 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4096 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4097 | D->addAttr(::new (S.Context) |
| 4098 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4099 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4100 | Attr.getLayoutCompatible(), |
| 4101 | Attr.getMustBeNull(), |
| 4102 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4103 | } |
| 4104 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4105 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4106 | // Checker-specific attribute handlers. |
| 4107 | //===----------------------------------------------------------------------===// |
| 4108 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4109 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4110 | return type->isDependentType() || |
| 4111 | type->isObjCObjectPointerType() || |
| 4112 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4113 | } |
| 4114 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4115 | return type->isDependentType() || |
| 4116 | type->isPointerType() || |
| 4117 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4120 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4121 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4122 | if (!param) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4123 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4124 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4125 | return; |
| 4126 | } |
| 4127 | |
| 4128 | bool typeOK, cf; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4129 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4130 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4131 | cf = false; |
| 4132 | } else { |
| 4133 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4134 | cf = true; |
| 4135 | } |
| 4136 | |
| 4137 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4138 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4139 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4140 | return; |
| 4141 | } |
| 4142 | |
| 4143 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4144 | param->addAttr(::new (S.Context) |
| 4145 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4146 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4147 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4148 | param->addAttr(::new (S.Context) |
| 4149 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4150 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4153 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4154 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4155 | if (!isa<ObjCMethodDecl>(D)) { |
| 4156 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4157 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4158 | return; |
| 4159 | } |
| 4160 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4161 | D->addAttr(::new (S.Context) |
| 4162 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4163 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4164 | } |
| 4165 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4166 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4167 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4168 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4169 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4170 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4171 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4172 | returnType = MD->getResultType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4173 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4174 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4175 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4176 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4177 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4178 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4179 | returnType = FD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4180 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4181 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4182 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4183 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4184 | return; |
| 4185 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4186 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4187 | bool typeOK; |
| 4188 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4189 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4190 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4191 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4192 | case AttributeList::AT_NSReturnsRetained: |
| 4193 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4194 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4195 | cf = false; |
| 4196 | break; |
| 4197 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4198 | case AttributeList::AT_CFReturnsRetained: |
| 4199 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4200 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4201 | cf = true; |
| 4202 | break; |
| 4203 | } |
| 4204 | |
| 4205 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4206 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4207 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4208 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4209 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4210 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4211 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4212 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4213 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4214 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4215 | D->addAttr(::new (S.Context) |
| 4216 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4217 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4218 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4219 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4220 | D->addAttr(::new (S.Context) |
| 4221 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4222 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4223 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4224 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4225 | D->addAttr(::new (S.Context) |
| 4226 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4227 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4228 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4229 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4230 | D->addAttr(::new (S.Context) |
| 4231 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4232 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4233 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4234 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4235 | D->addAttr(::new (S.Context) |
| 4236 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4237 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4238 | return; |
| 4239 | }; |
| 4240 | } |
| 4241 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4242 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4243 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4244 | const int EP_ObjCMethod = 1; |
| 4245 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4246 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4247 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4248 | QualType resultType; |
| 4249 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4250 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4251 | |
Fariborz Jahanian | a53e5d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4252 | if (!method) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4253 | ObjCPropertyDecl *property = dyn_cast<ObjCPropertyDecl>(D); |
| 4254 | if (!property) { |
| 4255 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4256 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethodOrProperty; |
| 4257 | return; |
| 4258 | } |
| 4259 | resultType = property->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4260 | } |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4261 | else |
| 4262 | // Check that the method returns a normal pointer. |
| 4263 | resultType = method->getResultType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4264 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4265 | if (!resultType->isReferenceType() && |
| 4266 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4267 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4268 | << SourceRange(loc) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4269 | << attr.getName() << (method ? EP_ObjCMethod : EP_ObjCProperty) |
| 4270 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4271 | |
| 4272 | // Drop the attribute. |
| 4273 | return; |
| 4274 | } |
| 4275 | |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4276 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4277 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4278 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4281 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4282 | const AttributeList &attr) { |
| 4283 | SourceLocation loc = attr.getLoc(); |
| 4284 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4285 | |
| 4286 | if (!method) { |
| 4287 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4288 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4289 | return; |
| 4290 | } |
| 4291 | DeclContext *DC = method->getDeclContext(); |
| 4292 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4293 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4294 | << attr.getName() << 0; |
| 4295 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4296 | return; |
| 4297 | } |
| 4298 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4299 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4300 | << attr.getName() << 1; |
| 4301 | return; |
| 4302 | } |
| 4303 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4304 | method->addAttr(::new (S.Context) |
| 4305 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4306 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4307 | } |
| 4308 | |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4309 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4310 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4311 | if (!isa<FunctionDecl>(D)) { |
| 4312 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4313 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4314 | return; |
| 4315 | } |
| 4316 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4317 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4318 | |
| 4319 | // Check whether there's a conflicting attribute already present. |
| 4320 | Attr *Existing; |
| 4321 | if (IsAudited) { |
| 4322 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4323 | } else { |
| 4324 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4325 | } |
| 4326 | if (Existing) { |
| 4327 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4328 | << A.getName() |
| 4329 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4330 | << A.getRange() << Existing->getRange(); |
| 4331 | return; |
| 4332 | } |
| 4333 | |
| 4334 | // All clear; add the attribute. |
| 4335 | if (IsAudited) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4336 | D->addAttr(::new (S.Context) |
| 4337 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4338 | A.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4339 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4340 | D->addAttr(::new (S.Context) |
| 4341 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4342 | A.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4343 | } |
| 4344 | } |
| 4345 | |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4346 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4347 | const AttributeList &Attr) { |
| 4348 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4349 | if (!RD || RD->isUnion()) { |
| 4350 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4351 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4352 | } |
| 4353 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4354 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4355 | |
| 4356 | // In Objective-C, verify that the type names an Objective-C type. |
| 4357 | // We don't want to check this outside of ObjC because people sometimes |
| 4358 | // do crazy C declarations of Objective-C types. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4359 | if (Parm && S.getLangOpts().ObjC1) { |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4360 | // Check for an existing type with this name. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4361 | LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc, |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4362 | Sema::LookupOrdinaryName); |
| 4363 | if (S.LookupName(R, Sc)) { |
| 4364 | NamedDecl *Target = R.getFoundDecl(); |
| 4365 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4366 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4367 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4368 | } |
| 4369 | } |
| 4370 | } |
| 4371 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4372 | D->addAttr(::new (S.Context) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4373 | NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4374 | Attr.getAttributeSpellingListIndex())); |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4375 | } |
| 4376 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4377 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 4378 | const AttributeList &Attr) { |
Fariborz Jahanian | db3d855 | 2013-11-19 00:09:48 +0000 | [diff] [blame] | 4379 | if (!isa<RecordDecl>(D)) { |
Fariborz Jahanian | f720f86 | 2013-11-19 17:42:25 +0000 | [diff] [blame] | 4380 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4381 | << Attr.getName() |
| 4382 | << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass |
| 4383 | : ExpectedStructOrUnion); |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4384 | return; |
| 4385 | } |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4386 | |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 4387 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4388 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4389 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4390 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4391 | return; |
| 4392 | } |
| 4393 | |
| 4394 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4395 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4396 | Attr.getAttributeSpellingListIndex())); |
| 4397 | } |
| 4398 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4399 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 4400 | const AttributeList &Attr) { |
| 4401 | if (!isa<RecordDecl>(D)) { |
| 4402 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4403 | << Attr.getName() |
| 4404 | << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass |
| 4405 | : ExpectedStructOrUnion); |
| 4406 | return; |
| 4407 | } |
| 4408 | |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 4409 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4410 | |
| 4411 | if (!Parm) { |
| 4412 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4413 | return; |
| 4414 | } |
| 4415 | |
| 4416 | D->addAttr(::new (S.Context) |
| 4417 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 4418 | Attr.getAttributeSpellingListIndex())); |
| 4419 | } |
| 4420 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4421 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4422 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4423 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4424 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4425 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4426 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4429 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4430 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4431 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4432 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4433 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4434 | return; |
| 4435 | } |
| 4436 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4437 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4438 | QualType type = vd->getType(); |
| 4439 | |
| 4440 | if (!type->isDependentType() && |
| 4441 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4442 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4443 | << type; |
| 4444 | return; |
| 4445 | } |
| 4446 | |
| 4447 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4448 | |
| 4449 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4450 | // going to infer. |
| 4451 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4452 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4453 | |
| 4454 | switch (lifetime) { |
| 4455 | case Qualifiers::OCL_None: |
| 4456 | assert(type->isDependentType() && |
| 4457 | "didn't infer lifetime for non-dependent type?"); |
| 4458 | break; |
| 4459 | |
| 4460 | case Qualifiers::OCL_Weak: // meaningful |
| 4461 | case Qualifiers::OCL_Strong: // meaningful |
| 4462 | break; |
| 4463 | |
| 4464 | case Qualifiers::OCL_ExplicitNone: |
| 4465 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4466 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4467 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4468 | break; |
| 4469 | } |
| 4470 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4471 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4472 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4473 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4476 | //===----------------------------------------------------------------------===// |
| 4477 | // Microsoft specific attribute handlers. |
| 4478 | //===----------------------------------------------------------------------===// |
| 4479 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4480 | // Check if MS extensions or some other language extensions are enabled. If |
| 4481 | // not, issue a diagnostic that the given attribute is unused. |
| 4482 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4483 | bool OtherExtension = false) { |
| 4484 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4485 | return true; |
| 4486 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4487 | return false; |
| 4488 | } |
| 4489 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4490 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 4491 | if (!S.LangOpts.CPlusPlus) { |
| 4492 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4493 | << Attr.getName() << AttributeLangSupport::C; |
| 4494 | return; |
| 4495 | } |
| 4496 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4497 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4498 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4499 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 4500 | if (!isa<CXXRecordDecl>(D)) { |
| 4501 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4502 | << Attr.getName() << ExpectedClass; |
| 4503 | return; |
| 4504 | } |
| 4505 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4506 | StringRef StrRef; |
| 4507 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4508 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4509 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4510 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4511 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4512 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4513 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4514 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4515 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4516 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4517 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4518 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4519 | return; |
| 4520 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4521 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4522 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4523 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4524 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4525 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4526 | return; |
| 4527 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4528 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4529 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4530 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4531 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4532 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4533 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4534 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 4535 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4538 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4539 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4540 | return; |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4541 | |
| 4542 | AttributeList::Kind Kind = Attr.getKind(); |
| 4543 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4544 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4545 | ::new (S.Context) |
| 4546 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4547 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4548 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4549 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4550 | ::new (S.Context) |
| 4551 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4552 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4553 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4554 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4555 | ::new (S.Context) |
| 4556 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4557 | Attr.getAttributeSpellingListIndex())); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4561 | if (!checkMicrosoftExt(S, Attr)) |
| 4562 | return; |
| 4563 | |
| 4564 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | cc14f3a | 2013-11-22 21:49:04 +0000 | [diff] [blame] | 4565 | if (Kind == AttributeList::AT_Win64) |
| 4566 | D->addAttr( |
| 4567 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4568 | Attr.getAttributeSpellingListIndex())); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4569 | } |
| 4570 | |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4571 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4572 | if (!checkMicrosoftExt(S, Attr)) |
| 4573 | return; |
| 4574 | D->addAttr(::new (S.Context) |
| 4575 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4576 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4579 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4580 | if (!checkMicrosoftExt(S, Attr)) |
| 4581 | return; |
| 4582 | // Check linkage after possibly merging declaratinos. See |
| 4583 | // checkAttributesAfterMerging(). |
| 4584 | D->addAttr(::new (S.Context) |
| 4585 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4586 | Attr.getAttributeSpellingListIndex())); |
| 4587 | } |
| 4588 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4589 | /// Handles semantic checking for features that are common to all attributes, |
| 4590 | /// such as checking whether a parameter was properly specified, or the correct |
| 4591 | /// number of arguments were passed, etc. |
| 4592 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 4593 | const AttributeList &Attr) { |
| 4594 | // Several attributes carry different semantics than the parsing requires, so |
| 4595 | // those are opted out of the common handling. |
| 4596 | // |
| 4597 | // We also bail on unknown and ignored attributes because those are handled |
| 4598 | // as part of the target-specific handling logic. |
| 4599 | if (Attr.hasCustomParsing() || |
| 4600 | Attr.getKind() == AttributeList::UnknownAttribute || |
| 4601 | Attr.getKind() == AttributeList::IgnoredAttribute) |
| 4602 | return false; |
| 4603 | |
| 4604 | // If there are no optional arguments, then checking for the argument count |
| 4605 | // is trivial. |
| 4606 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 4607 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4608 | return true; |
| 4609 | return false; |
| 4610 | } |
| 4611 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4612 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4613 | // Top Level Sema Entry Points |
| 4614 | //===----------------------------------------------------------------------===// |
| 4615 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4616 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4617 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4618 | /// silently ignore it if a GNU attribute. |
| 4619 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4620 | const AttributeList &Attr, |
| 4621 | bool IncludeCXX11Attributes) { |
| 4622 | if (Attr.isInvalid()) |
| 4623 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4624 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4625 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4626 | // instead. |
| 4627 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4628 | return; |
| 4629 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4630 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4631 | return; |
| 4632 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4633 | switch (Attr.getKind()) { |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4634 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4635 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4636 | case AttributeList::AT_IBOutletCollection: |
| 4637 | handleIBOutletCollection(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4638 | case AttributeList::AT_AddressSpace: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4639 | case AttributeList::AT_ObjCGC: |
| 4640 | case AttributeList::AT_VectorSize: |
| 4641 | case AttributeList::AT_NeonVectorType: |
| 4642 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4643 | case AttributeList::AT_Ptr32: |
| 4644 | case AttributeList::AT_Ptr64: |
| 4645 | case AttributeList::AT_SPtr: |
| 4646 | case AttributeList::AT_UPtr: |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4647 | // Ignore these, these are type attributes, handled by |
| 4648 | // ProcessTypeAttributes. |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4649 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4650 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4651 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4652 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4653 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4654 | handleAlwaysInlineAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4655 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4656 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4657 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4658 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4659 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4660 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4661 | handleDependencyAttr(S, scope, D, Attr); |
| 4662 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4663 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4664 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4665 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4666 | case AttributeList::AT_CXX11NoReturn: |
| 4667 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4668 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4669 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4670 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4671 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4672 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4673 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4674 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4675 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4676 | case AttributeList::AT_MinSize: |
| 4677 | handleMinSizeAttr(S, D, Attr); |
| 4678 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4679 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4680 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4681 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | 9a22621 | 2013-08-28 23:13:26 +0000 | [diff] [blame] | 4682 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4683 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4684 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4685 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4686 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4687 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4688 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4689 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4690 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4691 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4692 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4693 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4694 | case AttributeList::AT_ownership_returns: |
| 4695 | case AttributeList::AT_ownership_takes: |
| 4696 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4697 | handleOwnershipAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4698 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4699 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4700 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4701 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4702 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4703 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4704 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4705 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4706 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4707 | handleObjCOwnershipAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4708 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4709 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4710 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4711 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4712 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4713 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4714 | case AttributeList::AT_ObjCRequiresSuper: |
| 4715 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4716 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4717 | case AttributeList::AT_NSBridged: |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4718 | handleNSBridgedAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4719 | |
| 4720 | case AttributeList::AT_ObjCBridge: |
| 4721 | handleObjCBridgeAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4722 | |
| 4723 | case AttributeList::AT_ObjCBridgeMutable: |
| 4724 | handleObjCBridgeMutableAttr(S, scope, D, Attr); break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4725 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4726 | case AttributeList::AT_CFAuditedTransfer: |
| 4727 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4728 | handleCFTransferAttr(S, D, Attr); break; |
| 4729 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4730 | // Checker-specific. |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4731 | case AttributeList::AT_CFConsumed: |
| 4732 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4733 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4734 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4735 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4736 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4737 | case AttributeList::AT_NSReturnsNotRetained: |
| 4738 | case AttributeList::AT_CFReturnsNotRetained: |
| 4739 | case AttributeList::AT_NSReturnsRetained: |
| 4740 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4741 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4742 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4743 | case AttributeList::AT_WorkGroupSizeHint: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4744 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4745 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4746 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4747 | case AttributeList::AT_VecTypeHint: |
| 4748 | handleVecTypeHint(S, D, Attr); break; |
| 4749 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4750 | case AttributeList::AT_InitPriority: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4751 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4752 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4753 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4754 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4755 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4756 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4757 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4758 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4759 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4760 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4761 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4762 | handleObjCRootClassAttr(S, D, Attr); |
| 4763 | break; |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 4764 | case AttributeList::AT_ObjCSuppressProtocol: |
| 4765 | handleObjCSuppresProtocolAttr(S, D, Attr); |
| 4766 | break; |
| 4767 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 0c2c90b | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4768 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | 9d4d20a | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4769 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4770 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4771 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4772 | handleReturnsTwiceAttr(S, D, Attr); |
| 4773 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4774 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4775 | case AttributeList::AT_Visibility: |
| 4776 | handleVisibilityAttr(S, D, Attr, false); |
| 4777 | break; |
| 4778 | case AttributeList::AT_TypeVisibility: |
| 4779 | handleVisibilityAttr(S, D, Attr, true); |
| 4780 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4781 | case AttributeList::AT_WarnUnused: |
| 4782 | handleWarnUnusedAttr(S, D, Attr); |
| 4783 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4784 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4785 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4786 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4787 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4788 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4789 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4790 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4791 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4792 | case AttributeList::AT_ObjCException: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4793 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4794 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4795 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4796 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4797 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4798 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4799 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4800 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4801 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4802 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4803 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4804 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4805 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4806 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4807 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | b4f3134 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4808 | // Just ignore |
| 4809 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4810 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4811 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4812 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4813 | case AttributeList::AT_StdCall: |
| 4814 | case AttributeList::AT_CDecl: |
| 4815 | case AttributeList::AT_FastCall: |
| 4816 | case AttributeList::AT_ThisCall: |
| 4817 | case AttributeList::AT_Pascal: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4818 | case AttributeList::AT_MSABI: |
| 4819 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4820 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4821 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4822 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4823 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4824 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4825 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4826 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4827 | break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4828 | case AttributeList::AT_OpenCLImageAccess: |
| 4829 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4830 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4831 | |
| 4832 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4833 | case AttributeList::AT_MsStruct: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4834 | handleMsStructAttr(S, D, Attr); |
| 4835 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4836 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4837 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4838 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4839 | case AttributeList::AT_SingleInheritance: |
| 4840 | case AttributeList::AT_MultipleInheritance: |
| 4841 | case AttributeList::AT_VirtualInheritance: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4842 | handleInheritanceAttr(S, D, Attr); |
| 4843 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4844 | case AttributeList::AT_Win64: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4845 | handlePortabilityAttr(S, D, Attr); |
| 4846 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4847 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4848 | handleForceInlineAttr(S, D, Attr); |
| 4849 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4850 | case AttributeList::AT_SelectAny: |
| 4851 | handleSelectAnyAttr(S, D, Attr); |
| 4852 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4853 | |
| 4854 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4855 | case AttributeList::AT_AssertExclusiveLock: |
| 4856 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4857 | break; |
| 4858 | case AttributeList::AT_AssertSharedLock: |
| 4859 | handleAssertSharedLockAttr(S, D, Attr); |
| 4860 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4861 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4862 | handleGuardedVarAttr(S, D, Attr); |
| 4863 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4864 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4865 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4866 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4867 | case AttributeList::AT_ScopedLockable: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4868 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4869 | break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4870 | case AttributeList::AT_NoSanitizeAddress: |
| 4871 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4872 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4873 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4874 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 4875 | break; |
| 4876 | case AttributeList::AT_NoSanitizeThread: |
| 4877 | handleNoSanitizeThread(S, D, Attr); |
| 4878 | break; |
| 4879 | case AttributeList::AT_NoSanitizeMemory: |
| 4880 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4881 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4882 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4883 | handleLockableAttr(S, D, Attr); |
| 4884 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4885 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4886 | handleGuardedByAttr(S, D, Attr); |
| 4887 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4888 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4889 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4890 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4891 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4892 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4893 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4894 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4895 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4896 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4897 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4898 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4899 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4900 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4901 | handleLockReturnedAttr(S, D, Attr); |
| 4902 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4903 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4904 | handleLocksExcludedAttr(S, D, Attr); |
| 4905 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4906 | case AttributeList::AT_SharedLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4907 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4908 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4909 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4910 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4911 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4912 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4913 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4914 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4915 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4916 | handleUnlockFunAttr(S, D, Attr); |
| 4917 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4918 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4919 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4920 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4921 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4922 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4923 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4924 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4925 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4926 | case AttributeList::AT_Consumable: |
| 4927 | handleConsumableAttr(S, D, Attr); |
| 4928 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4929 | case AttributeList::AT_CallableWhen: |
| 4930 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4931 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4932 | case AttributeList::AT_ParamTypestate: |
| 4933 | handleParamTypestateAttr(S, D, Attr); |
| 4934 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4935 | case AttributeList::AT_ReturnTypestate: |
| 4936 | handleReturnTypestateAttr(S, D, Attr); |
| 4937 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4938 | case AttributeList::AT_SetTypestate: |
| 4939 | handleSetTypestateAttr(S, D, Attr); |
| 4940 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4941 | case AttributeList::AT_TestTypestate: |
| 4942 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4943 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4944 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4945 | // Type safety attributes. |
| 4946 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4947 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4948 | break; |
| 4949 | case AttributeList::AT_TypeTagForDatatype: |
| 4950 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4951 | break; |
| 4952 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4953 | default: |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4954 | // Ask target about the attribute. |
| 4955 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4956 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4957 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4958 | diag::warn_unhandled_ms_attribute_ignored : |
| 4959 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4960 | break; |
| 4961 | } |
| 4962 | } |
| 4963 | |
| 4964 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4965 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4966 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4967 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4968 | bool IncludeCXX11Attributes) { |
| 4969 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4970 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4971 | |
| 4972 | // GCC accepts |
| 4973 | // static int a9 __attribute__((weakref)); |
| 4974 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4975 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4976 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4977 | cast<NamedDecl>(D)->getNameAsString(); |
| 4978 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4979 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4980 | } |
| 4981 | } |
| 4982 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4983 | // Annotation attributes are the only attributes allowed after an access |
| 4984 | // specifier. |
| 4985 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4986 | const AttributeList *AttrList) { |
| 4987 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4988 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4989 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4990 | } else { |
| 4991 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4992 | return true; |
| 4993 | } |
| 4994 | } |
| 4995 | |
| 4996 | return false; |
| 4997 | } |
| 4998 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4999 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 5000 | /// contains any decl attributes that we should warn about. |
| 5001 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 5002 | for ( ; A; A = A->getNext()) { |
| 5003 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 5004 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5005 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 5006 | |
| 5007 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 5008 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 5009 | << A->getName() << A->getRange(); |
| 5010 | } else { |
| 5011 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 5012 | << A->getName() << A->getRange(); |
| 5013 | } |
| 5014 | } |
| 5015 | } |
| 5016 | |
| 5017 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 5018 | /// used to build a declaration, complain about any decl attributes |
| 5019 | /// which might be lying around on it. |
| 5020 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 5021 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 5022 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5023 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5024 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5025 | } |
| 5026 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5027 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5028 | /// \#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] | 5029 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5030 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5031 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5032 | NamedDecl *NewD = 0; |
| 5033 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5034 | FunctionDecl *NewFD; |
| 5035 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 5036 | // FIXME: Mangling? |
| 5037 | // FIXME: Is the qualifier info correct? |
| 5038 | // FIXME: Is the DeclContext correct? |
| 5039 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 5040 | Loc, Loc, DeclarationName(II), |
| 5041 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5042 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5043 | FD->hasPrototype(), |
| 5044 | false/*isConstexprSpecified*/); |
| 5045 | NewD = NewFD; |
| 5046 | |
| 5047 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5048 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5049 | |
| 5050 | // Fake up parameter variables; they are declared as if this were |
| 5051 | // a typedef. |
| 5052 | QualType FDTy = FD->getType(); |
| 5053 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5054 | SmallVector<ParmVarDecl*, 16> Params; |
| 5055 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 5056 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 5057 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 5058 | Param->setScopeInfo(0, Params.size()); |
| 5059 | Params.push_back(Param); |
| 5060 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5061 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5062 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5063 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5064 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5065 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5066 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5067 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5068 | if (VD->getQualifier()) { |
| 5069 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5070 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5071 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5072 | } |
| 5073 | return NewD; |
| 5074 | } |
| 5075 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5076 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5077 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5078 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5079 | if (W.getUsed()) return; // only do this once |
| 5080 | W.setUsed(true); |
| 5081 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5082 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5083 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5084 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 5085 | NDId->getName())); |
| 5086 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5087 | WeakTopLevelDecl.push_back(NewD); |
| 5088 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5089 | // to insert Decl at TU scope, sorry. |
| 5090 | DeclContext *SavedContext = CurContext; |
| 5091 | CurContext = Context.getTranslationUnitDecl(); |
| 5092 | PushOnScopeChains(NewD, S); |
| 5093 | CurContext = SavedContext; |
| 5094 | } else { // just add weak to existing |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5095 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5096 | } |
| 5097 | } |
| 5098 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5099 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5100 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5101 | // have to do this. |
| 5102 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5103 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5104 | NamedDecl *ND = NULL; |
| 5105 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5106 | if (VD->isExternC()) |
| 5107 | ND = VD; |
| 5108 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5109 | if (FD->isExternC()) |
| 5110 | ND = FD; |
| 5111 | if (ND) { |
| 5112 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5113 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5114 | = WeakUndeclaredIdentifiers.find(Id); |
| 5115 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5116 | WeakInfo W = I->second; |
| 5117 | DeclApplyPragmaWeak(S, ND, W); |
| 5118 | WeakUndeclaredIdentifiers[Id] = W; |
| 5119 | } |
| 5120 | } |
| 5121 | } |
| 5122 | } |
| 5123 | } |
| 5124 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5125 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5126 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5127 | /// 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] | 5128 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5129 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5130 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5131 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5132 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5133 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5134 | // position to the decl itself. This handles cases like: |
| 5135 | // int *__attr__(x)** D; |
| 5136 | // when X is a decl attribute. |
| 5137 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5138 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5139 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5140 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5141 | // Finally, apply any attributes on the decl itself. |
| 5142 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5143 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5144 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5145 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5146 | /// Is the given declaration allowed to use a forbidden type? |
| 5147 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5148 | // Private ivars are always okay. Unfortunately, people don't |
| 5149 | // always properly make their ivars private, even in system headers. |
| 5150 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5151 | // Function declarations in sys headers will be marked unavailable. |
| 5152 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5153 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5154 | return false; |
| 5155 | |
| 5156 | // Require it to be declared in a system header. |
| 5157 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5158 | } |
| 5159 | |
| 5160 | /// Handle a delayed forbidden-type diagnostic. |
| 5161 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5162 | Decl *decl) { |
| 5163 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5164 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5165 | "this system declaration uses an unsupported type")); |
| 5166 | return; |
| 5167 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5168 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5169 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5170 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5171 | // kind of forbidden type messages on unavailable functions. |
| 5172 | if (FD->hasAttr<UnavailableAttr>() && |
| 5173 | diag.getForbiddenTypeDiagnostic() == |
| 5174 | diag::err_arc_array_param_no_ownership) { |
| 5175 | diag.Triggered = true; |
| 5176 | return; |
| 5177 | } |
| 5178 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5179 | |
| 5180 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5181 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5182 | diag.Triggered = true; |
| 5183 | } |
| 5184 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5185 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5186 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5187 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5188 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5189 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5190 | // When delaying diagnostics to run in the context of a parsed |
| 5191 | // declaration, we only want to actually emit anything if parsing |
| 5192 | // succeeds. |
| 5193 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5194 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5195 | // We emit all the active diagnostics in this pool or any of its |
| 5196 | // parents. In general, we'll get one pool for the decl spec |
| 5197 | // and a child pool for each declarator; in a decl group like: |
| 5198 | // deprecated_typedef foo, *bar, baz(); |
| 5199 | // only the declarator pops will be passed decls. This is correct; |
| 5200 | // we really do need to consider delayed diagnostics from the decl spec |
| 5201 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5202 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5203 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5204 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5205 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5206 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5207 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5208 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5209 | continue; |
| 5210 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5211 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5212 | case DelayedDiagnostic::Deprecation: |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5213 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5214 | if (!decl->isInvalidDecl()) |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5215 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5216 | break; |
| 5217 | |
| 5218 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5219 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5220 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5221 | |
| 5222 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5223 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5224 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5225 | } |
| 5226 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5227 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5228 | } |
| 5229 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5230 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5231 | /// been delayed in the current context instead of in the given pool. |
| 5232 | /// Essentially, this just moves them to the current pool. |
| 5233 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5234 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5235 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5236 | curPool->steal(pool); |
| 5237 | } |
| 5238 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5239 | static bool isDeclDeprecated(Decl *D) { |
| 5240 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5241 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5242 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5243 | // A category implicitly has the availability of the interface. |
| 5244 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5245 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5246 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5247 | return false; |
| 5248 | } |
| 5249 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5250 | static void |
| 5251 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5252 | SourceLocation Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5253 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5254 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5255 | DeclarationName Name = D->getDeclName(); |
| 5256 | if (!Message.empty()) { |
| 5257 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5258 | S.Diag(D->getLocation(), |
| 5259 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5260 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5261 | if (ObjCPropery) |
| 5262 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5263 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5264 | } else if (!UnknownObjCClass) { |
| 5265 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5266 | S.Diag(D->getLocation(), |
| 5267 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5268 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5269 | if (ObjCPropery) |
| 5270 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5271 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5272 | } else { |
| 5273 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5274 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5275 | } |
| 5276 | } |
| 5277 | |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5278 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5279 | Decl *Ctx) { |
| 5280 | if (isDeclDeprecated(Ctx)) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5281 | return; |
| 5282 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5283 | DD.Triggered = true; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5284 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5285 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5286 | DD.getUnknownObjCClass(), |
| 5287 | DD.getObjCProperty()); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5290 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5291 | SourceLocation Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5292 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5293 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5294 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5295 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5296 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5297 | UnknownObjCClass, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5298 | ObjCProperty, |
Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5299 | Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5300 | return; |
| 5301 | } |
| 5302 | |
| 5303 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5304 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5305 | return; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5306 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5307 | } |