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" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 26 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 28 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 31 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 32 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 33 | |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 34 | /// These constants match the enumerated choices of |
| 35 | /// warn_attribute_wrong_decl_type and err_attribute_wrong_decl_type. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 36 | enum AttributeDeclKind { |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 37 | ExpectedFunction, |
| 38 | ExpectedUnion, |
| 39 | ExpectedVariableOrFunction, |
| 40 | ExpectedFunctionOrMethod, |
| 41 | ExpectedParameter, |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 42 | ExpectedFunctionMethodOrBlock, |
Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 43 | ExpectedFunctionMethodOrClass, |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 44 | ExpectedFunctionMethodOrParameter, |
| 45 | ExpectedClass, |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 46 | ExpectedVariable, |
| 47 | ExpectedMethod, |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 48 | ExpectedVariableFunctionOrLabel, |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 49 | ExpectedFieldOrGlobalVar, |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 50 | ExpectedStruct, |
Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 51 | ExpectedVariableFunctionOrTag, |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 52 | ExpectedTLSVar, |
| 53 | ExpectedVariableOrField, |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 54 | ExpectedVariableFieldOrTag, |
| 55 | ExpectedTypeOrNamespace |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 58 | //===----------------------------------------------------------------------===// |
| 59 | // Helper functions |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 62 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 63 | bool blocksToo = true) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 64 | QualType Ty; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 65 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 66 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 67 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 68 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 69 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 70 | Ty = decl->getUnderlyingType(); |
| 71 | else |
| 72 | return 0; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 74 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 75 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 28c433d | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 76 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 77 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 78 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 79 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 82 | // FIXME: We should provide an abstraction around a method or function |
| 83 | // to provide the following bits of information. |
| 84 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 85 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 86 | /// type (function or function-typed variable). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 87 | static bool isFunction(const Decl *D) { |
| 88 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 92 | /// type (function or function-typed variable) or an Objective-C |
| 93 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 94 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 95 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 98 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 99 | /// type (function or function-typed variable) or an Objective-C |
| 100 | /// method or a block. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 101 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 102 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 103 | return true; |
| 104 | // check for block is more involved. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 105 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 106 | QualType Ty = V->getType(); |
| 107 | return Ty->isBlockPointerType(); |
| 108 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 109 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 110 | } |
| 111 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 112 | /// Return true if the given decl has a declarator that should have |
| 113 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 114 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 115 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 116 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 117 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 120 | /// hasFunctionProto - Return true if the given decl has a argument |
| 121 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 122 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 123 | static bool hasFunctionProto(const Decl *D) { |
| 124 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 125 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 126 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 127 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 133 | /// arguments. It is an error to call this on a K&R function (use |
| 134 | /// hasFunctionProto first). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 135 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 136 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 137 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 138 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 139 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 140 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 143 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 144 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 145 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 146 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 147 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 148 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 149 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 152 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 153 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 154 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 155 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 158 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 159 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 160 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 161 | return proto->isVariadic(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 162 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 163 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 164 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 165 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 169 | static bool isInstanceMethod(const Decl *D) { |
| 170 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 171 | return MethodDecl->isInstance(); |
| 172 | return false; |
| 173 | } |
| 174 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 175 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 176 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 177 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 178 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 179 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 180 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 181 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 182 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 183 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 184 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 186 | // FIXME: Should we walk the chain of classes? |
| 187 | return ClsName == &Ctx.Idents.get("NSString") || |
| 188 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 189 | } |
| 190 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 191 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 192 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 193 | if (!PT) |
| 194 | return false; |
| 195 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 196 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 197 | if (!RT) |
| 198 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 199 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 200 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 201 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 202 | return false; |
| 203 | |
| 204 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 205 | } |
| 206 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 207 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 208 | /// output an error. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 209 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 210 | unsigned int Num) { |
| 211 | if (Attr.getNumArgs() != Num) { |
| 212 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << Num; |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 219 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 220 | /// \brief Check if the attribute has at least as many args as Num. May |
| 221 | /// output an error. |
| 222 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
| 223 | unsigned int Num) { |
| 224 | if (Attr.getNumArgs() < Num) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 225 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 226 | return false; |
| 227 | } |
| 228 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 229 | return true; |
| 230 | } |
| 231 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 232 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 233 | /// instance method D. May output an error. |
| 234 | /// |
| 235 | /// \returns true if IdxExpr is a valid index. |
| 236 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 237 | StringRef AttrName, |
| 238 | SourceLocation AttrLoc, |
| 239 | unsigned AttrArgNum, |
| 240 | const Expr *IdxExpr, |
| 241 | uint64_t &Idx) |
| 242 | { |
| 243 | assert(isFunctionOrMethod(D) && hasFunctionProto(D)); |
| 244 | |
| 245 | // In C++ the implicit 'this' function parameter also counts. |
| 246 | // Parameters are counted from one. |
| 247 | const bool HasImplicitThisParam = isInstanceMethod(D); |
| 248 | const unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 249 | const unsigned FirstIdx = 1; |
| 250 | |
| 251 | llvm::APSInt IdxInt; |
| 252 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 253 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
| 254 | S.Diag(AttrLoc, diag::err_attribute_argument_n_not_int) |
| 255 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | Idx = IdxInt.getLimitedValue(); |
| 260 | if (Idx < FirstIdx || (!isFunctionOrMethodVariadic(D) && Idx > NumArgs)) { |
| 261 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 262 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 263 | return false; |
| 264 | } |
| 265 | Idx--; // Convert to zero-based. |
| 266 | if (HasImplicitThisParam) { |
| 267 | if (Idx == 0) { |
| 268 | S.Diag(AttrLoc, |
| 269 | diag::err_attribute_invalid_implicit_this_argument) |
| 270 | << AttrName << IdxExpr->getSourceRange(); |
| 271 | return false; |
| 272 | } |
| 273 | --Idx; |
| 274 | } |
| 275 | |
| 276 | return true; |
| 277 | } |
| 278 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 279 | /// |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 280 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 281 | /// \return true if the Decl is a field or potentially shared global variable |
| 282 | /// |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 283 | static bool mayBeSharedVariable(const Decl *D) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 284 | if (isa<FieldDecl>(D)) |
| 285 | return true; |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 286 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 287 | return (vd->hasGlobalStorage() && !(vd->isThreadSpecified())); |
| 288 | |
| 289 | return false; |
| 290 | } |
| 291 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 292 | /// \brief Check if the passed-in expression is of type int or bool. |
| 293 | static bool isIntOrBool(Expr *Exp) { |
| 294 | QualType QT = Exp->getType(); |
| 295 | return QT->isBooleanType() || QT->isIntegerType(); |
| 296 | } |
| 297 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 298 | |
| 299 | // Check to see if the type is a smart pointer of some kind. We assume |
| 300 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 301 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 302 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 303 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 304 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 305 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 306 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 307 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 308 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 309 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 310 | return false; |
| 311 | |
| 312 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 315 | /// \brief Check if passed in Decl is a pointer type. |
| 316 | /// Note that this function may produce an error message. |
| 317 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 318 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 319 | const AttributeList &Attr) { |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 320 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 321 | QualType QT = vd->getType(); |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 322 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 323 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 324 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 325 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 326 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 327 | // (We don't want to force template instantiation if we can avoid it, |
| 328 | // since that would alter the order in which templates are instantiated.) |
| 329 | if (RT->isIncompleteType()) |
| 330 | return true; |
| 331 | |
| 332 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 333 | return true; |
| 334 | } |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 335 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 336 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 337 | << Attr.getName()->getName() << QT; |
| 338 | } else { |
| 339 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 340 | << Attr.getName(); |
| 341 | } |
| 342 | return false; |
| 343 | } |
| 344 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 345 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 346 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 347 | static const RecordType *getRecordType(QualType QT) { |
| 348 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 349 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 350 | |
| 351 | // Now check if we point to record type. |
| 352 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 353 | return PT->getPointeeType()->getAs<RecordType>(); |
| 354 | |
| 355 | return 0; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 356 | } |
| 357 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 358 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 359 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 360 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 361 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 362 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 363 | return true; |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 368 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 369 | /// resolves to a lockable object. |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 370 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 371 | QualType Ty) { |
| 372 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 373 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 374 | // Warn if could not get record type for this argument. |
Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 375 | if (!RT) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 376 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 377 | << Attr.getName() << Ty.getAsString(); |
| 378 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 379 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 380 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 381 | // 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] | 382 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 383 | return; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 384 | |
| 385 | // Allow smart pointers to be used as lockable objects. |
| 386 | // FIXME -- Check the type that the smart pointer points to. |
| 387 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 388 | return; |
| 389 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 390 | // Check if the type is lockable. |
| 391 | RecordDecl *RD = RT->getDecl(); |
| 392 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 393 | return; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 394 | |
| 395 | // Else check if any base classes are lockable. |
| 396 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 397 | CXXBasePaths BPaths(false, false); |
| 398 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 399 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 400 | } |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 401 | |
| 402 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 403 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 406 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 407 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 408 | /// \param Sidx The attribute argument index to start checking with. |
| 409 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 410 | /// parameter list. |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 411 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 412 | const AttributeList &Attr, |
| 413 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 414 | int Sidx = 0, |
| 415 | bool ParamIdxOk = false) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 416 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 417 | Expr *ArgExp = Attr.getArg(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 418 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 419 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 420 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 421 | Args.push_back(ArgExp); |
| 422 | continue; |
| 423 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 424 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 425 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 426 | if (StrLit->getLength() == 0 || |
| 427 | StrLit->getString() == StringRef("*")) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 428 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 429 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 430 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 431 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 432 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 433 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 434 | // We allow constant strings to be used as a placeholder for expressions |
| 435 | // that are not valid C++ syntax, but warn that they are ignored. |
| 436 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 437 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 438 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 439 | continue; |
| 440 | } |
| 441 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 442 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 443 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 444 | // A pointer to member expression of the form &MyClass::mu is treated |
| 445 | // specially -- we need to look at the type of the member. |
| 446 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 447 | if (UOp->getOpcode() == UO_AddrOf) |
| 448 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 449 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 450 | ArgTy = DRE->getDecl()->getType(); |
| 451 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 452 | // First see if we can just cast to record type, or point to record type. |
| 453 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 454 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 455 | // Now check if we index into a record type function param. |
| 456 | if(!RT && ParamIdxOk) { |
| 457 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 458 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 459 | if(FD && IL) { |
| 460 | unsigned int NumParams = FD->getNumParams(); |
| 461 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 462 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 463 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 464 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 465 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 466 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 467 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 468 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 469 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 473 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 474 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 475 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 476 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 479 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 480 | // Attribute Implementations |
| 481 | //===----------------------------------------------------------------------===// |
| 482 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 483 | // FIXME: All this manual attribute parsing code is gross. At the |
| 484 | // least add some helper functions to check most argument patterns (# |
| 485 | // and types of args). |
| 486 | |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 487 | enum ThreadAttributeDeclKind { |
| 488 | ThreadExpectedFieldOrGlobalVar, |
| 489 | ThreadExpectedFunctionOrMethod, |
| 490 | ThreadExpectedClassOrStruct |
| 491 | }; |
| 492 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 493 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 494 | const AttributeList &Attr) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 495 | assert(!Attr.isInvalid()); |
| 496 | |
| 497 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 498 | return false; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 499 | |
| 500 | // D must be either a member field or global (potentially shared) variable. |
| 501 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 502 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 503 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 504 | return false; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 507 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 510 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 511 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 512 | return; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 513 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 514 | D->addAttr(::new (S.Context) |
| 515 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 516 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 519 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 520 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 521 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 522 | return; |
| 523 | |
| 524 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 525 | return; |
| 526 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 527 | D->addAttr(::new (S.Context) |
| 528 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 529 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 532 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 533 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 534 | Expr* &Arg) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 535 | assert(!Attr.isInvalid()); |
| 536 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 537 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 538 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 539 | |
| 540 | // D must be either a member field or global (potentially shared) variable. |
| 541 | if (!mayBeSharedVariable(D)) { |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 542 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 543 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 544 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 545 | } |
| 546 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 547 | SmallVector<Expr*, 1> Args; |
| 548 | // check that all arguments are lockable objects |
| 549 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 550 | unsigned Size = Args.size(); |
| 551 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 552 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 553 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 554 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 555 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 556 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 559 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 560 | Expr *Arg = 0; |
| 561 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 562 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 563 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 564 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 565 | } |
| 566 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 567 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 568 | const AttributeList &Attr) { |
| 569 | Expr *Arg = 0; |
| 570 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 571 | return; |
| 572 | |
| 573 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 574 | return; |
| 575 | |
| 576 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 577 | S.Context, Arg)); |
| 578 | } |
| 579 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 580 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 581 | const AttributeList &Attr) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 582 | assert(!Attr.isInvalid()); |
| 583 | |
| 584 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 585 | return false; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 586 | |
Caitlin Sadowski | 086fb95 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 587 | // FIXME: Lockable structs for C code. |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 588 | if (!isa<CXXRecordDecl>(D)) { |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 589 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 590 | << Attr.getName() << ThreadExpectedClassOrStruct; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 591 | return false; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 594 | return true; |
| 595 | } |
| 596 | |
| 597 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 598 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 599 | return; |
| 600 | |
| 601 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 602 | } |
| 603 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 604 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 605 | const AttributeList &Attr) { |
| 606 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 607 | return; |
| 608 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 609 | D->addAttr(::new (S.Context) |
| 610 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 611 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 614 | static void handleNoThreadSafetyAnalysis(Sema &S, Decl *D, |
| 615 | const AttributeList &Attr) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 616 | assert(!Attr.isInvalid()); |
| 617 | |
| 618 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 619 | return; |
| 620 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 621 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 622 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 623 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 624 | return; |
| 625 | } |
| 626 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 627 | D->addAttr(::new (S.Context) NoThreadSafetyAnalysisAttr(Attr.getRange(), |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 628 | S.Context)); |
| 629 | } |
| 630 | |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 631 | static void handleNoSanitizeAddressAttr(Sema &S, Decl *D, |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 632 | const AttributeList &Attr) { |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 633 | assert(!Attr.isInvalid()); |
| 634 | |
| 635 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 636 | return; |
| 637 | |
| 638 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 639 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 640 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 641 | return; |
| 642 | } |
| 643 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 644 | D->addAttr(::new (S.Context) |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 645 | NoSanitizeAddressAttr(Attr.getRange(), S.Context, |
| 646 | Attr.getAttributeSpellingListIndex())); |
| 647 | } |
| 648 | |
| 649 | static void handleNoSanitizeMemory(Sema &S, Decl *D, |
| 650 | const AttributeList &Attr) { |
| 651 | assert(!Attr.isInvalid()); |
| 652 | |
| 653 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 654 | return; |
| 655 | |
| 656 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 657 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 658 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | D->addAttr(::new (S.Context) NoSanitizeMemoryAttr(Attr.getRange(), |
| 663 | S.Context)); |
| 664 | } |
| 665 | |
| 666 | static void handleNoSanitizeThread(Sema &S, Decl *D, |
| 667 | const AttributeList &Attr) { |
| 668 | assert(!Attr.isInvalid()); |
| 669 | |
| 670 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 671 | return; |
| 672 | |
| 673 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
| 674 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 675 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | D->addAttr(::new (S.Context) NoSanitizeThreadAttr(Attr.getRange(), |
| 680 | S.Context)); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 683 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 684 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 685 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 686 | assert(!Attr.isInvalid()); |
| 687 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 688 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 689 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 690 | |
| 691 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 692 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 693 | if (!VD || !mayBeSharedVariable(D)) { |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 694 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 695 | << Attr.getName() << ThreadExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 696 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 697 | } |
| 698 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 699 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 700 | QualType QT = VD->getType(); |
| 701 | if (!QT->isDependentType()) { |
| 702 | const RecordType *RT = getRecordType(QT); |
| 703 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 704 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 705 | << Attr.getName(); |
| 706 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 710 | // Check that all arguments are lockable objects. |
| 711 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 712 | if (Args.size() == 0) |
| 713 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 714 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 715 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 718 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 719 | const AttributeList &Attr) { |
| 720 | SmallVector<Expr*, 1> Args; |
| 721 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 722 | return; |
| 723 | |
| 724 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 725 | D->addAttr(::new (S.Context) |
| 726 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 727 | StartArg, Args.size(), |
| 728 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 731 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 732 | const AttributeList &Attr) { |
| 733 | SmallVector<Expr*, 1> Args; |
| 734 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 735 | return; |
| 736 | |
| 737 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 738 | D->addAttr(::new (S.Context) |
| 739 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 740 | StartArg, Args.size(), |
| 741 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 744 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 745 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 746 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 747 | assert(!Attr.isInvalid()); |
| 748 | |
| 749 | // zero or more arguments ok |
| 750 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 751 | // check that the attribute is applied to a function |
| 752 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 753 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 754 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 755 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 758 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 759 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 760 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 761 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 762 | } |
| 763 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 764 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 765 | const AttributeList &Attr) { |
| 766 | SmallVector<Expr*, 1> Args; |
| 767 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 768 | return; |
| 769 | |
| 770 | unsigned Size = Args.size(); |
| 771 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 772 | D->addAttr(::new (S.Context) |
| 773 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 774 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 775 | } |
| 776 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 777 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 778 | const AttributeList &Attr) { |
| 779 | SmallVector<Expr*, 1> Args; |
| 780 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 781 | return; |
| 782 | |
| 783 | unsigned Size = Args.size(); |
| 784 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 785 | D->addAttr(::new (S.Context) |
| 786 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 787 | StartArg, Size, |
| 788 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 791 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 792 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 793 | SmallVector<Expr*, 2> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 794 | assert(!Attr.isInvalid()); |
| 795 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 796 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 797 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 798 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 799 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 800 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 801 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 802 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 805 | if (!isIntOrBool(Attr.getArg(0))) { |
| 806 | S.Diag(Attr.getLoc(), diag::err_attribute_first_argument_not_int_or_bool) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 807 | << Attr.getName(); |
| 808 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 812 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 813 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 814 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 815 | } |
| 816 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 817 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 818 | const AttributeList &Attr) { |
| 819 | SmallVector<Expr*, 2> Args; |
| 820 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 821 | return; |
| 822 | |
| 823 | unsigned Size = Args.size(); |
| 824 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 825 | D->addAttr(::new (S.Context) |
| 826 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 827 | Attr.getArg(0), StartArg, Size, |
| 828 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 831 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 832 | const AttributeList &Attr) { |
| 833 | SmallVector<Expr*, 2> Args; |
| 834 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 835 | return; |
| 836 | |
| 837 | unsigned Size = Args.size(); |
| 838 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 839 | D->addAttr(::new (S.Context) |
| 840 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
| 841 | Attr.getArg(0), StartArg, Size, |
| 842 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 845 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 846 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 847 | SmallVector<Expr*, 1> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 848 | assert(!Attr.isInvalid()); |
| 849 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 850 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 851 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 852 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 853 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 854 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 855 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 856 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 859 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 860 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 861 | if (Args.size() == 0) |
| 862 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 863 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 864 | return true; |
| 865 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 866 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 867 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 868 | const AttributeList &Attr) { |
| 869 | SmallVector<Expr*, 1> Args; |
| 870 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 871 | return; |
| 872 | |
| 873 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 874 | D->addAttr(::new (S.Context) |
| 875 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 876 | StartArg, Args.size(), |
| 877 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 880 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 881 | const AttributeList &Attr) { |
| 882 | SmallVector<Expr*, 1> Args; |
| 883 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 884 | return; |
| 885 | |
| 886 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 887 | D->addAttr(::new (S.Context) |
| 888 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 889 | StartArg, Args.size(), |
| 890 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 893 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 894 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 895 | assert(!Attr.isInvalid()); |
| 896 | |
| 897 | // zero or more arguments ok |
| 898 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 899 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 900 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 901 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 902 | return; |
| 903 | } |
| 904 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 905 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 906 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 907 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 908 | unsigned Size = Args.size(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 909 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 910 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 911 | D->addAttr(::new (S.Context) |
| 912 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 913 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 917 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 918 | assert(!Attr.isInvalid()); |
| 919 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 920 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 921 | return; |
| 922 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 923 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 924 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 925 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 926 | return; |
| 927 | } |
| 928 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 929 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 930 | SmallVector<Expr*, 1> Args; |
| 931 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 932 | unsigned Size = Args.size(); |
| 933 | if (Size == 0) |
| 934 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 935 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 936 | D->addAttr(::new (S.Context) |
| 937 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 938 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 942 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 943 | assert(!Attr.isInvalid()); |
| 944 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 945 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 946 | return; |
| 947 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 948 | if (!isa<FunctionDecl>(D) && !isa<FunctionTemplateDecl>(D)) { |
DeLesley Hutchins | 312e742 | 2012-06-19 23:25:19 +0000 | [diff] [blame] | 949 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_wrong_decl_type) |
| 950 | << Attr.getName() << ThreadExpectedFunctionOrMethod; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 951 | return; |
| 952 | } |
| 953 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 954 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 955 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 956 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 957 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 958 | if (Size == 0) |
| 959 | return; |
| 960 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 961 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 962 | D->addAttr(::new (S.Context) |
| 963 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 964 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 968 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 969 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 970 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 971 | if (TD == 0) { |
| 972 | // __attribute__((ext_vector_type(N))) can only be applied to typedefs |
| 973 | // and type-ids. |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 974 | S.Diag(Attr.getLoc(), diag::err_typecheck_ext_vector_not_typedef); |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 975 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 976 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 977 | |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 978 | // Remember this typedef decl, we will need it later for diagnostics. |
| 979 | S.ExtVectorDecls.push_back(TD); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 982 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 983 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 984 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 985 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 986 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 987 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 988 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 989 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 990 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 991 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 992 | if (!FD->getType()->isDependentType() && |
| 993 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 994 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 995 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 996 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 997 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 998 | FD->addAttr(::new (S.Context) |
| 999 | PackedAttr(Attr.getRange(), S.Context, |
| 1000 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1001 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1002 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1005 | static void handleMsStructAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 9ee2d047 | 2012-10-12 23:29:20 +0000 | [diff] [blame] | 1006 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1007 | RD->addAttr(::new (S.Context) |
| 1008 | MsStructAttr(Attr.getRange(), S.Context, |
| 1009 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 6b4e26b | 2011-04-26 17:54:40 +0000 | [diff] [blame] | 1010 | else |
| 1011 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1012 | } |
| 1013 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1014 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1015 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1016 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1017 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1018 | |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1019 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1020 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1021 | if (MD->isInstanceMethod()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1022 | D->addAttr(::new (S.Context) |
| 1023 | IBActionAttr(Attr.getRange(), S.Context, |
| 1024 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1025 | return; |
| 1026 | } |
| 1027 | |
Ted Kremenek | d68ec81 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1028 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1031 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1032 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1033 | // variables or properties of Objective-C classes. The outlet must also |
| 1034 | // have an object reference type. |
| 1035 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1036 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1037 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1038 | << Attr.getName() << VD->getType() << 0; |
| 1039 | return false; |
| 1040 | } |
| 1041 | } |
| 1042 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1043 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1044 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1045 | << Attr.getName() << PD->getType() << 1; |
| 1046 | return false; |
| 1047 | } |
| 1048 | } |
| 1049 | else { |
| 1050 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1051 | return false; |
| 1052 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1053 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1054 | return true; |
| 1055 | } |
| 1056 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1057 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1058 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1059 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1060 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1061 | |
| 1062 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1063 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1064 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1065 | D->addAttr(::new (S.Context) |
| 1066 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1067 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1070 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1071 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1072 | |
| 1073 | // The iboutletcollection attribute can have zero or one arguments. |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1074 | if (Attr.getParameterName() && Attr.getNumArgs() > 0) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1075 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1076 | return; |
| 1077 | } |
| 1078 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1079 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1080 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1081 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1082 | IdentifierInfo *II = Attr.getParameterName(); |
| 1083 | if (!II) |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1084 | II = &S.Context.Idents.get("NSObject"); |
Fariborz Jahanian | 798f832 | 2010-08-17 21:39:27 +0000 | [diff] [blame] | 1085 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1086 | ParsedType TypeRep = S.getTypeName(*II, Attr.getLoc(), |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1087 | S.getScopeForContext(D->getDeclContext()->getParent())); |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1088 | if (!TypeRep) { |
| 1089 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1090 | return; |
| 1091 | } |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1092 | QualType QT = TypeRep.get(); |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1093 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1094 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1095 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1096 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1097 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1098 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << II; |
| 1099 | return; |
| 1100 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1101 | D->addAttr(::new (S.Context) |
| 1102 | IBOutletCollectionAttr(Attr.getRange(),S.Context, |
| 1103 | QT, Attr.getParameterLoc(), |
| 1104 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1107 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1108 | if (const RecordType *UT = T->getAsUnionType()) |
| 1109 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1110 | RecordDecl *UD = UT->getDecl(); |
| 1111 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1112 | itend = UD->field_end(); it != itend; ++it) { |
| 1113 | QualType QT = it->getType(); |
| 1114 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1115 | T = QT; |
| 1116 | return; |
| 1117 | } |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1122 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | e881ce2 | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1123 | if (!isFunctionOrMethod(D)) { |
| 1124 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1125 | << "alloc_size" << ExpectedFunctionOrMethod; |
| 1126 | return; |
| 1127 | } |
| 1128 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1129 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1130 | return; |
| 1131 | |
| 1132 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1133 | // counted from one. |
| 1134 | bool HasImplicitThisParam = isInstanceMethod(D); |
Argyrios Kyrtzidis | e936505 | 2013-02-22 06:58:28 +0000 | [diff] [blame] | 1135 | unsigned NumArgs; |
| 1136 | if (hasFunctionProto(D)) |
| 1137 | NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
| 1138 | else |
| 1139 | NumArgs = 0; |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1140 | |
| 1141 | SmallVector<unsigned, 8> SizeArgs; |
| 1142 | |
| 1143 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1144 | E = Attr.arg_end(); I!=E; ++I) { |
| 1145 | // The argument must be an integer constant expression. |
| 1146 | Expr *Ex = *I; |
| 1147 | llvm::APSInt ArgNum; |
| 1148 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1149 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1150 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1151 | << "alloc_size" << Ex->getSourceRange(); |
| 1152 | return; |
| 1153 | } |
| 1154 | |
| 1155 | uint64_t x = ArgNum.getZExtValue(); |
| 1156 | |
| 1157 | if (x < 1 || x > NumArgs) { |
| 1158 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1159 | << "alloc_size" << I.getArgNum() << Ex->getSourceRange(); |
| 1160 | return; |
| 1161 | } |
| 1162 | |
| 1163 | --x; |
| 1164 | if (HasImplicitThisParam) { |
| 1165 | if (x == 0) { |
| 1166 | S.Diag(Attr.getLoc(), |
| 1167 | diag::err_attribute_invalid_implicit_this_argument) |
| 1168 | << "alloc_size" << Ex->getSourceRange(); |
| 1169 | return; |
| 1170 | } |
| 1171 | --x; |
| 1172 | } |
| 1173 | |
| 1174 | // check if the function argument is of an integer type |
| 1175 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
| 1176 | if (!T->isIntegerType()) { |
| 1177 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1178 | << "alloc_size" << Ex->getSourceRange(); |
| 1179 | return; |
| 1180 | } |
| 1181 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1182 | SizeArgs.push_back(x); |
| 1183 | } |
| 1184 | |
| 1185 | // check if the function returns a pointer |
| 1186 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1187 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
| 1188 | << "alloc_size" << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
| 1189 | } |
| 1190 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1191 | D->addAttr(::new (S.Context) |
| 1192 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1193 | SizeArgs.data(), SizeArgs.size(), |
| 1194 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1197 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1198 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1199 | // ignore it as well |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1200 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1201 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1202 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1203 | return; |
| 1204 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1205 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1206 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1207 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1208 | bool HasImplicitThisParam = isInstanceMethod(D); |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1209 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1210 | |
| 1211 | // The nonnull attribute only applies to pointers. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1212 | SmallVector<unsigned, 10> NonNullArgs; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1213 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1214 | for (AttributeList::arg_iterator I = Attr.arg_begin(), |
| 1215 | E = Attr.arg_end(); I != E; ++I) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1216 | // The argument must be an integer constant expression. |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1217 | Expr *Ex = *I; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1218 | llvm::APSInt ArgNum(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1219 | if (Ex->isTypeDependent() || Ex->isValueDependent() || |
| 1220 | !Ex->isIntegerConstantExpr(ArgNum, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1221 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 1222 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1223 | return; |
| 1224 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1225 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1226 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1227 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1228 | if (x < 1 || x > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1229 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 91aea71 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 1230 | << "nonnull" << I.getArgNum() << Ex->getSourceRange(); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1231 | return; |
| 1232 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1233 | |
Ted Kremenek | 5224e6a | 2008-07-21 22:09:15 +0000 | [diff] [blame] | 1234 | --x; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1235 | if (HasImplicitThisParam) { |
| 1236 | if (x == 0) { |
| 1237 | S.Diag(Attr.getLoc(), |
| 1238 | diag::err_attribute_invalid_implicit_this_argument) |
| 1239 | << "nonnull" << Ex->getSourceRange(); |
| 1240 | return; |
| 1241 | } |
| 1242 | --x; |
| 1243 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1244 | |
| 1245 | // Is the function argument a pointer type? |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1246 | QualType T = getFunctionOrMethodArgType(D, x).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1247 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1248 | |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1249 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1250 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | 62157e5 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1251 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1252 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1253 | continue; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1254 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1255 | |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1256 | NonNullArgs.push_back(x); |
| 1257 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1258 | |
| 1259 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1260 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1261 | if (NonNullArgs.empty()) { |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1262 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1263 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1264 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1265 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1266 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1267 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1268 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1269 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1270 | if (NonNullArgs.empty()) { |
| 1271 | // Warn the trivial case only if attribute is not coming from a |
| 1272 | // macro instantiation. |
| 1273 | if (Attr.getLoc().isFileID()) |
| 1274 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1275 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1276 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1277 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1278 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1279 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1280 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1281 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1282 | D->addAttr(::new (S.Context) |
| 1283 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1284 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1287 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1288 | // This attribute must be applied to a function declaration. |
| 1289 | // The first argument to the attribute must be a string, |
| 1290 | // the name of the resource, for example "malloc". |
| 1291 | // The following arguments must be argument indexes, the arguments must be |
| 1292 | // of integer type for Returns, otherwise of pointer type. |
| 1293 | // The difference between Holds and Takes is that a pointer may still be used |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1294 | // after being held. free() should be __attribute((ownership_takes)), whereas |
| 1295 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1296 | |
| 1297 | if (!AL.getParameterName()) { |
| 1298 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1299 | << AL.getName()->getName() << 1; |
| 1300 | return; |
| 1301 | } |
| 1302 | // Figure out our Kind, and check arguments while we're at it. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1303 | OwnershipAttr::OwnershipKind K; |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1304 | switch (AL.getKind()) { |
| 1305 | case AttributeList::AT_ownership_takes: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1306 | K = OwnershipAttr::Takes; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1307 | if (AL.getNumArgs() < 1) { |
| 1308 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1309 | return; |
| 1310 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1311 | break; |
| 1312 | case AttributeList::AT_ownership_holds: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1313 | K = OwnershipAttr::Holds; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1314 | if (AL.getNumArgs() < 1) { |
| 1315 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1316 | return; |
| 1317 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1318 | break; |
| 1319 | case AttributeList::AT_ownership_returns: |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1320 | K = OwnershipAttr::Returns; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1321 | if (AL.getNumArgs() > 1) { |
| 1322 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1323 | << AL.getNumArgs() + 1; |
| 1324 | return; |
| 1325 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1326 | break; |
| 1327 | default: |
| 1328 | // This should never happen given how we are called. |
| 1329 | llvm_unreachable("Unknown ownership attribute"); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1332 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1333 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1334 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1335 | return; |
| 1336 | } |
| 1337 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1338 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 1339 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1340 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 1341 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1342 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1343 | StringRef Module = AL.getParameterName()->getName(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1344 | |
| 1345 | // Normalize the argument, __foo__ becomes foo. |
| 1346 | if (Module.startswith("__") && Module.endswith("__")) |
| 1347 | Module = Module.substr(2, Module.size() - 4); |
| 1348 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1349 | SmallVector<unsigned, 10> OwnershipArgs; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1350 | |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1351 | for (AttributeList::arg_iterator I = AL.arg_begin(), E = AL.arg_end(); I != E; |
| 1352 | ++I) { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1353 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1354 | Expr *IdxExpr = *I; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1355 | llvm::APSInt ArgNum(32); |
| 1356 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1357 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1358 | S.Diag(AL.getLoc(), diag::err_attribute_argument_not_int) |
| 1359 | << AL.getName()->getName() << IdxExpr->getSourceRange(); |
| 1360 | continue; |
| 1361 | } |
| 1362 | |
| 1363 | unsigned x = (unsigned) ArgNum.getZExtValue(); |
| 1364 | |
| 1365 | if (x > NumArgs || x < 1) { |
| 1366 | S.Diag(AL.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 1367 | << AL.getName()->getName() << x << IdxExpr->getSourceRange(); |
| 1368 | continue; |
| 1369 | } |
| 1370 | --x; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1371 | if (HasImplicitThisParam) { |
| 1372 | if (x == 0) { |
| 1373 | S.Diag(AL.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 1374 | << "ownership" << IdxExpr->getSourceRange(); |
| 1375 | return; |
| 1376 | } |
| 1377 | --x; |
| 1378 | } |
| 1379 | |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1380 | switch (K) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1381 | case OwnershipAttr::Takes: |
| 1382 | case OwnershipAttr::Holds: { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1383 | // Is the function argument a pointer type? |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1384 | QualType T = getFunctionOrMethodArgType(D, x); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1385 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
| 1386 | // FIXME: Should also highlight argument in decl. |
| 1387 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1388 | << ((K==OwnershipAttr::Takes)?"ownership_takes":"ownership_holds") |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1389 | << "pointer" |
| 1390 | << IdxExpr->getSourceRange(); |
| 1391 | continue; |
| 1392 | } |
| 1393 | break; |
| 1394 | } |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1395 | case OwnershipAttr::Returns: { |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1396 | if (AL.getNumArgs() > 1) { |
| 1397 | // Is the function argument an integer type? |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1398 | Expr *IdxExpr = AL.getArg(0); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1399 | llvm::APSInt ArgNum(32); |
| 1400 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() |
| 1401 | || !IdxExpr->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 1402 | S.Diag(AL.getLoc(), diag::err_ownership_type) |
| 1403 | << "ownership_returns" << "integer" |
| 1404 | << IdxExpr->getSourceRange(); |
| 1405 | return; |
| 1406 | } |
| 1407 | } |
| 1408 | break; |
| 1409 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1410 | } // switch |
| 1411 | |
| 1412 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1413 | for (specific_attr_iterator<OwnershipAttr> |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1414 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1415 | e = D->specific_attr_end<OwnershipAttr>(); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1416 | i != e; ++i) { |
| 1417 | if ((*i)->getOwnKind() != K) { |
| 1418 | for (const unsigned *I = (*i)->args_begin(), *E = (*i)->args_end(); |
| 1419 | I!=E; ++I) { |
| 1420 | if (x == *I) { |
| 1421 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1422 | << AL.getName()->getName() << "ownership_*"; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | } |
| 1426 | } |
| 1427 | OwnershipArgs.push_back(x); |
| 1428 | } |
| 1429 | |
| 1430 | unsigned* start = OwnershipArgs.data(); |
| 1431 | unsigned size = OwnershipArgs.size(); |
| 1432 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1433 | |
| 1434 | if (K != OwnershipAttr::Returns && OwnershipArgs.empty()) { |
| 1435 | S.Diag(AL.getLoc(), diag::err_attribute_wrong_number_arguments) << 2; |
| 1436 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1437 | } |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1438 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1439 | D->addAttr(::new (S.Context) |
| 1440 | OwnershipAttr(AL.getLoc(), S.Context, K, Module, start, size, |
| 1441 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1444 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1445 | // Check the attribute arguments. |
| 1446 | if (Attr.getNumArgs() > 1) { |
| 1447 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1448 | return; |
| 1449 | } |
| 1450 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1451 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1452 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1453 | << Attr.getName() << ExpectedVariableOrFunction; |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1454 | return; |
| 1455 | } |
| 1456 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1457 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1458 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1459 | // gcc rejects |
| 1460 | // class c { |
| 1461 | // static int a __attribute__((weakref ("v2"))); |
| 1462 | // static int b() __attribute__((weakref ("f3"))); |
| 1463 | // }; |
| 1464 | // and ignores the attributes of |
| 1465 | // void f(void) { |
| 1466 | // static int a __attribute__((weakref ("v2"))); |
| 1467 | // } |
| 1468 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1469 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1470 | if (!Ctx->isFileContext()) { |
| 1471 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1472 | nd->getNameAsString(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1473 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | // The GCC manual says |
| 1477 | // |
| 1478 | // At present, a declaration to which `weakref' is attached can only |
| 1479 | // be `static'. |
| 1480 | // |
| 1481 | // It also says |
| 1482 | // |
| 1483 | // Without a TARGET, |
| 1484 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1485 | // equivalent to `weak'. |
| 1486 | // |
| 1487 | // gcc 4.4.1 will accept |
| 1488 | // int a7 __attribute__((weakref)); |
| 1489 | // as |
| 1490 | // int a7 __attribute__((weak)); |
| 1491 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1492 | // it if this behaviour is actually used. |
| 1493 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1494 | // GCC rejects |
| 1495 | // static ((alias ("y"), weakref)). |
| 1496 | // Should we? How to check that weakref is before or after alias? |
| 1497 | |
| 1498 | if (Attr.getNumArgs() == 1) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1499 | Expr *Arg = Attr.getArg(0); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1500 | Arg = Arg->IgnoreParenCasts(); |
| 1501 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1502 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1503 | if (!Str || !Str->isAscii()) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1504 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 1505 | << "weakref" << 1; |
| 1506 | return; |
| 1507 | } |
| 1508 | // GCC will accept anything as the argument of weakref. Should we |
| 1509 | // check for an existing decl? |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1510 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1511 | Str->getString())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1514 | D->addAttr(::new (S.Context) |
| 1515 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1516 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1519 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1520 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 1521 | if (Attr.getNumArgs() != 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1522 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1523 | return; |
| 1524 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1525 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1526 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1527 | Arg = Arg->IgnoreParenCasts(); |
| 1528 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1529 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1530 | if (!Str || !Str->isAscii()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1531 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1532 | << "alias" << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1533 | return; |
| 1534 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1535 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1536 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1537 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1538 | return; |
| 1539 | } |
| 1540 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1541 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1542 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1543 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1544 | Str->getString(), |
| 1545 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1548 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1549 | // Check the attribute arguments. |
| 1550 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1551 | return; |
| 1552 | |
| 1553 | if (!isa<FunctionDecl>(D) && !isa<ObjCMethodDecl>(D)) { |
| 1554 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1555 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1556 | return; |
| 1557 | } |
| 1558 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1559 | D->addAttr(::new (S.Context) |
| 1560 | MinSizeAttr(Attr.getRange(), S.Context, |
| 1561 | Attr.getAttributeSpellingListIndex())); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1564 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1565 | // Check the attribute arguments. |
| 1566 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1567 | return; |
| 1568 | |
| 1569 | if (!isa<FunctionDecl>(D)) { |
| 1570 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1571 | << Attr.getName() << ExpectedFunction; |
| 1572 | return; |
| 1573 | } |
| 1574 | |
| 1575 | if (D->hasAttr<HotAttr>()) { |
| 1576 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1577 | << Attr.getName() << "hot"; |
| 1578 | return; |
| 1579 | } |
| 1580 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1581 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1582 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1586 | // Check the attribute arguments. |
| 1587 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 1588 | return; |
| 1589 | |
| 1590 | if (!isa<FunctionDecl>(D)) { |
| 1591 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1592 | << Attr.getName() << ExpectedFunction; |
| 1593 | return; |
| 1594 | } |
| 1595 | |
| 1596 | if (D->hasAttr<ColdAttr>()) { |
| 1597 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1598 | << Attr.getName() << "cold"; |
| 1599 | return; |
| 1600 | } |
| 1601 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1602 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1603 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1606 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1607 | // Check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1608 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1609 | return; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1610 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1611 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1612 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1613 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1614 | return; |
| 1615 | } |
| 1616 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1617 | D->addAttr(::new (S.Context) |
| 1618 | NakedAttr(Attr.getRange(), S.Context, |
| 1619 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1622 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 1623 | const AttributeList &Attr) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1624 | // Check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1625 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1626 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1627 | return; |
| 1628 | } |
| 1629 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1630 | if (!isa<FunctionDecl>(D)) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1631 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1632 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 1633 | return; |
| 1634 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1635 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1636 | D->addAttr(::new (S.Context) |
| 1637 | AlwaysInlineAttr(Attr.getRange(), S.Context, |
| 1638 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 03a3844 | 2008-10-28 00:17:57 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1641 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1642 | const AttributeList &Attr) { |
| 1643 | // Check the attribute arguments. |
| 1644 | if (Attr.getNumArgs() != 1) { |
| 1645 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 1646 | return; |
| 1647 | } |
| 1648 | |
| 1649 | Expr *Arg = Attr.getArg(0); |
| 1650 | Arg = Arg->IgnoreParenCasts(); |
| 1651 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
| 1652 | |
| 1653 | // Check that it is a string. |
| 1654 | if (!Str) { |
| 1655 | S.Diag(Attr.getLoc(), diag::err_attribute_not_string) << "tls_model"; |
| 1656 | return; |
| 1657 | } |
| 1658 | |
| 1659 | if (!isa<VarDecl>(D) || !cast<VarDecl>(D)->isThreadSpecified()) { |
| 1660 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1661 | << Attr.getName() << ExpectedTLSVar; |
| 1662 | return; |
| 1663 | } |
| 1664 | |
| 1665 | // Check that the value. |
| 1666 | StringRef Model = Str->getString(); |
| 1667 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1668 | && Model != "initial-exec" && Model != "local-exec") { |
| 1669 | S.Diag(Attr.getLoc(), diag::err_attr_tlsmodel_arg); |
| 1670 | return; |
| 1671 | } |
| 1672 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1673 | D->addAttr(::new (S.Context) |
| 1674 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1675 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1678 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 8caf641 | 2010-09-29 18:20:25 +0000 | [diff] [blame] | 1679 | // Check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1680 | if (Attr.hasParameterOrArguments()) { |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1681 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1682 | return; |
| 1683 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1684 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1685 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1686 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1687 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1688 | D->addAttr(::new (S.Context) |
| 1689 | MallocAttr(Attr.getRange(), S.Context, |
| 1690 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1691 | return; |
| 1692 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1695 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1698 | static void handleMayAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1699 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1700 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1701 | return; |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1702 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1703 | D->addAttr(::new (S.Context) |
| 1704 | MayAliasAttr(Attr.getRange(), S.Context, |
| 1705 | Attr.getAttributeSpellingListIndex())); |
Dan Gohman | bbb7d62 | 2010-11-17 00:03:07 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1708 | static void handleNoCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 9312c64 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1709 | assert(!Attr.isInvalid()); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1710 | if (isa<VarDecl>(D)) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1711 | D->addAttr(::new (S.Context) |
| 1712 | NoCommonAttr(Attr.getRange(), S.Context, |
| 1713 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1714 | else |
| 1715 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1716 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1719 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | 9312c64 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 1720 | assert(!Attr.isInvalid()); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1721 | if (isa<VarDecl>(D)) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1722 | D->addAttr(::new (S.Context) |
| 1723 | CommonAttr(Attr.getRange(), S.Context, |
| 1724 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 515d87f | 2010-12-03 06:58:14 +0000 | [diff] [blame] | 1725 | else |
| 1726 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1727 | << Attr.getName() << ExpectedVariable; |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1730 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1731 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1732 | |
| 1733 | if (S.CheckNoReturnAttr(attr)) return; |
| 1734 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1735 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1736 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1737 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1738 | return; |
| 1739 | } |
| 1740 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1741 | D->addAttr(::new (S.Context) |
| 1742 | NoReturnAttr(attr.getRange(), S.Context, |
| 1743 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
| 1746 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1747 | if (attr.hasParameterOrArguments()) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1748 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1749 | attr.setInvalid(); |
| 1750 | return true; |
| 1751 | } |
| 1752 | |
| 1753 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1756 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1757 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1758 | |
| 1759 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1760 | // because 'analyzer_noreturn' does not impact the type. |
| 1761 | |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 1762 | if(!checkAttributeNumArgs(S, Attr, 0)) |
| 1763 | return; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1764 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1765 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1766 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1767 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1768 | && !VD->getType()->isFunctionPointerType())) { |
| 1769 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1770 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1771 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1772 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1773 | return; |
| 1774 | } |
| 1775 | } |
| 1776 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1777 | D->addAttr(::new (S.Context) |
| 1778 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1779 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1782 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1783 | const AttributeList &Attr) { |
| 1784 | // C++11 [dcl.attr.noreturn]p1: |
| 1785 | // The attribute may be applied to the declarator-id in a function |
| 1786 | // declaration. |
| 1787 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1788 | if (!FD) { |
| 1789 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1790 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1791 | return; |
| 1792 | } |
| 1793 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1794 | D->addAttr(::new (S.Context) |
| 1795 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1796 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1799 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1800 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1801 | /* |
| 1802 | Returning a Vector Class in Registers |
| 1803 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1804 | According to the PPU ABI specifications, a class with a single member of |
| 1805 | vector type is returned in memory when used as the return value of a function. |
| 1806 | This results in inefficient code when implementing vector classes. To return |
| 1807 | the value in a single vector register, add the vecreturn attribute to the |
| 1808 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1809 | |
| 1810 | Example: |
| 1811 | |
| 1812 | struct Vector |
| 1813 | { |
| 1814 | __vector float xyzw; |
| 1815 | } __attribute__((vecreturn)); |
| 1816 | |
| 1817 | Vector Add(Vector lhs, Vector rhs) |
| 1818 | { |
| 1819 | Vector result; |
| 1820 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1821 | return result; // This will be returned in a register |
| 1822 | } |
| 1823 | */ |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1824 | if (!isa<RecordDecl>(D)) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1825 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1826 | << Attr.getName() << ExpectedClass; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1827 | return; |
| 1828 | } |
| 1829 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1830 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1831 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1832 | return; |
| 1833 | } |
| 1834 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1835 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1836 | int count = 0; |
| 1837 | |
| 1838 | if (!isa<CXXRecordDecl>(record)) { |
| 1839 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1840 | return; |
| 1841 | } |
| 1842 | |
| 1843 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1844 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1845 | return; |
| 1846 | } |
| 1847 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1848 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1849 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1850 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1851 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1852 | return; |
| 1853 | } |
| 1854 | count++; |
| 1855 | } |
| 1856 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1857 | D->addAttr(::new (S.Context) |
| 1858 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1859 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1862 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1863 | const AttributeList &Attr) { |
| 1864 | if (isa<ParmVarDecl>(D)) { |
| 1865 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1866 | // parameter of a function declaration or lambda. |
| 1867 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1868 | S.Diag(Attr.getLoc(), |
| 1869 | diag::err_carries_dependency_param_not_function_decl); |
| 1870 | return; |
| 1871 | } |
| 1872 | } else if (!isa<FunctionDecl>(D)) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1873 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1874 | << Attr.getName() << ExpectedFunctionMethodOrParameter; |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1875 | return; |
| 1876 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1877 | |
| 1878 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1879 | Attr.getRange(), S.Context, |
| 1880 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1883 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1884 | // check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1885 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1886 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1887 | return; |
| 1888 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1889 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1890 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 429c134 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1891 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1892 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1893 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1894 | return; |
| 1895 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1896 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1897 | D->addAttr(::new (S.Context) |
| 1898 | UnusedAttr(Attr.getRange(), S.Context, |
| 1899 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1900 | } |
| 1901 | |
Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1902 | static void handleReturnsTwiceAttr(Sema &S, Decl *D, |
| 1903 | const AttributeList &Attr) { |
| 1904 | // check the attribute arguments. |
| 1905 | if (Attr.hasParameterOrArguments()) { |
| 1906 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1907 | return; |
| 1908 | } |
| 1909 | |
| 1910 | if (!isa<FunctionDecl>(D)) { |
| 1911 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1912 | << Attr.getName() << ExpectedFunction; |
| 1913 | return; |
| 1914 | } |
| 1915 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1916 | D->addAttr(::new (S.Context) |
| 1917 | ReturnsTwiceAttr(Attr.getRange(), S.Context, |
| 1918 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1921 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1922 | // check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 1923 | if (Attr.hasParameterOrArguments()) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1924 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 1925 | return; |
| 1926 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1927 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1928 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Daniel Dunbar | 311bf29 | 2009-02-13 22:48:56 +0000 | [diff] [blame] | 1929 | if (VD->hasLocalStorage() || VD->hasExternalStorage()) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1930 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1931 | return; |
| 1932 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1933 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1934 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1935 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1936 | return; |
| 1937 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1938 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1939 | D->addAttr(::new (S.Context) |
| 1940 | UsedAttr(Attr.getRange(), S.Context, |
| 1941 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1944 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1945 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1946 | if (Attr.getNumArgs() > 1) { |
| 1947 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1948 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1949 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1950 | |
| 1951 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1952 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1953 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1954 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1955 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1956 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1957 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1958 | << "constructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1959 | return; |
| 1960 | } |
| 1961 | priority = Idx.getZExtValue(); |
| 1962 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1963 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1964 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1965 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1966 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1967 | return; |
| 1968 | } |
| 1969 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1970 | D->addAttr(::new (S.Context) |
| 1971 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1972 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1975 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1976 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1977 | if (Attr.getNumArgs() > 1) { |
| 1978 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1979 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1980 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1981 | |
| 1982 | int priority = 65535; // FIXME: Do not hardcode such constants. |
| 1983 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 1984 | Expr *E = Attr.getArg(0); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1985 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 1986 | if (E->isTypeDependent() || E->isValueDependent() || |
| 1987 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1988 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1989 | << "destructor" << 1 << E->getSourceRange(); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1990 | return; |
| 1991 | } |
| 1992 | priority = Idx.getZExtValue(); |
| 1993 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1994 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1995 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1996 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1997 | << Attr.getName() << ExpectedFunction; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1998 | return; |
| 1999 | } |
| 2000 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2001 | D->addAttr(::new (S.Context) |
| 2002 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2003 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2004 | } |
| 2005 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2006 | template <typename AttrTy> |
| 2007 | static void handleAttrWithMessage(Sema &S, Decl *D, const AttributeList &Attr, |
| 2008 | const char *Name) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2009 | unsigned NumArgs = Attr.getNumArgs(); |
| 2010 | if (NumArgs > 1) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2011 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2012 | return; |
| 2013 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2014 | |
| 2015 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2016 | StringRef Str; |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2017 | if (NumArgs == 1) { |
| 2018 | StringLiteral *SE = dyn_cast<StringLiteral>(Attr.getArg(0)); |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2019 | if (!SE) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2020 | S.Diag(Attr.getArg(0)->getLocStart(), diag::err_attribute_not_string) |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2021 | << Name; |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 2022 | return; |
| 2023 | } |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 2024 | Str = SE->getString(); |
Fariborz Jahanian | 55106310 | 2010-10-06 21:18:44 +0000 | [diff] [blame] | 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) AttrTy(Attr.getRange(), S.Context, Str, |
| 2028 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2031 | static void handleArcWeakrefUnavailableAttr(Sema &S, Decl *D, |
| 2032 | const AttributeList &Attr) { |
| 2033 | unsigned NumArgs = Attr.getNumArgs(); |
| 2034 | if (NumArgs > 0) { |
| 2035 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 2036 | return; |
| 2037 | } |
| 2038 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2039 | D->addAttr(::new (S.Context) |
| 2040 | ArcWeakrefUnavailableAttr(Attr.getRange(), S.Context, |
| 2041 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2044 | static void handleObjCRootClassAttr(Sema &S, Decl *D, |
| 2045 | const AttributeList &Attr) { |
| 2046 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2047 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 2048 | return; |
| 2049 | } |
| 2050 | |
| 2051 | unsigned NumArgs = Attr.getNumArgs(); |
| 2052 | if (NumArgs > 0) { |
| 2053 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 2054 | return; |
| 2055 | } |
| 2056 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2057 | D->addAttr(::new (S.Context) |
| 2058 | ObjCRootClassAttr(Attr.getRange(), S.Context, |
| 2059 | Attr.getAttributeSpellingListIndex())); |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 2060 | } |
| 2061 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2062 | static void handleObjCRequiresPropertyDefsAttr(Sema &S, Decl *D, |
| 2063 | const AttributeList &Attr) { |
Fariborz Jahanian | 7249e36 | 2012-01-03 22:52:32 +0000 | [diff] [blame] | 2064 | if (!isa<ObjCInterfaceDecl>(D)) { |
| 2065 | S.Diag(Attr.getLoc(), diag::err_suppress_autosynthesis); |
| 2066 | return; |
| 2067 | } |
| 2068 | |
Fariborz Jahanian | 9d4d20a | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2069 | unsigned NumArgs = Attr.getNumArgs(); |
| 2070 | if (NumArgs > 0) { |
| 2071 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 0; |
| 2072 | return; |
| 2073 | } |
| 2074 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2075 | D->addAttr(::new (S.Context) |
| 2076 | ObjCRequiresPropertyDefsAttr(Attr.getRange(), S.Context, |
| 2077 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 9d4d20a | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 2078 | } |
| 2079 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2080 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2081 | IdentifierInfo *Platform, |
| 2082 | VersionTuple Introduced, |
| 2083 | VersionTuple Deprecated, |
| 2084 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2085 | StringRef PlatformName |
| 2086 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2087 | if (PlatformName.empty()) |
| 2088 | PlatformName = Platform->getName(); |
| 2089 | |
| 2090 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2091 | // of these steps are needed). |
| 2092 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2093 | !(Introduced <= Deprecated)) { |
| 2094 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2095 | << 1 << PlatformName << Deprecated.getAsString() |
| 2096 | << 0 << Introduced.getAsString(); |
| 2097 | return true; |
| 2098 | } |
| 2099 | |
| 2100 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2101 | !(Introduced <= Obsoleted)) { |
| 2102 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2103 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2104 | << 0 << Introduced.getAsString(); |
| 2105 | return true; |
| 2106 | } |
| 2107 | |
| 2108 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2109 | !(Deprecated <= Obsoleted)) { |
| 2110 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2111 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2112 | << 1 << Deprecated.getAsString(); |
| 2113 | return true; |
| 2114 | } |
| 2115 | |
| 2116 | return false; |
| 2117 | } |
| 2118 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2119 | /// \brief Check whether the two versions match. |
| 2120 | /// |
| 2121 | /// If either version tuple is empty, then they are assumed to match. If |
| 2122 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2123 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2124 | bool BeforeIsOkay) { |
| 2125 | if (X.empty() || Y.empty()) |
| 2126 | return true; |
| 2127 | |
| 2128 | if (X == Y) |
| 2129 | return true; |
| 2130 | |
| 2131 | if (BeforeIsOkay && X < Y) |
| 2132 | return true; |
| 2133 | |
| 2134 | return false; |
| 2135 | } |
| 2136 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2137 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2138 | IdentifierInfo *Platform, |
| 2139 | VersionTuple Introduced, |
| 2140 | VersionTuple Deprecated, |
| 2141 | VersionTuple Obsoleted, |
| 2142 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2143 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2144 | bool Override, |
| 2145 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2146 | VersionTuple MergedIntroduced = Introduced; |
| 2147 | VersionTuple MergedDeprecated = Deprecated; |
| 2148 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2149 | bool FoundAny = false; |
| 2150 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2151 | if (D->hasAttrs()) { |
| 2152 | AttrVec &Attrs = D->getAttrs(); |
| 2153 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2154 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2155 | if (!OldAA) { |
| 2156 | ++i; |
| 2157 | continue; |
| 2158 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2159 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2160 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2161 | if (OldPlatform != Platform) { |
| 2162 | ++i; |
| 2163 | continue; |
| 2164 | } |
| 2165 | |
| 2166 | FoundAny = true; |
| 2167 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2168 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2169 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2170 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2171 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2172 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 2173 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 2174 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 2175 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 2176 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2177 | if (Override) { |
| 2178 | int Which = -1; |
| 2179 | VersionTuple FirstVersion; |
| 2180 | VersionTuple SecondVersion; |
| 2181 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 2182 | Which = 0; |
| 2183 | FirstVersion = OldIntroduced; |
| 2184 | SecondVersion = Introduced; |
| 2185 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 2186 | Which = 1; |
| 2187 | FirstVersion = Deprecated; |
| 2188 | SecondVersion = OldDeprecated; |
| 2189 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 2190 | Which = 2; |
| 2191 | FirstVersion = Obsoleted; |
| 2192 | SecondVersion = OldObsoleted; |
| 2193 | } |
| 2194 | |
| 2195 | if (Which == -1) { |
| 2196 | Diag(OldAA->getLocation(), |
| 2197 | diag::warn_mismatched_availability_override_unavail) |
| 2198 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2199 | } else { |
| 2200 | Diag(OldAA->getLocation(), |
| 2201 | diag::warn_mismatched_availability_override) |
| 2202 | << Which |
| 2203 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2204 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 2205 | } |
| 2206 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2207 | } else { |
| 2208 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2209 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2210 | } |
| 2211 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2212 | Attrs.erase(Attrs.begin() + i); |
| 2213 | --e; |
| 2214 | continue; |
| 2215 | } |
| 2216 | |
| 2217 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2218 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2219 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2220 | |
| 2221 | if (MergedIntroduced2.empty()) |
| 2222 | MergedIntroduced2 = OldIntroduced; |
| 2223 | if (MergedDeprecated2.empty()) |
| 2224 | MergedDeprecated2 = OldDeprecated; |
| 2225 | if (MergedObsoleted2.empty()) |
| 2226 | MergedObsoleted2 = OldObsoleted; |
| 2227 | |
| 2228 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2229 | MergedIntroduced2, MergedDeprecated2, |
| 2230 | MergedObsoleted2)) { |
| 2231 | Attrs.erase(Attrs.begin() + i); |
| 2232 | --e; |
| 2233 | continue; |
| 2234 | } |
| 2235 | |
| 2236 | MergedIntroduced = MergedIntroduced2; |
| 2237 | MergedDeprecated = MergedDeprecated2; |
| 2238 | MergedObsoleted = MergedObsoleted2; |
| 2239 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2240 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | if (FoundAny && |
| 2244 | MergedIntroduced == Introduced && |
| 2245 | MergedDeprecated == Deprecated && |
| 2246 | MergedObsoleted == Obsoleted) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2247 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2248 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2249 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2250 | MergedDeprecated, MergedObsoleted)) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2251 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2252 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2253 | Obsoleted, IsUnavailable, Message, |
| 2254 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2255 | } |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2256 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2259 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2260 | const AttributeList &Attr) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2261 | IdentifierInfo *Platform = Attr.getParameterName(); |
| 2262 | SourceLocation PlatformLoc = Attr.getParameterLoc(); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2263 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2264 | |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2265 | if (AvailabilityAttr::getPrettyPlatformName(Platform->getName()).empty()) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2266 | S.Diag(PlatformLoc, diag::warn_availability_unknown_platform) |
| 2267 | << Platform; |
| 2268 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2269 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2270 | if (!ND) { |
| 2271 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2272 | return; |
| 2273 | } |
| 2274 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2275 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2276 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2277 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2278 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2279 | StringRef Str; |
| 2280 | const StringLiteral *SE = |
| 2281 | dyn_cast_or_null<const StringLiteral>(Attr.getMessageExpr()); |
| 2282 | if (SE) |
| 2283 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2284 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2285 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2286 | Platform, |
| 2287 | Introduced.Version, |
| 2288 | Deprecated.Version, |
| 2289 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2290 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2291 | /*Override=*/false, |
| 2292 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2293 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2294 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2297 | template <class T> |
| 2298 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2299 | typename T::VisibilityType value, |
| 2300 | unsigned attrSpellingListIndex) { |
| 2301 | T *existingAttr = D->getAttr<T>(); |
| 2302 | if (existingAttr) { |
| 2303 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2304 | if (existingValue == value) |
| 2305 | return NULL; |
| 2306 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2307 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2308 | D->dropAttr<T>(); |
| 2309 | } |
| 2310 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2311 | } |
| 2312 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2313 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2314 | VisibilityAttr::VisibilityType Vis, |
| 2315 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2316 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2317 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2320 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2321 | TypeVisibilityAttr::VisibilityType Vis, |
| 2322 | unsigned AttrSpellingListIndex) { |
| 2323 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2324 | AttrSpellingListIndex); |
| 2325 | } |
| 2326 | |
| 2327 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2328 | bool isTypeVisibility) { |
| 2329 | // Visibility attributes don't mean anything on a typedef. |
| 2330 | if (isa<TypedefNameDecl>(D)) { |
| 2331 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2332 | << Attr.getName(); |
| 2333 | return; |
| 2334 | } |
| 2335 | |
| 2336 | // 'type_visibility' can only go on a type or namespace. |
| 2337 | if (isTypeVisibility && |
| 2338 | !(isa<TagDecl>(D) || |
| 2339 | isa<ObjCInterfaceDecl>(D) || |
| 2340 | isa<NamespaceDecl>(D))) { |
| 2341 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2342 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2343 | return; |
| 2344 | } |
| 2345 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2346 | // check the attribute arguments. |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2347 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2348 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2349 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2350 | Expr *Arg = Attr.getArg(0); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2351 | Arg = Arg->IgnoreParenCasts(); |
| 2352 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2353 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2354 | if (!Str || !Str->isAscii()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2355 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2356 | << (isTypeVisibility ? "type_visibility" : "visibility") << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2357 | return; |
| 2358 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2359 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2360 | StringRef TypeStr = Str->getString(); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2361 | VisibilityAttr::VisibilityType type; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2362 | |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2363 | if (TypeStr == "default") |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2364 | type = VisibilityAttr::Default; |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2365 | else if (TypeStr == "hidden") |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2366 | type = VisibilityAttr::Hidden; |
Benjamin Kramer | 12a6ce7 | 2010-01-23 18:16:35 +0000 | [diff] [blame] | 2367 | else if (TypeStr == "internal") |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2368 | type = VisibilityAttr::Hidden; // FIXME |
John McCall | eed64c7 | 2012-01-29 01:20:30 +0000 | [diff] [blame] | 2369 | else if (TypeStr == "protected") { |
| 2370 | // Complain about attempts to use protected visibility on targets |
| 2371 | // (like Darwin) that don't support it. |
| 2372 | if (!S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2373 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2374 | type = VisibilityAttr::Default; |
| 2375 | } else { |
| 2376 | type = VisibilityAttr::Protected; |
| 2377 | } |
| 2378 | } else { |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 2379 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_visibility) << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2380 | return; |
| 2381 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2382 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2383 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2384 | clang::Attr *newAttr; |
| 2385 | if (isTypeVisibility) { |
| 2386 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2387 | (TypeVisibilityAttr::VisibilityType) type, |
| 2388 | Index); |
| 2389 | } else { |
| 2390 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2391 | } |
| 2392 | if (newAttr) |
| 2393 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2396 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2397 | const AttributeList &Attr) { |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2398 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(decl); |
| 2399 | if (!method) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2400 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2401 | << ExpectedMethod; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2402 | return; |
| 2403 | } |
| 2404 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2405 | if (Attr.getNumArgs() != 0 || !Attr.getParameterName()) { |
| 2406 | if (!Attr.getParameterName() && Attr.getNumArgs() == 1) { |
| 2407 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2408 | << "objc_method_family" << 1; |
| 2409 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2410 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2411 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2412 | Attr.setInvalid(); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2413 | return; |
| 2414 | } |
| 2415 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2416 | StringRef param = Attr.getParameterName()->getName(); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2417 | ObjCMethodFamilyAttr::FamilyKind family; |
| 2418 | if (param == "none") |
| 2419 | family = ObjCMethodFamilyAttr::OMF_None; |
| 2420 | else if (param == "alloc") |
| 2421 | family = ObjCMethodFamilyAttr::OMF_alloc; |
| 2422 | else if (param == "copy") |
| 2423 | family = ObjCMethodFamilyAttr::OMF_copy; |
| 2424 | else if (param == "init") |
| 2425 | family = ObjCMethodFamilyAttr::OMF_init; |
| 2426 | else if (param == "mutableCopy") |
| 2427 | family = ObjCMethodFamilyAttr::OMF_mutableCopy; |
| 2428 | else if (param == "new") |
| 2429 | family = ObjCMethodFamilyAttr::OMF_new; |
| 2430 | else { |
| 2431 | // Just warn and ignore it. This is future-proof against new |
| 2432 | // families being used in system headers. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2433 | S.Diag(Attr.getParameterLoc(), diag::warn_unknown_method_family); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2434 | return; |
| 2435 | } |
| 2436 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2437 | if (family == ObjCMethodFamilyAttr::OMF_init && |
| 2438 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2439 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2440 | << method->getResultType(); |
| 2441 | // Ignore the attribute. |
| 2442 | return; |
| 2443 | } |
| 2444 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2445 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2446 | S.Context, family)); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2449 | static void handleObjCExceptionAttr(Sema &S, Decl *D, |
| 2450 | const AttributeList &Attr) { |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2451 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2452 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2453 | |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2454 | ObjCInterfaceDecl *OCI = dyn_cast<ObjCInterfaceDecl>(D); |
| 2455 | if (OCI == 0) { |
| 2456 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_objc_interface); |
| 2457 | return; |
| 2458 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2459 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2460 | D->addAttr(::new (S.Context) |
| 2461 | ObjCExceptionAttr(Attr.getRange(), S.Context, |
| 2462 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2465 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2466 | if (Attr.getNumArgs() != 0) { |
John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2467 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2468 | return; |
| 2469 | } |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2470 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2471 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2472 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2473 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2474 | return; |
| 2475 | } |
| 2476 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2477 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2478 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2479 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2480 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2481 | return; |
| 2482 | } |
| 2483 | } |
| 2484 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2485 | // It is okay to include this attribute on properties, e.g.: |
| 2486 | // |
| 2487 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2488 | // |
| 2489 | // In this case it follows tradition and suppresses an error in the above |
| 2490 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2491 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2492 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2493 | D->addAttr(::new (S.Context) |
| 2494 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2495 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2496 | } |
| 2497 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2498 | static void |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2499 | handleOverloadableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2500 | if (Attr.getNumArgs() != 0) { |
John McCall | 61d8258 | 2010-05-28 18:25:28 +0000 | [diff] [blame] | 2501 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2502 | return; |
| 2503 | } |
| 2504 | |
| 2505 | if (!isa<FunctionDecl>(D)) { |
| 2506 | S.Diag(Attr.getLoc(), diag::err_attribute_overloadable_not_function); |
| 2507 | return; |
| 2508 | } |
| 2509 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2510 | D->addAttr(::new (S.Context) |
| 2511 | OverloadableAttr(Attr.getRange(), S.Context, |
| 2512 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4e5cbdc | 2009-02-11 23:02:49 +0000 | [diff] [blame] | 2513 | } |
| 2514 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2515 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2516 | if (!Attr.getParameterName()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2517 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2518 | << "blocks" << 1; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2519 | return; |
| 2520 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2521 | |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2522 | if (Attr.getNumArgs() != 0) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2523 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2524 | return; |
| 2525 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2526 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2527 | BlocksAttr::BlockType type; |
Chris Lattner | 68e4868 | 2008-11-20 04:42:34 +0000 | [diff] [blame] | 2528 | if (Attr.getParameterName()->isStr("byref")) |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2529 | type = BlocksAttr::ByRef; |
| 2530 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2531 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2532 | << "blocks" << Attr.getParameterName(); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2533 | return; |
| 2534 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2535 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2536 | D->addAttr(::new (S.Context) |
| 2537 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2538 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2541 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2542 | // check the attribute arguments. |
| 2543 | if (Attr.getNumArgs() > 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2544 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2545 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2548 | unsigned sentinel = 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2549 | if (Attr.getNumArgs() > 0) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2550 | Expr *E = Attr.getArg(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2551 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2552 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2553 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2554 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2555 | << "sentinel" << 1 << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2556 | return; |
| 2557 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2558 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2559 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2560 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2561 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2562 | return; |
| 2563 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2564 | |
| 2565 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2566 | } |
| 2567 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2568 | unsigned nullPos = 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2569 | if (Attr.getNumArgs() > 1) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2570 | Expr *E = Attr.getArg(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2571 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2572 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2573 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2574 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2575 | << "sentinel" << 2 << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2576 | return; |
| 2577 | } |
| 2578 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2579 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2580 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2581 | // FIXME: This error message could be improved, it would be nice |
| 2582 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2583 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2584 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2585 | return; |
| 2586 | } |
| 2587 | } |
| 2588 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2589 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2590 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2591 | if (isa<FunctionNoProtoType>(FT)) { |
| 2592 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2593 | return; |
| 2594 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2595 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2596 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2597 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2598 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2599 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2600 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2601 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2602 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2603 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2604 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2605 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2606 | if (!BD->isVariadic()) { |
| 2607 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2608 | return; |
| 2609 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2610 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2611 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2612 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2613 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2614 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2615 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2616 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2617 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2618 | return; |
| 2619 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2620 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2621 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2622 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2623 | return; |
| 2624 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2625 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2626 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2627 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2628 | return; |
| 2629 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2630 | D->addAttr(::new (S.Context) |
| 2631 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2632 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2635 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2636 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2637 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2638 | return; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2639 | |
Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2640 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2641 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | 3d699e0 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2642 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2643 | return; |
| 2644 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2645 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2646 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2647 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2648 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2649 | return; |
| 2650 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2651 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2652 | if (MD->getResultType()->isVoidType()) { |
| 2653 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2654 | << Attr.getName() << 1; |
| 2655 | return; |
| 2656 | } |
| 2657 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2658 | D->addAttr(::new (S.Context) |
| 2659 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2660 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2663 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2664 | // check the attribute arguments. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2665 | if (Attr.hasParameterOrArguments()) { |
| 2666 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2667 | return; |
| 2668 | } |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2669 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2670 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 47f9a73 | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2671 | if (isa<CXXRecordDecl>(D)) { |
| 2672 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2673 | return; |
| 2674 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2675 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2676 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | 41136ee | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2677 | return; |
| 2678 | } |
| 2679 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2680 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2681 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2682 | nd->addAttr(::new (S.Context) |
| 2683 | WeakAttr(Attr.getRange(), S.Context, |
| 2684 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2687 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2688 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2689 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2690 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2691 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2692 | |
| 2693 | // weak_import only applies to variable & function declarations. |
| 2694 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2695 | if (!D->canBeWeakImported(isDef)) { |
| 2696 | if (isDef) |
| 2697 | S.Diag(Attr.getLoc(), |
| 2698 | diag::warn_attribute_weak_import_invalid_on_definition) |
| 2699 | << "weak_import" << 2 /*variable and function*/; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2700 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2701 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2702 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2703 | // Nothing to warn about here. |
| 2704 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2705 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2706 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2707 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2708 | return; |
| 2709 | } |
| 2710 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2711 | D->addAttr(::new (S.Context) |
| 2712 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2713 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2714 | } |
| 2715 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2716 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2717 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2718 | const AttributeList &Attr) { |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2719 | assert(Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2720 | || Attr.getKind() == AttributeList::AT_WorkGroupSizeHint); |
| 2721 | |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2722 | // Attribute has 3 arguments. |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2723 | if (!checkAttributeNumArgs(S, Attr, 3)) return; |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2724 | |
| 2725 | unsigned WGSize[3]; |
| 2726 | for (unsigned i = 0; i < 3; ++i) { |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2727 | Expr *E = Attr.getArg(i); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2728 | llvm::APSInt ArgNum(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2729 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2730 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2731 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2732 | << Attr.getName()->getName() << E->getSourceRange(); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2733 | return; |
| 2734 | } |
| 2735 | WGSize[i] = (unsigned) ArgNum.getZExtValue(); |
| 2736 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2737 | |
| 2738 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2739 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2740 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2741 | if (!(A->getXDim() == WGSize[0] && |
| 2742 | A->getYDim() == WGSize[1] && |
| 2743 | A->getZDim() == WGSize[2])) { |
| 2744 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2745 | Attr.getName(); |
| 2746 | } |
| 2747 | } |
| 2748 | |
| 2749 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2750 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2751 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2752 | if (!(A->getXDim() == WGSize[0] && |
| 2753 | A->getYDim() == WGSize[1] && |
| 2754 | A->getZDim() == WGSize[2])) { |
| 2755 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2756 | Attr.getName(); |
| 2757 | } |
| 2758 | } |
| 2759 | |
| 2760 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2761 | D->addAttr(::new (S.Context) |
| 2762 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2763 | WGSize[0], WGSize[1], WGSize[2], |
| 2764 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2765 | else |
| 2766 | D->addAttr(::new (S.Context) |
| 2767 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2768 | WGSize[0], WGSize[1], WGSize[2], |
| 2769 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2770 | } |
| 2771 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2772 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2773 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2774 | |
| 2775 | // Attribute has 1 argument. |
| 2776 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2777 | return; |
| 2778 | |
| 2779 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg()); |
| 2780 | |
| 2781 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2782 | (ParmType->isBooleanType() || |
| 2783 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2784 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2785 | << ParmType; |
| 2786 | return; |
| 2787 | } |
| 2788 | |
| 2789 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2790 | D->hasAttr<VecTypeHintAttr>()) { |
| 2791 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
| 2792 | if (A->getTypeHint() != ParmType) { |
| 2793 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2794 | return; |
| 2795 | } |
| 2796 | } |
| 2797 | |
| 2798 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
| 2799 | ParmType, Attr.getLoc())); |
| 2800 | } |
| 2801 | |
Joey Gouly | 0608ae8 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 2802 | static void handleEndianAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2803 | if (!dyn_cast<VarDecl>(D)) |
| 2804 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) << "endian" |
| 2805 | << 9; |
| 2806 | StringRef EndianType = Attr.getParameterName()->getName(); |
| 2807 | if (EndianType != "host" && EndianType != "device") |
| 2808 | S.Diag(Attr.getLoc(), diag::warn_attribute_unknown_endian) << EndianType; |
| 2809 | } |
| 2810 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2811 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2812 | StringRef Name, |
| 2813 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2814 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2815 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2816 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2817 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2818 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2819 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2820 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2821 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2822 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2823 | } |
| 2824 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2825 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2826 | // Attribute has no arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2827 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2828 | return; |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2829 | |
| 2830 | // Make sure that there is a string literal as the sections's single |
| 2831 | // argument. |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2832 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2833 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2834 | if (!SE) { |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2835 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) << "section"; |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2836 | return; |
| 2837 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2838 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2839 | // If the target wants to validate the section specifier, make it happen. |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2840 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(SE->getString()); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2841 | if (!Error.empty()) { |
| 2842 | S.Diag(SE->getLocStart(), diag::err_attribute_section_invalid_for_target) |
| 2843 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2844 | return; |
| 2845 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2847 | // This attribute cannot be applied to local variables. |
| 2848 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
| 2849 | S.Diag(SE->getLocStart(), diag::err_attribute_section_local_variable); |
| 2850 | return; |
| 2851 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2852 | |
| 2853 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2854 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2855 | SE->getString(), Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2856 | if (NewAttr) |
| 2857 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2858 | } |
| 2859 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2860 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2861 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2862 | // check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2863 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2864 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2865 | return; |
| 2866 | } |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2867 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2868 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2869 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2870 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2871 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2872 | D->addAttr(::new (S.Context) |
| 2873 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2874 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2875 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2878 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2879 | // check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 2880 | if (Attr.hasParameterOrArguments()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2881 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2882 | return; |
| 2883 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2884 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2885 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2886 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2887 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2888 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2889 | D->addAttr(::new (S.Context) |
| 2890 | ConstAttr(Attr.getRange(), S.Context, |
| 2891 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2892 | } |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2895 | static void handlePureAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2896 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2897 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2898 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2899 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2900 | D->addAttr(::new (S.Context) |
| 2901 | PureAttr(Attr.getRange(), S.Context, |
| 2902 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2903 | } |
| 2904 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2905 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2906 | if (!Attr.getParameterName()) { |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2907 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2908 | return; |
| 2909 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2910 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2911 | if (Attr.getNumArgs() != 0) { |
| 2912 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 2913 | return; |
| 2914 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2915 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2916 | VarDecl *VD = dyn_cast<VarDecl>(D); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2917 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2918 | if (!VD || !VD->hasLocalStorage()) { |
| 2919 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "cleanup"; |
| 2920 | return; |
| 2921 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2922 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2923 | // Look up the function |
Douglas Gregor | b2ccf01 | 2010-04-15 22:33:43 +0000 | [diff] [blame] | 2924 | // FIXME: Lookup probably isn't looking in the right place |
John McCall | 9f3059a | 2009-10-09 21:13:30 +0000 | [diff] [blame] | 2925 | NamedDecl *CleanupDecl |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2926 | = S.LookupSingleName(S.TUScope, Attr.getParameterName(), |
| 2927 | Attr.getParameterLoc(), Sema::LookupOrdinaryName); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2928 | if (!CleanupDecl) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2929 | S.Diag(Attr.getParameterLoc(), diag::err_attribute_cleanup_arg_not_found) << |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2930 | Attr.getParameterName(); |
| 2931 | return; |
| 2932 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2933 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2934 | FunctionDecl *FD = dyn_cast<FunctionDecl>(CleanupDecl); |
| 2935 | if (!FD) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2936 | S.Diag(Attr.getParameterLoc(), |
| 2937 | diag::err_attribute_cleanup_arg_not_function) |
| 2938 | << Attr.getParameterName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2939 | return; |
| 2940 | } |
| 2941 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2942 | if (FD->getNumParams() != 1) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2943 | S.Diag(Attr.getParameterLoc(), |
| 2944 | diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2945 | << Attr.getParameterName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2946 | return; |
| 2947 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2948 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2949 | // We're currently more strict than GCC about what function types we accept. |
| 2950 | // If this ever proves to be a problem it should be easy to fix. |
| 2951 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2952 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2953 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2954 | ParamTy, Ty) != Sema::Compatible) { |
Argyrios Kyrtzidis | 7b60897 | 2010-12-06 17:51:50 +0000 | [diff] [blame] | 2955 | S.Diag(Attr.getParameterLoc(), |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2956 | diag::err_attribute_cleanup_func_arg_incompatible_type) << |
| 2957 | Attr.getParameterName() << ParamTy << Ty; |
| 2958 | return; |
| 2959 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2960 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2961 | D->addAttr(::new (S.Context) |
| 2962 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2963 | Attr.getAttributeSpellingListIndex())); |
Eli Friedman | fa0df83 | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 2964 | S.MarkFunctionReferenced(Attr.getParameterLoc(), FD); |
Nick Lewycky | a096b14 | 2013-02-12 08:08:54 +0000 | [diff] [blame] | 2965 | S.DiagnoseUseOfDecl(FD, Attr.getParameterLoc()); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2966 | } |
| 2967 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2968 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2969 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2970 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2971 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2972 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 2973 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2974 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2975 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2976 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2977 | return; |
| 2978 | } |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2979 | |
| 2980 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2981 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2982 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2983 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2984 | unsigned FirstIdx = 1; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2985 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2986 | // checks for the 2nd argument |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 2987 | Expr *IdxExpr = Attr.getArg(0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2988 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2989 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 2990 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2991 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 2992 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2993 | return; |
| 2994 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2995 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2996 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
| 2997 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 2998 | << "format" << 2 << IdxExpr->getSourceRange(); |
| 2999 | return; |
| 3000 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3001 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3002 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3003 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3004 | if (HasImplicitThisParam) { |
| 3005 | if (ArgIdx == 0) { |
| 3006 | S.Diag(Attr.getLoc(), diag::err_attribute_invalid_implicit_this_argument) |
| 3007 | << "format_arg" << IdxExpr->getSourceRange(); |
| 3008 | return; |
| 3009 | } |
| 3010 | ArgIdx--; |
| 3011 | } |
| 3012 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3013 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3014 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3015 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3016 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 3017 | if (not_nsstring_type && |
| 3018 | !isCFStringType(Ty, S.Context) && |
| 3019 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3020 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3021 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 3022 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3023 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3024 | << IdxExpr->getSourceRange(); |
| 3025 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3026 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3027 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3028 | if (!isNSStringType(Ty, S.Context) && |
| 3029 | !isCFStringType(Ty, S.Context) && |
| 3030 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3031 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3032 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 3033 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3034 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3035 | << IdxExpr->getSourceRange(); |
| 3036 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3039 | D->addAttr(::new (S.Context) |
| 3040 | FormatArgAttr(Attr.getRange(), S.Context, Idx.getZExtValue(), |
| 3041 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3042 | } |
| 3043 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3044 | enum FormatAttrKind { |
| 3045 | CFStringFormat, |
| 3046 | NSStringFormat, |
| 3047 | StrftimeFormat, |
| 3048 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3049 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3050 | InvalidFormat |
| 3051 | }; |
| 3052 | |
| 3053 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3054 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3055 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3056 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 3057 | // Check for formats that get handled specially. |
| 3058 | .Case("NSString", NSStringFormat) |
| 3059 | .Case("CFString", CFStringFormat) |
| 3060 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3061 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3062 | // Otherwise, check for supported formats. |
| 3063 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3064 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3065 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3066 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3067 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3068 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3071 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3072 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3073 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3074 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3075 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3076 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3077 | return; |
| 3078 | } |
| 3079 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3080 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3081 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3082 | Attr.setInvalid(); |
| 3083 | return; |
| 3084 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3085 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3086 | if (S.Context.getAsArrayType(T)) |
| 3087 | T = S.Context.getBaseElementType(T); |
| 3088 | if (!T->getAs<RecordType>()) { |
| 3089 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3090 | Attr.setInvalid(); |
| 3091 | return; |
| 3092 | } |
| 3093 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3094 | if (Attr.getNumArgs() != 1) { |
| 3095 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 3096 | Attr.setInvalid(); |
| 3097 | return; |
| 3098 | } |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3099 | Expr *priorityExpr = Attr.getArg(0); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3100 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3101 | llvm::APSInt priority(32); |
| 3102 | if (priorityExpr->isTypeDependent() || priorityExpr->isValueDependent() || |
| 3103 | !priorityExpr->isIntegerConstantExpr(priority, S.Context)) { |
| 3104 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 3105 | << "init_priority" << priorityExpr->getSourceRange(); |
| 3106 | Attr.setInvalid(); |
| 3107 | return; |
| 3108 | } |
Fariborz Jahanian | 9f2a4ee | 2010-06-21 18:45:05 +0000 | [diff] [blame] | 3109 | unsigned prioritynum = priority.getZExtValue(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3110 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3111 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
| 3112 | << priorityExpr->getSourceRange(); |
| 3113 | Attr.setInvalid(); |
| 3114 | return; |
| 3115 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3116 | D->addAttr(::new (S.Context) |
| 3117 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3118 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3121 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, StringRef Format, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3122 | int FormatIdx, int FirstArg, |
| 3123 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3124 | // Check whether we already have an equivalent format attribute. |
| 3125 | for (specific_attr_iterator<FormatAttr> |
| 3126 | i = D->specific_attr_begin<FormatAttr>(), |
| 3127 | e = D->specific_attr_end<FormatAttr>(); |
| 3128 | i != e ; ++i) { |
| 3129 | FormatAttr *f = *i; |
| 3130 | if (f->getType() == Format && |
| 3131 | f->getFormatIdx() == FormatIdx && |
| 3132 | f->getFirstArg() == FirstArg) { |
| 3133 | // If we don't have a valid location for this attribute, adopt the |
| 3134 | // location. |
| 3135 | if (f->getLocation().isInvalid()) |
| 3136 | f->setRange(Range); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3137 | return NULL; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3138 | } |
| 3139 | } |
| 3140 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3141 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, FirstArg, |
| 3142 | AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3145 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3146 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3147 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3148 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3149 | if (!Attr.getParameterName()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3150 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3151 | << "format" << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3152 | return; |
| 3153 | } |
| 3154 | |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3155 | if (Attr.getNumArgs() != 2) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3156 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 3; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3157 | return; |
| 3158 | } |
| 3159 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3160 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3161 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3162 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3163 | return; |
| 3164 | } |
| 3165 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3166 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3167 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3168 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 3169 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3170 | unsigned FirstIdx = 1; |
| 3171 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3172 | StringRef Format = Attr.getParameterName()->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3173 | |
| 3174 | // Normalize the argument, __foo__ becomes foo. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3175 | if (Format.startswith("__") && Format.endswith("__")) |
| 3176 | Format = Format.substr(2, Format.size() - 4); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3177 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3178 | // Check for supported formats. |
| 3179 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3180 | |
| 3181 | if (Kind == IgnoredFormat) |
| 3182 | return; |
| 3183 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3184 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3185 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Daniel Dunbar | 07d0785 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 3186 | << "format" << Attr.getParameterName()->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3187 | return; |
| 3188 | } |
| 3189 | |
| 3190 | // checks for the 2nd argument |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3191 | Expr *IdxExpr = Attr.getArg(0); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3192 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3193 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 3194 | !IdxExpr->isIntegerConstantExpr(Idx, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3195 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3196 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3197 | return; |
| 3198 | } |
| 3199 | |
| 3200 | if (Idx.getZExtValue() < FirstIdx || Idx.getZExtValue() > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3201 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3202 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3203 | return; |
| 3204 | } |
| 3205 | |
| 3206 | // FIXME: Do we need to bounds check? |
| 3207 | unsigned ArgIdx = Idx.getZExtValue() - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3208 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3209 | if (HasImplicitThisParam) { |
| 3210 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3211 | S.Diag(Attr.getLoc(), |
| 3212 | diag::err_format_attribute_implicit_this_format_string) |
| 3213 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3214 | return; |
| 3215 | } |
| 3216 | ArgIdx--; |
| 3217 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3218 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3219 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3220 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3221 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3222 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3223 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3224 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3225 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3226 | return; |
| 3227 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3228 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3229 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3230 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3231 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3232 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3233 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3234 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3235 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3236 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3237 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3238 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3239 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3240 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 3241 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3242 | return; |
| 3243 | } |
| 3244 | |
| 3245 | // check the 3rd argument |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3246 | Expr *FirstArgExpr = Attr.getArg(1); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3247 | llvm::APSInt FirstArg(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 3248 | if (FirstArgExpr->isTypeDependent() || FirstArgExpr->isValueDependent() || |
| 3249 | !FirstArgExpr->isIntegerConstantExpr(FirstArg, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3250 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3251 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3252 | return; |
| 3253 | } |
| 3254 | |
| 3255 | // check if the function is variadic if the 3rd argument non-zero |
| 3256 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3257 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3258 | ++NumArgs; // +1 for ... |
| 3259 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3260 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3261 | return; |
| 3262 | } |
| 3263 | } |
| 3264 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3265 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3266 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3267 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3268 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3269 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3270 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3271 | return; |
| 3272 | } |
| 3273 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3274 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3275 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3276 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3277 | return; |
| 3278 | } |
| 3279 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3280 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), Format, |
| 3281 | Idx.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3282 | FirstArg.getZExtValue(), |
| 3283 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3284 | if (NewAttr) |
| 3285 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3286 | } |
| 3287 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3288 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3289 | const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3290 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3291 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3292 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3293 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3294 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3295 | // Try to find the underlying union declaration. |
| 3296 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3297 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3298 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3299 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3300 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3301 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3302 | |
| 3303 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3304 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3305 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3306 | return; |
| 3307 | } |
| 3308 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3309 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3310 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3311 | diag::warn_transparent_union_attribute_not_definition); |
| 3312 | return; |
| 3313 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3314 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3315 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3316 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3317 | if (Field == FieldEnd) { |
| 3318 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3319 | return; |
| 3320 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3321 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3322 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3323 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3324 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3325 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3326 | diag::warn_transparent_union_attribute_floating) |
| 3327 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3328 | return; |
| 3329 | } |
| 3330 | |
| 3331 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3332 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3333 | for (; Field != FieldEnd; ++Field) { |
| 3334 | QualType FieldType = Field->getType(); |
| 3335 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 3336 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 3337 | // Warn if we drop the attribute. |
| 3338 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3339 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3340 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3341 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3342 | diag::warn_transparent_union_attribute_field_size_align) |
| 3343 | << isSize << Field->getDeclName() << FieldBits; |
| 3344 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3345 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3346 | diag::note_transparent_union_first_field_size_align) |
| 3347 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3348 | return; |
| 3349 | } |
| 3350 | } |
| 3351 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3352 | RD->addAttr(::new (S.Context) |
| 3353 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3354 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3355 | } |
| 3356 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3357 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3358 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3359 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3360 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3361 | |
Peter Collingbourne | e57e9ef | 2010-11-23 20:45:58 +0000 | [diff] [blame] | 3362 | Expr *ArgExpr = Attr.getArg(0); |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3363 | StringLiteral *SE = dyn_cast<StringLiteral>(ArgExpr); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3364 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3365 | // Make sure that there is a string literal as the annotation's single |
| 3366 | // argument. |
| 3367 | if (!SE) { |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3368 | S.Diag(ArgExpr->getLocStart(), diag::err_attribute_not_string) <<"annotate"; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3369 | return; |
| 3370 | } |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3371 | |
| 3372 | // Don't duplicate annotations that are already set. |
| 3373 | for (specific_attr_iterator<AnnotateAttr> |
| 3374 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 3375 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
| 3376 | if ((*i)->getAnnotation() == SE->getString()) |
| 3377 | return; |
| 3378 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3379 | |
| 3380 | D->addAttr(::new (S.Context) |
| 3381 | AnnotateAttr(Attr.getRange(), S.Context, SE->getString(), |
| 3382 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3383 | } |
| 3384 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3385 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3386 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3387 | if (Attr.getNumArgs() > 1) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3388 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3389 | return; |
| 3390 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3391 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3392 | if (Attr.getNumArgs() == 0) { |
| 3393 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 3394 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 3395 | return; |
| 3396 | } |
| 3397 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3398 | Expr *E = Attr.getArg(0); |
| 3399 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3400 | S.Diag(Attr.getEllipsisLoc(), |
| 3401 | diag::err_pack_expansion_without_parameter_packs); |
| 3402 | return; |
| 3403 | } |
| 3404 | |
| 3405 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3406 | return; |
| 3407 | |
| 3408 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3409 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3413 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3414 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3415 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3416 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3417 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3418 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3419 | // C++11 [dcl.align]p1: |
| 3420 | // An alignment-specifier may be applied to a variable or to a class |
| 3421 | // data member, but it shall not be applied to a bit-field, a function |
| 3422 | // parameter, the formal parameter of a catch clause, or a variable |
| 3423 | // declared with the register storage class specifier. An |
| 3424 | // alignment-specifier may also be applied to the declaration of a class |
| 3425 | // or enumeration type. |
| 3426 | // C11 6.7.5/2: |
| 3427 | // An alignment attribute shall not be specified in a declaration of |
| 3428 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3429 | // object declared with the register storage-class specifier. |
| 3430 | int DiagKind = -1; |
| 3431 | if (isa<ParmVarDecl>(D)) { |
| 3432 | DiagKind = 0; |
| 3433 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3434 | if (VD->getStorageClass() == SC_Register) |
| 3435 | DiagKind = 1; |
| 3436 | if (VD->isExceptionVariable()) |
| 3437 | DiagKind = 2; |
| 3438 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3439 | if (FD->isBitField()) |
| 3440 | DiagKind = 3; |
| 3441 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3442 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3443 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3444 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3445 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3446 | return; |
| 3447 | } |
| 3448 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3449 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3450 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3451 | return; |
| 3452 | } |
| 3453 | } |
| 3454 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3455 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3456 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3457 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3458 | AA->setPackExpansion(IsPackExpansion); |
| 3459 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3460 | return; |
| 3461 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3462 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3463 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3464 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3465 | ExprResult ICE |
| 3466 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3467 | diag::err_aligned_attribute_argument_not_int, |
| 3468 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3469 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3470 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3471 | |
| 3472 | // C++11 [dcl.align]p2: |
| 3473 | // -- if the constant expression evaluates to zero, the alignment |
| 3474 | // specifier shall have no effect |
| 3475 | // C11 6.7.5p6: |
| 3476 | // An alignment specification of zero has no effect. |
| 3477 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3478 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3479 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3480 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3481 | return; |
| 3482 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3483 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3484 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3485 | // We've already verified it's a power of 2, now let's make sure it's |
| 3486 | // 8192 or less. |
| 3487 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3488 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3489 | << E->getSourceRange(); |
| 3490 | return; |
| 3491 | } |
| 3492 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3493 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3494 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3495 | ICE.take(), SpellingListIndex); |
| 3496 | AA->setPackExpansion(IsPackExpansion); |
| 3497 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3500 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3501 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3502 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3503 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3504 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3505 | SpellingListIndex); |
| 3506 | AA->setPackExpansion(IsPackExpansion); |
| 3507 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3508 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3509 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3510 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3511 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3512 | |
| 3513 | QualType Ty; |
| 3514 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3515 | Ty = VD->getType(); |
| 3516 | else |
| 3517 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 3653b7e | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3518 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3519 | return; |
| 3520 | |
| 3521 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3522 | // The combined effect of all alignment attributes in a declaration shall |
| 3523 | // not specify an alignment that is less strict than the alignment that |
| 3524 | // would otherwise be required for the entity being declared. |
| 3525 | AlignedAttr *AlignasAttr = 0; |
| 3526 | unsigned Align = 0; |
| 3527 | for (specific_attr_iterator<AlignedAttr> |
| 3528 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3529 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3530 | if (I->isAlignmentDependent()) |
| 3531 | return; |
| 3532 | if (I->isAlignas()) |
| 3533 | AlignasAttr = *I; |
| 3534 | Align = std::max(Align, I->getAlignment(Context)); |
| 3535 | } |
| 3536 | |
| 3537 | if (AlignasAttr && Align) { |
| 3538 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3539 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3540 | if (NaturalAlign > RequestedAlign) |
| 3541 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3542 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3543 | } |
| 3544 | } |
| 3545 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3546 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3547 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3548 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3549 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3550 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3551 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3552 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3553 | // This attribute isn't documented, but glibc uses it. It changes |
| 3554 | // the width of an int or unsigned int to the specified size. |
| 3555 | |
| 3556 | // Check that there aren't any arguments |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3557 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3558 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3559 | |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3560 | |
| 3561 | IdentifierInfo *Name = Attr.getParameterName(); |
| 3562 | if (!Name) { |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3563 | S.Diag(Attr.getLoc(), diag::err_attribute_missing_parameter_name); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3564 | return; |
| 3565 | } |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3566 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3567 | StringRef Str = Attr.getParameterName()->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3568 | |
| 3569 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3570 | if (Str.startswith("__") && Str.endswith("__")) |
| 3571 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3572 | |
| 3573 | unsigned DestWidth = 0; |
| 3574 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3575 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3576 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3577 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3578 | switch (Str[0]) { |
| 3579 | case 'Q': DestWidth = 8; break; |
| 3580 | case 'H': DestWidth = 16; break; |
| 3581 | case 'S': DestWidth = 32; break; |
| 3582 | case 'D': DestWidth = 64; break; |
| 3583 | case 'X': DestWidth = 96; break; |
| 3584 | case 'T': DestWidth = 128; break; |
| 3585 | } |
| 3586 | if (Str[1] == 'F') { |
| 3587 | IntegerMode = false; |
| 3588 | } else if (Str[1] == 'C') { |
| 3589 | IntegerMode = false; |
| 3590 | ComplexMode = true; |
| 3591 | } else if (Str[1] != 'I') { |
| 3592 | DestWidth = 0; |
| 3593 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3594 | break; |
| 3595 | case 4: |
| 3596 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3597 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3598 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3599 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3600 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3601 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3602 | break; |
| 3603 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3604 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3605 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3606 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3607 | case 11: |
| 3608 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3609 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3610 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3614 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3615 | OldTy = TD->getUnderlyingType(); |
| 3616 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3617 | OldTy = VD->getType(); |
| 3618 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3619 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3620 | << "mode" << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3621 | return; |
| 3622 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3623 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3624 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3625 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3626 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3627 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3628 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3629 | } else if (ComplexMode) { |
| 3630 | if (!OldTy->isComplexType()) |
| 3631 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3632 | } else { |
| 3633 | if (!OldTy->isFloatingType()) |
| 3634 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3635 | } |
| 3636 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3637 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3638 | // and friends, at least with glibc. |
| 3639 | // FIXME: Make sure 32/64-bit integers don't get defined to types of the wrong |
| 3640 | // width on unusual platforms. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3641 | // FIXME: Make sure floating-point mappings are accurate |
| 3642 | // FIXME: Support XF and TF types |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3643 | QualType NewTy; |
| 3644 | switch (DestWidth) { |
| 3645 | case 0: |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3646 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3647 | return; |
| 3648 | default: |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3649 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3650 | return; |
| 3651 | case 8: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3652 | if (!IntegerMode) { |
| 3653 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3654 | return; |
| 3655 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3656 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3657 | NewTy = S.Context.SignedCharTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3658 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3659 | NewTy = S.Context.UnsignedCharTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3660 | break; |
| 3661 | case 16: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3662 | if (!IntegerMode) { |
| 3663 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3664 | return; |
| 3665 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3666 | if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3667 | NewTy = S.Context.ShortTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3668 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3669 | NewTy = S.Context.UnsignedShortTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3670 | break; |
| 3671 | case 32: |
| 3672 | if (!IntegerMode) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3673 | NewTy = S.Context.FloatTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3674 | else if (OldTy->isSignedIntegerType()) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3675 | NewTy = S.Context.IntTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3676 | else |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3677 | NewTy = S.Context.UnsignedIntTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3678 | break; |
| 3679 | case 64: |
| 3680 | if (!IntegerMode) |
Chris Lattner | a663a0a | 2008-06-29 00:28:59 +0000 | [diff] [blame] | 3681 | NewTy = S.Context.DoubleTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3682 | else if (OldTy->isSignedIntegerType()) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3683 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | 7234370 | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3684 | NewTy = S.Context.LongTy; |
| 3685 | else |
| 3686 | NewTy = S.Context.LongLongTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3687 | else |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3688 | if (S.Context.getTargetInfo().getLongWidth() == 64) |
Chandler Carruth | 7234370 | 2010-01-26 06:39:24 +0000 | [diff] [blame] | 3689 | NewTy = S.Context.UnsignedLongTy; |
| 3690 | else |
| 3691 | NewTy = S.Context.UnsignedLongLongTy; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3692 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3693 | case 96: |
| 3694 | NewTy = S.Context.LongDoubleTy; |
| 3695 | break; |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3696 | case 128: |
| 3697 | if (!IntegerMode) { |
| 3698 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
| 3699 | return; |
| 3700 | } |
Anders Carlsson | 88ea245 | 2009-12-29 07:07:36 +0000 | [diff] [blame] | 3701 | if (OldTy->isSignedIntegerType()) |
| 3702 | NewTy = S.Context.Int128Ty; |
| 3703 | else |
| 3704 | NewTy = S.Context.UnsignedInt128Ty; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3705 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3708 | if (ComplexMode) { |
| 3709 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3710 | } |
| 3711 | |
| 3712 | // Install the new type. |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3713 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3714 | // FIXME: preserve existing source info. |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3715 | TD->setTypeSourceInfo(S.Context.getTrivialTypeSourceInfo(NewTy)); |
John McCall | 703a3f8 | 2009-10-24 08:00:42 +0000 | [diff] [blame] | 3716 | } else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3717 | cast<ValueDecl>(D)->setType(NewTy); |
| 3718 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3719 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3720 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3721 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3722 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3723 | return; |
Anders Carlsson | 63784f4 | 2009-02-13 08:11:52 +0000 | [diff] [blame] | 3724 | |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3725 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3726 | if (!VD->hasGlobalStorage()) |
| 3727 | S.Diag(Attr.getLoc(), |
| 3728 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3729 | << Attr.getName(); |
| 3730 | } else if (!isFunctionOrMethod(D)) { |
| 3731 | S.Diag(Attr.getLoc(), |
| 3732 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3733 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3734 | return; |
| 3735 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3736 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3737 | D->addAttr(::new (S.Context) |
| 3738 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3739 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3742 | static void handleNoInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3743 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3744 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3745 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3746 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3747 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3748 | if (!isa<FunctionDecl>(D)) { |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3749 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3750 | << Attr.getName() << ExpectedFunction; |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3751 | return; |
| 3752 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3753 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3754 | D->addAttr(::new (S.Context) |
| 3755 | NoInlineAttr(Attr.getRange(), S.Context, |
| 3756 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 8809712 | 2009-02-19 19:16:48 +0000 | [diff] [blame] | 3757 | } |
| 3758 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3759 | static void handleNoInstrumentFunctionAttr(Sema &S, Decl *D, |
| 3760 | const AttributeList &Attr) { |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3761 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3762 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3763 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3764 | |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3765 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3766 | if (!isa<FunctionDecl>(D)) { |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3767 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3768 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3769 | return; |
| 3770 | } |
| 3771 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3772 | D->addAttr(::new (S.Context) |
| 3773 | NoInstrumentFunctionAttr(Attr.getRange(), S.Context, |
| 3774 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3777 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3778 | if (S.LangOpts.CUDA) { |
| 3779 | // check the attribute arguments. |
Ted Kremenek | 1551d55 | 2011-04-15 05:49:29 +0000 | [diff] [blame] | 3780 | if (Attr.hasParameterOrArguments()) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3781 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3782 | return; |
| 3783 | } |
| 3784 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3785 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3786 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3787 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3788 | return; |
| 3789 | } |
| 3790 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3791 | D->addAttr(::new (S.Context) |
| 3792 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3793 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3794 | } else { |
| 3795 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "constant"; |
| 3796 | } |
| 3797 | } |
| 3798 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3799 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3800 | if (S.LangOpts.CUDA) { |
| 3801 | // check the attribute arguments. |
| 3802 | if (Attr.getNumArgs() != 0) { |
| 3803 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0; |
| 3804 | return; |
| 3805 | } |
| 3806 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3807 | if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3808 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3809 | << Attr.getName() << ExpectedVariableOrFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3810 | return; |
| 3811 | } |
| 3812 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3813 | D->addAttr(::new (S.Context) |
| 3814 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3815 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3816 | } else { |
| 3817 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "device"; |
| 3818 | } |
| 3819 | } |
| 3820 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3821 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3822 | if (S.LangOpts.CUDA) { |
| 3823 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3824 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3825 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3826 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3827 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3828 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3829 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3830 | return; |
| 3831 | } |
| 3832 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3833 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3834 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3835 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3836 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3837 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3838 | << FD->getType() |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3839 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3840 | "void"); |
| 3841 | } else { |
| 3842 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3843 | << FD->getType(); |
| 3844 | } |
| 3845 | return; |
| 3846 | } |
| 3847 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3848 | D->addAttr(::new (S.Context) |
| 3849 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3850 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3851 | } else { |
| 3852 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "global"; |
| 3853 | } |
| 3854 | } |
| 3855 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3856 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3857 | if (S.LangOpts.CUDA) { |
| 3858 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3859 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3860 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3861 | |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3862 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3863 | if (!isa<FunctionDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3864 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3865 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3866 | return; |
| 3867 | } |
| 3868 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3869 | D->addAttr(::new (S.Context) |
| 3870 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3871 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3872 | } else { |
| 3873 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "host"; |
| 3874 | } |
| 3875 | } |
| 3876 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3877 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3878 | if (S.LangOpts.CUDA) { |
| 3879 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3880 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3881 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3882 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3883 | if (!isa<VarDecl>(D)) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3884 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3885 | << Attr.getName() << ExpectedVariable; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3886 | return; |
| 3887 | } |
| 3888 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3889 | D->addAttr(::new (S.Context) |
| 3890 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3891 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3892 | } else { |
| 3893 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "shared"; |
| 3894 | } |
| 3895 | } |
| 3896 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3897 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3898 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3899 | if (!checkAttributeNumArgs(S, Attr, 0)) |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3900 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3901 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3902 | FunctionDecl *Fn = dyn_cast<FunctionDecl>(D); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3903 | if (Fn == 0) { |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3904 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3905 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3906 | return; |
| 3907 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3908 | |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3909 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3910 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3911 | return; |
| 3912 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3913 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3914 | D->addAttr(::new (S.Context) |
| 3915 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3916 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3917 | } |
| 3918 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3919 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3920 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3921 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3922 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3923 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3924 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3925 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3926 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3927 | return; |
| 3928 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3929 | if (!isa<ObjCMethodDecl>(D)) { |
| 3930 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3931 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3932 | return; |
| 3933 | } |
| 3934 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3935 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3936 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3937 | D->addAttr(::new (S.Context) |
| 3938 | FastCallAttr(Attr.getRange(), S.Context, |
| 3939 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3940 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3941 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3942 | D->addAttr(::new (S.Context) |
| 3943 | StdCallAttr(Attr.getRange(), S.Context, |
| 3944 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3945 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3946 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3947 | D->addAttr(::new (S.Context) |
| 3948 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3949 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3950 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3951 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3952 | D->addAttr(::new (S.Context) |
| 3953 | CDeclAttr(Attr.getRange(), S.Context, |
| 3954 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3955 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3956 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3957 | D->addAttr(::new (S.Context) |
| 3958 | PascalAttr(Attr.getRange(), S.Context, |
| 3959 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3960 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3961 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3962 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3963 | switch (CC) { |
| 3964 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3965 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3966 | break; |
| 3967 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3968 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3969 | break; |
| 3970 | default: |
| 3971 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3972 | } |
| 3973 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3974 | D->addAttr(::new (S.Context) |
| 3975 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3976 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3977 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3978 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3979 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3980 | D->addAttr(::new (S.Context) |
| 3981 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3982 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3983 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3984 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3985 | D->addAttr(::new (S.Context) |
| 3986 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3987 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3988 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3989 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3990 | default: |
| 3991 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3992 | } |
| 3993 | } |
| 3994 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3995 | static void handleOpenCLKernelAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Chandler Carruth | 9312c64 | 2011-07-11 23:33:05 +0000 | [diff] [blame] | 3996 | assert(!Attr.isInvalid()); |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3997 | D->addAttr(::new (S.Context) OpenCLKernelAttr(Attr.getRange(), S.Context)); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 3998 | } |
| 3999 | |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame^] | 4000 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
| 4001 | assert(!Attr.isInvalid()); |
| 4002 | |
| 4003 | Expr *E = Attr.getArg(0); |
| 4004 | llvm::APSInt ArgNum(32); |
| 4005 | if (E->isTypeDependent() || E->isValueDependent() || |
| 4006 | !E->isIntegerConstantExpr(ArgNum, S.Context)) { |
| 4007 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
| 4008 | << Attr.getName()->getName() << E->getSourceRange(); |
| 4009 | return; |
| 4010 | } |
| 4011 | |
| 4012 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr( |
| 4013 | Attr.getRange(), S.Context, ArgNum.getZExtValue())); |
| 4014 | } |
| 4015 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4016 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 4017 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4018 | if (attr.isInvalid()) |
| 4019 | return true; |
| 4020 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4021 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
| 4022 | if (attr.getNumArgs() != ReqArgs || attr.getParameterName()) { |
| 4023 | Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << ReqArgs; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4024 | attr.setInvalid(); |
| 4025 | return true; |
| 4026 | } |
| 4027 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4028 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 4029 | // move to TargetAttributesSema one day. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4030 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4031 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 4032 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 4033 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 4034 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 4035 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
| 4036 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4037 | Expr *Arg = attr.getArg(0); |
| 4038 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4039 | if (!Str || !Str->isAscii()) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4040 | Diag(attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 4041 | << "pcs" << 1; |
| 4042 | attr.setInvalid(); |
| 4043 | return true; |
| 4044 | } |
| 4045 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4046 | StringRef StrRef = Str->getString(); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4047 | if (StrRef == "aapcs") { |
| 4048 | CC = CC_AAPCS; |
| 4049 | break; |
| 4050 | } else if (StrRef == "aapcs-vfp") { |
| 4051 | CC = CC_AAPCS_VFP; |
| 4052 | break; |
| 4053 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4054 | |
| 4055 | attr.setInvalid(); |
| 4056 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 4057 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4058 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4059 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4060 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4061 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4062 | } |
| 4063 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4064 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4065 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 4066 | if (A == TargetInfo::CCCR_Warning) { |
| 4067 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4068 | |
| 4069 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 4070 | if (FD) |
| 4071 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 4072 | TargetInfo::CCMT_NonMember; |
| 4073 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4076 | return false; |
| 4077 | } |
| 4078 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4079 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4080 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4081 | |
| 4082 | unsigned numParams; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4083 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4084 | return; |
| 4085 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4086 | if (!isa<ObjCMethodDecl>(D)) { |
| 4087 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4088 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4089 | return; |
| 4090 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4091 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4092 | D->addAttr(::new (S.Context) |
| 4093 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 4094 | Attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4095 | } |
| 4096 | |
| 4097 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4098 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4099 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 4100 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4101 | return true; |
| 4102 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4103 | if (Attr.getNumArgs() != 1) { |
| 4104 | Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 1; |
| 4105 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4106 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4107 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4108 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4109 | Expr *NumParamsExpr = Attr.getArg(0); |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4110 | llvm::APSInt NumParams(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 4111 | if (NumParamsExpr->isTypeDependent() || NumParamsExpr->isValueDependent() || |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4112 | !NumParamsExpr->isIntegerConstantExpr(NumParams, Context)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4113 | Diag(Attr.getLoc(), diag::err_attribute_argument_not_int) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4114 | << "regparm" << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4115 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4116 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4117 | } |
| 4118 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4119 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4120 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4121 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4122 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4123 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4124 | } |
| 4125 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4126 | numParams = NumParams.getZExtValue(); |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4127 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4128 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4129 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4130 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4131 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4134 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4137 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4138 | if (S.LangOpts.CUDA) { |
| 4139 | // check the attribute arguments. |
| 4140 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 4141 | // FIXME: 0 is not okay. |
| 4142 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4143 | return; |
| 4144 | } |
| 4145 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4146 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4147 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4148 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4149 | return; |
| 4150 | } |
| 4151 | |
| 4152 | Expr *MaxThreadsExpr = Attr.getArg(0); |
| 4153 | llvm::APSInt MaxThreads(32); |
| 4154 | if (MaxThreadsExpr->isTypeDependent() || |
| 4155 | MaxThreadsExpr->isValueDependent() || |
| 4156 | !MaxThreadsExpr->isIntegerConstantExpr(MaxThreads, S.Context)) { |
| 4157 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 4158 | << "launch_bounds" << 1 << MaxThreadsExpr->getSourceRange(); |
| 4159 | return; |
| 4160 | } |
| 4161 | |
| 4162 | llvm::APSInt MinBlocks(32); |
| 4163 | if (Attr.getNumArgs() > 1) { |
| 4164 | Expr *MinBlocksExpr = Attr.getArg(1); |
| 4165 | if (MinBlocksExpr->isTypeDependent() || |
| 4166 | MinBlocksExpr->isValueDependent() || |
| 4167 | !MinBlocksExpr->isIntegerConstantExpr(MinBlocks, S.Context)) { |
| 4168 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_int) |
| 4169 | << "launch_bounds" << 2 << MinBlocksExpr->getSourceRange(); |
| 4170 | return; |
| 4171 | } |
| 4172 | } |
| 4173 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4174 | D->addAttr(::new (S.Context) |
| 4175 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 4176 | MaxThreads.getZExtValue(), |
| 4177 | MinBlocks.getZExtValue(), |
| 4178 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4179 | } else { |
| 4180 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 4181 | } |
| 4182 | } |
| 4183 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4184 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4185 | const AttributeList &Attr) { |
| 4186 | StringRef AttrName = Attr.getName()->getName(); |
| 4187 | if (!Attr.getParameterName()) { |
| 4188 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 4189 | << Attr.getName() << /* arg num = */ 1; |
| 4190 | return; |
| 4191 | } |
| 4192 | |
| 4193 | if (Attr.getNumArgs() != 2) { |
| 4194 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 4195 | << /* required args = */ 3; |
| 4196 | return; |
| 4197 | } |
| 4198 | |
| 4199 | IdentifierInfo *ArgumentKind = Attr.getParameterName(); |
| 4200 | |
| 4201 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4202 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4203 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4204 | return; |
| 4205 | } |
| 4206 | |
| 4207 | uint64_t ArgumentIdx; |
| 4208 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4209 | Attr.getLoc(), 2, |
| 4210 | Attr.getArg(0), ArgumentIdx)) |
| 4211 | return; |
| 4212 | |
| 4213 | uint64_t TypeTagIdx; |
| 4214 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 4215 | Attr.getLoc(), 3, |
| 4216 | Attr.getArg(1), TypeTagIdx)) |
| 4217 | return; |
| 4218 | |
| 4219 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 4220 | if (IsPointer) { |
| 4221 | // Ensure that buffer has a pointer type. |
| 4222 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 4223 | if (!BufferTy->isPointerType()) { |
| 4224 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
| 4225 | << AttrName; |
| 4226 | } |
| 4227 | } |
| 4228 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4229 | D->addAttr(::new (S.Context) |
| 4230 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4231 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4232 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4236 | const AttributeList &Attr) { |
| 4237 | IdentifierInfo *PointerKind = Attr.getParameterName(); |
| 4238 | if (!PointerKind) { |
| 4239 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_identifier) |
| 4240 | << "type_tag_for_datatype" << 1; |
| 4241 | return; |
| 4242 | } |
| 4243 | |
| 4244 | QualType MatchingCType = S.GetTypeFromParser(Attr.getMatchingCType(), NULL); |
| 4245 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4246 | D->addAttr(::new (S.Context) |
| 4247 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
| 4248 | MatchingCType, |
| 4249 | Attr.getLayoutCompatible(), |
| 4250 | Attr.getMustBeNull(), |
| 4251 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4252 | } |
| 4253 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4254 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4255 | // Checker-specific attribute handlers. |
| 4256 | //===----------------------------------------------------------------------===// |
| 4257 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4258 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4259 | return type->isDependentType() || |
| 4260 | type->isObjCObjectPointerType() || |
| 4261 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4262 | } |
| 4263 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4264 | return type->isDependentType() || |
| 4265 | type->isPointerType() || |
| 4266 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4269 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4270 | ParmVarDecl *param = dyn_cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4271 | if (!param) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4272 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4273 | << Attr.getRange() << Attr.getName() << ExpectedParameter; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4274 | return; |
| 4275 | } |
| 4276 | |
| 4277 | bool typeOK, cf; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4278 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4279 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 4280 | cf = false; |
| 4281 | } else { |
| 4282 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 4283 | cf = true; |
| 4284 | } |
| 4285 | |
| 4286 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4287 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4288 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4289 | return; |
| 4290 | } |
| 4291 | |
| 4292 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4293 | param->addAttr(::new (S.Context) |
| 4294 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 4295 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4296 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4297 | param->addAttr(::new (S.Context) |
| 4298 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 4299 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4300 | } |
| 4301 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4302 | static void handleNSConsumesSelfAttr(Sema &S, Decl *D, |
| 4303 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4304 | if (!isa<ObjCMethodDecl>(D)) { |
| 4305 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4306 | << Attr.getRange() << Attr.getName() << ExpectedMethod; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4307 | return; |
| 4308 | } |
| 4309 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4310 | D->addAttr(::new (S.Context) |
| 4311 | NSConsumesSelfAttr(Attr.getRange(), S.Context, |
| 4312 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4313 | } |
| 4314 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4315 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4316 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4317 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4318 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4319 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4320 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4321 | returnType = MD->getResultType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4322 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4323 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4324 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4325 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4326 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4327 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4328 | returnType = FD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4329 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4330 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4331 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 4332 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4333 | return; |
| 4334 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4335 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4336 | bool typeOK; |
| 4337 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4338 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4339 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4340 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4341 | case AttributeList::AT_NSReturnsRetained: |
| 4342 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4343 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4344 | cf = false; |
| 4345 | break; |
| 4346 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4347 | case AttributeList::AT_CFReturnsRetained: |
| 4348 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4349 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4350 | cf = true; |
| 4351 | break; |
| 4352 | } |
| 4353 | |
| 4354 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4355 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 4356 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4357 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4358 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4359 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4360 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4361 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4362 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4363 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4364 | D->addAttr(::new (S.Context) |
| 4365 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 4366 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4367 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4368 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4369 | D->addAttr(::new (S.Context) |
| 4370 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4371 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4372 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4373 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4374 | D->addAttr(::new (S.Context) |
| 4375 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 4376 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4377 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4378 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4379 | D->addAttr(::new (S.Context) |
| 4380 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4381 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4382 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4383 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4384 | D->addAttr(::new (S.Context) |
| 4385 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 4386 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4387 | return; |
| 4388 | }; |
| 4389 | } |
| 4390 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4391 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4392 | const AttributeList &attr) { |
| 4393 | SourceLocation loc = attr.getLoc(); |
| 4394 | |
| 4395 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4396 | |
Fariborz Jahanian | a53e5d7 | 2012-04-21 17:51:44 +0000 | [diff] [blame] | 4397 | if (!method) { |
Fariborz Jahanian | 344d65c | 2012-04-20 22:00:46 +0000 | [diff] [blame] | 4398 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4399 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4400 | return; |
| 4401 | } |
| 4402 | |
| 4403 | // Check that the method returns a normal pointer. |
| 4404 | QualType resultType = method->getResultType(); |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4405 | |
| 4406 | if (!resultType->isReferenceType() && |
| 4407 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4408 | S.Diag(method->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4409 | << SourceRange(loc) |
| 4410 | << attr.getName() << /*method*/ 1 << /*non-retainable pointer*/ 2; |
| 4411 | |
| 4412 | // Drop the attribute. |
| 4413 | return; |
| 4414 | } |
| 4415 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4416 | method->addAttr(::new (S.Context) |
| 4417 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 4418 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4421 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4422 | const AttributeList &attr) { |
| 4423 | SourceLocation loc = attr.getLoc(); |
| 4424 | ObjCMethodDecl *method = dyn_cast<ObjCMethodDecl>(D); |
| 4425 | |
| 4426 | if (!method) { |
| 4427 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
| 4428 | << SourceRange(loc, loc) << attr.getName() << ExpectedMethod; |
| 4429 | return; |
| 4430 | } |
| 4431 | DeclContext *DC = method->getDeclContext(); |
| 4432 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4433 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4434 | << attr.getName() << 0; |
| 4435 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4436 | return; |
| 4437 | } |
| 4438 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4439 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4440 | << attr.getName() << 1; |
| 4441 | return; |
| 4442 | } |
| 4443 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4444 | method->addAttr(::new (S.Context) |
| 4445 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4446 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4449 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 4450 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
| 4451 | if (!isa<FunctionDecl>(D)) { |
| 4452 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4453 | << A.getRange() << A.getName() << ExpectedFunction; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4454 | return; |
| 4455 | } |
| 4456 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4457 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4458 | |
| 4459 | // Check whether there's a conflicting attribute already present. |
| 4460 | Attr *Existing; |
| 4461 | if (IsAudited) { |
| 4462 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 4463 | } else { |
| 4464 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 4465 | } |
| 4466 | if (Existing) { |
| 4467 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 4468 | << A.getName() |
| 4469 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 4470 | << A.getRange() << Existing->getRange(); |
| 4471 | return; |
| 4472 | } |
| 4473 | |
| 4474 | // All clear; add the attribute. |
| 4475 | if (IsAudited) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4476 | D->addAttr(::new (S.Context) |
| 4477 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 4478 | A.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4479 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4480 | D->addAttr(::new (S.Context) |
| 4481 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 4482 | A.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4483 | } |
| 4484 | } |
| 4485 | |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4486 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4487 | const AttributeList &Attr) { |
| 4488 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4489 | if (!RD || RD->isUnion()) { |
| 4490 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4491 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
| 4494 | IdentifierInfo *ParmName = Attr.getParameterName(); |
| 4495 | |
| 4496 | // In Objective-C, verify that the type names an Objective-C type. |
| 4497 | // We don't want to check this outside of ObjC because people sometimes |
| 4498 | // do crazy C declarations of Objective-C types. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4499 | if (ParmName && S.getLangOpts().ObjC1) { |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4500 | // Check for an existing type with this name. |
| 4501 | LookupResult R(S, DeclarationName(ParmName), Attr.getParameterLoc(), |
| 4502 | Sema::LookupOrdinaryName); |
| 4503 | if (S.LookupName(R, Sc)) { |
| 4504 | NamedDecl *Target = R.getFoundDecl(); |
| 4505 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 4506 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 4507 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 4508 | } |
| 4509 | } |
| 4510 | } |
| 4511 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4512 | D->addAttr(::new (S.Context) |
| 4513 | NSBridgedAttr(Attr.getRange(), S.Context, ParmName, |
| 4514 | Attr.getAttributeSpellingListIndex())); |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4517 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4518 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4519 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4520 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4521 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4522 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4525 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4526 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4527 | if (!isa<VarDecl>(D) && !isa<FieldDecl>(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4528 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4529 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4530 | return; |
| 4531 | } |
| 4532 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4533 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4534 | QualType type = vd->getType(); |
| 4535 | |
| 4536 | if (!type->isDependentType() && |
| 4537 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4538 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4539 | << type; |
| 4540 | return; |
| 4541 | } |
| 4542 | |
| 4543 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4544 | |
| 4545 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4546 | // going to infer. |
| 4547 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4548 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4549 | |
| 4550 | switch (lifetime) { |
| 4551 | case Qualifiers::OCL_None: |
| 4552 | assert(type->isDependentType() && |
| 4553 | "didn't infer lifetime for non-dependent type?"); |
| 4554 | break; |
| 4555 | |
| 4556 | case Qualifiers::OCL_Weak: // meaningful |
| 4557 | case Qualifiers::OCL_Strong: // meaningful |
| 4558 | break; |
| 4559 | |
| 4560 | case Qualifiers::OCL_ExplicitNone: |
| 4561 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4562 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4563 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4564 | break; |
| 4565 | } |
| 4566 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4567 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4568 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4569 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4570 | } |
| 4571 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4572 | //===----------------------------------------------------------------------===// |
| 4573 | // Microsoft specific attribute handlers. |
| 4574 | //===----------------------------------------------------------------------===// |
| 4575 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4576 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Francois Pichet | 0706d20 | 2011-09-17 17:15:52 +0000 | [diff] [blame] | 4577 | if (S.LangOpts.MicrosoftExt || S.LangOpts.Borland) { |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4578 | // check the attribute arguments. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4579 | if (!checkAttributeNumArgs(S, Attr, 1)) |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4580 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4581 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4582 | Expr *Arg = Attr.getArg(0); |
| 4583 | StringLiteral *Str = dyn_cast<StringLiteral>(Arg); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 4584 | if (!Str || !Str->isAscii()) { |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4585 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_not_string) |
| 4586 | << "uuid" << 1; |
| 4587 | return; |
| 4588 | } |
| 4589 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4590 | StringRef StrRef = Str->getString(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4591 | |
| 4592 | bool IsCurly = StrRef.size() > 1 && StrRef.front() == '{' && |
| 4593 | StrRef.back() == '}'; |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4594 | |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4595 | // Validate GUID length. |
| 4596 | if (IsCurly && StrRef.size() != 38) { |
| 4597 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4598 | return; |
| 4599 | } |
| 4600 | if (!IsCurly && StrRef.size() != 36) { |
| 4601 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4602 | return; |
| 4603 | } |
| 4604 | |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4605 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4606 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}" |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4607 | StringRef::iterator I = StrRef.begin(); |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4608 | if (IsCurly) // Skip the optional '{' |
| 4609 | ++I; |
| 4610 | |
| 4611 | for (int i = 0; i < 36; ++i) { |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4612 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
| 4613 | if (*I != '-') { |
| 4614 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4615 | return; |
| 4616 | } |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 4617 | } else if (!isHexDigit(*I)) { |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4618 | S.Diag(Attr.getLoc(), diag::err_attribute_uuid_malformed_guid); |
| 4619 | return; |
| 4620 | } |
| 4621 | I++; |
| 4622 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4623 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4624 | D->addAttr(::new (S.Context) |
| 4625 | UuidAttr(Attr.getRange(), S.Context, Str->getString(), |
| 4626 | Attr.getAttributeSpellingListIndex())); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4627 | } else |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4628 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "uuid"; |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4631 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4632 | if (!S.LangOpts.MicrosoftExt) { |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4633 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4634 | return; |
| 4635 | } |
| 4636 | |
| 4637 | AttributeList::Kind Kind = Attr.getKind(); |
| 4638 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4639 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4640 | ::new (S.Context) |
| 4641 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4642 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4643 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4644 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4645 | ::new (S.Context) |
| 4646 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4647 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4648 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4649 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4650 | ::new (S.Context) |
| 4651 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4652 | Attr.getAttributeSpellingListIndex())); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4653 | } |
| 4654 | |
| 4655 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4656 | if (S.LangOpts.MicrosoftExt) { |
| 4657 | AttributeList::Kind Kind = Attr.getKind(); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4658 | if (Kind == AttributeList::AT_Ptr32) |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4659 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4660 | ::new (S.Context) Ptr32Attr(Attr.getRange(), S.Context, |
| 4661 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4662 | else if (Kind == AttributeList::AT_Ptr64) |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4663 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4664 | ::new (S.Context) Ptr64Attr(Attr.getRange(), S.Context, |
| 4665 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4666 | else if (Kind == AttributeList::AT_Win64) |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4667 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4668 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4669 | Attr.getAttributeSpellingListIndex())); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4670 | } else |
| 4671 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4672 | } |
| 4673 | |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4674 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4675 | if (S.LangOpts.MicrosoftExt) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4676 | D->addAttr(::new (S.Context) |
| 4677 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4678 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4679 | else |
| 4680 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4681 | } |
| 4682 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4683 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4684 | // Top Level Sema Entry Points |
| 4685 | //===----------------------------------------------------------------------===// |
| 4686 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4687 | static void ProcessNonInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4688 | const AttributeList &Attr) { |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4689 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4690 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4691 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
| 4692 | case AttributeList::AT_Overloadable:handleOverloadableAttr(S, D, Attr); break; |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4693 | default: |
| 4694 | break; |
| 4695 | } |
| 4696 | } |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4697 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4698 | static void ProcessInheritableDeclAttr(Sema &S, Scope *scope, Decl *D, |
| 4699 | const AttributeList &Attr) { |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4700 | switch (Attr.getKind()) { |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4701 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4702 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4703 | case AttributeList::AT_IBOutletCollection: |
| 4704 | handleIBOutletCollection(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4705 | case AttributeList::AT_AddressSpace: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4706 | case AttributeList::AT_ObjCGC: |
| 4707 | case AttributeList::AT_VectorSize: |
| 4708 | case AttributeList::AT_NeonVectorType: |
| 4709 | case AttributeList::AT_NeonPolyVectorType: |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4710 | // Ignore these, these are type attributes, handled by |
| 4711 | // ProcessTypeAttributes. |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4712 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4713 | case AttributeList::AT_CUDADevice: |
| 4714 | case AttributeList::AT_CUDAHost: |
| 4715 | case AttributeList::AT_Overloadable: |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4716 | // Ignore, this is a non-inheritable attribute, handled |
| 4717 | // by ProcessNonInheritableDeclAttr. |
| 4718 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4719 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4720 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4721 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4722 | case AttributeList::AT_AlwaysInline: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4723 | handleAlwaysInlineAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4724 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4725 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4726 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4727 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4728 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4729 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4730 | handleDependencyAttr(S, scope, D, Attr); |
| 4731 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4732 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4733 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4734 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4735 | case AttributeList::AT_CXX11NoReturn: |
| 4736 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4737 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4738 | case AttributeList::AT_Deprecated: |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4739 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr, "deprecated"); |
| 4740 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4741 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4742 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4743 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4744 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4745 | case AttributeList::AT_MinSize: |
| 4746 | handleMinSizeAttr(S, D, Attr); |
| 4747 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4748 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4749 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4750 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
| 4751 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4752 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4753 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4754 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4755 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
| 4756 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
| 4757 | case AttributeList::AT_MayAlias: handleMayAliasAttr (S, D, Attr); break; |
| 4758 | case AttributeList::AT_NoCommon: handleNoCommonAttr (S, D, Attr); break; |
| 4759 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 4760 | case AttributeList::AT_ownership_returns: |
| 4761 | case AttributeList::AT_ownership_takes: |
| 4762 | case AttributeList::AT_ownership_holds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4763 | handleOwnershipAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4764 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4765 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
| 4766 | case AttributeList::AT_Naked: handleNakedAttr (S, D, Attr); break; |
| 4767 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4768 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4769 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4770 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4771 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4772 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4773 | handleObjCOwnershipAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4774 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4775 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4776 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4777 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4778 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4779 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4780 | case AttributeList::AT_ObjCRequiresSuper: |
| 4781 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4782 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4783 | case AttributeList::AT_NSBridged: |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4784 | handleNSBridgedAttr(S, scope, D, Attr); break; |
| 4785 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4786 | case AttributeList::AT_CFAuditedTransfer: |
| 4787 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4788 | handleCFTransferAttr(S, D, Attr); break; |
| 4789 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4790 | // Checker-specific. |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4791 | case AttributeList::AT_CFConsumed: |
| 4792 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4793 | case AttributeList::AT_NSConsumesSelf: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4794 | handleNSConsumesSelfAttr(S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4795 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4796 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4797 | case AttributeList::AT_NSReturnsNotRetained: |
| 4798 | case AttributeList::AT_CFReturnsNotRetained: |
| 4799 | case AttributeList::AT_NSReturnsRetained: |
| 4800 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4801 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4802 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4803 | case AttributeList::AT_WorkGroupSizeHint: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4804 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4805 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4806 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4807 | case AttributeList::AT_VecTypeHint: |
| 4808 | handleVecTypeHint(S, D, Attr); break; |
| 4809 | |
Joey Gouly | 0608ae8 | 2013-03-14 09:54:43 +0000 | [diff] [blame] | 4810 | case AttributeList::AT_Endian: |
| 4811 | handleEndianAttr(S, D, Attr); |
| 4812 | break; |
| 4813 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4814 | case AttributeList::AT_InitPriority: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4815 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4816 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4817 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4818 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4819 | case AttributeList::AT_Unavailable: |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4820 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr, "unavailable"); |
| 4821 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4822 | case AttributeList::AT_ArcWeakrefUnavailable: |
Fariborz Jahanian | 1f626d6 | 2011-07-06 19:24:05 +0000 | [diff] [blame] | 4823 | handleArcWeakrefUnavailableAttr (S, D, Attr); |
| 4824 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4825 | case AttributeList::AT_ObjCRootClass: |
Patrick Beard | acfbe9e | 2012-04-06 18:12:22 +0000 | [diff] [blame] | 4826 | handleObjCRootClassAttr(S, D, Attr); |
| 4827 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4828 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Ted Kremenek | 0c2c90b | 2012-01-05 22:47:47 +0000 | [diff] [blame] | 4829 | handleObjCRequiresPropertyDefsAttr (S, D, Attr); |
Fariborz Jahanian | 9d4d20a | 2012-01-03 18:45:41 +0000 | [diff] [blame] | 4830 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4831 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4832 | case AttributeList::AT_ReturnsTwice: |
Rafael Espindola | 70107f9 | 2011-10-03 14:59:42 +0000 | [diff] [blame] | 4833 | handleReturnsTwiceAttr(S, D, Attr); |
| 4834 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4835 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4836 | case AttributeList::AT_Visibility: |
| 4837 | handleVisibilityAttr(S, D, Attr, false); |
| 4838 | break; |
| 4839 | case AttributeList::AT_TypeVisibility: |
| 4840 | handleVisibilityAttr(S, D, Attr, true); |
| 4841 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4842 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4843 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4844 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4845 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4846 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4847 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4848 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4849 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4850 | case AttributeList::AT_ObjCException: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4851 | handleObjCExceptionAttr(S, D, Attr); |
Chris Lattner | 677a358 | 2009-02-14 08:09:34 +0000 | [diff] [blame] | 4852 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4853 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4854 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4855 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4856 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4857 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4858 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4859 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
| 4860 | case AttributeList::AT_Pure: handlePureAttr (S, D, Attr); break; |
| 4861 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4862 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
| 4863 | case AttributeList::AT_NoInline: handleNoInlineAttr (S, D, Attr); break; |
| 4864 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4865 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | b4f3134 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4866 | // Just ignore |
| 4867 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4868 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4869 | handleNoInstrumentFunctionAttr(S, D, Attr); |
Chris Lattner | 3c77a35 | 2010-06-22 00:03:40 +0000 | [diff] [blame] | 4870 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4871 | case AttributeList::AT_StdCall: |
| 4872 | case AttributeList::AT_CDecl: |
| 4873 | case AttributeList::AT_FastCall: |
| 4874 | case AttributeList::AT_ThisCall: |
| 4875 | case AttributeList::AT_Pascal: |
| 4876 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4877 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4878 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4879 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4880 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4881 | case AttributeList::AT_OpenCLKernel: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4882 | handleOpenCLKernelAttr(S, D, Attr); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 4883 | break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame^] | 4884 | case AttributeList::AT_OpenCLImageAccess: |
| 4885 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4886 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4887 | |
| 4888 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4889 | case AttributeList::AT_MsStruct: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4890 | handleMsStructAttr(S, D, Attr); |
| 4891 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4892 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4893 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4894 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4895 | case AttributeList::AT_SingleInheritance: |
| 4896 | case AttributeList::AT_MultipleInheritance: |
| 4897 | case AttributeList::AT_VirtualInheritance: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4898 | handleInheritanceAttr(S, D, Attr); |
| 4899 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4900 | case AttributeList::AT_Win64: |
| 4901 | case AttributeList::AT_Ptr32: |
| 4902 | case AttributeList::AT_Ptr64: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4903 | handlePortabilityAttr(S, D, Attr); |
| 4904 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4905 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4906 | handleForceInlineAttr(S, D, Attr); |
| 4907 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4908 | |
| 4909 | // Thread safety attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4910 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4911 | handleGuardedVarAttr(S, D, Attr); |
| 4912 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4913 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4914 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4915 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4916 | case AttributeList::AT_ScopedLockable: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4917 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4918 | break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4919 | case AttributeList::AT_NoSanitizeAddress: |
| 4920 | handleNoSanitizeAddressAttr(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4921 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4922 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4923 | handleNoThreadSafetyAnalysis(S, D, Attr); |
| 4924 | break; |
| 4925 | case AttributeList::AT_NoSanitizeThread: |
| 4926 | handleNoSanitizeThread(S, D, Attr); |
| 4927 | break; |
| 4928 | case AttributeList::AT_NoSanitizeMemory: |
| 4929 | handleNoSanitizeMemory(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4930 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4931 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4932 | handleLockableAttr(S, D, Attr); |
| 4933 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4934 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4935 | handleGuardedByAttr(S, D, Attr); |
| 4936 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4937 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4938 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4939 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4940 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4941 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4942 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4943 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4944 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4945 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4946 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4947 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4948 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4949 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4950 | handleLockReturnedAttr(S, D, Attr); |
| 4951 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4952 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4953 | handleLocksExcludedAttr(S, D, Attr); |
| 4954 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4955 | case AttributeList::AT_SharedLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4956 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4957 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4958 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4959 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4960 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4961 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4962 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4963 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4964 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4965 | handleUnlockFunAttr(S, D, Attr); |
| 4966 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4967 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4968 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4969 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4970 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4971 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4972 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4973 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4974 | // Type safety attributes. |
| 4975 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4976 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4977 | break; |
| 4978 | case AttributeList::AT_TypeTagForDatatype: |
| 4979 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4980 | break; |
| 4981 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4982 | default: |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4983 | // Ask target about the attribute. |
| 4984 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4985 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4986 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4987 | diag::warn_unhandled_ms_attribute_ignored : |
| 4988 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4989 | break; |
| 4990 | } |
| 4991 | } |
| 4992 | |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4993 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4994 | /// the attribute applies to decls. If the attribute is a type attribute, just |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4995 | /// silently ignore it if a GNU attribute. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4996 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4997 | const AttributeList &Attr, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4998 | bool NonInheritable, bool Inheritable, |
| 4999 | bool IncludeCXX11Attributes) { |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5000 | if (Attr.isInvalid()) |
| 5001 | return; |
| 5002 | |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5003 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 5004 | // instead. |
| 5005 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 5006 | return; |
| 5007 | |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5008 | if (NonInheritable) |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5009 | ProcessNonInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5010 | |
| 5011 | if (Inheritable) |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5012 | ProcessInheritableDeclAttr(S, scope, D, Attr); |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5013 | } |
| 5014 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5015 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 5016 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 5017 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5018 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5019 | bool NonInheritable, bool Inheritable, |
| 5020 | bool IncludeCXX11Attributes) { |
| 5021 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
| 5022 | ProcessDeclAttribute(*this, S, D, *l, NonInheritable, Inheritable, |
| 5023 | IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5024 | |
| 5025 | // GCC accepts |
| 5026 | // static int a9 __attribute__((weakref)); |
| 5027 | // but that looks really pointless. We reject it. |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5028 | if (Inheritable && D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5029 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5030 | cast<NamedDecl>(D)->getNameAsString(); |
| 5031 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5032 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5033 | } |
| 5034 | } |
| 5035 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5036 | // Annotation attributes are the only attributes allowed after an access |
| 5037 | // specifier. |
| 5038 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 5039 | const AttributeList *AttrList) { |
| 5040 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5041 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5042 | handleAnnotateAttr(*this, ASDecl, *l); |
| 5043 | } else { |
| 5044 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 5045 | return true; |
| 5046 | } |
| 5047 | } |
| 5048 | |
| 5049 | return false; |
| 5050 | } |
| 5051 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5052 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 5053 | /// contains any decl attributes that we should warn about. |
| 5054 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 5055 | for ( ; A; A = A->getNext()) { |
| 5056 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 5057 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5058 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 5059 | |
| 5060 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 5061 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 5062 | << A->getName() << A->getRange(); |
| 5063 | } else { |
| 5064 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 5065 | << A->getName() << A->getRange(); |
| 5066 | } |
| 5067 | } |
| 5068 | } |
| 5069 | |
| 5070 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 5071 | /// used to build a declaration, complain about any decl attributes |
| 5072 | /// which might be lying around on it. |
| 5073 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 5074 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 5075 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5076 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5077 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5078 | } |
| 5079 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5080 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5081 | /// \#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] | 5082 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5083 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5084 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5085 | NamedDecl *NewD = 0; |
| 5086 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5087 | FunctionDecl *NewFD; |
| 5088 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 5089 | // FIXME: Mangling? |
| 5090 | // FIXME: Is the qualifier info correct? |
| 5091 | // FIXME: Is the DeclContext correct? |
| 5092 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 5093 | Loc, Loc, DeclarationName(II), |
| 5094 | FD->getType(), FD->getTypeSourceInfo(), |
| 5095 | SC_None, SC_None, |
| 5096 | false/*isInlineSpecified*/, |
| 5097 | FD->hasPrototype(), |
| 5098 | false/*isConstexprSpecified*/); |
| 5099 | NewD = NewFD; |
| 5100 | |
| 5101 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5102 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5103 | |
| 5104 | // Fake up parameter variables; they are declared as if this were |
| 5105 | // a typedef. |
| 5106 | QualType FDTy = FD->getType(); |
| 5107 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5108 | SmallVector<ParmVarDecl*, 16> Params; |
| 5109 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 5110 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 5111 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 5112 | Param->setScopeInfo(0, Params.size()); |
| 5113 | Params.push_back(Param); |
| 5114 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5115 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5116 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5117 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5118 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5119 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5120 | VD->getType(), VD->getTypeSourceInfo(), |
Douglas Gregor | c4df407 | 2010-04-19 22:54:31 +0000 | [diff] [blame] | 5121 | VD->getStorageClass(), |
| 5122 | VD->getStorageClassAsWritten()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5123 | if (VD->getQualifier()) { |
| 5124 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5125 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5126 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5127 | } |
| 5128 | return NewD; |
| 5129 | } |
| 5130 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5131 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5132 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5133 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5134 | if (W.getUsed()) return; // only do this once |
| 5135 | W.setUsed(true); |
| 5136 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5137 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5138 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5139 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 5140 | NDId->getName())); |
| 5141 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5142 | WeakTopLevelDecl.push_back(NewD); |
| 5143 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5144 | // to insert Decl at TU scope, sorry. |
| 5145 | DeclContext *SavedContext = CurContext; |
| 5146 | CurContext = Context.getTranslationUnitDecl(); |
| 5147 | PushOnScopeChains(NewD, S); |
| 5148 | CurContext = SavedContext; |
| 5149 | } else { // just add weak to existing |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 5150 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5151 | } |
| 5152 | } |
| 5153 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 5154 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 5155 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 5156 | // have to do this. |
| 5157 | LoadExternalWeakUndeclaredIdentifiers(); |
| 5158 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 5159 | NamedDecl *ND = NULL; |
| 5160 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 5161 | if (VD->isExternC()) |
| 5162 | ND = VD; |
| 5163 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 5164 | if (FD->isExternC()) |
| 5165 | ND = FD; |
| 5166 | if (ND) { |
| 5167 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 5168 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 5169 | = WeakUndeclaredIdentifiers.find(Id); |
| 5170 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 5171 | WeakInfo W = I->second; |
| 5172 | DeclApplyPragmaWeak(S, ND, W); |
| 5173 | WeakUndeclaredIdentifiers[Id] = W; |
| 5174 | } |
| 5175 | } |
| 5176 | } |
| 5177 | } |
| 5178 | } |
| 5179 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5180 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 5181 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 5182 | /// specified in many different places, and we need to find and apply them all. |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5183 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD, |
| 5184 | bool NonInheritable, bool Inheritable) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5185 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5186 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5187 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5188 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5189 | // Walk the declarator structure, applying decl attributes that were in a type |
| 5190 | // position to the decl itself. This handles cases like: |
| 5191 | // int *__attr__(x)** D; |
| 5192 | // when X is a decl attribute. |
| 5193 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 5194 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5195 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable, |
| 5196 | /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 5197 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5198 | // Finally, apply any attributes on the decl itself. |
| 5199 | if (const AttributeList *Attrs = PD.getAttributes()) |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5200 | ProcessDeclAttributeList(S, D, Attrs, NonInheritable, Inheritable); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5201 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5202 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5203 | /// Is the given declaration allowed to use a forbidden type? |
| 5204 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 5205 | // Private ivars are always okay. Unfortunately, people don't |
| 5206 | // always properly make their ivars private, even in system headers. |
| 5207 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 5208 | // Function declarations in sys headers will be marked unavailable. |
| 5209 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 5210 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5211 | return false; |
| 5212 | |
| 5213 | // Require it to be declared in a system header. |
| 5214 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 5215 | } |
| 5216 | |
| 5217 | /// Handle a delayed forbidden-type diagnostic. |
| 5218 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 5219 | Decl *decl) { |
| 5220 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 5221 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 5222 | "this system declaration uses an unsupported type")); |
| 5223 | return; |
| 5224 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5225 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5226 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 5227 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 5228 | // kind of forbidden type messages on unavailable functions. |
| 5229 | if (FD->hasAttr<UnavailableAttr>() && |
| 5230 | diag.getForbiddenTypeDiagnostic() == |
| 5231 | diag::err_arc_array_param_no_ownership) { |
| 5232 | diag.Triggered = true; |
| 5233 | return; |
| 5234 | } |
| 5235 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5236 | |
| 5237 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 5238 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 5239 | diag.Triggered = true; |
| 5240 | } |
| 5241 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5242 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 5243 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5244 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5245 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5246 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5247 | // When delaying diagnostics to run in the context of a parsed |
| 5248 | // declaration, we only want to actually emit anything if parsing |
| 5249 | // succeeds. |
| 5250 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5251 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5252 | // We emit all the active diagnostics in this pool or any of its |
| 5253 | // parents. In general, we'll get one pool for the decl spec |
| 5254 | // and a child pool for each declarator; in a decl group like: |
| 5255 | // deprecated_typedef foo, *bar, baz(); |
| 5256 | // only the declarator pops will be passed decls. This is correct; |
| 5257 | // we really do need to consider delayed diagnostics from the decl spec |
| 5258 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5259 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5260 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5261 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5262 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 5263 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 5264 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5265 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5266 | continue; |
| 5267 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5268 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5269 | case DelayedDiagnostic::Deprecation: |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 5270 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 5271 | if (!decl->isInvalidDecl()) |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5272 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5273 | break; |
| 5274 | |
| 5275 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5276 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5277 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5278 | |
| 5279 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5280 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5281 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5282 | } |
| 5283 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 5284 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5285 | } |
| 5286 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 5287 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 5288 | /// been delayed in the current context instead of in the given pool. |
| 5289 | /// Essentially, this just moves them to the current pool. |
| 5290 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 5291 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 5292 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 5293 | curPool->steal(pool); |
| 5294 | } |
| 5295 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5296 | static bool isDeclDeprecated(Decl *D) { |
| 5297 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 5298 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5299 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 5300 | // A category implicitly has the availability of the interface. |
| 5301 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 5302 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5303 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 5304 | return false; |
| 5305 | } |
| 5306 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5307 | static void |
| 5308 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 5309 | SourceLocation Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5310 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5311 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5312 | DeclarationName Name = D->getDeclName(); |
| 5313 | if (!Message.empty()) { |
| 5314 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 5315 | S.Diag(D->getLocation(), |
| 5316 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5317 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5318 | if (ObjCPropery) |
| 5319 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5320 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5321 | } else if (!UnknownObjCClass) { |
| 5322 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 5323 | S.Diag(D->getLocation(), |
| 5324 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 5325 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5326 | if (ObjCPropery) |
| 5327 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 5328 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5329 | } else { |
| 5330 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 5331 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 5332 | } |
| 5333 | } |
| 5334 | |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 5335 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5336 | Decl *Ctx) { |
| 5337 | if (isDeclDeprecated(Ctx)) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5338 | return; |
| 5339 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 5340 | DD.Triggered = true; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 5341 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 5342 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5343 | DD.getUnknownObjCClass(), |
| 5344 | DD.getObjCProperty()); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5345 | } |
| 5346 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5347 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 5348 | SourceLocation Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5349 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 5350 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5351 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 5352 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5353 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 5354 | UnknownObjCClass, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5355 | ObjCProperty, |
Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 5356 | Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5357 | return; |
| 5358 | } |
| 5359 | |
| 5360 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 5361 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5362 | return; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 5363 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5364 | } |