Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetAttributesSema.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 22 | #include "clang/AST/Mangle.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 23 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 30 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 32 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 33 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 34 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 35 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 36 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 37 | C, |
| 38 | Cpp, |
| 39 | ObjC |
| 40 | }; |
| 41 | } |
| 42 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 43 | //===----------------------------------------------------------------------===// |
| 44 | // Helper functions |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 47 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 48 | bool blocksToo = true) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 49 | QualType Ty; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 50 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 51 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 52 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 53 | Ty = decl->getUnderlyingType(); |
| 54 | else |
| 55 | return 0; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 57 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 58 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 28c433d | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 59 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 60 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 61 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 62 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 65 | // FIXME: We should provide an abstraction around a method or function |
| 66 | // to provide the following bits of information. |
| 67 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 68 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 69 | /// type (function or function-typed variable). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 70 | static bool isFunction(const Decl *D) { |
| 71 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 75 | /// type (function or function-typed variable) or an Objective-C |
| 76 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 77 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 78 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 81 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 82 | /// type (function or function-typed variable) or an Objective-C |
| 83 | /// method or a block. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 84 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 85 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 86 | return true; |
| 87 | // check for block is more involved. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 88 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 89 | QualType Ty = V->getType(); |
| 90 | return Ty->isBlockPointerType(); |
| 91 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 92 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 93 | } |
| 94 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 95 | /// Return true if the given decl has a declarator that should have |
| 96 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 97 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 98 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 99 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 100 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 103 | /// hasFunctionProto - Return true if the given decl has a argument |
| 104 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 105 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 106 | static bool hasFunctionProto(const Decl *D) { |
| 107 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 108 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 109 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 110 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 116 | /// arguments. It is an error to call this on a K&R function (use |
| 117 | /// hasFunctionProto first). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 118 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 119 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 120 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 121 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 122 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 123 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 126 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 127 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 128 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 129 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 130 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 131 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 132 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 135 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 136 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 137 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 138 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 141 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 142 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 143 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 144 | return proto->isVariadic(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 145 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 146 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 147 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 148 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 152 | static bool isInstanceMethod(const Decl *D) { |
| 153 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 154 | return MethodDecl->isInstance(); |
| 155 | return false; |
| 156 | } |
| 157 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 158 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 159 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 160 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 161 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 162 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 163 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 164 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 165 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 166 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 167 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 169 | // FIXME: Should we walk the chain of classes? |
| 170 | return ClsName == &Ctx.Idents.get("NSString") || |
| 171 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 172 | } |
| 173 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 174 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 175 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 176 | if (!PT) |
| 177 | return false; |
| 178 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 179 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 180 | if (!RT) |
| 181 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 182 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 183 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 184 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 185 | return false; |
| 186 | |
| 187 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 188 | } |
| 189 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 190 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 191 | // FIXME: Include the type in the argument list. |
| 192 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 193 | } |
| 194 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 195 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 196 | /// output an error. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 197 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 198 | unsigned Num) { |
| 199 | if (getNumAttributeArgs(Attr) != Num) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 200 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 201 | << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 208 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 209 | /// \brief Check if the attribute has at least as many args as Num. May |
| 210 | /// output an error. |
| 211 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 212 | unsigned Num) { |
| 213 | if (getNumAttributeArgs(Attr) < Num) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 214 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 215 | return false; |
| 216 | } |
| 217 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 218 | return true; |
| 219 | } |
| 220 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 221 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 222 | /// expression and return success or failure. May output an error. |
| 223 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 224 | const Expr *Expr, uint32_t &Val, |
| 225 | unsigned Idx = UINT_MAX) { |
| 226 | llvm::APSInt I(32); |
| 227 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 228 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 229 | if (Idx != UINT_MAX) |
| 230 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 231 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 232 | << Expr->getSourceRange(); |
| 233 | else |
| 234 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 235 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 236 | << Expr->getSourceRange(); |
| 237 | return false; |
| 238 | } |
| 239 | Val = (uint32_t)I.getZExtValue(); |
| 240 | return true; |
| 241 | } |
| 242 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 243 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 244 | /// declaration. Returns true if diagnosed. |
| 245 | template <typename AttrTy> |
| 246 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, |
| 247 | const AttributeList &Attr, |
| 248 | const char *OtherName) { |
| 249 | // FIXME: it would be nice if OtherName did not have to be passed in, but was |
| 250 | // instead determined based on the AttrTy template parameter. |
| 251 | if (D->hasAttr<AttrTy>()) { |
| 252 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 253 | << Attr.getName() << OtherName; |
| 254 | return true; |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 259 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 260 | /// instance method D. May output an error. |
| 261 | /// |
| 262 | /// \returns true if IdxExpr is a valid index. |
| 263 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 264 | StringRef AttrName, |
| 265 | SourceLocation AttrLoc, |
| 266 | unsigned AttrArgNum, |
| 267 | const Expr *IdxExpr, |
| 268 | uint64_t &Idx) |
| 269 | { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 270 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 271 | |
| 272 | // In C++ the implicit 'this' function parameter also counts. |
| 273 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 274 | bool HP = hasFunctionProto(D); |
| 275 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 276 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 277 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 278 | HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 279 | |
| 280 | llvm::APSInt IdxInt; |
| 281 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 282 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 283 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 284 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 285 | << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | |
| 289 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 290 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 291 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 292 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 293 | return false; |
| 294 | } |
| 295 | Idx--; // Convert to zero-based. |
| 296 | if (HasImplicitThisParam) { |
| 297 | if (Idx == 0) { |
| 298 | S.Diag(AttrLoc, |
| 299 | diag::err_attribute_invalid_implicit_this_argument) |
| 300 | << AttrName << IdxExpr->getSourceRange(); |
| 301 | return false; |
| 302 | } |
| 303 | --Idx; |
| 304 | } |
| 305 | |
| 306 | return true; |
| 307 | } |
| 308 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 309 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 310 | /// If not emit an error and return false. If the argument is an identifier it |
| 311 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 312 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 313 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 314 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 315 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 316 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 317 | if (Attr.isArgIdent(ArgNum)) { |
| 318 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 319 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 320 | << Attr.getName() << AANT_ArgumentString |
| 321 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 322 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 323 | Str = Loc->Ident->getName(); |
| 324 | if (ArgLocation) |
| 325 | *ArgLocation = Loc->Loc; |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | // Now check for an actual string literal. |
| 330 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 331 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 332 | if (ArgLocation) |
| 333 | *ArgLocation = ArgExpr->getLocStart(); |
| 334 | |
| 335 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 336 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 337 | << Attr.getName() << AANT_ArgumentString; |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | Str = Literal->getString(); |
| 342 | return true; |
| 343 | } |
| 344 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 345 | /// \brief Applies the given attribute to the Decl without performing any |
| 346 | /// additional semantic checking. |
| 347 | template <typename AttrType> |
| 348 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 349 | const AttributeList &Attr) { |
| 350 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 351 | Attr.getAttributeSpellingListIndex())); |
| 352 | } |
| 353 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 354 | /// \brief Check if the passed-in expression is of type int or bool. |
| 355 | static bool isIntOrBool(Expr *Exp) { |
| 356 | QualType QT = Exp->getType(); |
| 357 | return QT->isBooleanType() || QT->isIntegerType(); |
| 358 | } |
| 359 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 360 | |
| 361 | // Check to see if the type is a smart pointer of some kind. We assume |
| 362 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 363 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 364 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 365 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 366 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 367 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 368 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 369 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 370 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 371 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 372 | return false; |
| 373 | |
| 374 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 377 | /// \brief Check if passed in Decl is a pointer type. |
| 378 | /// Note that this function may produce an error message. |
| 379 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 380 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 381 | const AttributeList &Attr) { |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 382 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 383 | QualType QT = vd->getType(); |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 384 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 385 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 386 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 387 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 388 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 389 | // (We don't want to force template instantiation if we can avoid it, |
| 390 | // since that would alter the order in which templates are instantiated.) |
| 391 | if (RT->isIncompleteType()) |
| 392 | return true; |
| 393 | |
| 394 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 395 | return true; |
| 396 | } |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 397 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 398 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 399 | << Attr.getName()->getName() << QT; |
| 400 | } else { |
| 401 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 402 | << Attr.getName(); |
| 403 | } |
| 404 | return false; |
| 405 | } |
| 406 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 407 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 408 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 409 | static const RecordType *getRecordType(QualType QT) { |
| 410 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 411 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 412 | |
| 413 | // Now check if we point to record type. |
| 414 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 415 | return PT->getPointeeType()->getAs<RecordType>(); |
| 416 | |
| 417 | return 0; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 418 | } |
| 419 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 420 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 421 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 422 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 423 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 424 | return RT->getDecl()->hasAttr<LockableAttr>(); |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 428 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 429 | /// resolves to a lockable object. |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 430 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 431 | QualType Ty) { |
| 432 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 433 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 434 | // Warn if could not get record type for this argument. |
Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 435 | if (!RT) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 436 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 437 | << Attr.getName() << Ty.getAsString(); |
| 438 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 439 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 440 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 441 | // 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] | 442 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 443 | return; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 444 | |
| 445 | // Allow smart pointers to be used as lockable objects. |
| 446 | // FIXME -- Check the type that the smart pointer points to. |
| 447 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 448 | return; |
| 449 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 450 | // Check if the type is lockable. |
| 451 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 452 | if (RD->hasAttr<LockableAttr>()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 453 | return; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 454 | |
| 455 | // Else check if any base classes are lockable. |
| 456 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 457 | CXXBasePaths BPaths(false, false); |
| 458 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 459 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 460 | } |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 461 | |
| 462 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 463 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 466 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 467 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 468 | /// \param Sidx The attribute argument index to start checking with. |
| 469 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 470 | /// parameter list. |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 471 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 472 | const AttributeList &Attr, |
| 473 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 474 | int Sidx = 0, |
| 475 | bool ParamIdxOk = false) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 476 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 477 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 478 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 479 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 480 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 481 | Args.push_back(ArgExp); |
| 482 | continue; |
| 483 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 484 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 485 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 486 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 487 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 488 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 489 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 490 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 491 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 492 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 493 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 494 | // We allow constant strings to be used as a placeholder for expressions |
| 495 | // that are not valid C++ syntax, but warn that they are ignored. |
| 496 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 497 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 498 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 499 | continue; |
| 500 | } |
| 501 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 502 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 503 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 504 | // A pointer to member expression of the form &MyClass::mu is treated |
| 505 | // specially -- we need to look at the type of the member. |
| 506 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 507 | if (UOp->getOpcode() == UO_AddrOf) |
| 508 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 509 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 510 | ArgTy = DRE->getDecl()->getType(); |
| 511 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 512 | // First see if we can just cast to record type, or point to record type. |
| 513 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 514 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 515 | // Now check if we index into a record type function param. |
| 516 | if(!RT && ParamIdxOk) { |
| 517 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 518 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 519 | if(FD && IL) { |
| 520 | unsigned int NumParams = FD->getNumParams(); |
| 521 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 522 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 523 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 524 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 525 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 526 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 527 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 528 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 529 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 533 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 534 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 535 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 536 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 539 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 540 | // Attribute Implementations |
| 541 | //===----------------------------------------------------------------------===// |
| 542 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 543 | // FIXME: All this manual attribute parsing code is gross. At the |
| 544 | // least add some helper functions to check most argument patterns (# |
| 545 | // and types of args). |
| 546 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 547 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 548 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 549 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 550 | return; |
| 551 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 552 | D->addAttr(::new (S.Context) |
| 553 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 554 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 555 | } |
| 556 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 557 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 558 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 559 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 560 | SmallVector<Expr*, 1> Args; |
| 561 | // check that all arguments are lockable objects |
| 562 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 563 | unsigned Size = Args.size(); |
| 564 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 565 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 566 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 567 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 568 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 569 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 572 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 573 | Expr *Arg = 0; |
| 574 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 575 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 576 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 577 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 578 | } |
| 579 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 580 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 581 | const AttributeList &Attr) { |
| 582 | Expr *Arg = 0; |
| 583 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 584 | return; |
| 585 | |
| 586 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 587 | return; |
| 588 | |
| 589 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 590 | S.Context, Arg)); |
| 591 | } |
| 592 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 593 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 594 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 595 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 596 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 597 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 598 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 599 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 600 | QualType QT = cast<ValueDecl>(D)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 601 | if (!QT->isDependentType()) { |
| 602 | const RecordType *RT = getRecordType(QT); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 603 | if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 604 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 605 | << Attr.getName(); |
| 606 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 610 | // Check that all arguments are lockable objects. |
| 611 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 612 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 613 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 614 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 615 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 618 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 619 | const AttributeList &Attr) { |
| 620 | SmallVector<Expr*, 1> Args; |
| 621 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 622 | return; |
| 623 | |
| 624 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 625 | D->addAttr(::new (S.Context) |
| 626 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 627 | StartArg, Args.size(), |
| 628 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 631 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 632 | const AttributeList &Attr) { |
| 633 | SmallVector<Expr*, 1> Args; |
| 634 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 635 | return; |
| 636 | |
| 637 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 638 | D->addAttr(::new (S.Context) |
| 639 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 640 | StartArg, Args.size(), |
| 641 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 642 | } |
| 643 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 644 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 645 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 646 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 647 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 648 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 649 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 650 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 651 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 654 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 655 | const AttributeList &Attr) { |
| 656 | SmallVector<Expr*, 1> Args; |
| 657 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 658 | return; |
| 659 | |
| 660 | unsigned Size = Args.size(); |
| 661 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 662 | D->addAttr(::new (S.Context) |
| 663 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 664 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 667 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 668 | const AttributeList &Attr) { |
| 669 | SmallVector<Expr*, 1> Args; |
| 670 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 671 | return; |
| 672 | |
| 673 | unsigned Size = Args.size(); |
| 674 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 675 | D->addAttr(::new (S.Context) |
| 676 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 677 | StartArg, Size, |
| 678 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 679 | } |
| 680 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 681 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 682 | const AttributeList &Attr) { |
| 683 | SmallVector<Expr*, 1> Args; |
| 684 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 685 | return; |
| 686 | |
| 687 | unsigned Size = Args.size(); |
| 688 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 689 | D->addAttr(::new (S.Context) |
| 690 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 691 | Attr.getAttributeSpellingListIndex())); |
| 692 | } |
| 693 | |
| 694 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 695 | const AttributeList &Attr) { |
| 696 | SmallVector<Expr*, 1> Args; |
| 697 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 698 | return; |
| 699 | |
| 700 | unsigned Size = Args.size(); |
| 701 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 702 | D->addAttr(::new (S.Context) |
| 703 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 704 | StartArg, Size, |
| 705 | Attr.getAttributeSpellingListIndex())); |
| 706 | } |
| 707 | |
| 708 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 709 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 710 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 711 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 712 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 713 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 714 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 715 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 716 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 717 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 718 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 722 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 723 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 724 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 727 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 728 | const AttributeList &Attr) { |
| 729 | SmallVector<Expr*, 2> Args; |
| 730 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 731 | return; |
| 732 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 733 | D->addAttr(::new (S.Context) |
| 734 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 735 | Attr.getArgAsExpr(0), |
| 736 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 737 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 740 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 741 | const AttributeList &Attr) { |
| 742 | SmallVector<Expr*, 2> Args; |
| 743 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 744 | return; |
| 745 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 746 | D->addAttr(::new (S.Context) |
| 747 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 748 | Attr.getArgAsExpr(0), |
| 749 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 750 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 753 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 754 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 755 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 756 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 757 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 758 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 759 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 760 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 761 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 762 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 763 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 764 | return true; |
| 765 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 766 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 767 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 768 | const AttributeList &Attr) { |
| 769 | SmallVector<Expr*, 1> Args; |
| 770 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 771 | return; |
| 772 | |
| 773 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 774 | D->addAttr(::new (S.Context) |
| 775 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 776 | StartArg, Args.size(), |
| 777 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 780 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 781 | const AttributeList &Attr) { |
| 782 | SmallVector<Expr*, 1> Args; |
| 783 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 784 | return; |
| 785 | |
| 786 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 787 | D->addAttr(::new (S.Context) |
| 788 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 789 | StartArg, Args.size(), |
| 790 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 793 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 794 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 795 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 796 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 797 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 798 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 799 | unsigned Size = Args.size(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 800 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 801 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 802 | D->addAttr(::new (S.Context) |
| 803 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 804 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 808 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 809 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 810 | SmallVector<Expr*, 1> Args; |
| 811 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 812 | unsigned Size = Args.size(); |
| 813 | if (Size == 0) |
| 814 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 815 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 816 | D->addAttr(::new (S.Context) |
| 817 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 818 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 822 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 823 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 824 | return; |
| 825 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 826 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 827 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 828 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 829 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 830 | if (Size == 0) |
| 831 | return; |
| 832 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 833 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 834 | D->addAttr(::new (S.Context) |
| 835 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 836 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 837 | } |
| 838 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 839 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 840 | ConsumableAttr::ConsumedState DefaultState; |
| 841 | |
| 842 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 843 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 844 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 845 | DefaultState)) { |
| 846 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 847 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 848 | return; |
| 849 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 850 | } else { |
| 851 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 852 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 853 | return; |
| 854 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 855 | |
| 856 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 857 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 858 | Attr.getAttributeSpellingListIndex())); |
| 859 | } |
| 860 | |
| 861 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 862 | const AttributeList &Attr) { |
| 863 | ASTContext &CurrContext = S.getASTContext(); |
| 864 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 865 | |
| 866 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 867 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 868 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 869 | RD->getNameAsString(); |
| 870 | |
| 871 | return false; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | return true; |
| 876 | } |
| 877 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 878 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 879 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 880 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 881 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 882 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 883 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 884 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 885 | return; |
| 886 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 887 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 888 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 889 | CallableWhenAttr::ConsumedState CallableState; |
| 890 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 891 | StringRef StateString; |
| 892 | SourceLocation Loc; |
| 893 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 894 | return; |
| 895 | |
| 896 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 897 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 898 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 899 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 900 | return; |
| 901 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 902 | |
| 903 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 904 | } |
| 905 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 906 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 907 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 908 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 909 | } |
| 910 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 911 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 912 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 913 | const AttributeList &Attr) { |
| 914 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 915 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 916 | ParamTypestateAttr::ConsumedState ParamState; |
| 917 | |
| 918 | if (Attr.isArgIdent(0)) { |
| 919 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 920 | StringRef StateString = Ident->Ident->getName(); |
| 921 | |
| 922 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 923 | ParamState)) { |
| 924 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 925 | << Attr.getName() << StateString; |
| 926 | return; |
| 927 | } |
| 928 | } else { |
| 929 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 930 | Attr.getName() << AANT_ArgumentIdentifier; |
| 931 | return; |
| 932 | } |
| 933 | |
| 934 | // FIXME: This check is currently being done in the analysis. It can be |
| 935 | // enabled here only after the parser propagates attributes at |
| 936 | // template specialization definition, not declaration. |
| 937 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 938 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 939 | // |
| 940 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 941 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 942 | // ReturnType.getAsString(); |
| 943 | // return; |
| 944 | //} |
| 945 | |
| 946 | D->addAttr(::new (S.Context) |
| 947 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 948 | Attr.getAttributeSpellingListIndex())); |
| 949 | } |
| 950 | |
| 951 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 952 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 953 | const AttributeList &Attr) { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 954 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 955 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 956 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 957 | |
| 958 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 959 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 960 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 961 | ReturnState)) { |
| 962 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 963 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 964 | return; |
| 965 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 966 | } else { |
| 967 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 968 | Attr.getName() << AANT_ArgumentIdentifier; |
| 969 | return; |
| 970 | } |
| 971 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 972 | // FIXME: This check is currently being done in the analysis. It can be |
| 973 | // enabled here only after the parser propagates attributes at |
| 974 | // template specialization definition, not declaration. |
| 975 | //QualType ReturnType; |
| 976 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 977 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 978 | // ReturnType = Param->getType(); |
| 979 | // |
| 980 | //} else if (const CXXConstructorDecl *Constructor = |
| 981 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 982 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 983 | // |
| 984 | //} else { |
| 985 | // |
| 986 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 987 | //} |
| 988 | // |
| 989 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 990 | // |
| 991 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 992 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 993 | // ReturnType.getAsString(); |
| 994 | // return; |
| 995 | //} |
| 996 | |
| 997 | D->addAttr(::new (S.Context) |
| 998 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 999 | Attr.getAttributeSpellingListIndex())); |
| 1000 | } |
| 1001 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1002 | |
| 1003 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1004 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1005 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1006 | |
| 1007 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1008 | return; |
| 1009 | |
| 1010 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1011 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1012 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1013 | StringRef Param = Ident->Ident->getName(); |
| 1014 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1015 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1016 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1017 | return; |
| 1018 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1019 | } else { |
| 1020 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1021 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1022 | return; |
| 1023 | } |
| 1024 | |
| 1025 | D->addAttr(::new (S.Context) |
| 1026 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1027 | Attr.getAttributeSpellingListIndex())); |
| 1028 | } |
| 1029 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1030 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1031 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1032 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1033 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1034 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1035 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1036 | return; |
| 1037 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1038 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1039 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1040 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1041 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1042 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1043 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1044 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1045 | return; |
| 1046 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1047 | } else { |
| 1048 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1049 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1050 | return; |
| 1051 | } |
| 1052 | |
| 1053 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1054 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1055 | Attr.getAttributeSpellingListIndex())); |
| 1056 | } |
| 1057 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1058 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1059 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1060 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1061 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1064 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1065 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1066 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1067 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1068 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1069 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1070 | if (!FD->getType()->isDependentType() && |
| 1071 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1072 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1073 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1074 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1075 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1076 | FD->addAttr(::new (S.Context) |
| 1077 | PackedAttr(Attr.getRange(), S.Context, |
| 1078 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1079 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1080 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1083 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1084 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1085 | // variables or properties of Objective-C classes. The outlet must also |
| 1086 | // have an object reference type. |
| 1087 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1088 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1089 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1090 | << Attr.getName() << VD->getType() << 0; |
| 1091 | return false; |
| 1092 | } |
| 1093 | } |
| 1094 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1095 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1096 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1097 | << Attr.getName() << PD->getType() << 1; |
| 1098 | return false; |
| 1099 | } |
| 1100 | } |
| 1101 | else { |
| 1102 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1103 | return false; |
| 1104 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1105 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1106 | return true; |
| 1107 | } |
| 1108 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1109 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1110 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1111 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1112 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1113 | D->addAttr(::new (S.Context) |
| 1114 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1115 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1118 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1119 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1120 | |
| 1121 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1122 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1123 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1124 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1125 | return; |
| 1126 | } |
| 1127 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1128 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1129 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1130 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1131 | ParsedType PT; |
| 1132 | |
| 1133 | if (Attr.hasParsedType()) |
| 1134 | PT = Attr.getTypeArg(); |
| 1135 | else { |
| 1136 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1137 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1138 | if (!PT) { |
| 1139 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1140 | return; |
| 1141 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1142 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1143 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1144 | TypeSourceInfo *QTLoc = 0; |
| 1145 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1146 | if (!QTLoc) |
| 1147 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1148 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1149 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1150 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1151 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1152 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1153 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1154 | S.Diag(Attr.getLoc(), |
| 1155 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1156 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1157 | return; |
| 1158 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1159 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1160 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1161 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1162 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1165 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1166 | if (const RecordType *UT = T->getAsUnionType()) |
| 1167 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1168 | RecordDecl *UD = UT->getDecl(); |
| 1169 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1170 | itend = UD->field_end(); it != itend; ++it) { |
| 1171 | QualType QT = it->getType(); |
| 1172 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1173 | T = QT; |
| 1174 | return; |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1180 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | e881ce2 | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1181 | if (!isFunctionOrMethod(D)) { |
| 1182 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1183 | << Attr.getName() << ExpectedFunctionOrMethod; |
Nuno Lopes | e881ce2 | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1184 | return; |
| 1185 | } |
| 1186 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1187 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1188 | return; |
| 1189 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1190 | SmallVector<unsigned, 8> SizeArgs; |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1191 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1192 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1193 | uint64_t Idx; |
| 1194 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1195 | Attr.getLoc(), i + 1, Ex, Idx)) |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1196 | return; |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1197 | |
| 1198 | // check if the function argument is of an integer type |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1199 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1200 | if (!T->isIntegerType()) { |
Aaron Ballman | 9d69509 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 1201 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1202 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 1203 | << Ex->getSourceRange(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1204 | return; |
| 1205 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1206 | SizeArgs.push_back(Idx); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | // check if the function returns a pointer |
| 1210 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1211 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1212 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1215 | D->addAttr(::new (S.Context) |
| 1216 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1217 | SizeArgs.data(), SizeArgs.size(), |
| 1218 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1221 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1222 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1223 | // ignore it as well |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1224 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1225 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1226 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1227 | return; |
| 1228 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1229 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1230 | SmallVector<unsigned, 8> NonNullArgs; |
| 1231 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1232 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1233 | uint64_t Idx; |
| 1234 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1235 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1236 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1237 | |
| 1238 | // Is the function argument a pointer type? |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1239 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1240 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1241 | |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1242 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1243 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | 62157e5 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1244 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1245 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1246 | continue; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1247 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1248 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1249 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1250 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1251 | |
| 1252 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1253 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1254 | if (NonNullArgs.empty()) { |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1255 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1256 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1257 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1258 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1259 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1260 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1261 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1262 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1263 | if (NonNullArgs.empty()) { |
| 1264 | // Warn the trivial case only if attribute is not coming from a |
| 1265 | // macro instantiation. |
| 1266 | if (Attr.getLoc().isFileID()) |
| 1267 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1268 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1269 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1270 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1271 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1272 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1273 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1274 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1275 | D->addAttr(::new (S.Context) |
| 1276 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1277 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1280 | static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) { |
| 1281 | switch (K) { |
| 1282 | case OwnershipAttr::Holds: return "'ownership_holds'"; |
| 1283 | case OwnershipAttr::Takes: return "'ownership_takes'"; |
| 1284 | case OwnershipAttr::Returns: return "'ownership_returns'"; |
| 1285 | } |
| 1286 | llvm_unreachable("unknown ownership"); |
| 1287 | } |
| 1288 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1289 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1290 | // This attribute must be applied to a function declaration. The first |
| 1291 | // argument to the attribute must be an identifier, the name of the resource, |
| 1292 | // for example: malloc. The following arguments must be argument indexes, the |
| 1293 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1294 | // The difference between Holds and Takes is that a pointer may still be used |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1295 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1296 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1297 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1298 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1299 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1300 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1301 | return; |
| 1302 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1303 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1304 | // Figure out our Kind. |
| 1305 | OwnershipAttr::OwnershipKind K = |
| 1306 | OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0, |
| 1307 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1308 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1309 | // Check arguments. |
| 1310 | switch (K) { |
| 1311 | case OwnershipAttr::Takes: |
| 1312 | case OwnershipAttr::Holds: |
| 1313 | if (AL.getNumArgs() < 2) { |
| 1314 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2; |
| 1315 | return; |
| 1316 | } |
| 1317 | break; |
| 1318 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1319 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1320 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1321 | return; |
| 1322 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1323 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1326 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1327 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1328 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1329 | return; |
| 1330 | } |
| 1331 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1332 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1333 | |
| 1334 | // Normalize the argument, __foo__ becomes foo. |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1335 | StringRef ModuleName = Module->getName(); |
| 1336 | if (ModuleName.startswith("__") && ModuleName.endswith("__") && |
| 1337 | ModuleName.size() > 4) { |
| 1338 | ModuleName = ModuleName.drop_front(2).drop_back(2); |
| 1339 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1340 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1341 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1342 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1343 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1344 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1345 | uint64_t Idx; |
| 1346 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1347 | AL.getLoc(), i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1348 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1349 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1350 | // Is the function argument a pointer type? |
| 1351 | QualType T = getFunctionOrMethodArgType(D, Idx); |
| 1352 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1353 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1354 | case OwnershipAttr::Takes: |
| 1355 | case OwnershipAttr::Holds: |
| 1356 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1357 | Err = 0; |
| 1358 | break; |
| 1359 | case OwnershipAttr::Returns: |
| 1360 | if (!T->isIntegerType()) |
| 1361 | Err = 1; |
| 1362 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1363 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1364 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1365 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1366 | << Ex->getSourceRange(); |
| 1367 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1368 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1369 | |
| 1370 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1371 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1372 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1373 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1374 | // FIXME: A returns attribute should conflict with any returns attribute |
| 1375 | // with a different index too. |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1376 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1377 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1378 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1379 | << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind()); |
| 1380 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1381 | } |
| 1382 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1383 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | unsigned* start = OwnershipArgs.data(); |
| 1387 | unsigned size = OwnershipArgs.size(); |
| 1388 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1389 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1390 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1391 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1392 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1395 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1396 | // Check the attribute arguments. |
| 1397 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1398 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1399 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1400 | return; |
| 1401 | } |
| 1402 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1403 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1404 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1405 | // gcc rejects |
| 1406 | // class c { |
| 1407 | // static int a __attribute__((weakref ("v2"))); |
| 1408 | // static int b() __attribute__((weakref ("f3"))); |
| 1409 | // }; |
| 1410 | // and ignores the attributes of |
| 1411 | // void f(void) { |
| 1412 | // static int a __attribute__((weakref ("v2"))); |
| 1413 | // } |
| 1414 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1415 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1416 | if (!Ctx->isFileContext()) { |
| 1417 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1418 | nd->getNameAsString(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1419 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | // The GCC manual says |
| 1423 | // |
| 1424 | // At present, a declaration to which `weakref' is attached can only |
| 1425 | // be `static'. |
| 1426 | // |
| 1427 | // It also says |
| 1428 | // |
| 1429 | // Without a TARGET, |
| 1430 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1431 | // equivalent to `weak'. |
| 1432 | // |
| 1433 | // gcc 4.4.1 will accept |
| 1434 | // int a7 __attribute__((weakref)); |
| 1435 | // as |
| 1436 | // int a7 __attribute__((weak)); |
| 1437 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1438 | // it if this behaviour is actually used. |
| 1439 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1440 | // GCC rejects |
| 1441 | // static ((alias ("y"), weakref)). |
| 1442 | // Should we? How to check that weakref is before or after alias? |
| 1443 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1444 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1445 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1446 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1447 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1448 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1449 | // GCC will accept anything as the argument of weakref. Should we |
| 1450 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1451 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1452 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1453 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1454 | D->addAttr(::new (S.Context) |
| 1455 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1456 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1459 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1460 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1461 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1462 | return; |
| 1463 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1464 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1465 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1466 | return; |
| 1467 | } |
| 1468 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1469 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1470 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1471 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1472 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1475 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 1476 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr, "hot")) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1477 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1478 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1479 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1480 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
| 1483 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 1484 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr, "cold")) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1485 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1486 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1487 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1488 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1491 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1492 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1493 | StringRef Model; |
| 1494 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1495 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1496 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1497 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1498 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1499 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1500 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1501 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1502 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1503 | return; |
| 1504 | } |
| 1505 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1506 | D->addAttr(::new (S.Context) |
| 1507 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1508 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1511 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1512 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1513 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1514 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1515 | D->addAttr(::new (S.Context) |
| 1516 | MallocAttr(Attr.getRange(), S.Context, |
| 1517 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1518 | return; |
| 1519 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1522 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1525 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1526 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1527 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1528 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1529 | return; |
| 1530 | } |
| 1531 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1532 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context, |
| 1533 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1534 | } |
| 1535 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1536 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1537 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1538 | |
| 1539 | if (S.CheckNoReturnAttr(attr)) return; |
| 1540 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1541 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1542 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1543 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1544 | return; |
| 1545 | } |
| 1546 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1547 | D->addAttr(::new (S.Context) |
| 1548 | NoReturnAttr(attr.getRange(), S.Context, |
| 1549 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
| 1552 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1553 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1554 | attr.setInvalid(); |
| 1555 | return true; |
| 1556 | } |
| 1557 | |
| 1558 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1561 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1562 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1563 | |
| 1564 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1565 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1566 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1567 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1568 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1569 | && !VD->getType()->isFunctionPointerType())) { |
| 1570 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1571 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1572 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1573 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1574 | return; |
| 1575 | } |
| 1576 | } |
| 1577 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1578 | D->addAttr(::new (S.Context) |
| 1579 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1580 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1583 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1584 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1585 | /* |
| 1586 | Returning a Vector Class in Registers |
| 1587 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1588 | According to the PPU ABI specifications, a class with a single member of |
| 1589 | vector type is returned in memory when used as the return value of a function. |
| 1590 | This results in inefficient code when implementing vector classes. To return |
| 1591 | the value in a single vector register, add the vecreturn attribute to the |
| 1592 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1593 | |
| 1594 | Example: |
| 1595 | |
| 1596 | struct Vector |
| 1597 | { |
| 1598 | __vector float xyzw; |
| 1599 | } __attribute__((vecreturn)); |
| 1600 | |
| 1601 | Vector Add(Vector lhs, Vector rhs) |
| 1602 | { |
| 1603 | Vector result; |
| 1604 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1605 | return result; // This will be returned in a register |
| 1606 | } |
| 1607 | */ |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 1608 | if (D->hasAttr<VecReturnAttr>()) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1609 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1610 | return; |
| 1611 | } |
| 1612 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1613 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1614 | int count = 0; |
| 1615 | |
| 1616 | if (!isa<CXXRecordDecl>(record)) { |
| 1617 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1618 | return; |
| 1619 | } |
| 1620 | |
| 1621 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1622 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1623 | return; |
| 1624 | } |
| 1625 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1626 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1627 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1628 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1629 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1630 | return; |
| 1631 | } |
| 1632 | count++; |
| 1633 | } |
| 1634 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1635 | D->addAttr(::new (S.Context) |
| 1636 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1637 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1638 | } |
| 1639 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1640 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1641 | const AttributeList &Attr) { |
| 1642 | if (isa<ParmVarDecl>(D)) { |
| 1643 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1644 | // parameter of a function declaration or lambda. |
| 1645 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1646 | S.Diag(Attr.getLoc(), |
| 1647 | diag::err_carries_dependency_param_not_function_decl); |
| 1648 | return; |
| 1649 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1650 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1651 | |
| 1652 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1653 | Attr.getRange(), S.Context, |
| 1654 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1655 | } |
| 1656 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1657 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1658 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 429c134 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1659 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1660 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1661 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1662 | return; |
| 1663 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1664 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1665 | D->addAttr(::new (S.Context) |
| 1666 | UnusedAttr(Attr.getRange(), S.Context, |
| 1667 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1668 | } |
| 1669 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1670 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1671 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1672 | if (VD->hasLocalStorage()) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1673 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1674 | return; |
| 1675 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1676 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1677 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1678 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1679 | return; |
| 1680 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1681 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1682 | D->addAttr(::new (S.Context) |
| 1683 | UsedAttr(Attr.getRange(), S.Context, |
| 1684 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1687 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1688 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1689 | if (Attr.getNumArgs() > 1) { |
| 1690 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1691 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1692 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1693 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1694 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1695 | if (Attr.getNumArgs() > 0 && |
| 1696 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1697 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1698 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1699 | D->addAttr(::new (S.Context) |
| 1700 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1701 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1704 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1705 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1706 | if (Attr.getNumArgs() > 1) { |
| 1707 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1708 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1709 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1710 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1711 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1712 | if (Attr.getNumArgs() > 0 && |
| 1713 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1714 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1715 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1716 | D->addAttr(::new (S.Context) |
| 1717 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1718 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1721 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1722 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1723 | const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1724 | unsigned NumArgs = Attr.getNumArgs(); |
| 1725 | if (NumArgs > 1) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1726 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1727 | return; |
| 1728 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1729 | |
| 1730 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1731 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1732 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1733 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1734 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1735 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1736 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1739 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
| 1740 | const AttributeList &Attr) { |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1741 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1742 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1743 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1746 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1747 | IdentifierInfo *Platform, |
| 1748 | VersionTuple Introduced, |
| 1749 | VersionTuple Deprecated, |
| 1750 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1751 | StringRef PlatformName |
| 1752 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1753 | if (PlatformName.empty()) |
| 1754 | PlatformName = Platform->getName(); |
| 1755 | |
| 1756 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1757 | // of these steps are needed). |
| 1758 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1759 | !(Introduced <= Deprecated)) { |
| 1760 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1761 | << 1 << PlatformName << Deprecated.getAsString() |
| 1762 | << 0 << Introduced.getAsString(); |
| 1763 | return true; |
| 1764 | } |
| 1765 | |
| 1766 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1767 | !(Introduced <= Obsoleted)) { |
| 1768 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1769 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1770 | << 0 << Introduced.getAsString(); |
| 1771 | return true; |
| 1772 | } |
| 1773 | |
| 1774 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1775 | !(Deprecated <= Obsoleted)) { |
| 1776 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1777 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1778 | << 1 << Deprecated.getAsString(); |
| 1779 | return true; |
| 1780 | } |
| 1781 | |
| 1782 | return false; |
| 1783 | } |
| 1784 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1785 | /// \brief Check whether the two versions match. |
| 1786 | /// |
| 1787 | /// If either version tuple is empty, then they are assumed to match. If |
| 1788 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1789 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1790 | bool BeforeIsOkay) { |
| 1791 | if (X.empty() || Y.empty()) |
| 1792 | return true; |
| 1793 | |
| 1794 | if (X == Y) |
| 1795 | return true; |
| 1796 | |
| 1797 | if (BeforeIsOkay && X < Y) |
| 1798 | return true; |
| 1799 | |
| 1800 | return false; |
| 1801 | } |
| 1802 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1803 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1804 | IdentifierInfo *Platform, |
| 1805 | VersionTuple Introduced, |
| 1806 | VersionTuple Deprecated, |
| 1807 | VersionTuple Obsoleted, |
| 1808 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1809 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1810 | bool Override, |
| 1811 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1812 | VersionTuple MergedIntroduced = Introduced; |
| 1813 | VersionTuple MergedDeprecated = Deprecated; |
| 1814 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1815 | bool FoundAny = false; |
| 1816 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1817 | if (D->hasAttrs()) { |
| 1818 | AttrVec &Attrs = D->getAttrs(); |
| 1819 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1820 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1821 | if (!OldAA) { |
| 1822 | ++i; |
| 1823 | continue; |
| 1824 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1825 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1826 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1827 | if (OldPlatform != Platform) { |
| 1828 | ++i; |
| 1829 | continue; |
| 1830 | } |
| 1831 | |
| 1832 | FoundAny = true; |
| 1833 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1834 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1835 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1836 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1837 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1838 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 1839 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 1840 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 1841 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 1842 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1843 | if (Override) { |
| 1844 | int Which = -1; |
| 1845 | VersionTuple FirstVersion; |
| 1846 | VersionTuple SecondVersion; |
| 1847 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 1848 | Which = 0; |
| 1849 | FirstVersion = OldIntroduced; |
| 1850 | SecondVersion = Introduced; |
| 1851 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 1852 | Which = 1; |
| 1853 | FirstVersion = Deprecated; |
| 1854 | SecondVersion = OldDeprecated; |
| 1855 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 1856 | Which = 2; |
| 1857 | FirstVersion = Obsoleted; |
| 1858 | SecondVersion = OldObsoleted; |
| 1859 | } |
| 1860 | |
| 1861 | if (Which == -1) { |
| 1862 | Diag(OldAA->getLocation(), |
| 1863 | diag::warn_mismatched_availability_override_unavail) |
| 1864 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1865 | } else { |
| 1866 | Diag(OldAA->getLocation(), |
| 1867 | diag::warn_mismatched_availability_override) |
| 1868 | << Which |
| 1869 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1870 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 1871 | } |
| 1872 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1873 | } else { |
| 1874 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1875 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1876 | } |
| 1877 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1878 | Attrs.erase(Attrs.begin() + i); |
| 1879 | --e; |
| 1880 | continue; |
| 1881 | } |
| 1882 | |
| 1883 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 1884 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 1885 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 1886 | |
| 1887 | if (MergedIntroduced2.empty()) |
| 1888 | MergedIntroduced2 = OldIntroduced; |
| 1889 | if (MergedDeprecated2.empty()) |
| 1890 | MergedDeprecated2 = OldDeprecated; |
| 1891 | if (MergedObsoleted2.empty()) |
| 1892 | MergedObsoleted2 = OldObsoleted; |
| 1893 | |
| 1894 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 1895 | MergedIntroduced2, MergedDeprecated2, |
| 1896 | MergedObsoleted2)) { |
| 1897 | Attrs.erase(Attrs.begin() + i); |
| 1898 | --e; |
| 1899 | continue; |
| 1900 | } |
| 1901 | |
| 1902 | MergedIntroduced = MergedIntroduced2; |
| 1903 | MergedDeprecated = MergedDeprecated2; |
| 1904 | MergedObsoleted = MergedObsoleted2; |
| 1905 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1906 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | if (FoundAny && |
| 1910 | MergedIntroduced == Introduced && |
| 1911 | MergedDeprecated == Deprecated && |
| 1912 | MergedObsoleted == Obsoleted) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1913 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1914 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1915 | // Only create a new attribute if !Override, but we want to do |
| 1916 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1917 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1918 | MergedDeprecated, MergedObsoleted) && |
| 1919 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1920 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 1921 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1922 | Obsoleted, IsUnavailable, Message, |
| 1923 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1924 | } |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1925 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1928 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1929 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1930 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1931 | return; |
| 1932 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1933 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 1934 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1935 | IdentifierInfo *II = Platform->Ident; |
| 1936 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 1937 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 1938 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1939 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 1940 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1941 | if (!ND) { |
| 1942 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1943 | return; |
| 1944 | } |
| 1945 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1946 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1947 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1948 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1949 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1950 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 1951 | if (const StringLiteral *SE = |
| 1952 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1953 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1954 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1955 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1956 | Introduced.Version, |
| 1957 | Deprecated.Version, |
| 1958 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1959 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1960 | /*Override=*/false, |
| 1961 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1962 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1963 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1966 | template <class T> |
| 1967 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 1968 | typename T::VisibilityType value, |
| 1969 | unsigned attrSpellingListIndex) { |
| 1970 | T *existingAttr = D->getAttr<T>(); |
| 1971 | if (existingAttr) { |
| 1972 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 1973 | if (existingValue == value) |
| 1974 | return NULL; |
| 1975 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 1976 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 1977 | D->dropAttr<T>(); |
| 1978 | } |
| 1979 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 1980 | } |
| 1981 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1982 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1983 | VisibilityAttr::VisibilityType Vis, |
| 1984 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1985 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 1986 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1989 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 1990 | TypeVisibilityAttr::VisibilityType Vis, |
| 1991 | unsigned AttrSpellingListIndex) { |
| 1992 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 1993 | AttrSpellingListIndex); |
| 1994 | } |
| 1995 | |
| 1996 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 1997 | bool isTypeVisibility) { |
| 1998 | // Visibility attributes don't mean anything on a typedef. |
| 1999 | if (isa<TypedefNameDecl>(D)) { |
| 2000 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2001 | << Attr.getName(); |
| 2002 | return; |
| 2003 | } |
| 2004 | |
| 2005 | // 'type_visibility' can only go on a type or namespace. |
| 2006 | if (isTypeVisibility && |
| 2007 | !(isa<TagDecl>(D) || |
| 2008 | isa<ObjCInterfaceDecl>(D) || |
| 2009 | isa<NamespaceDecl>(D))) { |
| 2010 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2011 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2012 | return; |
| 2013 | } |
| 2014 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2015 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2016 | StringRef TypeStr; |
| 2017 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2018 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2019 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2020 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2021 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2022 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2023 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2024 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2025 | return; |
| 2026 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2027 | |
| 2028 | // Complain about attempts to use protected visibility on targets |
| 2029 | // (like Darwin) that don't support it. |
| 2030 | if (type == VisibilityAttr::Protected && |
| 2031 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2032 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2033 | type = VisibilityAttr::Default; |
| 2034 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2035 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2036 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2037 | clang::Attr *newAttr; |
| 2038 | if (isTypeVisibility) { |
| 2039 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2040 | (TypeVisibilityAttr::VisibilityType) type, |
| 2041 | Index); |
| 2042 | } else { |
| 2043 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2044 | } |
| 2045 | if (newAttr) |
| 2046 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2049 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2050 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2051 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2052 | if (!Attr.isArgIdent(0)) { |
| 2053 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2054 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2055 | return; |
| 2056 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2057 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2058 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2059 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2060 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2061 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2062 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2063 | return; |
| 2064 | } |
| 2065 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2066 | if (F == ObjCMethodFamilyAttr::OMF_init && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2067 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2068 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2069 | << method->getResultType(); |
| 2070 | // Ignore the attribute. |
| 2071 | return; |
| 2072 | } |
| 2073 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2074 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2075 | S.Context, F)); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2076 | } |
| 2077 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2078 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2079 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2080 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2081 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2082 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2083 | return; |
| 2084 | } |
| 2085 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2086 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2087 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2088 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2089 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2090 | return; |
| 2091 | } |
| 2092 | } |
| 2093 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2094 | // It is okay to include this attribute on properties, e.g.: |
| 2095 | // |
| 2096 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2097 | // |
| 2098 | // In this case it follows tradition and suppresses an error in the above |
| 2099 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2100 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2101 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2102 | D->addAttr(::new (S.Context) |
| 2103 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2104 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2105 | } |
| 2106 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2107 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2108 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2109 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2110 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2111 | return; |
| 2112 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2113 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2114 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2115 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2116 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2117 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2118 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2119 | return; |
| 2120 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2121 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2122 | D->addAttr(::new (S.Context) |
| 2123 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2124 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2127 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2128 | // check the attribute arguments. |
| 2129 | if (Attr.getNumArgs() > 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2130 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2131 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2134 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2135 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2136 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2137 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2138 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2139 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2140 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2141 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2142 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2143 | return; |
| 2144 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2145 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2146 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2147 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2148 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2149 | return; |
| 2150 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2151 | |
| 2152 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2155 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2156 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2157 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2158 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2159 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2160 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2161 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2162 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2163 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2164 | return; |
| 2165 | } |
| 2166 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2167 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2168 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2169 | // FIXME: This error message could be improved, it would be nice |
| 2170 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2171 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2172 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2173 | return; |
| 2174 | } |
| 2175 | } |
| 2176 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2177 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2178 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2179 | if (isa<FunctionNoProtoType>(FT)) { |
| 2180 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2181 | return; |
| 2182 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2183 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2184 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2185 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2186 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2187 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2188 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2189 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2190 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2191 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2192 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2193 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2194 | if (!BD->isVariadic()) { |
| 2195 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2196 | return; |
| 2197 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2198 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2199 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2200 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2201 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2202 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2203 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2204 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2205 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2206 | return; |
| 2207 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2208 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2209 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2210 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2211 | return; |
| 2212 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2213 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2214 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2215 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2216 | return; |
| 2217 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2218 | D->addAttr(::new (S.Context) |
| 2219 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2220 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2223 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2224 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2225 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | 3d699e0 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2226 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2227 | return; |
| 2228 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2229 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2230 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2231 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2232 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2233 | return; |
| 2234 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2235 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2236 | if (MD->getResultType()->isVoidType()) { |
| 2237 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2238 | << Attr.getName() << 1; |
| 2239 | return; |
| 2240 | } |
| 2241 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2242 | D->addAttr(::new (S.Context) |
| 2243 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2244 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2247 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2248 | // weak_import only applies to variable & function declarations. |
| 2249 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2250 | if (!D->canBeWeakImported(isDef)) { |
| 2251 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2252 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2253 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2254 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2255 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2256 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2257 | // Nothing to warn about here. |
| 2258 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2259 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2260 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2261 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2262 | return; |
| 2263 | } |
| 2264 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2265 | D->addAttr(::new (S.Context) |
| 2266 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2267 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2270 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2271 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2272 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2273 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2274 | uint32_t WGSize[3]; |
| 2275 | for (unsigned i = 0; i < 3; ++i) |
| 2276 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2277 | return; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2278 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2279 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2280 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2281 | Existing->getYDim() == WGSize[1] && |
| 2282 | Existing->getZDim() == WGSize[2])) |
| 2283 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2284 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2285 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2286 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2287 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2290 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2291 | if (!Attr.hasParsedType()) { |
| 2292 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2293 | << Attr.getName() << 1; |
| 2294 | return; |
| 2295 | } |
| 2296 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2297 | TypeSourceInfo *ParmTSI = 0; |
| 2298 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2299 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2300 | |
| 2301 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2302 | (ParmType->isBooleanType() || |
| 2303 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2304 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2305 | << ParmType; |
| 2306 | return; |
| 2307 | } |
| 2308 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2309 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2310 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2311 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2312 | return; |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2317 | ParmTSI)); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2320 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2321 | StringRef Name, |
| 2322 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2323 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2324 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2325 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2326 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2327 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2328 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2329 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2330 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2331 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2334 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2335 | // Make sure that there is a string literal as the sections's single |
| 2336 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2337 | StringRef Str; |
| 2338 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2339 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2340 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2341 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2342 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2343 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2344 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2345 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2346 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2347 | return; |
| 2348 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2349 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2350 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2351 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2352 | if (NewAttr) |
| 2353 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2356 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2357 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2358 | VarDecl *VD = cast<VarDecl>(D); |
| 2359 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2360 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2361 | return; |
| 2362 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2363 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2364 | Expr *E = Attr.getArgAsExpr(0); |
| 2365 | SourceLocation Loc = E->getExprLoc(); |
| 2366 | FunctionDecl *FD = 0; |
| 2367 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2368 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2369 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2370 | // will warn the user. |
| 2371 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2372 | if (DRE->hasQualifier()) |
| 2373 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2374 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2375 | NI = DRE->getNameInfo(); |
| 2376 | if (!FD) { |
| 2377 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2378 | << NI.getName(); |
| 2379 | return; |
| 2380 | } |
| 2381 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2382 | if (ULE->hasExplicitTemplateArgs()) |
| 2383 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2384 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2385 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2386 | if (!FD) { |
| 2387 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2388 | << NI.getName(); |
| 2389 | if (ULE->getType() == S.Context.OverloadTy) |
| 2390 | S.NoteAllOverloadCandidates(ULE); |
| 2391 | return; |
| 2392 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2393 | } else { |
| 2394 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2395 | return; |
| 2396 | } |
| 2397 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2398 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2399 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2400 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2401 | return; |
| 2402 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2403 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2404 | // We're currently more strict than GCC about what function types we accept. |
| 2405 | // If this ever proves to be a problem it should be easy to fix. |
| 2406 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2407 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2408 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2409 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2410 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2411 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2412 | return; |
| 2413 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2414 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2415 | D->addAttr(::new (S.Context) |
| 2416 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2417 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2420 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2421 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2422 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2423 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2424 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2425 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2426 | return; |
| 2427 | } |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2428 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2429 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2430 | uint64_t ArgIdx; |
| 2431 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 2432 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2433 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2434 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2435 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2436 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2437 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2438 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2439 | if (not_nsstring_type && |
| 2440 | !isCFStringType(Ty, S.Context) && |
| 2441 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2442 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2443 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2444 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2445 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2446 | << IdxExpr->getSourceRange(); |
| 2447 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2448 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2449 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2450 | if (!isNSStringType(Ty, S.Context) && |
| 2451 | !isCFStringType(Ty, S.Context) && |
| 2452 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2453 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2454 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2455 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2456 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2457 | << IdxExpr->getSourceRange(); |
| 2458 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2461 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2462 | // because that has corrected for the implicit this parameter, and is zero- |
| 2463 | // based. The attribute expects what the user wrote explicitly. |
| 2464 | llvm::APSInt Val; |
| 2465 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2466 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2467 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2468 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2469 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2470 | } |
| 2471 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2472 | enum FormatAttrKind { |
| 2473 | CFStringFormat, |
| 2474 | NSStringFormat, |
| 2475 | StrftimeFormat, |
| 2476 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2477 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2478 | InvalidFormat |
| 2479 | }; |
| 2480 | |
| 2481 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2482 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2483 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2484 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2485 | // Check for formats that get handled specially. |
| 2486 | .Case("NSString", NSStringFormat) |
| 2487 | .Case("CFString", CFStringFormat) |
| 2488 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2489 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2490 | // Otherwise, check for supported formats. |
| 2491 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2492 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2493 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2494 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2495 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2496 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2499 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2500 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2501 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2502 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2503 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2504 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2505 | return; |
| 2506 | } |
| 2507 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2508 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2509 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2510 | Attr.setInvalid(); |
| 2511 | return; |
| 2512 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2513 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2514 | if (S.Context.getAsArrayType(T)) |
| 2515 | T = S.Context.getBaseElementType(T); |
| 2516 | if (!T->getAs<RecordType>()) { |
| 2517 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2518 | Attr.setInvalid(); |
| 2519 | return; |
| 2520 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2521 | |
| 2522 | Expr *E = Attr.getArgAsExpr(0); |
| 2523 | uint32_t prioritynum; |
| 2524 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2525 | Attr.setInvalid(); |
| 2526 | return; |
| 2527 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2528 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2529 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2530 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2531 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2532 | Attr.setInvalid(); |
| 2533 | return; |
| 2534 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2535 | D->addAttr(::new (S.Context) |
| 2536 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2537 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2540 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2541 | IdentifierInfo *Format, int FormatIdx, |
| 2542 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2543 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2544 | // Check whether we already have an equivalent format attribute. |
| 2545 | for (specific_attr_iterator<FormatAttr> |
| 2546 | i = D->specific_attr_begin<FormatAttr>(), |
| 2547 | e = D->specific_attr_end<FormatAttr>(); |
| 2548 | i != e ; ++i) { |
| 2549 | FormatAttr *f = *i; |
| 2550 | if (f->getType() == Format && |
| 2551 | f->getFormatIdx() == FormatIdx && |
| 2552 | f->getFirstArg() == FirstArg) { |
| 2553 | // If we don't have a valid location for this attribute, adopt the |
| 2554 | // location. |
| 2555 | if (f->getLocation().isInvalid()) |
| 2556 | f->setRange(Range); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2557 | return NULL; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2558 | } |
| 2559 | } |
| 2560 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2561 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2562 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2565 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2566 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2567 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2568 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2569 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2570 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2571 | return; |
| 2572 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2573 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2574 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2575 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2576 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2577 | return; |
| 2578 | } |
| 2579 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2580 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2581 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2582 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2583 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2584 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2585 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2586 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2587 | |
| 2588 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2589 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2590 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2591 | // If we've modified the string name, we need a new identifier for it. |
| 2592 | II = &S.Context.Idents.get(Format); |
| 2593 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2594 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2595 | // Check for supported formats. |
| 2596 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2597 | |
| 2598 | if (Kind == IgnoredFormat) |
| 2599 | return; |
| 2600 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2601 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2602 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2603 | << "format" << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2604 | return; |
| 2605 | } |
| 2606 | |
| 2607 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2608 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2609 | uint32_t Idx; |
| 2610 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2611 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2612 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2613 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2614 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2615 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2616 | return; |
| 2617 | } |
| 2618 | |
| 2619 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2620 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2621 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2622 | if (HasImplicitThisParam) { |
| 2623 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2624 | S.Diag(Attr.getLoc(), |
| 2625 | diag::err_format_attribute_implicit_this_format_string) |
| 2626 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2627 | return; |
| 2628 | } |
| 2629 | ArgIdx--; |
| 2630 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2631 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2632 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2633 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2634 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2635 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2636 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2637 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2638 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2639 | return; |
| 2640 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2641 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2642 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2643 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2644 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2645 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2646 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2647 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2648 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2649 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2650 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2651 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2652 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2653 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2654 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2655 | return; |
| 2656 | } |
| 2657 | |
| 2658 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2659 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2660 | uint32_t FirstArg; |
| 2661 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2662 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2663 | |
| 2664 | // check if the function is variadic if the 3rd argument non-zero |
| 2665 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2666 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2667 | ++NumArgs; // +1 for ... |
| 2668 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2669 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2670 | return; |
| 2671 | } |
| 2672 | } |
| 2673 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2674 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2675 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2676 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2677 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2678 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2679 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2680 | return; |
| 2681 | } |
| 2682 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2683 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2684 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2685 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2686 | return; |
| 2687 | } |
| 2688 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2689 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2690 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2691 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2692 | if (NewAttr) |
| 2693 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2696 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2697 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2698 | // Try to find the underlying union declaration. |
| 2699 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2700 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2701 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2702 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2703 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2704 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2705 | |
| 2706 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2707 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2708 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2709 | return; |
| 2710 | } |
| 2711 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2712 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2713 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2714 | diag::warn_transparent_union_attribute_not_definition); |
| 2715 | return; |
| 2716 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2717 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2718 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2719 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2720 | if (Field == FieldEnd) { |
| 2721 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2722 | return; |
| 2723 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2724 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2725 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2726 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2727 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2728 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2729 | diag::warn_transparent_union_attribute_floating) |
| 2730 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2731 | return; |
| 2732 | } |
| 2733 | |
| 2734 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2735 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2736 | for (; Field != FieldEnd; ++Field) { |
| 2737 | QualType FieldType = Field->getType(); |
| 2738 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 2739 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 2740 | // Warn if we drop the attribute. |
| 2741 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2742 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2743 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2744 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2745 | diag::warn_transparent_union_attribute_field_size_align) |
| 2746 | << isSize << Field->getDeclName() << FieldBits; |
| 2747 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2748 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2749 | diag::note_transparent_union_first_field_size_align) |
| 2750 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2751 | return; |
| 2752 | } |
| 2753 | } |
| 2754 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2755 | RD->addAttr(::new (S.Context) |
| 2756 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2757 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2760 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2761 | // Make sure that there is a string literal as the annotation's single |
| 2762 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2763 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2764 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2765 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2766 | |
| 2767 | // Don't duplicate annotations that are already set. |
| 2768 | for (specific_attr_iterator<AnnotateAttr> |
| 2769 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 2770 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2771 | if ((*i)->getAnnotation() == Str) |
| 2772 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2773 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2774 | |
| 2775 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2776 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2777 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2780 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2781 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2782 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2783 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2784 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2785 | return; |
| 2786 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2787 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2788 | if (Attr.getNumArgs() == 0) { |
| 2789 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 2790 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 2791 | return; |
| 2792 | } |
| 2793 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2794 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2795 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 2796 | S.Diag(Attr.getEllipsisLoc(), |
| 2797 | diag::err_pack_expansion_without_parameter_packs); |
| 2798 | return; |
| 2799 | } |
| 2800 | |
| 2801 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 2802 | return; |
| 2803 | |
| 2804 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 2805 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2809 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2810 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 2811 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 2812 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2813 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2814 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2815 | // C++11 [dcl.align]p1: |
| 2816 | // An alignment-specifier may be applied to a variable or to a class |
| 2817 | // data member, but it shall not be applied to a bit-field, a function |
| 2818 | // parameter, the formal parameter of a catch clause, or a variable |
| 2819 | // declared with the register storage class specifier. An |
| 2820 | // alignment-specifier may also be applied to the declaration of a class |
| 2821 | // or enumeration type. |
| 2822 | // C11 6.7.5/2: |
| 2823 | // An alignment attribute shall not be specified in a declaration of |
| 2824 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 2825 | // object declared with the register storage-class specifier. |
| 2826 | int DiagKind = -1; |
| 2827 | if (isa<ParmVarDecl>(D)) { |
| 2828 | DiagKind = 0; |
| 2829 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2830 | if (VD->getStorageClass() == SC_Register) |
| 2831 | DiagKind = 1; |
| 2832 | if (VD->isExceptionVariable()) |
| 2833 | DiagKind = 2; |
| 2834 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 2835 | if (FD->isBitField()) |
| 2836 | DiagKind = 3; |
| 2837 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2838 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 2839 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 2840 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 2841 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2842 | return; |
| 2843 | } |
| 2844 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2845 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2846 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2847 | return; |
| 2848 | } |
| 2849 | } |
| 2850 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2851 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2852 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2853 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 2854 | AA->setPackExpansion(IsPackExpansion); |
| 2855 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2856 | return; |
| 2857 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2858 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2859 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2860 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 2861 | ExprResult ICE |
| 2862 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2863 | diag::err_aligned_attribute_argument_not_int, |
| 2864 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2865 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2866 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2867 | |
| 2868 | // C++11 [dcl.align]p2: |
| 2869 | // -- if the constant expression evaluates to zero, the alignment |
| 2870 | // specifier shall have no effect |
| 2871 | // C11 6.7.5p6: |
| 2872 | // An alignment specification of zero has no effect. |
| 2873 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 2874 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2875 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2876 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2877 | return; |
| 2878 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2879 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2880 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2881 | // We've already verified it's a power of 2, now let's make sure it's |
| 2882 | // 8192 or less. |
| 2883 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2884 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2885 | << E->getSourceRange(); |
| 2886 | return; |
| 2887 | } |
| 2888 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2889 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2890 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 2891 | ICE.take(), SpellingListIndex); |
| 2892 | AA->setPackExpansion(IsPackExpansion); |
| 2893 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2896 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2897 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2898 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2899 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2900 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 2901 | SpellingListIndex); |
| 2902 | AA->setPackExpansion(IsPackExpansion); |
| 2903 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2904 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2905 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2906 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 2907 | assert(D->hasAttrs() && "no attributes on decl"); |
| 2908 | |
| 2909 | QualType Ty; |
| 2910 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2911 | Ty = VD->getType(); |
| 2912 | else |
| 2913 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 3653b7e | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 2914 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2915 | return; |
| 2916 | |
| 2917 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 2918 | // The combined effect of all alignment attributes in a declaration shall |
| 2919 | // not specify an alignment that is less strict than the alignment that |
| 2920 | // would otherwise be required for the entity being declared. |
| 2921 | AlignedAttr *AlignasAttr = 0; |
| 2922 | unsigned Align = 0; |
| 2923 | for (specific_attr_iterator<AlignedAttr> |
| 2924 | I = D->specific_attr_begin<AlignedAttr>(), |
| 2925 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 2926 | if (I->isAlignmentDependent()) |
| 2927 | return; |
| 2928 | if (I->isAlignas()) |
| 2929 | AlignasAttr = *I; |
| 2930 | Align = std::max(Align, I->getAlignment(Context)); |
| 2931 | } |
| 2932 | |
| 2933 | if (AlignasAttr && Align) { |
| 2934 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 2935 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 2936 | if (NaturalAlign > RequestedAlign) |
| 2937 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 2938 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 2939 | } |
| 2940 | } |
| 2941 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 2942 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2943 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2944 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2945 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2946 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2947 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2948 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2949 | // This attribute isn't documented, but glibc uses it. It changes |
| 2950 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2951 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 2952 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 2953 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2954 | return; |
| 2955 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2956 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2957 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 2958 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2959 | |
| 2960 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2961 | if (Str.startswith("__") && Str.endswith("__")) |
| 2962 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2963 | |
| 2964 | unsigned DestWidth = 0; |
| 2965 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2966 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2967 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2968 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2969 | switch (Str[0]) { |
| 2970 | case 'Q': DestWidth = 8; break; |
| 2971 | case 'H': DestWidth = 16; break; |
| 2972 | case 'S': DestWidth = 32; break; |
| 2973 | case 'D': DestWidth = 64; break; |
| 2974 | case 'X': DestWidth = 96; break; |
| 2975 | case 'T': DestWidth = 128; break; |
| 2976 | } |
| 2977 | if (Str[1] == 'F') { |
| 2978 | IntegerMode = false; |
| 2979 | } else if (Str[1] == 'C') { |
| 2980 | IntegerMode = false; |
| 2981 | ComplexMode = true; |
| 2982 | } else if (Str[1] != 'I') { |
| 2983 | DestWidth = 0; |
| 2984 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2985 | break; |
| 2986 | case 4: |
| 2987 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2988 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2989 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2990 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2991 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2992 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2993 | break; |
| 2994 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2995 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2996 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2997 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2998 | case 11: |
| 2999 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3000 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3001 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3005 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3006 | OldTy = TD->getUnderlyingType(); |
| 3007 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3008 | OldTy = VD->getType(); |
| 3009 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3010 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3011 | << "mode" << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3012 | return; |
| 3013 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3014 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3015 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3016 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3017 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3018 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3019 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3020 | } else if (ComplexMode) { |
| 3021 | if (!OldTy->isComplexType()) |
| 3022 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3023 | } else { |
| 3024 | if (!OldTy->isFloatingType()) |
| 3025 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3026 | } |
| 3027 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3028 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3029 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3030 | // FIXME: Make sure floating-point mappings are accurate |
| 3031 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3032 | if (!DestWidth) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3033 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3034 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | QualType NewTy; |
| 3038 | |
| 3039 | if (IntegerMode) |
| 3040 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 3041 | OldTy->isSignedIntegerType()); |
| 3042 | else |
| 3043 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 3044 | |
| 3045 | if (NewTy.isNull()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3046 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3047 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3050 | if (ComplexMode) { |
| 3051 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3052 | } |
| 3053 | |
| 3054 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3055 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3056 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3057 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3058 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3059 | |
| 3060 | D->addAttr(::new (S.Context) |
| 3061 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3062 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3063 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3064 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3065 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3066 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3067 | if (!VD->hasGlobalStorage()) |
| 3068 | S.Diag(Attr.getLoc(), |
| 3069 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3070 | << Attr.getName(); |
| 3071 | } else if (!isFunctionOrMethod(D)) { |
| 3072 | S.Diag(Attr.getLoc(), |
| 3073 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3074 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3075 | return; |
| 3076 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3077 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3078 | D->addAttr(::new (S.Context) |
| 3079 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3080 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3083 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3084 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 3085 | if (!FD->getResultType()->isVoidType()) { |
| 3086 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
| 3087 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
| 3088 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3089 | << FD->getType() |
| 3090 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
| 3091 | "void"); |
| 3092 | } else { |
| 3093 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3094 | << FD->getType(); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3095 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3096 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3097 | } |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3098 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3099 | D->addAttr(::new (S.Context) |
| 3100 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3101 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3104 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3105 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3106 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3107 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3108 | return; |
| 3109 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3110 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3111 | D->addAttr(::new (S.Context) |
| 3112 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3113 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3116 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3117 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3118 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3119 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3120 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3121 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3122 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3123 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3124 | return; |
| 3125 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3126 | if (!isa<ObjCMethodDecl>(D)) { |
| 3127 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3128 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3129 | return; |
| 3130 | } |
| 3131 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3132 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3133 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3134 | D->addAttr(::new (S.Context) |
| 3135 | FastCallAttr(Attr.getRange(), S.Context, |
| 3136 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3137 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3138 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3139 | D->addAttr(::new (S.Context) |
| 3140 | StdCallAttr(Attr.getRange(), S.Context, |
| 3141 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3142 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3143 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3144 | D->addAttr(::new (S.Context) |
| 3145 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3146 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3147 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3148 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3149 | D->addAttr(::new (S.Context) |
| 3150 | CDeclAttr(Attr.getRange(), S.Context, |
| 3151 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3152 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3153 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3154 | D->addAttr(::new (S.Context) |
| 3155 | PascalAttr(Attr.getRange(), S.Context, |
| 3156 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3157 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3158 | case AttributeList::AT_MSABI: |
| 3159 | D->addAttr(::new (S.Context) |
| 3160 | MSABIAttr(Attr.getRange(), S.Context, |
| 3161 | Attr.getAttributeSpellingListIndex())); |
| 3162 | return; |
| 3163 | case AttributeList::AT_SysVABI: |
| 3164 | D->addAttr(::new (S.Context) |
| 3165 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3166 | Attr.getAttributeSpellingListIndex())); |
| 3167 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3168 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3169 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3170 | switch (CC) { |
| 3171 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3172 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3173 | break; |
| 3174 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3175 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3176 | break; |
| 3177 | default: |
| 3178 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3181 | D->addAttr(::new (S.Context) |
| 3182 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3183 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3184 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3185 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3186 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3187 | D->addAttr(::new (S.Context) |
| 3188 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3189 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3190 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3191 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3192 | D->addAttr(::new (S.Context) |
| 3193 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3194 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3195 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3196 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3197 | default: |
| 3198 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3199 | } |
| 3200 | } |
| 3201 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3202 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, |
| 3203 | const AttributeList &Attr) { |
| 3204 | uint32_t ArgNum; |
| 3205 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum)) |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3206 | return; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3207 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3208 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(), |
| 3209 | S.Context, ArgNum)); |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3210 | } |
| 3211 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3212 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3213 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3214 | if (attr.isInvalid()) |
| 3215 | return true; |
| 3216 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3217 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3218 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3219 | attr.setInvalid(); |
| 3220 | return true; |
| 3221 | } |
| 3222 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3223 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3224 | // move to TargetAttributesSema one day. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3225 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3226 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3227 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3228 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3229 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3230 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3231 | case AttributeList::AT_MSABI: |
| 3232 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3233 | CC_X86_64Win64; |
| 3234 | break; |
| 3235 | case AttributeList::AT_SysVABI: |
| 3236 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3237 | CC_C; |
| 3238 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3239 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3240 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3241 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3242 | attr.setInvalid(); |
| 3243 | return true; |
| 3244 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3245 | if (StrRef == "aapcs") { |
| 3246 | CC = CC_AAPCS; |
| 3247 | break; |
| 3248 | } else if (StrRef == "aapcs-vfp") { |
| 3249 | CC = CC_AAPCS_VFP; |
| 3250 | break; |
| 3251 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3252 | |
| 3253 | attr.setInvalid(); |
| 3254 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3255 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3256 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3257 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3258 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3259 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3262 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3263 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3264 | if (A == TargetInfo::CCCR_Warning) { |
| 3265 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3266 | |
| 3267 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3268 | if (FD) |
| 3269 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3270 | TargetInfo::CCMT_NonMember; |
| 3271 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3274 | return false; |
| 3275 | } |
| 3276 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3277 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3278 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3279 | |
| 3280 | unsigned numParams; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3281 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3282 | return; |
| 3283 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3284 | if (!isa<ObjCMethodDecl>(D)) { |
| 3285 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3286 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3287 | return; |
| 3288 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3289 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3290 | D->addAttr(::new (S.Context) |
| 3291 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 3292 | Attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3296 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3297 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3298 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3299 | return true; |
| 3300 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3301 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3302 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3303 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3304 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3305 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3306 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3307 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3308 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3309 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3310 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3313 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3314 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3315 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3316 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3317 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3320 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3321 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3322 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3323 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3324 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3325 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3326 | } |
| 3327 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3328 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3329 | } |
| 3330 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3331 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 3332 | const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3333 | // check the attribute arguments. |
| 3334 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
| 3335 | // FIXME: 0 is not okay. |
| 3336 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
| 3337 | return; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3338 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3339 | |
| 3340 | if (!isFunctionOrMethod(D)) { |
| 3341 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3342 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3343 | return; |
| 3344 | } |
| 3345 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3346 | uint32_t MaxThreads, MinBlocks = 0; |
| 3347 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1)) |
| 3348 | return; |
| 3349 | if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr, |
| 3350 | Attr.getArgAsExpr(1), |
| 3351 | MinBlocks, 2)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3352 | return; |
| 3353 | |
| 3354 | D->addAttr(::new (S.Context) |
| 3355 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 3356 | MaxThreads, MinBlocks, |
| 3357 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3358 | } |
| 3359 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3360 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3361 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3362 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3363 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3364 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3365 | return; |
| 3366 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3367 | |
| 3368 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3369 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3370 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3371 | StringRef AttrName = Attr.getName()->getName(); |
| 3372 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3373 | |
| 3374 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3375 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3376 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3377 | return; |
| 3378 | } |
| 3379 | |
| 3380 | uint64_t ArgumentIdx; |
| 3381 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3382 | Attr.getLoc(), 2, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3383 | Attr.getArgAsExpr(1), ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3384 | return; |
| 3385 | |
| 3386 | uint64_t TypeTagIdx; |
| 3387 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3388 | Attr.getLoc(), 3, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3389 | Attr.getArgAsExpr(2), TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3390 | return; |
| 3391 | |
| 3392 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 3393 | if (IsPointer) { |
| 3394 | // Ensure that buffer has a pointer type. |
| 3395 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 3396 | if (!BufferTy->isPointerType()) { |
| 3397 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3398 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3399 | } |
| 3400 | } |
| 3401 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3402 | D->addAttr(::new (S.Context) |
| 3403 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3404 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3405 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3409 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3410 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3411 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3412 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3413 | return; |
| 3414 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3415 | |
| 3416 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3417 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3418 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3419 | if (!isa<VarDecl>(D)) { |
| 3420 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3421 | << Attr.getName() << ExpectedVariable; |
| 3422 | return; |
| 3423 | } |
| 3424 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3425 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3426 | TypeSourceInfo *MatchingCTypeLoc = 0; |
| 3427 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3428 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3429 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3430 | D->addAttr(::new (S.Context) |
| 3431 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3432 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3433 | Attr.getLayoutCompatible(), |
| 3434 | Attr.getMustBeNull(), |
| 3435 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3438 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3439 | // Checker-specific attribute handlers. |
| 3440 | //===----------------------------------------------------------------------===// |
| 3441 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3442 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3443 | return type->isDependentType() || |
| 3444 | type->isObjCObjectPointerType() || |
| 3445 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3446 | } |
| 3447 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3448 | return type->isDependentType() || |
| 3449 | type->isPointerType() || |
| 3450 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3453 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3454 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3455 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3456 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3457 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3458 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3459 | cf = false; |
| 3460 | } else { |
| 3461 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3462 | cf = true; |
| 3463 | } |
| 3464 | |
| 3465 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3466 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3467 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3468 | return; |
| 3469 | } |
| 3470 | |
| 3471 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3472 | param->addAttr(::new (S.Context) |
| 3473 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3474 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3475 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3476 | param->addAttr(::new (S.Context) |
| 3477 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3478 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3479 | } |
| 3480 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3481 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3482 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3483 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3484 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3485 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3486 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3487 | returnType = MD->getResultType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3488 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3489 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3490 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3491 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3492 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3493 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3494 | returnType = FD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3495 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3496 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3497 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3498 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3499 | return; |
| 3500 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3501 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3502 | bool typeOK; |
| 3503 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3504 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3505 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3506 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3507 | case AttributeList::AT_NSReturnsRetained: |
| 3508 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3509 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3510 | cf = false; |
| 3511 | break; |
| 3512 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3513 | case AttributeList::AT_CFReturnsRetained: |
| 3514 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3515 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3516 | cf = true; |
| 3517 | break; |
| 3518 | } |
| 3519 | |
| 3520 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3521 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3522 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3523 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3524 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3525 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3526 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3527 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3528 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3529 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3530 | D->addAttr(::new (S.Context) |
| 3531 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 3532 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3533 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3534 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3535 | D->addAttr(::new (S.Context) |
| 3536 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3537 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3538 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3539 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3540 | D->addAttr(::new (S.Context) |
| 3541 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3542 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3543 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3544 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3545 | D->addAttr(::new (S.Context) |
| 3546 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3547 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3548 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3549 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3550 | D->addAttr(::new (S.Context) |
| 3551 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3552 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3553 | return; |
| 3554 | }; |
| 3555 | } |
| 3556 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3557 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3558 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 3559 | const int EP_ObjCMethod = 1; |
| 3560 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3561 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3562 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3563 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3564 | if (isa<ObjCMethodDecl>(D)) |
| 3565 | resultType = cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3566 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3567 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3568 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3569 | if (!resultType->isReferenceType() && |
| 3570 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3571 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3572 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3573 | << attr.getName() |
| 3574 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3575 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3576 | |
| 3577 | // Drop the attribute. |
| 3578 | return; |
| 3579 | } |
| 3580 | |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3581 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3582 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 3583 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3586 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3587 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3588 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3589 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3590 | DeclContext *DC = method->getDeclContext(); |
| 3591 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3592 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3593 | << attr.getName() << 0; |
| 3594 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3595 | return; |
| 3596 | } |
| 3597 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3598 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3599 | << attr.getName() << 1; |
| 3600 | return; |
| 3601 | } |
| 3602 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3603 | method->addAttr(::new (S.Context) |
| 3604 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 3605 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3606 | } |
| 3607 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3608 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 3609 | const AttributeList &Attr) { |
| 3610 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr, |
| 3611 | "cf_unknown_transfer")) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3612 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3613 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3614 | D->addAttr(::new (S.Context) |
| 3615 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 3616 | Attr.getAttributeSpellingListIndex())); |
| 3617 | } |
| 3618 | |
| 3619 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 3620 | const AttributeList &Attr) { |
| 3621 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr, |
| 3622 | "cf_audited_transfer")) |
| 3623 | return; |
| 3624 | |
| 3625 | D->addAttr(::new (S.Context) |
| 3626 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 3627 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3628 | } |
| 3629 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3630 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 3631 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3632 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3633 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3634 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3635 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3636 | return; |
| 3637 | } |
| 3638 | |
| 3639 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3640 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3641 | Attr.getAttributeSpellingListIndex())); |
| 3642 | } |
| 3643 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3644 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 3645 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3646 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3647 | |
| 3648 | if (!Parm) { |
| 3649 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3650 | return; |
| 3651 | } |
| 3652 | |
| 3653 | D->addAttr(::new (S.Context) |
| 3654 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 3655 | Attr.getAttributeSpellingListIndex())); |
| 3656 | } |
| 3657 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3658 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3659 | const AttributeList &Attr) { |
| 3660 | IdentifierInfo *RelatedClass = |
| 3661 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0; |
| 3662 | if (!RelatedClass) { |
| 3663 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3664 | return; |
| 3665 | } |
| 3666 | IdentifierInfo *ClassMethod = |
| 3667 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0; |
| 3668 | IdentifierInfo *InstanceMethod = |
| 3669 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0; |
| 3670 | D->addAttr(::new (S.Context) |
| 3671 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 3672 | ClassMethod, InstanceMethod, |
| 3673 | Attr.getAttributeSpellingListIndex())); |
| 3674 | } |
| 3675 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3676 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 3677 | const AttributeList &Attr) { |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3678 | ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 3679 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3680 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3681 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 3682 | Attr.getAttributeSpellingListIndex())); |
| 3683 | } |
| 3684 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3685 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3686 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3687 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3688 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3689 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3690 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3691 | } |
| 3692 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3693 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3694 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3695 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3696 | QualType type = vd->getType(); |
| 3697 | |
| 3698 | if (!type->isDependentType() && |
| 3699 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3700 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3701 | << type; |
| 3702 | return; |
| 3703 | } |
| 3704 | |
| 3705 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3706 | |
| 3707 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3708 | // going to infer. |
| 3709 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3710 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3711 | |
| 3712 | switch (lifetime) { |
| 3713 | case Qualifiers::OCL_None: |
| 3714 | assert(type->isDependentType() && |
| 3715 | "didn't infer lifetime for non-dependent type?"); |
| 3716 | break; |
| 3717 | |
| 3718 | case Qualifiers::OCL_Weak: // meaningful |
| 3719 | case Qualifiers::OCL_Strong: // meaningful |
| 3720 | break; |
| 3721 | |
| 3722 | case Qualifiers::OCL_ExplicitNone: |
| 3723 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3724 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3725 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3726 | break; |
| 3727 | } |
| 3728 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3729 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3730 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 3731 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3734 | //===----------------------------------------------------------------------===// |
| 3735 | // Microsoft specific attribute handlers. |
| 3736 | //===----------------------------------------------------------------------===// |
| 3737 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3738 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 3739 | if (!S.LangOpts.CPlusPlus) { |
| 3740 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3741 | << Attr.getName() << AttributeLangSupport::C; |
| 3742 | return; |
| 3743 | } |
| 3744 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 3745 | if (!isa<CXXRecordDecl>(D)) { |
| 3746 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3747 | << Attr.getName() << ExpectedClass; |
| 3748 | return; |
| 3749 | } |
| 3750 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3751 | StringRef StrRef; |
| 3752 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3753 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3754 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3755 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3756 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 3757 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3758 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 3759 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3760 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3761 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3762 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3763 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3764 | return; |
| 3765 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3766 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3767 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3768 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3769 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3770 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3771 | return; |
| 3772 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3773 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3774 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3775 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3776 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3777 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3778 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3779 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 3780 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3783 | /// Handles semantic checking for features that are common to all attributes, |
| 3784 | /// such as checking whether a parameter was properly specified, or the correct |
| 3785 | /// number of arguments were passed, etc. |
| 3786 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 3787 | const AttributeList &Attr) { |
| 3788 | // Several attributes carry different semantics than the parsing requires, so |
| 3789 | // those are opted out of the common handling. |
| 3790 | // |
| 3791 | // We also bail on unknown and ignored attributes because those are handled |
| 3792 | // as part of the target-specific handling logic. |
| 3793 | if (Attr.hasCustomParsing() || |
| 3794 | Attr.getKind() == AttributeList::UnknownAttribute || |
| 3795 | Attr.getKind() == AttributeList::IgnoredAttribute) |
| 3796 | return false; |
| 3797 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3798 | // Check whether the attribute requires specific language extensions to be |
| 3799 | // enabled. |
| 3800 | if (!Attr.diagnoseLangOpts(S)) |
| 3801 | return true; |
| 3802 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3803 | // If there are no optional arguments, then checking for the argument count |
| 3804 | // is trivial. |
| 3805 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 3806 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 3807 | return true; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3808 | |
| 3809 | // Check whether the attribute appertains to the given subject. |
| 3810 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 3811 | return true; |
| 3812 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3813 | return false; |
| 3814 | } |
| 3815 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3816 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3817 | // Top Level Sema Entry Points |
| 3818 | //===----------------------------------------------------------------------===// |
| 3819 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3820 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 3821 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 3822 | /// silently ignore it if a GNU attribute. |
| 3823 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 3824 | const AttributeList &Attr, |
| 3825 | bool IncludeCXX11Attributes) { |
| 3826 | if (Attr.isInvalid()) |
| 3827 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3828 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3829 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 3830 | // instead. |
| 3831 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 3832 | return; |
| 3833 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3834 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 3835 | return; |
| 3836 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3837 | switch (Attr.getKind()) { |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 3838 | case AttributeList::AT_IBAction: |
| 3839 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3840 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 3841 | case AttributeList::AT_IBOutletCollection: |
| 3842 | handleIBOutletCollection(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3843 | case AttributeList::AT_AddressSpace: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3844 | case AttributeList::AT_ObjCGC: |
| 3845 | case AttributeList::AT_VectorSize: |
| 3846 | case AttributeList::AT_NeonVectorType: |
| 3847 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3848 | case AttributeList::AT_Ptr32: |
| 3849 | case AttributeList::AT_Ptr64: |
| 3850 | case AttributeList::AT_SPtr: |
| 3851 | case AttributeList::AT_UPtr: |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3852 | // Ignore these, these are type attributes, handled by |
| 3853 | // ProcessTypeAttributes. |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3854 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3855 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 3856 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 3857 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 3858 | case AttributeList::AT_AlwaysInline: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3859 | handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3860 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3861 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 3862 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3863 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 3864 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 3865 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 3866 | handleDependencyAttr(S, scope, D, Attr); |
| 3867 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3868 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3869 | case AttributeList::AT_CUDAConstant: |
| 3870 | handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3871 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3872 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 3873 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3874 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 3875 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 3876 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3877 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 3878 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3879 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3880 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 3881 | case AttributeList::AT_MinSize: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3882 | handleSimpleAttribute<MinSizeAttr>(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 3883 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3884 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 3885 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 3886 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 3887 | case AttributeList::AT_CUDADevice: |
| 3888 | handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3889 | case AttributeList::AT_CUDAHost: |
| 3890 | handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3891 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 3892 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3893 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3894 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3895 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3896 | case AttributeList::AT_MayAlias: |
| 3897 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break; |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3898 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3899 | case AttributeList::AT_NoCommon: |
| 3900 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3901 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3902 | case AttributeList::AT_Overloadable: |
| 3903 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 3904 | case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3905 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 3906 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3907 | case AttributeList::AT_Naked: |
| 3908 | handleSimpleAttribute<NakedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3909 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame^] | 3910 | case AttributeList::AT_NoThrow: |
| 3911 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3912 | case AttributeList::AT_CUDAShared: |
| 3913 | handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3914 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3915 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3916 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3917 | handleObjCOwnershipAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3918 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3919 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3920 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3921 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3922 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 3923 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3924 | case AttributeList::AT_ObjCRequiresSuper: |
| 3925 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 3926 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3927 | case AttributeList::AT_ObjCBridge: |
| 3928 | handleObjCBridgeAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3929 | |
| 3930 | case AttributeList::AT_ObjCBridgeMutable: |
| 3931 | handleObjCBridgeMutableAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3932 | |
| 3933 | case AttributeList::AT_ObjCBridgeRelated: |
| 3934 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3935 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3936 | case AttributeList::AT_ObjCDesignatedInitializer: |
| 3937 | handleObjCDesignatedInitializer(S, D, Attr); break; |
| 3938 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3939 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3940 | handleCFAuditedTransferAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3941 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3942 | handleCFUnknownTransferAttr(S, D, Attr); break; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3943 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3944 | // Checker-specific. |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3945 | case AttributeList::AT_CFConsumed: |
| 3946 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 3947 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3948 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3949 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3950 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3951 | case AttributeList::AT_NSReturnsNotRetained: |
| 3952 | case AttributeList::AT_CFReturnsNotRetained: |
| 3953 | case AttributeList::AT_NSReturnsRetained: |
| 3954 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3955 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 3956 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 3957 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3958 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 3959 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 3960 | case AttributeList::AT_VecTypeHint: |
| 3961 | handleVecTypeHint(S, D, Attr); break; |
| 3962 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3963 | case AttributeList::AT_InitPriority: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3964 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3965 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3966 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 3967 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 3968 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 3969 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 3970 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3971 | case AttributeList::AT_ArcWeakrefUnavailable: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3972 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3973 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3974 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 3975 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 3976 | handleObjCSuppresProtocolAttr(S, D, Attr); |
| 3977 | break; |
| 3978 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3979 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3980 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 3981 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3982 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3983 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 3984 | case AttributeList::AT_Visibility: |
| 3985 | handleVisibilityAttr(S, D, Attr, false); |
| 3986 | break; |
| 3987 | case AttributeList::AT_TypeVisibility: |
| 3988 | handleVisibilityAttr(S, D, Attr, true); |
| 3989 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 3990 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 6a42b5a | 2013-11-27 16:59:17 +0000 | [diff] [blame] | 3991 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3992 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 3993 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 3994 | case AttributeList::AT_Weak: |
| 3995 | handleSimpleAttribute<WeakAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3996 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 3997 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 3998 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3999 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4000 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4001 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4002 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4003 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4004 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4005 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4006 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4007 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4008 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame^] | 4009 | case AttributeList::AT_Const: |
| 4010 | handleSimpleAttribute<ConstAttr>(S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4011 | case AttributeList::AT_Pure: |
| 4012 | handleSimpleAttribute<PureAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4013 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4014 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4015 | case AttributeList::AT_NoInline: |
| 4016 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4017 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4018 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | b4f3134 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4019 | // Just ignore |
| 4020 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4021 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4022 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4023 | case AttributeList::AT_StdCall: |
| 4024 | case AttributeList::AT_CDecl: |
| 4025 | case AttributeList::AT_FastCall: |
| 4026 | case AttributeList::AT_ThisCall: |
| 4027 | case AttributeList::AT_Pascal: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4028 | case AttributeList::AT_MSABI: |
| 4029 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4030 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4031 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4032 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4033 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4034 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4035 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4036 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4037 | case AttributeList::AT_OpenCLImageAccess: |
| 4038 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4039 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4040 | |
| 4041 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4042 | case AttributeList::AT_MsStruct: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4043 | handleSimpleAttribute<MsStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4044 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4045 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4046 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4047 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 4048 | case AttributeList::AT_MSInheritance: |
| 4049 | handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4050 | case AttributeList::AT_ForceInline: |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4051 | handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4052 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4053 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4054 | |
| 4055 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4056 | case AttributeList::AT_AssertExclusiveLock: |
| 4057 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4058 | break; |
| 4059 | case AttributeList::AT_AssertSharedLock: |
| 4060 | handleAssertSharedLockAttr(S, D, Attr); |
| 4061 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4062 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 4063 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4064 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4065 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4066 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4067 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4068 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4069 | case AttributeList::AT_NoSanitizeAddress: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4070 | handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4071 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4072 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4073 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4074 | break; |
| 4075 | case AttributeList::AT_NoSanitizeThread: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4076 | handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4077 | break; |
| 4078 | case AttributeList::AT_NoSanitizeMemory: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4079 | handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4080 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4081 | case AttributeList::AT_Lockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4082 | handleSimpleAttribute<LockableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4083 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4084 | handleGuardedByAttr(S, D, Attr); |
| 4085 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4086 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4087 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4088 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4089 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4090 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4091 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4092 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4093 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4094 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4095 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4096 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4097 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4098 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4099 | handleLockReturnedAttr(S, D, Attr); |
| 4100 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4101 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4102 | handleLocksExcludedAttr(S, D, Attr); |
| 4103 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4104 | case AttributeList::AT_SharedLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4105 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4106 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4107 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4108 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4109 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4110 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4111 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4112 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4113 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4114 | handleUnlockFunAttr(S, D, Attr); |
| 4115 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4116 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4117 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4118 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4119 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4120 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4121 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4122 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4123 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4124 | case AttributeList::AT_Consumable: |
| 4125 | handleConsumableAttr(S, D, Attr); |
| 4126 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4127 | case AttributeList::AT_CallableWhen: |
| 4128 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4129 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4130 | case AttributeList::AT_ParamTypestate: |
| 4131 | handleParamTypestateAttr(S, D, Attr); |
| 4132 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4133 | case AttributeList::AT_ReturnTypestate: |
| 4134 | handleReturnTypestateAttr(S, D, Attr); |
| 4135 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4136 | case AttributeList::AT_SetTypestate: |
| 4137 | handleSetTypestateAttr(S, D, Attr); |
| 4138 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4139 | case AttributeList::AT_TestTypestate: |
| 4140 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4141 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4142 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4143 | // Type safety attributes. |
| 4144 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4145 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4146 | break; |
| 4147 | case AttributeList::AT_TypeTagForDatatype: |
| 4148 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4149 | break; |
| 4150 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4151 | default: |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4152 | // Ask target about the attribute. |
| 4153 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4154 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4155 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4156 | diag::warn_unhandled_ms_attribute_ignored : |
| 4157 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4158 | break; |
| 4159 | } |
| 4160 | } |
| 4161 | |
| 4162 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4163 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4164 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4165 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4166 | bool IncludeCXX11Attributes) { |
| 4167 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4168 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4169 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4170 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4171 | // GCC accepts |
| 4172 | // static int a9 __attribute__((weakref)); |
| 4173 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4174 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4175 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4176 | cast<NamedDecl>(D)->getNameAsString(); |
| 4177 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4178 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4179 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4180 | |
| 4181 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 4182 | // These attributes cannot be applied to a non-kernel function. |
| 4183 | if (D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 4184 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) |
| 4185 | << "reqd_work_group_size"; |
| 4186 | D->setInvalidDecl(); |
| 4187 | } |
| 4188 | if (D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 4189 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) |
| 4190 | << "work_group_size_hint"; |
| 4191 | D->setInvalidDecl(); |
| 4192 | } |
| 4193 | if (D->hasAttr<VecTypeHintAttr>()) { |
| 4194 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << "vec_type_hint"; |
| 4195 | D->setInvalidDecl(); |
| 4196 | } |
| 4197 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4198 | } |
| 4199 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4200 | // Annotation attributes are the only attributes allowed after an access |
| 4201 | // specifier. |
| 4202 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4203 | const AttributeList *AttrList) { |
| 4204 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4205 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4206 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4207 | } else { |
| 4208 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4209 | return true; |
| 4210 | } |
| 4211 | } |
| 4212 | |
| 4213 | return false; |
| 4214 | } |
| 4215 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4216 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4217 | /// contains any decl attributes that we should warn about. |
| 4218 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4219 | for ( ; A; A = A->getNext()) { |
| 4220 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4221 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4222 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4223 | |
| 4224 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4225 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4226 | << A->getName() << A->getRange(); |
| 4227 | } else { |
| 4228 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4229 | << A->getName() << A->getRange(); |
| 4230 | } |
| 4231 | } |
| 4232 | } |
| 4233 | |
| 4234 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4235 | /// used to build a declaration, complain about any decl attributes |
| 4236 | /// which might be lying around on it. |
| 4237 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4238 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4239 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4240 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4241 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4242 | } |
| 4243 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4244 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4245 | /// \#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] | 4246 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4247 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4248 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4249 | NamedDecl *NewD = 0; |
| 4250 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4251 | FunctionDecl *NewFD; |
| 4252 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4253 | // FIXME: Mangling? |
| 4254 | // FIXME: Is the qualifier info correct? |
| 4255 | // FIXME: Is the DeclContext correct? |
| 4256 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4257 | Loc, Loc, DeclarationName(II), |
| 4258 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4259 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4260 | FD->hasPrototype(), |
| 4261 | false/*isConstexprSpecified*/); |
| 4262 | NewD = NewFD; |
| 4263 | |
| 4264 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4265 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4266 | |
| 4267 | // Fake up parameter variables; they are declared as if this were |
| 4268 | // a typedef. |
| 4269 | QualType FDTy = FD->getType(); |
| 4270 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4271 | SmallVector<ParmVarDecl*, 16> Params; |
| 4272 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4273 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4274 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4275 | Param->setScopeInfo(0, Params.size()); |
| 4276 | Params.push_back(Param); |
| 4277 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4278 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4279 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4280 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4281 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4282 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4283 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4284 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4285 | if (VD->getQualifier()) { |
| 4286 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4287 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4288 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4289 | } |
| 4290 | return NewD; |
| 4291 | } |
| 4292 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4293 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4294 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4295 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4296 | if (W.getUsed()) return; // only do this once |
| 4297 | W.setUsed(true); |
| 4298 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4299 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4300 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4301 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 4302 | NDId->getName())); |
| 4303 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4304 | WeakTopLevelDecl.push_back(NewD); |
| 4305 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4306 | // to insert Decl at TU scope, sorry. |
| 4307 | DeclContext *SavedContext = CurContext; |
| 4308 | CurContext = Context.getTranslationUnitDecl(); |
| 4309 | PushOnScopeChains(NewD, S); |
| 4310 | CurContext = SavedContext; |
| 4311 | } else { // just add weak to existing |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4312 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4313 | } |
| 4314 | } |
| 4315 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 4316 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 4317 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4318 | // have to do this. |
| 4319 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4320 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4321 | NamedDecl *ND = NULL; |
| 4322 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 4323 | if (VD->isExternC()) |
| 4324 | ND = VD; |
| 4325 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 4326 | if (FD->isExternC()) |
| 4327 | ND = FD; |
| 4328 | if (ND) { |
| 4329 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4330 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4331 | = WeakUndeclaredIdentifiers.find(Id); |
| 4332 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 4333 | WeakInfo W = I->second; |
| 4334 | DeclApplyPragmaWeak(S, ND, W); |
| 4335 | WeakUndeclaredIdentifiers[Id] = W; |
| 4336 | } |
| 4337 | } |
| 4338 | } |
| 4339 | } |
| 4340 | } |
| 4341 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4342 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4343 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4344 | /// specified in many different places, and we need to find and apply them all. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4345 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4346 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4347 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4348 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4349 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4350 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4351 | // position to the decl itself. This handles cases like: |
| 4352 | // int *__attr__(x)** D; |
| 4353 | // when X is a decl attribute. |
| 4354 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4355 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4356 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4357 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4358 | // Finally, apply any attributes on the decl itself. |
| 4359 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4360 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4361 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4362 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4363 | /// Is the given declaration allowed to use a forbidden type? |
| 4364 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4365 | // Private ivars are always okay. Unfortunately, people don't |
| 4366 | // always properly make their ivars private, even in system headers. |
| 4367 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4368 | // Function declarations in sys headers will be marked unavailable. |
| 4369 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4370 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4371 | return false; |
| 4372 | |
| 4373 | // Require it to be declared in a system header. |
| 4374 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4375 | } |
| 4376 | |
| 4377 | /// Handle a delayed forbidden-type diagnostic. |
| 4378 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4379 | Decl *decl) { |
| 4380 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 4381 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 4382 | "this system declaration uses an unsupported type")); |
| 4383 | return; |
| 4384 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4385 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4386 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4387 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4388 | // kind of forbidden type messages on unavailable functions. |
| 4389 | if (FD->hasAttr<UnavailableAttr>() && |
| 4390 | diag.getForbiddenTypeDiagnostic() == |
| 4391 | diag::err_arc_array_param_no_ownership) { |
| 4392 | diag.Triggered = true; |
| 4393 | return; |
| 4394 | } |
| 4395 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4396 | |
| 4397 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4398 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4399 | diag.Triggered = true; |
| 4400 | } |
| 4401 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4402 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4403 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4404 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4405 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4406 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4407 | // When delaying diagnostics to run in the context of a parsed |
| 4408 | // declaration, we only want to actually emit anything if parsing |
| 4409 | // succeeds. |
| 4410 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4411 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4412 | // We emit all the active diagnostics in this pool or any of its |
| 4413 | // parents. In general, we'll get one pool for the decl spec |
| 4414 | // and a child pool for each declarator; in a decl group like: |
| 4415 | // deprecated_typedef foo, *bar, baz(); |
| 4416 | // only the declarator pops will be passed decls. This is correct; |
| 4417 | // we really do need to consider delayed diagnostics from the decl spec |
| 4418 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4419 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4420 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4421 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4422 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4423 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4424 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4425 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4426 | continue; |
| 4427 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4428 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4429 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4430 | case DelayedDiagnostic::Unavailable: |
| 4431 | // Don't bother giving deprecation/unavailable diagnostics if |
| 4432 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4433 | if (!decl->isInvalidDecl()) |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4434 | HandleDelayedAvailabilityCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4435 | break; |
| 4436 | |
| 4437 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4438 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4439 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4440 | |
| 4441 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4442 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4443 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4444 | } |
| 4445 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4446 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4447 | } |
| 4448 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4449 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4450 | /// been delayed in the current context instead of in the given pool. |
| 4451 | /// Essentially, this just moves them to the current pool. |
| 4452 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4453 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4454 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4455 | curPool->steal(pool); |
| 4456 | } |
| 4457 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4458 | static bool isDeclDeprecated(Decl *D) { |
| 4459 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4460 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4461 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4462 | // A category implicitly has the availability of the interface. |
| 4463 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4464 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4465 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4466 | return false; |
| 4467 | } |
| 4468 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4469 | static bool isDeclUnavailable(Decl *D) { |
| 4470 | do { |
| 4471 | if (D->isUnavailable()) |
| 4472 | return true; |
| 4473 | // A category implicitly has the availability of the interface. |
| 4474 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4475 | return CatD->getClassInterface()->isUnavailable(); |
| 4476 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4477 | return false; |
| 4478 | } |
| 4479 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4480 | static void |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4481 | DoEmitAvailabilityWarning(Sema &S, |
| 4482 | DelayedDiagnostic::DDKind K, |
| 4483 | Decl *Ctx, |
| 4484 | const NamedDecl *D, |
| 4485 | StringRef Message, |
| 4486 | SourceLocation Loc, |
| 4487 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4488 | const ObjCPropertyDecl *ObjCProperty) { |
| 4489 | |
| 4490 | // Diagnostics for deprecated or unavailable. |
| 4491 | unsigned diag, diag_message, diag_fwdclass_message; |
| 4492 | |
| 4493 | // Matches 'diag::note_property_attribute' options. |
| 4494 | unsigned property_note_select; |
| 4495 | |
| 4496 | // Matches diag::note_availability_specified_here. |
| 4497 | unsigned available_here_select_kind; |
| 4498 | |
| 4499 | // Don't warn if our current context is deprecated or unavailable. |
| 4500 | switch (K) { |
| 4501 | case DelayedDiagnostic::Deprecation: |
| 4502 | if (isDeclDeprecated(Ctx)) |
| 4503 | return; |
| 4504 | diag = diag::warn_deprecated; |
| 4505 | diag_message = diag::warn_deprecated_message; |
| 4506 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 4507 | property_note_select = /* deprecated */ 0; |
| 4508 | available_here_select_kind = /* deprecated */ 2; |
| 4509 | break; |
| 4510 | |
| 4511 | case DelayedDiagnostic::Unavailable: |
| 4512 | if (isDeclUnavailable(Ctx)) |
| 4513 | return; |
| 4514 | diag = diag::err_unavailable; |
| 4515 | diag_message = diag::err_unavailable_message; |
| 4516 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 4517 | property_note_select = /* unavailable */ 1; |
| 4518 | available_here_select_kind = /* unavailable */ 0; |
| 4519 | break; |
| 4520 | |
| 4521 | default: |
| 4522 | llvm_unreachable("Neither a deprecation or unavailable kind"); |
| 4523 | } |
| 4524 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4525 | DeclarationName Name = D->getDeclName(); |
| 4526 | if (!Message.empty()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4527 | S.Diag(Loc, diag_message) << Name << Message; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4528 | if (ObjCProperty) |
| 4529 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4530 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4531 | } else if (!UnknownObjCClass) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4532 | S.Diag(Loc, diag) << Name; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4533 | if (ObjCProperty) |
| 4534 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4535 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4536 | } else { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4537 | S.Diag(Loc, diag_fwdclass_message) << Name; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4538 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4539 | } |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4540 | |
| 4541 | S.Diag(D->getLocation(), diag::note_availability_specified_here) |
| 4542 | << D << available_here_select_kind; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4545 | void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD, |
| 4546 | Decl *Ctx) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4547 | DD.Triggered = true; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4548 | DoEmitAvailabilityWarning(*this, |
| 4549 | (DelayedDiagnostic::DDKind) DD.Kind, |
| 4550 | Ctx, |
| 4551 | DD.getDeprecationDecl(), |
| 4552 | DD.getDeprecationMessage(), |
| 4553 | DD.Loc, |
| 4554 | DD.getUnknownObjCClass(), |
| 4555 | DD.getObjCProperty()); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4556 | } |
| 4557 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4558 | void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD, |
| 4559 | NamedDecl *D, StringRef Message, |
| 4560 | SourceLocation Loc, |
| 4561 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4562 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4563 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4564 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4565 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D, |
| 4566 | UnknownObjCClass, |
| 4567 | ObjCProperty, |
| 4568 | Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4569 | return; |
| 4570 | } |
| 4571 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4572 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
| 4573 | DelayedDiagnostic::DDKind K; |
| 4574 | switch (AD) { |
| 4575 | case AD_Deprecation: |
| 4576 | K = DelayedDiagnostic::Deprecation; |
| 4577 | break; |
| 4578 | case AD_Unavailable: |
| 4579 | K = DelayedDiagnostic::Unavailable; |
| 4580 | break; |
| 4581 | } |
| 4582 | |
| 4583 | DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc, |
| 4584 | UnknownObjCClass, ObjCProperty); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4585 | } |