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 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 243 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 244 | /// instance method D. May output an error. |
| 245 | /// |
| 246 | /// \returns true if IdxExpr is a valid index. |
| 247 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 248 | StringRef AttrName, |
| 249 | SourceLocation AttrLoc, |
| 250 | unsigned AttrArgNum, |
| 251 | const Expr *IdxExpr, |
| 252 | uint64_t &Idx) |
| 253 | { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 254 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 255 | |
| 256 | // In C++ the implicit 'this' function parameter also counts. |
| 257 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 258 | bool HP = hasFunctionProto(D); |
| 259 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 260 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 261 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 262 | HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 263 | |
| 264 | llvm::APSInt IdxInt; |
| 265 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 266 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 267 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 268 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 269 | << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 270 | return false; |
| 271 | } |
| 272 | |
| 273 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 274 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 275 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 276 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 277 | return false; |
| 278 | } |
| 279 | Idx--; // Convert to zero-based. |
| 280 | if (HasImplicitThisParam) { |
| 281 | if (Idx == 0) { |
| 282 | S.Diag(AttrLoc, |
| 283 | diag::err_attribute_invalid_implicit_this_argument) |
| 284 | << AttrName << IdxExpr->getSourceRange(); |
| 285 | return false; |
| 286 | } |
| 287 | --Idx; |
| 288 | } |
| 289 | |
| 290 | return true; |
| 291 | } |
| 292 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 293 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 294 | /// If not emit an error and return false. If the argument is an identifier it |
| 295 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 296 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 297 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 298 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 299 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 300 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 301 | if (Attr.isArgIdent(ArgNum)) { |
| 302 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 303 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 304 | << Attr.getName() << AANT_ArgumentString |
| 305 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 306 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 307 | Str = Loc->Ident->getName(); |
| 308 | if (ArgLocation) |
| 309 | *ArgLocation = Loc->Loc; |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | // Now check for an actual string literal. |
| 314 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 315 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 316 | if (ArgLocation) |
| 317 | *ArgLocation = ArgExpr->getLocStart(); |
| 318 | |
| 319 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 320 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 321 | << Attr.getName() << AANT_ArgumentString; |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | Str = Literal->getString(); |
| 326 | return true; |
| 327 | } |
| 328 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 329 | /// \brief Applies the given attribute to the Decl without performing any |
| 330 | /// additional semantic checking. |
| 331 | template <typename AttrType> |
| 332 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 333 | const AttributeList &Attr) { |
| 334 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 335 | Attr.getAttributeSpellingListIndex())); |
| 336 | } |
| 337 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 338 | /// \brief Check if the passed-in expression is of type int or bool. |
| 339 | static bool isIntOrBool(Expr *Exp) { |
| 340 | QualType QT = Exp->getType(); |
| 341 | return QT->isBooleanType() || QT->isIntegerType(); |
| 342 | } |
| 343 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 344 | |
| 345 | // Check to see if the type is a smart pointer of some kind. We assume |
| 346 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 347 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 348 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 349 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 350 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 351 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 352 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 353 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 354 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 355 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 356 | return false; |
| 357 | |
| 358 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 361 | /// \brief Check if passed in Decl is a pointer type. |
| 362 | /// Note that this function may produce an error message. |
| 363 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 364 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 365 | const AttributeList &Attr) { |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 366 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 367 | QualType QT = vd->getType(); |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 368 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 369 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 370 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 371 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 372 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 373 | // (We don't want to force template instantiation if we can avoid it, |
| 374 | // since that would alter the order in which templates are instantiated.) |
| 375 | if (RT->isIncompleteType()) |
| 376 | return true; |
| 377 | |
| 378 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 379 | return true; |
| 380 | } |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 381 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 382 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 383 | << Attr.getName()->getName() << QT; |
| 384 | } else { |
| 385 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 386 | << Attr.getName(); |
| 387 | } |
| 388 | return false; |
| 389 | } |
| 390 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 391 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 392 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 393 | static const RecordType *getRecordType(QualType QT) { |
| 394 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 395 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 396 | |
| 397 | // Now check if we point to record type. |
| 398 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 399 | return PT->getPointeeType()->getAs<RecordType>(); |
| 400 | |
| 401 | return 0; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 402 | } |
| 403 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 404 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 405 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 406 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 407 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 408 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 409 | return true; |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 414 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 415 | /// resolves to a lockable object. |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 416 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 417 | QualType Ty) { |
| 418 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 419 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 420 | // Warn if could not get record type for this argument. |
Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 421 | if (!RT) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 422 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 423 | << Attr.getName() << Ty.getAsString(); |
| 424 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 425 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 426 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 427 | // 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] | 428 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 429 | return; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 430 | |
| 431 | // Allow smart pointers to be used as lockable objects. |
| 432 | // FIXME -- Check the type that the smart pointer points to. |
| 433 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 434 | return; |
| 435 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 436 | // Check if the type is lockable. |
| 437 | RecordDecl *RD = RT->getDecl(); |
| 438 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 439 | return; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 440 | |
| 441 | // Else check if any base classes are lockable. |
| 442 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 443 | CXXBasePaths BPaths(false, false); |
| 444 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 445 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 446 | } |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 447 | |
| 448 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 449 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 452 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 453 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 454 | /// \param Sidx The attribute argument index to start checking with. |
| 455 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 456 | /// parameter list. |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 457 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 458 | const AttributeList &Attr, |
| 459 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 460 | int Sidx = 0, |
| 461 | bool ParamIdxOk = false) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 462 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 463 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 464 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 465 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 466 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 467 | Args.push_back(ArgExp); |
| 468 | continue; |
| 469 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 470 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 471 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 472 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 473 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 474 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 475 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 476 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 477 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 478 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 479 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 480 | // We allow constant strings to be used as a placeholder for expressions |
| 481 | // that are not valid C++ syntax, but warn that they are ignored. |
| 482 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 483 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 484 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 485 | continue; |
| 486 | } |
| 487 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 488 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 489 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 490 | // A pointer to member expression of the form &MyClass::mu is treated |
| 491 | // specially -- we need to look at the type of the member. |
| 492 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 493 | if (UOp->getOpcode() == UO_AddrOf) |
| 494 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 495 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 496 | ArgTy = DRE->getDecl()->getType(); |
| 497 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 498 | // First see if we can just cast to record type, or point to record type. |
| 499 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 500 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 501 | // Now check if we index into a record type function param. |
| 502 | if(!RT && ParamIdxOk) { |
| 503 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 504 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 505 | if(FD && IL) { |
| 506 | unsigned int NumParams = FD->getNumParams(); |
| 507 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 508 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 509 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 510 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 511 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 512 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 513 | continue; |
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 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 519 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 520 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 521 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 522 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 525 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 526 | // Attribute Implementations |
| 527 | //===----------------------------------------------------------------------===// |
| 528 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 529 | // FIXME: All this manual attribute parsing code is gross. At the |
| 530 | // least add some helper functions to check most argument patterns (# |
| 531 | // and types of args). |
| 532 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 533 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 534 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 535 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 536 | return; |
| 537 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 538 | D->addAttr(::new (S.Context) |
| 539 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 540 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 541 | } |
| 542 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 543 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 544 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 545 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 546 | SmallVector<Expr*, 1> Args; |
| 547 | // check that all arguments are lockable objects |
| 548 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 549 | unsigned Size = Args.size(); |
| 550 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 551 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 552 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 553 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 554 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 555 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 558 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 559 | Expr *Arg = 0; |
| 560 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 561 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 562 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 563 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 564 | } |
| 565 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 566 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 567 | const AttributeList &Attr) { |
| 568 | Expr *Arg = 0; |
| 569 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 570 | return; |
| 571 | |
| 572 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 573 | return; |
| 574 | |
| 575 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 576 | S.Context, Arg)); |
| 577 | } |
| 578 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 579 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 580 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 581 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 582 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 583 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 584 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 585 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 586 | QualType QT = cast<ValueDecl>(D)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 587 | if (!QT->isDependentType()) { |
| 588 | const RecordType *RT = getRecordType(QT); |
| 589 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 590 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 591 | << Attr.getName(); |
| 592 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 596 | // Check that all arguments are lockable objects. |
| 597 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 598 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 599 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 600 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 601 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 604 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 605 | const AttributeList &Attr) { |
| 606 | SmallVector<Expr*, 1> Args; |
| 607 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 608 | return; |
| 609 | |
| 610 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 611 | D->addAttr(::new (S.Context) |
| 612 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 613 | StartArg, Args.size(), |
| 614 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 617 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 618 | const AttributeList &Attr) { |
| 619 | SmallVector<Expr*, 1> Args; |
| 620 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 621 | return; |
| 622 | |
| 623 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 624 | D->addAttr(::new (S.Context) |
| 625 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 626 | StartArg, Args.size(), |
| 627 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 630 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 631 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 632 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 633 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 634 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 635 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 636 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 637 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 640 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 641 | const AttributeList &Attr) { |
| 642 | SmallVector<Expr*, 1> Args; |
| 643 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 644 | return; |
| 645 | |
| 646 | unsigned Size = Args.size(); |
| 647 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 648 | D->addAttr(::new (S.Context) |
| 649 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 650 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 653 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 654 | const AttributeList &Attr) { |
| 655 | SmallVector<Expr*, 1> Args; |
| 656 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 657 | return; |
| 658 | |
| 659 | unsigned Size = Args.size(); |
| 660 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 661 | D->addAttr(::new (S.Context) |
| 662 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 663 | StartArg, Size, |
| 664 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 665 | } |
| 666 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 667 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 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]; |
| 675 | D->addAttr(::new (S.Context) |
| 676 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 677 | Attr.getAttributeSpellingListIndex())); |
| 678 | } |
| 679 | |
| 680 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 681 | const AttributeList &Attr) { |
| 682 | SmallVector<Expr*, 1> Args; |
| 683 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 684 | return; |
| 685 | |
| 686 | unsigned Size = Args.size(); |
| 687 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 688 | D->addAttr(::new (S.Context) |
| 689 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 690 | StartArg, Size, |
| 691 | Attr.getAttributeSpellingListIndex())); |
| 692 | } |
| 693 | |
| 694 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 695 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 696 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 697 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 698 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 699 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 700 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 701 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 702 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 703 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 704 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 708 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 709 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 710 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 713 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 714 | const AttributeList &Attr) { |
| 715 | SmallVector<Expr*, 2> Args; |
| 716 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 717 | return; |
| 718 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 719 | D->addAttr(::new (S.Context) |
| 720 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 721 | Attr.getArgAsExpr(0), |
| 722 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 723 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 726 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 727 | const AttributeList &Attr) { |
| 728 | SmallVector<Expr*, 2> Args; |
| 729 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 730 | return; |
| 731 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 732 | D->addAttr(::new (S.Context) |
| 733 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 734 | Attr.getArgAsExpr(0), |
| 735 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 736 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 739 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 740 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 741 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 742 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 743 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 744 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 745 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 746 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 747 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 748 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 749 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 750 | return true; |
| 751 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 752 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 753 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 754 | const AttributeList &Attr) { |
| 755 | SmallVector<Expr*, 1> Args; |
| 756 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 757 | return; |
| 758 | |
| 759 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 760 | D->addAttr(::new (S.Context) |
| 761 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 762 | StartArg, Args.size(), |
| 763 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 766 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 767 | const AttributeList &Attr) { |
| 768 | SmallVector<Expr*, 1> Args; |
| 769 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 770 | return; |
| 771 | |
| 772 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 773 | D->addAttr(::new (S.Context) |
| 774 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 775 | StartArg, Args.size(), |
| 776 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 779 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 780 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 781 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 782 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 783 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 784 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 785 | unsigned Size = Args.size(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 786 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 787 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 788 | D->addAttr(::new (S.Context) |
| 789 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 790 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 794 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 795 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 796 | SmallVector<Expr*, 1> Args; |
| 797 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 798 | unsigned Size = Args.size(); |
| 799 | if (Size == 0) |
| 800 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 801 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 802 | D->addAttr(::new (S.Context) |
| 803 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 804 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 808 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 809 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 810 | return; |
| 811 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 812 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 813 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 814 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 815 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 816 | if (Size == 0) |
| 817 | return; |
| 818 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 819 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 820 | D->addAttr(::new (S.Context) |
| 821 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 822 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 823 | } |
| 824 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 825 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 826 | ConsumableAttr::ConsumedState DefaultState; |
| 827 | |
| 828 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 829 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 830 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 831 | DefaultState)) { |
| 832 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 833 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 834 | return; |
| 835 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 836 | } else { |
| 837 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 838 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 839 | return; |
| 840 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 841 | |
| 842 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 843 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 844 | Attr.getAttributeSpellingListIndex())); |
| 845 | } |
| 846 | |
| 847 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 848 | const AttributeList &Attr) { |
| 849 | ASTContext &CurrContext = S.getASTContext(); |
| 850 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 851 | |
| 852 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 853 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 854 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 855 | RD->getNameAsString(); |
| 856 | |
| 857 | return false; |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | return true; |
| 862 | } |
| 863 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 864 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 865 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 866 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 867 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 868 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 869 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 870 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 871 | return; |
| 872 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 873 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 874 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 875 | CallableWhenAttr::ConsumedState CallableState; |
| 876 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 877 | StringRef StateString; |
| 878 | SourceLocation Loc; |
| 879 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 880 | return; |
| 881 | |
| 882 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 883 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 884 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 885 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 886 | return; |
| 887 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 888 | |
| 889 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 890 | } |
| 891 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 892 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 893 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 894 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 895 | } |
| 896 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 897 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 898 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 899 | const AttributeList &Attr) { |
| 900 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 901 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 902 | ParamTypestateAttr::ConsumedState ParamState; |
| 903 | |
| 904 | if (Attr.isArgIdent(0)) { |
| 905 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 906 | StringRef StateString = Ident->Ident->getName(); |
| 907 | |
| 908 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 909 | ParamState)) { |
| 910 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 911 | << Attr.getName() << StateString; |
| 912 | return; |
| 913 | } |
| 914 | } else { |
| 915 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 916 | Attr.getName() << AANT_ArgumentIdentifier; |
| 917 | return; |
| 918 | } |
| 919 | |
| 920 | // FIXME: This check is currently being done in the analysis. It can be |
| 921 | // enabled here only after the parser propagates attributes at |
| 922 | // template specialization definition, not declaration. |
| 923 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 924 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 925 | // |
| 926 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 927 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 928 | // ReturnType.getAsString(); |
| 929 | // return; |
| 930 | //} |
| 931 | |
| 932 | D->addAttr(::new (S.Context) |
| 933 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 934 | Attr.getAttributeSpellingListIndex())); |
| 935 | } |
| 936 | |
| 937 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 938 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 939 | const AttributeList &Attr) { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 940 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 941 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 942 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 943 | |
| 944 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 945 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 946 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 947 | ReturnState)) { |
| 948 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 949 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 950 | return; |
| 951 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 952 | } else { |
| 953 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 954 | Attr.getName() << AANT_ArgumentIdentifier; |
| 955 | return; |
| 956 | } |
| 957 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 958 | // FIXME: This check is currently being done in the analysis. It can be |
| 959 | // enabled here only after the parser propagates attributes at |
| 960 | // template specialization definition, not declaration. |
| 961 | //QualType ReturnType; |
| 962 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 963 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 964 | // ReturnType = Param->getType(); |
| 965 | // |
| 966 | //} else if (const CXXConstructorDecl *Constructor = |
| 967 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 968 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 969 | // |
| 970 | //} else { |
| 971 | // |
| 972 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 973 | //} |
| 974 | // |
| 975 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 976 | // |
| 977 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 978 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 979 | // ReturnType.getAsString(); |
| 980 | // return; |
| 981 | //} |
| 982 | |
| 983 | D->addAttr(::new (S.Context) |
| 984 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 985 | Attr.getAttributeSpellingListIndex())); |
| 986 | } |
| 987 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 988 | |
| 989 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 990 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 991 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 992 | |
| 993 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 994 | return; |
| 995 | |
| 996 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 997 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 998 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 999 | StringRef Param = Ident->Ident->getName(); |
| 1000 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1001 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1002 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1003 | return; |
| 1004 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1005 | } else { |
| 1006 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1007 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1008 | return; |
| 1009 | } |
| 1010 | |
| 1011 | D->addAttr(::new (S.Context) |
| 1012 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1013 | Attr.getAttributeSpellingListIndex())); |
| 1014 | } |
| 1015 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1016 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1017 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1018 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1019 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1020 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1021 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1022 | return; |
| 1023 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1024 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1025 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1026 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1027 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1028 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1029 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1030 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1031 | return; |
| 1032 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1033 | } else { |
| 1034 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1035 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1036 | return; |
| 1037 | } |
| 1038 | |
| 1039 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1040 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1041 | Attr.getAttributeSpellingListIndex())); |
| 1042 | } |
| 1043 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1044 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1045 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1046 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1047 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1050 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1051 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1052 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1053 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1054 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1055 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1056 | if (!FD->getType()->isDependentType() && |
| 1057 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1058 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1059 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1060 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1061 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1062 | FD->addAttr(::new (S.Context) |
| 1063 | PackedAttr(Attr.getRange(), S.Context, |
| 1064 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1065 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1066 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1069 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1070 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1071 | // variables or properties of Objective-C classes. The outlet must also |
| 1072 | // have an object reference type. |
| 1073 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1074 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1075 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1076 | << Attr.getName() << VD->getType() << 0; |
| 1077 | return false; |
| 1078 | } |
| 1079 | } |
| 1080 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1081 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1082 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1083 | << Attr.getName() << PD->getType() << 1; |
| 1084 | return false; |
| 1085 | } |
| 1086 | } |
| 1087 | else { |
| 1088 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1089 | return false; |
| 1090 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1091 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1092 | return true; |
| 1093 | } |
| 1094 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1095 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1096 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1097 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1098 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1099 | D->addAttr(::new (S.Context) |
| 1100 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1101 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1104 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1105 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1106 | |
| 1107 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1108 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1109 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1110 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1111 | return; |
| 1112 | } |
| 1113 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1114 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1115 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1116 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1117 | ParsedType PT; |
| 1118 | |
| 1119 | if (Attr.hasParsedType()) |
| 1120 | PT = Attr.getTypeArg(); |
| 1121 | else { |
| 1122 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1123 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1124 | if (!PT) { |
| 1125 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1126 | return; |
| 1127 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1128 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1129 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1130 | TypeSourceInfo *QTLoc = 0; |
| 1131 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1132 | if (!QTLoc) |
| 1133 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1134 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1135 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1136 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1137 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1138 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1139 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1140 | S.Diag(Attr.getLoc(), |
| 1141 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1142 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1143 | return; |
| 1144 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1145 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1146 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1147 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1148 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1151 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1152 | if (const RecordType *UT = T->getAsUnionType()) |
| 1153 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1154 | RecordDecl *UD = UT->getDecl(); |
| 1155 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1156 | itend = UD->field_end(); it != itend; ++it) { |
| 1157 | QualType QT = it->getType(); |
| 1158 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1159 | T = QT; |
| 1160 | return; |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1166 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | e881ce2 | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1167 | if (!isFunctionOrMethod(D)) { |
| 1168 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1169 | << Attr.getName() << ExpectedFunctionOrMethod; |
Nuno Lopes | e881ce2 | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1170 | return; |
| 1171 | } |
| 1172 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1173 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1174 | return; |
| 1175 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1176 | SmallVector<unsigned, 8> SizeArgs; |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1177 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1178 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1179 | uint64_t Idx; |
| 1180 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1181 | Attr.getLoc(), i + 1, Ex, Idx)) |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1182 | return; |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1183 | |
| 1184 | // check if the function argument is of an integer type |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1185 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1186 | if (!T->isIntegerType()) { |
Aaron Ballman | 9d69509 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 1187 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1188 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 1189 | << Ex->getSourceRange(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1190 | return; |
| 1191 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1192 | SizeArgs.push_back(Idx); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
| 1195 | // check if the function returns a pointer |
| 1196 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1197 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1198 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1201 | D->addAttr(::new (S.Context) |
| 1202 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1203 | SizeArgs.data(), SizeArgs.size(), |
| 1204 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1205 | } |
| 1206 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1207 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1208 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1209 | // ignore it as well |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1210 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1211 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1212 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1213 | return; |
| 1214 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1215 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1216 | SmallVector<unsigned, 8> NonNullArgs; |
| 1217 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1218 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1219 | uint64_t Idx; |
| 1220 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1221 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1222 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1223 | |
| 1224 | // Is the function argument a pointer type? |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1225 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1226 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1227 | |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1228 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1229 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | 62157e5 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1230 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1231 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1232 | continue; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1233 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1234 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1235 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1236 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1237 | |
| 1238 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1239 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1240 | if (NonNullArgs.empty()) { |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1241 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1242 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1243 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1244 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1245 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1246 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1247 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1248 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1249 | if (NonNullArgs.empty()) { |
| 1250 | // Warn the trivial case only if attribute is not coming from a |
| 1251 | // macro instantiation. |
| 1252 | if (Attr.getLoc().isFileID()) |
| 1253 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1254 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1255 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1256 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1257 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1258 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1259 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1260 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1261 | D->addAttr(::new (S.Context) |
| 1262 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1263 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1266 | static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) { |
| 1267 | switch (K) { |
| 1268 | case OwnershipAttr::Holds: return "'ownership_holds'"; |
| 1269 | case OwnershipAttr::Takes: return "'ownership_takes'"; |
| 1270 | case OwnershipAttr::Returns: return "'ownership_returns'"; |
| 1271 | } |
| 1272 | llvm_unreachable("unknown ownership"); |
| 1273 | } |
| 1274 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1275 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1276 | // This attribute must be applied to a function declaration. The first |
| 1277 | // argument to the attribute must be an identifier, the name of the resource, |
| 1278 | // for example: malloc. The following arguments must be argument indexes, the |
| 1279 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1280 | // 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] | 1281 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1282 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1283 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1284 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1285 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1286 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1287 | return; |
| 1288 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1289 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1290 | // Figure out our Kind. |
| 1291 | OwnershipAttr::OwnershipKind K = |
| 1292 | OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0, |
| 1293 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1294 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1295 | // Check arguments. |
| 1296 | switch (K) { |
| 1297 | case OwnershipAttr::Takes: |
| 1298 | case OwnershipAttr::Holds: |
| 1299 | if (AL.getNumArgs() < 2) { |
| 1300 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2; |
| 1301 | return; |
| 1302 | } |
| 1303 | break; |
| 1304 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1305 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1306 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1307 | return; |
| 1308 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1309 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1312 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1313 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1314 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1315 | return; |
| 1316 | } |
| 1317 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1318 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1319 | |
| 1320 | // Normalize the argument, __foo__ becomes foo. |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1321 | StringRef ModuleName = Module->getName(); |
| 1322 | if (ModuleName.startswith("__") && ModuleName.endswith("__") && |
| 1323 | ModuleName.size() > 4) { |
| 1324 | ModuleName = ModuleName.drop_front(2).drop_back(2); |
| 1325 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1326 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1327 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1328 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1329 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1330 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1331 | uint64_t Idx; |
| 1332 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1333 | AL.getLoc(), i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1334 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1335 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1336 | // Is the function argument a pointer type? |
| 1337 | QualType T = getFunctionOrMethodArgType(D, Idx); |
| 1338 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1339 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1340 | case OwnershipAttr::Takes: |
| 1341 | case OwnershipAttr::Holds: |
| 1342 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1343 | Err = 0; |
| 1344 | break; |
| 1345 | case OwnershipAttr::Returns: |
| 1346 | if (!T->isIntegerType()) |
| 1347 | Err = 1; |
| 1348 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1349 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1350 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1351 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1352 | << Ex->getSourceRange(); |
| 1353 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1354 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1357 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1358 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1359 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1360 | // FIXME: A returns attribute should conflict with any returns attribute |
| 1361 | // with a different index too. |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1362 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1363 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1364 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1365 | << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind()); |
| 1366 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1367 | } |
| 1368 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1369 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | unsigned* start = OwnershipArgs.data(); |
| 1373 | unsigned size = OwnershipArgs.size(); |
| 1374 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1375 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1376 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1377 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1378 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1381 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1382 | // Check the attribute arguments. |
| 1383 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1384 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1385 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1386 | return; |
| 1387 | } |
| 1388 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1389 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1390 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1391 | // gcc rejects |
| 1392 | // class c { |
| 1393 | // static int a __attribute__((weakref ("v2"))); |
| 1394 | // static int b() __attribute__((weakref ("f3"))); |
| 1395 | // }; |
| 1396 | // and ignores the attributes of |
| 1397 | // void f(void) { |
| 1398 | // static int a __attribute__((weakref ("v2"))); |
| 1399 | // } |
| 1400 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1401 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1402 | if (!Ctx->isFileContext()) { |
| 1403 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1404 | nd->getNameAsString(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1405 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | // The GCC manual says |
| 1409 | // |
| 1410 | // At present, a declaration to which `weakref' is attached can only |
| 1411 | // be `static'. |
| 1412 | // |
| 1413 | // It also says |
| 1414 | // |
| 1415 | // Without a TARGET, |
| 1416 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1417 | // equivalent to `weak'. |
| 1418 | // |
| 1419 | // gcc 4.4.1 will accept |
| 1420 | // int a7 __attribute__((weakref)); |
| 1421 | // as |
| 1422 | // int a7 __attribute__((weak)); |
| 1423 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1424 | // it if this behaviour is actually used. |
| 1425 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1426 | // GCC rejects |
| 1427 | // static ((alias ("y"), weakref)). |
| 1428 | // Should we? How to check that weakref is before or after alias? |
| 1429 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1430 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1431 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1432 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1433 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1434 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1435 | // GCC will accept anything as the argument of weakref. Should we |
| 1436 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1437 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1438 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1439 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1440 | D->addAttr(::new (S.Context) |
| 1441 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1442 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1443 | } |
| 1444 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1445 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1446 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1447 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1448 | return; |
| 1449 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1450 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1451 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1452 | return; |
| 1453 | } |
| 1454 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1455 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1456 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1457 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1458 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1461 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1462 | if (D->hasAttr<HotAttr>()) { |
| 1463 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1464 | << Attr.getName() << "hot"; |
| 1465 | return; |
| 1466 | } |
| 1467 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1468 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1469 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1473 | if (D->hasAttr<ColdAttr>()) { |
| 1474 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1475 | << Attr.getName() << "cold"; |
| 1476 | return; |
| 1477 | } |
| 1478 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1479 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1480 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1483 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1484 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1485 | StringRef Model; |
| 1486 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1487 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1488 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1489 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1490 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1491 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1492 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1493 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1494 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1495 | return; |
| 1496 | } |
| 1497 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1498 | D->addAttr(::new (S.Context) |
| 1499 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1500 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1503 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1504 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1505 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1506 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1507 | D->addAttr(::new (S.Context) |
| 1508 | MallocAttr(Attr.getRange(), S.Context, |
| 1509 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1510 | return; |
| 1511 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1514 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1517 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1518 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1519 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1520 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1521 | return; |
| 1522 | } |
| 1523 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1524 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context, |
| 1525 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1528 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1529 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1530 | |
| 1531 | if (S.CheckNoReturnAttr(attr)) return; |
| 1532 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1533 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1534 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1535 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1536 | return; |
| 1537 | } |
| 1538 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1539 | D->addAttr(::new (S.Context) |
| 1540 | NoReturnAttr(attr.getRange(), S.Context, |
| 1541 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1545 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1546 | attr.setInvalid(); |
| 1547 | return true; |
| 1548 | } |
| 1549 | |
| 1550 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1553 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1554 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1555 | |
| 1556 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1557 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1558 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1559 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1560 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1561 | && !VD->getType()->isFunctionPointerType())) { |
| 1562 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1563 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1564 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1565 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1566 | return; |
| 1567 | } |
| 1568 | } |
| 1569 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1570 | D->addAttr(::new (S.Context) |
| 1571 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1572 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1575 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1576 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1577 | /* |
| 1578 | Returning a Vector Class in Registers |
| 1579 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1580 | According to the PPU ABI specifications, a class with a single member of |
| 1581 | vector type is returned in memory when used as the return value of a function. |
| 1582 | This results in inefficient code when implementing vector classes. To return |
| 1583 | the value in a single vector register, add the vecreturn attribute to the |
| 1584 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1585 | |
| 1586 | Example: |
| 1587 | |
| 1588 | struct Vector |
| 1589 | { |
| 1590 | __vector float xyzw; |
| 1591 | } __attribute__((vecreturn)); |
| 1592 | |
| 1593 | Vector Add(Vector lhs, Vector rhs) |
| 1594 | { |
| 1595 | Vector result; |
| 1596 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1597 | return result; // This will be returned in a register |
| 1598 | } |
| 1599 | */ |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1600 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1601 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1602 | return; |
| 1603 | } |
| 1604 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1605 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1606 | int count = 0; |
| 1607 | |
| 1608 | if (!isa<CXXRecordDecl>(record)) { |
| 1609 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1610 | return; |
| 1611 | } |
| 1612 | |
| 1613 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1614 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1615 | return; |
| 1616 | } |
| 1617 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1618 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1619 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1620 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1621 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1622 | return; |
| 1623 | } |
| 1624 | count++; |
| 1625 | } |
| 1626 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1627 | D->addAttr(::new (S.Context) |
| 1628 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1629 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1632 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1633 | const AttributeList &Attr) { |
| 1634 | if (isa<ParmVarDecl>(D)) { |
| 1635 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1636 | // parameter of a function declaration or lambda. |
| 1637 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1638 | S.Diag(Attr.getLoc(), |
| 1639 | diag::err_carries_dependency_param_not_function_decl); |
| 1640 | return; |
| 1641 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1642 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1643 | |
| 1644 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1645 | Attr.getRange(), S.Context, |
| 1646 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1649 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1650 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 429c134 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1651 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1652 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1653 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1654 | return; |
| 1655 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1656 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1657 | D->addAttr(::new (S.Context) |
| 1658 | UnusedAttr(Attr.getRange(), S.Context, |
| 1659 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1662 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1663 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1664 | if (VD->hasLocalStorage()) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1665 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1666 | return; |
| 1667 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1668 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1669 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1670 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1671 | return; |
| 1672 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1673 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1674 | D->addAttr(::new (S.Context) |
| 1675 | UsedAttr(Attr.getRange(), S.Context, |
| 1676 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1679 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1680 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1681 | if (Attr.getNumArgs() > 1) { |
| 1682 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1683 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1684 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1685 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1686 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1687 | if (Attr.getNumArgs() > 0 && |
| 1688 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1689 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1690 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1691 | D->addAttr(::new (S.Context) |
| 1692 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1693 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1696 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1697 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1698 | if (Attr.getNumArgs() > 1) { |
| 1699 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1700 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1701 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1702 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1703 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1704 | if (Attr.getNumArgs() > 0 && |
| 1705 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1706 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1707 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1708 | D->addAttr(::new (S.Context) |
| 1709 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1710 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1713 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1714 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1715 | const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1716 | unsigned NumArgs = Attr.getNumArgs(); |
| 1717 | if (NumArgs > 1) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1718 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1719 | return; |
| 1720 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1721 | |
| 1722 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1723 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1724 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1725 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1726 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1727 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1728 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1731 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
| 1732 | const AttributeList &Attr) { |
Ted Kremenek | 7559b47 | 2013-11-23 22:29:11 +0000 | [diff] [blame] | 1733 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1734 | |
| 1735 | if (!Parm) { |
| 1736 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1; |
| 1737 | return; |
| 1738 | } |
| 1739 | |
| 1740 | D->addAttr(::new (S.Context) |
| 1741 | ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 1742 | Attr.getAttributeSpellingListIndex())); |
| 1743 | } |
| 1744 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1745 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1746 | IdentifierInfo *Platform, |
| 1747 | VersionTuple Introduced, |
| 1748 | VersionTuple Deprecated, |
| 1749 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1750 | StringRef PlatformName |
| 1751 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1752 | if (PlatformName.empty()) |
| 1753 | PlatformName = Platform->getName(); |
| 1754 | |
| 1755 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1756 | // of these steps are needed). |
| 1757 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1758 | !(Introduced <= Deprecated)) { |
| 1759 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1760 | << 1 << PlatformName << Deprecated.getAsString() |
| 1761 | << 0 << Introduced.getAsString(); |
| 1762 | return true; |
| 1763 | } |
| 1764 | |
| 1765 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1766 | !(Introduced <= Obsoleted)) { |
| 1767 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1768 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1769 | << 0 << Introduced.getAsString(); |
| 1770 | return true; |
| 1771 | } |
| 1772 | |
| 1773 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1774 | !(Deprecated <= Obsoleted)) { |
| 1775 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1776 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1777 | << 1 << Deprecated.getAsString(); |
| 1778 | return true; |
| 1779 | } |
| 1780 | |
| 1781 | return false; |
| 1782 | } |
| 1783 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1784 | /// \brief Check whether the two versions match. |
| 1785 | /// |
| 1786 | /// If either version tuple is empty, then they are assumed to match. If |
| 1787 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1788 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1789 | bool BeforeIsOkay) { |
| 1790 | if (X.empty() || Y.empty()) |
| 1791 | return true; |
| 1792 | |
| 1793 | if (X == Y) |
| 1794 | return true; |
| 1795 | |
| 1796 | if (BeforeIsOkay && X < Y) |
| 1797 | return true; |
| 1798 | |
| 1799 | return false; |
| 1800 | } |
| 1801 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1802 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1803 | IdentifierInfo *Platform, |
| 1804 | VersionTuple Introduced, |
| 1805 | VersionTuple Deprecated, |
| 1806 | VersionTuple Obsoleted, |
| 1807 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1808 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1809 | bool Override, |
| 1810 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1811 | VersionTuple MergedIntroduced = Introduced; |
| 1812 | VersionTuple MergedDeprecated = Deprecated; |
| 1813 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1814 | bool FoundAny = false; |
| 1815 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1816 | if (D->hasAttrs()) { |
| 1817 | AttrVec &Attrs = D->getAttrs(); |
| 1818 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1819 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1820 | if (!OldAA) { |
| 1821 | ++i; |
| 1822 | continue; |
| 1823 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1824 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1825 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1826 | if (OldPlatform != Platform) { |
| 1827 | ++i; |
| 1828 | continue; |
| 1829 | } |
| 1830 | |
| 1831 | FoundAny = true; |
| 1832 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1833 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1834 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1835 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1837 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 1838 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 1839 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 1840 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 1841 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1842 | if (Override) { |
| 1843 | int Which = -1; |
| 1844 | VersionTuple FirstVersion; |
| 1845 | VersionTuple SecondVersion; |
| 1846 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 1847 | Which = 0; |
| 1848 | FirstVersion = OldIntroduced; |
| 1849 | SecondVersion = Introduced; |
| 1850 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 1851 | Which = 1; |
| 1852 | FirstVersion = Deprecated; |
| 1853 | SecondVersion = OldDeprecated; |
| 1854 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 1855 | Which = 2; |
| 1856 | FirstVersion = Obsoleted; |
| 1857 | SecondVersion = OldObsoleted; |
| 1858 | } |
| 1859 | |
| 1860 | if (Which == -1) { |
| 1861 | Diag(OldAA->getLocation(), |
| 1862 | diag::warn_mismatched_availability_override_unavail) |
| 1863 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1864 | } else { |
| 1865 | Diag(OldAA->getLocation(), |
| 1866 | diag::warn_mismatched_availability_override) |
| 1867 | << Which |
| 1868 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1869 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 1870 | } |
| 1871 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1872 | } else { |
| 1873 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1874 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1875 | } |
| 1876 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1877 | Attrs.erase(Attrs.begin() + i); |
| 1878 | --e; |
| 1879 | continue; |
| 1880 | } |
| 1881 | |
| 1882 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 1883 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 1884 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 1885 | |
| 1886 | if (MergedIntroduced2.empty()) |
| 1887 | MergedIntroduced2 = OldIntroduced; |
| 1888 | if (MergedDeprecated2.empty()) |
| 1889 | MergedDeprecated2 = OldDeprecated; |
| 1890 | if (MergedObsoleted2.empty()) |
| 1891 | MergedObsoleted2 = OldObsoleted; |
| 1892 | |
| 1893 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 1894 | MergedIntroduced2, MergedDeprecated2, |
| 1895 | MergedObsoleted2)) { |
| 1896 | Attrs.erase(Attrs.begin() + i); |
| 1897 | --e; |
| 1898 | continue; |
| 1899 | } |
| 1900 | |
| 1901 | MergedIntroduced = MergedIntroduced2; |
| 1902 | MergedDeprecated = MergedDeprecated2; |
| 1903 | MergedObsoleted = MergedObsoleted2; |
| 1904 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1905 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | if (FoundAny && |
| 1909 | MergedIntroduced == Introduced && |
| 1910 | MergedDeprecated == Deprecated && |
| 1911 | MergedObsoleted == Obsoleted) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1912 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1913 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1914 | // Only create a new attribute if !Override, but we want to do |
| 1915 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1916 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1917 | MergedDeprecated, MergedObsoleted) && |
| 1918 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1919 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 1920 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1921 | Obsoleted, IsUnavailable, Message, |
| 1922 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1923 | } |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1924 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1927 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1928 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1929 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1930 | return; |
| 1931 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1932 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 1933 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1934 | IdentifierInfo *II = Platform->Ident; |
| 1935 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 1936 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 1937 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1938 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 1939 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1940 | if (!ND) { |
| 1941 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1942 | return; |
| 1943 | } |
| 1944 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1945 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1946 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1947 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1948 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1949 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 1950 | if (const StringLiteral *SE = |
| 1951 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1952 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1953 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1954 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1955 | Introduced.Version, |
| 1956 | Deprecated.Version, |
| 1957 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1958 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1959 | /*Override=*/false, |
| 1960 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1961 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1962 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1965 | template <class T> |
| 1966 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 1967 | typename T::VisibilityType value, |
| 1968 | unsigned attrSpellingListIndex) { |
| 1969 | T *existingAttr = D->getAttr<T>(); |
| 1970 | if (existingAttr) { |
| 1971 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 1972 | if (existingValue == value) |
| 1973 | return NULL; |
| 1974 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 1975 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 1976 | D->dropAttr<T>(); |
| 1977 | } |
| 1978 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 1979 | } |
| 1980 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1981 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1982 | VisibilityAttr::VisibilityType Vis, |
| 1983 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1984 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 1985 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1986 | } |
| 1987 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1988 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 1989 | TypeVisibilityAttr::VisibilityType Vis, |
| 1990 | unsigned AttrSpellingListIndex) { |
| 1991 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 1992 | AttrSpellingListIndex); |
| 1993 | } |
| 1994 | |
| 1995 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 1996 | bool isTypeVisibility) { |
| 1997 | // Visibility attributes don't mean anything on a typedef. |
| 1998 | if (isa<TypedefNameDecl>(D)) { |
| 1999 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2000 | << Attr.getName(); |
| 2001 | return; |
| 2002 | } |
| 2003 | |
| 2004 | // 'type_visibility' can only go on a type or namespace. |
| 2005 | if (isTypeVisibility && |
| 2006 | !(isa<TagDecl>(D) || |
| 2007 | isa<ObjCInterfaceDecl>(D) || |
| 2008 | isa<NamespaceDecl>(D))) { |
| 2009 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2010 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2011 | return; |
| 2012 | } |
| 2013 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2014 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2015 | StringRef TypeStr; |
| 2016 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2017 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2018 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2019 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2020 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2021 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2022 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2023 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2024 | return; |
| 2025 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2026 | |
| 2027 | // Complain about attempts to use protected visibility on targets |
| 2028 | // (like Darwin) that don't support it. |
| 2029 | if (type == VisibilityAttr::Protected && |
| 2030 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2031 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2032 | type = VisibilityAttr::Default; |
| 2033 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2034 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2035 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2036 | clang::Attr *newAttr; |
| 2037 | if (isTypeVisibility) { |
| 2038 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2039 | (TypeVisibilityAttr::VisibilityType) type, |
| 2040 | Index); |
| 2041 | } else { |
| 2042 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2043 | } |
| 2044 | if (newAttr) |
| 2045 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2046 | } |
| 2047 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2048 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2049 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2050 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2051 | if (!Attr.isArgIdent(0)) { |
| 2052 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2053 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2054 | return; |
| 2055 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2056 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2057 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2058 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2059 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2060 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2061 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2062 | return; |
| 2063 | } |
| 2064 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2065 | if (F == ObjCMethodFamilyAttr::OMF_init && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2066 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2067 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2068 | << method->getResultType(); |
| 2069 | // Ignore the attribute. |
| 2070 | return; |
| 2071 | } |
| 2072 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2073 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2074 | S.Context, F)); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2077 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2078 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2079 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2080 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2081 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2082 | return; |
| 2083 | } |
| 2084 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2085 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2086 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2087 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2088 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2089 | return; |
| 2090 | } |
| 2091 | } |
| 2092 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2093 | // It is okay to include this attribute on properties, e.g.: |
| 2094 | // |
| 2095 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2096 | // |
| 2097 | // In this case it follows tradition and suppresses an error in the above |
| 2098 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2099 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2100 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2101 | D->addAttr(::new (S.Context) |
| 2102 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2103 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2106 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2107 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2108 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2109 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2110 | return; |
| 2111 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2112 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2113 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2114 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2115 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2116 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2117 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2118 | return; |
| 2119 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2120 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2121 | D->addAttr(::new (S.Context) |
| 2122 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2123 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2126 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2127 | // check the attribute arguments. |
| 2128 | if (Attr.getNumArgs() > 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2129 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2130 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2133 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2134 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2135 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2136 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2137 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2138 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2139 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2140 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2141 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2142 | return; |
| 2143 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2144 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2145 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2146 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2147 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2148 | return; |
| 2149 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2150 | |
| 2151 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2154 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2155 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2156 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2157 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2158 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2159 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2160 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2161 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2162 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2163 | return; |
| 2164 | } |
| 2165 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2166 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2167 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2168 | // FIXME: This error message could be improved, it would be nice |
| 2169 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2170 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2171 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2172 | return; |
| 2173 | } |
| 2174 | } |
| 2175 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2176 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2177 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2178 | if (isa<FunctionNoProtoType>(FT)) { |
| 2179 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2180 | return; |
| 2181 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2182 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2183 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2184 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2185 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2186 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2187 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2188 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2189 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2190 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2191 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2192 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2193 | if (!BD->isVariadic()) { |
| 2194 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2195 | return; |
| 2196 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2197 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2198 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2199 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2200 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2201 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2202 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2203 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2204 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2205 | return; |
| 2206 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2207 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2208 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2209 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2210 | return; |
| 2211 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2212 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2213 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2214 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2215 | return; |
| 2216 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2217 | D->addAttr(::new (S.Context) |
| 2218 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2219 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2222 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2223 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2224 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | 3d699e0 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2225 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2226 | return; |
| 2227 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2228 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2229 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2230 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2231 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2232 | return; |
| 2233 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2234 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2235 | if (MD->getResultType()->isVoidType()) { |
| 2236 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2237 | << Attr.getName() << 1; |
| 2238 | return; |
| 2239 | } |
| 2240 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2241 | D->addAttr(::new (S.Context) |
| 2242 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2243 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2244 | } |
| 2245 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2246 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2247 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 47f9a73 | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2248 | if (isa<CXXRecordDecl>(D)) { |
| 2249 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2250 | return; |
| 2251 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2252 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2253 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | 41136ee | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2254 | return; |
| 2255 | } |
| 2256 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2257 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2258 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2259 | nd->addAttr(::new (S.Context) |
| 2260 | WeakAttr(Attr.getRange(), S.Context, |
| 2261 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2264 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2265 | // weak_import only applies to variable & function declarations. |
| 2266 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2267 | if (!D->canBeWeakImported(isDef)) { |
| 2268 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2269 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2270 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2271 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2272 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2273 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2274 | // Nothing to warn about here. |
| 2275 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2276 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2277 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2278 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2279 | return; |
| 2280 | } |
| 2281 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2282 | D->addAttr(::new (S.Context) |
| 2283 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2284 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2287 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2288 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2289 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2290 | uint32_t WGSize[3]; |
| 2291 | for (unsigned i = 0; i < 3; ++i) |
| 2292 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2293 | return; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2294 | |
| 2295 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2296 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2297 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2298 | if (!(A->getXDim() == WGSize[0] && |
| 2299 | A->getYDim() == WGSize[1] && |
| 2300 | A->getZDim() == WGSize[2])) { |
| 2301 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2302 | Attr.getName(); |
| 2303 | } |
| 2304 | } |
| 2305 | |
| 2306 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2307 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2308 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2309 | if (!(A->getXDim() == WGSize[0] && |
| 2310 | A->getYDim() == WGSize[1] && |
| 2311 | A->getZDim() == WGSize[2])) { |
| 2312 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2313 | Attr.getName(); |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2318 | D->addAttr(::new (S.Context) |
| 2319 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2320 | WGSize[0], WGSize[1], WGSize[2], |
| 2321 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2322 | else |
| 2323 | D->addAttr(::new (S.Context) |
| 2324 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2325 | WGSize[0], WGSize[1], WGSize[2], |
| 2326 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2327 | } |
| 2328 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2329 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2330 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2331 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2332 | if (!Attr.hasParsedType()) { |
| 2333 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2334 | << Attr.getName() << 1; |
| 2335 | return; |
| 2336 | } |
| 2337 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2338 | TypeSourceInfo *ParmTSI = 0; |
| 2339 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2340 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2341 | |
| 2342 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2343 | (ParmType->isBooleanType() || |
| 2344 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2345 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2346 | << ParmType; |
| 2347 | return; |
| 2348 | } |
| 2349 | |
| 2350 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2351 | D->hasAttr<VecTypeHintAttr>()) { |
| 2352 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2353 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2354 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2355 | return; |
| 2356 | } |
| 2357 | } |
| 2358 | |
| 2359 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2360 | ParmTSI)); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2361 | } |
| 2362 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2363 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2364 | StringRef Name, |
| 2365 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2366 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2367 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2368 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2369 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2370 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2371 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2372 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2373 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2374 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2375 | } |
| 2376 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2377 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2378 | // Make sure that there is a string literal as the sections's single |
| 2379 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2380 | StringRef Str; |
| 2381 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2382 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2383 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2384 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2385 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2386 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2387 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2388 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2389 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2390 | return; |
| 2391 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2392 | |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2393 | // This attribute cannot be applied to local variables. |
| 2394 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2395 | S.Diag(LiteralLoc, diag::err_attribute_section_local_variable); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2396 | return; |
| 2397 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2398 | |
| 2399 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2400 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2401 | if (NewAttr) |
| 2402 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2403 | } |
| 2404 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2405 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2406 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2407 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2408 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2409 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2410 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2411 | D->addAttr(::new (S.Context) |
| 2412 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2413 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2414 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2415 | } |
| 2416 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2417 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2418 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2419 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2420 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2421 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2422 | D->addAttr(::new (S.Context) |
| 2423 | ConstAttr(Attr.getRange(), S.Context, |
| 2424 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2425 | } |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2428 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2429 | VarDecl *VD = cast<VarDecl>(D); |
| 2430 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2431 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2432 | return; |
| 2433 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2434 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2435 | Expr *E = Attr.getArgAsExpr(0); |
| 2436 | SourceLocation Loc = E->getExprLoc(); |
| 2437 | FunctionDecl *FD = 0; |
| 2438 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2439 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2440 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2441 | // will warn the user. |
| 2442 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2443 | if (DRE->hasQualifier()) |
| 2444 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2445 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2446 | NI = DRE->getNameInfo(); |
| 2447 | if (!FD) { |
| 2448 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2449 | << NI.getName(); |
| 2450 | return; |
| 2451 | } |
| 2452 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2453 | if (ULE->hasExplicitTemplateArgs()) |
| 2454 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2455 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2456 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2457 | if (!FD) { |
| 2458 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2459 | << NI.getName(); |
| 2460 | if (ULE->getType() == S.Context.OverloadTy) |
| 2461 | S.NoteAllOverloadCandidates(ULE); |
| 2462 | return; |
| 2463 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2464 | } else { |
| 2465 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2466 | return; |
| 2467 | } |
| 2468 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2469 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2470 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2471 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2472 | return; |
| 2473 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2474 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2475 | // We're currently more strict than GCC about what function types we accept. |
| 2476 | // If this ever proves to be a problem it should be easy to fix. |
| 2477 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2478 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2479 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2480 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2481 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2482 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2483 | return; |
| 2484 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2485 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2486 | D->addAttr(::new (S.Context) |
| 2487 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2488 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2491 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2492 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2493 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2494 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2495 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2496 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2497 | return; |
| 2498 | } |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2499 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2500 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2501 | uint64_t ArgIdx; |
| 2502 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 2503 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2504 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2505 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2506 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2507 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2508 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2509 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2510 | if (not_nsstring_type && |
| 2511 | !isCFStringType(Ty, S.Context) && |
| 2512 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2513 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2514 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2515 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2516 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2517 | << IdxExpr->getSourceRange(); |
| 2518 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2519 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2520 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2521 | if (!isNSStringType(Ty, S.Context) && |
| 2522 | !isCFStringType(Ty, S.Context) && |
| 2523 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2524 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2525 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2526 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2527 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2528 | << IdxExpr->getSourceRange(); |
| 2529 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2530 | } |
| 2531 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2532 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2533 | // because that has corrected for the implicit this parameter, and is zero- |
| 2534 | // based. The attribute expects what the user wrote explicitly. |
| 2535 | llvm::APSInt Val; |
| 2536 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2537 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2538 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2539 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2540 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2543 | enum FormatAttrKind { |
| 2544 | CFStringFormat, |
| 2545 | NSStringFormat, |
| 2546 | StrftimeFormat, |
| 2547 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2548 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2549 | InvalidFormat |
| 2550 | }; |
| 2551 | |
| 2552 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2553 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2554 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2555 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2556 | // Check for formats that get handled specially. |
| 2557 | .Case("NSString", NSStringFormat) |
| 2558 | .Case("CFString", CFStringFormat) |
| 2559 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2560 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2561 | // Otherwise, check for supported formats. |
| 2562 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2563 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2564 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2565 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2566 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2567 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2568 | } |
| 2569 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2570 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2571 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2572 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2573 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2574 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2575 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2576 | return; |
| 2577 | } |
| 2578 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2579 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2580 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2581 | Attr.setInvalid(); |
| 2582 | return; |
| 2583 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2584 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2585 | if (S.Context.getAsArrayType(T)) |
| 2586 | T = S.Context.getBaseElementType(T); |
| 2587 | if (!T->getAs<RecordType>()) { |
| 2588 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2589 | Attr.setInvalid(); |
| 2590 | return; |
| 2591 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2592 | |
| 2593 | Expr *E = Attr.getArgAsExpr(0); |
| 2594 | uint32_t prioritynum; |
| 2595 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2596 | Attr.setInvalid(); |
| 2597 | return; |
| 2598 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2599 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2600 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2601 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2602 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2603 | Attr.setInvalid(); |
| 2604 | return; |
| 2605 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2606 | D->addAttr(::new (S.Context) |
| 2607 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2608 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2611 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2612 | IdentifierInfo *Format, int FormatIdx, |
| 2613 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2614 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2615 | // Check whether we already have an equivalent format attribute. |
| 2616 | for (specific_attr_iterator<FormatAttr> |
| 2617 | i = D->specific_attr_begin<FormatAttr>(), |
| 2618 | e = D->specific_attr_end<FormatAttr>(); |
| 2619 | i != e ; ++i) { |
| 2620 | FormatAttr *f = *i; |
| 2621 | if (f->getType() == Format && |
| 2622 | f->getFormatIdx() == FormatIdx && |
| 2623 | f->getFirstArg() == FirstArg) { |
| 2624 | // If we don't have a valid location for this attribute, adopt the |
| 2625 | // location. |
| 2626 | if (f->getLocation().isInvalid()) |
| 2627 | f->setRange(Range); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2628 | return NULL; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2629 | } |
| 2630 | } |
| 2631 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2632 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2633 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2636 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2637 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2638 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2639 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2640 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2641 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2642 | return; |
| 2643 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2644 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2645 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2646 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2647 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2648 | return; |
| 2649 | } |
| 2650 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2651 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2652 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2653 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2654 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2655 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2656 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2657 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2658 | |
| 2659 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2660 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2661 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2662 | // If we've modified the string name, we need a new identifier for it. |
| 2663 | II = &S.Context.Idents.get(Format); |
| 2664 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2665 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2666 | // Check for supported formats. |
| 2667 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2668 | |
| 2669 | if (Kind == IgnoredFormat) |
| 2670 | return; |
| 2671 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2672 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2673 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2674 | << "format" << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2675 | return; |
| 2676 | } |
| 2677 | |
| 2678 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2679 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2680 | uint32_t Idx; |
| 2681 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2682 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2683 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2684 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2685 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2686 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2687 | return; |
| 2688 | } |
| 2689 | |
| 2690 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2691 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2692 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2693 | if (HasImplicitThisParam) { |
| 2694 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2695 | S.Diag(Attr.getLoc(), |
| 2696 | diag::err_format_attribute_implicit_this_format_string) |
| 2697 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2698 | return; |
| 2699 | } |
| 2700 | ArgIdx--; |
| 2701 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2702 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2703 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2704 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2705 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2706 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2707 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2708 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2709 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2710 | return; |
| 2711 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2712 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2713 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2714 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2715 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2716 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2717 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2718 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2719 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2720 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2721 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2722 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2723 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2724 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2725 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2726 | return; |
| 2727 | } |
| 2728 | |
| 2729 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2730 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2731 | uint32_t FirstArg; |
| 2732 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2733 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2734 | |
| 2735 | // check if the function is variadic if the 3rd argument non-zero |
| 2736 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2737 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2738 | ++NumArgs; // +1 for ... |
| 2739 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2740 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2741 | return; |
| 2742 | } |
| 2743 | } |
| 2744 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2745 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2746 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2747 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2748 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2749 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2750 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2751 | return; |
| 2752 | } |
| 2753 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2754 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2755 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2756 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2757 | return; |
| 2758 | } |
| 2759 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2760 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2761 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2762 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2763 | if (NewAttr) |
| 2764 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2767 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2768 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2769 | // Try to find the underlying union declaration. |
| 2770 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2771 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2772 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2773 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2774 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2775 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2776 | |
| 2777 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2778 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2779 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2780 | return; |
| 2781 | } |
| 2782 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2783 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2784 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2785 | diag::warn_transparent_union_attribute_not_definition); |
| 2786 | return; |
| 2787 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2788 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2789 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2790 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2791 | if (Field == FieldEnd) { |
| 2792 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2793 | return; |
| 2794 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2795 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2796 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2797 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2798 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2799 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2800 | diag::warn_transparent_union_attribute_floating) |
| 2801 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2802 | return; |
| 2803 | } |
| 2804 | |
| 2805 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2806 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2807 | for (; Field != FieldEnd; ++Field) { |
| 2808 | QualType FieldType = Field->getType(); |
| 2809 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 2810 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 2811 | // Warn if we drop the attribute. |
| 2812 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2813 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2814 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2815 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2816 | diag::warn_transparent_union_attribute_field_size_align) |
| 2817 | << isSize << Field->getDeclName() << FieldBits; |
| 2818 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2819 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2820 | diag::note_transparent_union_first_field_size_align) |
| 2821 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2822 | return; |
| 2823 | } |
| 2824 | } |
| 2825 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2826 | RD->addAttr(::new (S.Context) |
| 2827 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2828 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2829 | } |
| 2830 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2831 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2832 | // Make sure that there is a string literal as the annotation's single |
| 2833 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2834 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2835 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2836 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2837 | |
| 2838 | // Don't duplicate annotations that are already set. |
| 2839 | for (specific_attr_iterator<AnnotateAttr> |
| 2840 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 2841 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2842 | if ((*i)->getAnnotation() == Str) |
| 2843 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2844 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2845 | |
| 2846 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2847 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2848 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2849 | } |
| 2850 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2851 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2852 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2853 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2854 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2855 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2856 | return; |
| 2857 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2858 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2859 | if (Attr.getNumArgs() == 0) { |
| 2860 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 2861 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 2862 | return; |
| 2863 | } |
| 2864 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2865 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2866 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 2867 | S.Diag(Attr.getEllipsisLoc(), |
| 2868 | diag::err_pack_expansion_without_parameter_packs); |
| 2869 | return; |
| 2870 | } |
| 2871 | |
| 2872 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 2873 | return; |
| 2874 | |
| 2875 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 2876 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2880 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2881 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 2882 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 2883 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2884 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2885 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2886 | // C++11 [dcl.align]p1: |
| 2887 | // An alignment-specifier may be applied to a variable or to a class |
| 2888 | // data member, but it shall not be applied to a bit-field, a function |
| 2889 | // parameter, the formal parameter of a catch clause, or a variable |
| 2890 | // declared with the register storage class specifier. An |
| 2891 | // alignment-specifier may also be applied to the declaration of a class |
| 2892 | // or enumeration type. |
| 2893 | // C11 6.7.5/2: |
| 2894 | // An alignment attribute shall not be specified in a declaration of |
| 2895 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 2896 | // object declared with the register storage-class specifier. |
| 2897 | int DiagKind = -1; |
| 2898 | if (isa<ParmVarDecl>(D)) { |
| 2899 | DiagKind = 0; |
| 2900 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2901 | if (VD->getStorageClass() == SC_Register) |
| 2902 | DiagKind = 1; |
| 2903 | if (VD->isExceptionVariable()) |
| 2904 | DiagKind = 2; |
| 2905 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 2906 | if (FD->isBitField()) |
| 2907 | DiagKind = 3; |
| 2908 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2909 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 2910 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 2911 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 2912 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2913 | return; |
| 2914 | } |
| 2915 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2916 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 2917 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2918 | return; |
| 2919 | } |
| 2920 | } |
| 2921 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2922 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2923 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2924 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 2925 | AA->setPackExpansion(IsPackExpansion); |
| 2926 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2927 | return; |
| 2928 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2929 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2930 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2931 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 2932 | ExprResult ICE |
| 2933 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2934 | diag::err_aligned_attribute_argument_not_int, |
| 2935 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2936 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2937 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2938 | |
| 2939 | // C++11 [dcl.align]p2: |
| 2940 | // -- if the constant expression evaluates to zero, the alignment |
| 2941 | // specifier shall have no effect |
| 2942 | // C11 6.7.5p6: |
| 2943 | // An alignment specification of zero has no effect. |
| 2944 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 2945 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2946 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2947 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2948 | return; |
| 2949 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2950 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2951 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2952 | // We've already verified it's a power of 2, now let's make sure it's |
| 2953 | // 8192 or less. |
| 2954 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2955 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2956 | << E->getSourceRange(); |
| 2957 | return; |
| 2958 | } |
| 2959 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2960 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2961 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 2962 | ICE.take(), SpellingListIndex); |
| 2963 | AA->setPackExpansion(IsPackExpansion); |
| 2964 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2965 | } |
| 2966 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2967 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2968 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2969 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2970 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2971 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 2972 | SpellingListIndex); |
| 2973 | AA->setPackExpansion(IsPackExpansion); |
| 2974 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2975 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2976 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2977 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 2978 | assert(D->hasAttrs() && "no attributes on decl"); |
| 2979 | |
| 2980 | QualType Ty; |
| 2981 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2982 | Ty = VD->getType(); |
| 2983 | else |
| 2984 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 3653b7e | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 2985 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2986 | return; |
| 2987 | |
| 2988 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 2989 | // The combined effect of all alignment attributes in a declaration shall |
| 2990 | // not specify an alignment that is less strict than the alignment that |
| 2991 | // would otherwise be required for the entity being declared. |
| 2992 | AlignedAttr *AlignasAttr = 0; |
| 2993 | unsigned Align = 0; |
| 2994 | for (specific_attr_iterator<AlignedAttr> |
| 2995 | I = D->specific_attr_begin<AlignedAttr>(), |
| 2996 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 2997 | if (I->isAlignmentDependent()) |
| 2998 | return; |
| 2999 | if (I->isAlignas()) |
| 3000 | AlignasAttr = *I; |
| 3001 | Align = std::max(Align, I->getAlignment(Context)); |
| 3002 | } |
| 3003 | |
| 3004 | if (AlignasAttr && Align) { |
| 3005 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3006 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3007 | if (NaturalAlign > RequestedAlign) |
| 3008 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3009 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3010 | } |
| 3011 | } |
| 3012 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3013 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3014 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3015 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3016 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3017 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3018 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3019 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3020 | // This attribute isn't documented, but glibc uses it. It changes |
| 3021 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3022 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 3023 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3024 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3025 | return; |
| 3026 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3027 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3028 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 3029 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3030 | |
| 3031 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3032 | if (Str.startswith("__") && Str.endswith("__")) |
| 3033 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3034 | |
| 3035 | unsigned DestWidth = 0; |
| 3036 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3037 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3038 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3039 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3040 | switch (Str[0]) { |
| 3041 | case 'Q': DestWidth = 8; break; |
| 3042 | case 'H': DestWidth = 16; break; |
| 3043 | case 'S': DestWidth = 32; break; |
| 3044 | case 'D': DestWidth = 64; break; |
| 3045 | case 'X': DestWidth = 96; break; |
| 3046 | case 'T': DestWidth = 128; break; |
| 3047 | } |
| 3048 | if (Str[1] == 'F') { |
| 3049 | IntegerMode = false; |
| 3050 | } else if (Str[1] == 'C') { |
| 3051 | IntegerMode = false; |
| 3052 | ComplexMode = true; |
| 3053 | } else if (Str[1] != 'I') { |
| 3054 | DestWidth = 0; |
| 3055 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3056 | break; |
| 3057 | case 4: |
| 3058 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3059 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3060 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3061 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3062 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3063 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3064 | break; |
| 3065 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3066 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3067 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3068 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3069 | case 11: |
| 3070 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3071 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3072 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
| 3075 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3076 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3077 | OldTy = TD->getUnderlyingType(); |
| 3078 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3079 | OldTy = VD->getType(); |
| 3080 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3081 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3082 | << "mode" << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3083 | return; |
| 3084 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3085 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3086 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3087 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3088 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3089 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3090 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3091 | } else if (ComplexMode) { |
| 3092 | if (!OldTy->isComplexType()) |
| 3093 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3094 | } else { |
| 3095 | if (!OldTy->isFloatingType()) |
| 3096 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3097 | } |
| 3098 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3099 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3100 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3101 | // FIXME: Make sure floating-point mappings are accurate |
| 3102 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3103 | if (!DestWidth) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3104 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3105 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | QualType NewTy; |
| 3109 | |
| 3110 | if (IntegerMode) |
| 3111 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 3112 | OldTy->isSignedIntegerType()); |
| 3113 | else |
| 3114 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 3115 | |
| 3116 | if (NewTy.isNull()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3117 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3118 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3119 | } |
| 3120 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3121 | if (ComplexMode) { |
| 3122 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3126 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3127 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3128 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3129 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3130 | |
| 3131 | D->addAttr(::new (S.Context) |
| 3132 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3133 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3134 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3135 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3136 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3137 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3138 | if (!VD->hasGlobalStorage()) |
| 3139 | S.Diag(Attr.getLoc(), |
| 3140 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3141 | << Attr.getName(); |
| 3142 | } else if (!isFunctionOrMethod(D)) { |
| 3143 | S.Diag(Attr.getLoc(), |
| 3144 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3145 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3146 | return; |
| 3147 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3148 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3149 | D->addAttr(::new (S.Context) |
| 3150 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3151 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3154 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3155 | if (S.LangOpts.CUDA) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3156 | D->addAttr(::new (S.Context) |
| 3157 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3158 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3159 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3160 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3161 | } |
| 3162 | } |
| 3163 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3164 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3165 | if (S.LangOpts.CUDA) { |
| 3166 | // check the attribute arguments. |
| 3167 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3168 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3169 | << Attr.getName() << 0; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3170 | return; |
| 3171 | } |
| 3172 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3173 | D->addAttr(::new (S.Context) |
| 3174 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3175 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3176 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3177 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3178 | } |
| 3179 | } |
| 3180 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3181 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3182 | if (S.LangOpts.CUDA) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3183 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3184 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3185 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3186 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3187 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3188 | << FD->getType() |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3189 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3190 | "void"); |
| 3191 | } else { |
| 3192 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3193 | << FD->getType(); |
| 3194 | } |
| 3195 | return; |
| 3196 | } |
| 3197 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3198 | D->addAttr(::new (S.Context) |
| 3199 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3200 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3201 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3202 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3203 | } |
| 3204 | } |
| 3205 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3206 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3207 | if (S.LangOpts.CUDA) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3208 | D->addAttr(::new (S.Context) |
| 3209 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3210 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3211 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3212 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3213 | } |
| 3214 | } |
| 3215 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3216 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3217 | if (S.LangOpts.CUDA) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3218 | D->addAttr(::new (S.Context) |
| 3219 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3220 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3221 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3222 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3223 | } |
| 3224 | } |
| 3225 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3226 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3227 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3228 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3229 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3230 | return; |
| 3231 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3232 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3233 | D->addAttr(::new (S.Context) |
| 3234 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3235 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3238 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3239 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3240 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3241 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3242 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3243 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3244 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3245 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3246 | return; |
| 3247 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3248 | if (!isa<ObjCMethodDecl>(D)) { |
| 3249 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3250 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3251 | return; |
| 3252 | } |
| 3253 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3254 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3255 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3256 | D->addAttr(::new (S.Context) |
| 3257 | FastCallAttr(Attr.getRange(), S.Context, |
| 3258 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3259 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3260 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3261 | D->addAttr(::new (S.Context) |
| 3262 | StdCallAttr(Attr.getRange(), S.Context, |
| 3263 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3264 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3265 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3266 | D->addAttr(::new (S.Context) |
| 3267 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3268 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3269 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3270 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3271 | D->addAttr(::new (S.Context) |
| 3272 | CDeclAttr(Attr.getRange(), S.Context, |
| 3273 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3274 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3275 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3276 | D->addAttr(::new (S.Context) |
| 3277 | PascalAttr(Attr.getRange(), S.Context, |
| 3278 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3279 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3280 | case AttributeList::AT_MSABI: |
| 3281 | D->addAttr(::new (S.Context) |
| 3282 | MSABIAttr(Attr.getRange(), S.Context, |
| 3283 | Attr.getAttributeSpellingListIndex())); |
| 3284 | return; |
| 3285 | case AttributeList::AT_SysVABI: |
| 3286 | D->addAttr(::new (S.Context) |
| 3287 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3288 | Attr.getAttributeSpellingListIndex())); |
| 3289 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3290 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3291 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3292 | switch (CC) { |
| 3293 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3294 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3295 | break; |
| 3296 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3297 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3298 | break; |
| 3299 | default: |
| 3300 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3301 | } |
| 3302 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3303 | D->addAttr(::new (S.Context) |
| 3304 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3305 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3306 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3307 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3308 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3309 | D->addAttr(::new (S.Context) |
| 3310 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3311 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3312 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3313 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3314 | D->addAttr(::new (S.Context) |
| 3315 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3316 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3317 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3318 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3319 | default: |
| 3320 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3321 | } |
| 3322 | } |
| 3323 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3324 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, |
| 3325 | const AttributeList &Attr) { |
| 3326 | uint32_t ArgNum; |
| 3327 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum)) |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3328 | return; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3329 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3330 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(), |
| 3331 | S.Context, ArgNum)); |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3334 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3335 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3336 | if (attr.isInvalid()) |
| 3337 | return true; |
| 3338 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3339 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3340 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3341 | attr.setInvalid(); |
| 3342 | return true; |
| 3343 | } |
| 3344 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3345 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3346 | // move to TargetAttributesSema one day. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3347 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3348 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3349 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3350 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3351 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3352 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3353 | case AttributeList::AT_MSABI: |
| 3354 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3355 | CC_X86_64Win64; |
| 3356 | break; |
| 3357 | case AttributeList::AT_SysVABI: |
| 3358 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3359 | CC_C; |
| 3360 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3361 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3362 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3363 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3364 | attr.setInvalid(); |
| 3365 | return true; |
| 3366 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3367 | if (StrRef == "aapcs") { |
| 3368 | CC = CC_AAPCS; |
| 3369 | break; |
| 3370 | } else if (StrRef == "aapcs-vfp") { |
| 3371 | CC = CC_AAPCS_VFP; |
| 3372 | break; |
| 3373 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3374 | |
| 3375 | attr.setInvalid(); |
| 3376 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3377 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3378 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3379 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3380 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3381 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3382 | } |
| 3383 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3384 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3385 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3386 | if (A == TargetInfo::CCCR_Warning) { |
| 3387 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3388 | |
| 3389 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3390 | if (FD) |
| 3391 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3392 | TargetInfo::CCMT_NonMember; |
| 3393 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3394 | } |
| 3395 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3396 | return false; |
| 3397 | } |
| 3398 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3399 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3400 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3401 | |
| 3402 | unsigned numParams; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3403 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3404 | return; |
| 3405 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3406 | if (!isa<ObjCMethodDecl>(D)) { |
| 3407 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3408 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3409 | return; |
| 3410 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3411 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3412 | D->addAttr(::new (S.Context) |
| 3413 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 3414 | Attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3418 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3419 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3420 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3421 | return true; |
| 3422 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3423 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3424 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3425 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3426 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3427 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3428 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3429 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3430 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3431 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3432 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3435 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3436 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3437 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3438 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3439 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3442 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3443 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3444 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3445 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3446 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3447 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3448 | } |
| 3449 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3450 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3453 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3454 | if (S.LangOpts.CUDA) { |
| 3455 | // check the attribute arguments. |
| 3456 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 3457 | // FIXME: 0 is not okay. |
| 3458 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3459 | return; |
| 3460 | } |
| 3461 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3462 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3463 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3464 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3465 | return; |
| 3466 | } |
| 3467 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3468 | uint32_t MaxThreads, MinBlocks; |
| 3469 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) || |
| 3470 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2)) |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3471 | return; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3472 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3473 | D->addAttr(::new (S.Context) |
| 3474 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3475 | MaxThreads, MinBlocks, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3476 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3477 | } else { |
| 3478 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 3479 | } |
| 3480 | } |
| 3481 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3482 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3483 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3484 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3485 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3486 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3487 | return; |
| 3488 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3489 | |
| 3490 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3491 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3492 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3493 | StringRef AttrName = Attr.getName()->getName(); |
| 3494 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3495 | |
| 3496 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3497 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3498 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3499 | return; |
| 3500 | } |
| 3501 | |
| 3502 | uint64_t ArgumentIdx; |
| 3503 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3504 | Attr.getLoc(), 2, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3505 | Attr.getArgAsExpr(1), ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3506 | return; |
| 3507 | |
| 3508 | uint64_t TypeTagIdx; |
| 3509 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3510 | Attr.getLoc(), 3, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3511 | Attr.getArgAsExpr(2), TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3512 | return; |
| 3513 | |
| 3514 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 3515 | if (IsPointer) { |
| 3516 | // Ensure that buffer has a pointer type. |
| 3517 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 3518 | if (!BufferTy->isPointerType()) { |
| 3519 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3520 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3521 | } |
| 3522 | } |
| 3523 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3524 | D->addAttr(::new (S.Context) |
| 3525 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3526 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3527 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3531 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3532 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3533 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3534 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3535 | return; |
| 3536 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3537 | |
| 3538 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3539 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3540 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3541 | if (!isa<VarDecl>(D)) { |
| 3542 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3543 | << Attr.getName() << ExpectedVariable; |
| 3544 | return; |
| 3545 | } |
| 3546 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3547 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3548 | TypeSourceInfo *MatchingCTypeLoc = 0; |
| 3549 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3550 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3551 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3552 | D->addAttr(::new (S.Context) |
| 3553 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3554 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3555 | Attr.getLayoutCompatible(), |
| 3556 | Attr.getMustBeNull(), |
| 3557 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3558 | } |
| 3559 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3560 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3561 | // Checker-specific attribute handlers. |
| 3562 | //===----------------------------------------------------------------------===// |
| 3563 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3564 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3565 | return type->isDependentType() || |
| 3566 | type->isObjCObjectPointerType() || |
| 3567 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3568 | } |
| 3569 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3570 | return type->isDependentType() || |
| 3571 | type->isPointerType() || |
| 3572 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3575 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3576 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3577 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3578 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3579 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3580 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3581 | cf = false; |
| 3582 | } else { |
| 3583 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3584 | cf = true; |
| 3585 | } |
| 3586 | |
| 3587 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3588 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3589 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3590 | return; |
| 3591 | } |
| 3592 | |
| 3593 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3594 | param->addAttr(::new (S.Context) |
| 3595 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3596 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3597 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3598 | param->addAttr(::new (S.Context) |
| 3599 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3600 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3603 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3604 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3605 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3606 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3607 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3608 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3609 | returnType = MD->getResultType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3610 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3611 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3612 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3613 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3614 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3615 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3616 | returnType = FD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3617 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3618 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3619 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3620 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3621 | return; |
| 3622 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3623 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3624 | bool typeOK; |
| 3625 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3626 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3627 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3628 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3629 | case AttributeList::AT_NSReturnsRetained: |
| 3630 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3631 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3632 | cf = false; |
| 3633 | break; |
| 3634 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3635 | case AttributeList::AT_CFReturnsRetained: |
| 3636 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3637 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3638 | cf = true; |
| 3639 | break; |
| 3640 | } |
| 3641 | |
| 3642 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3643 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3644 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3645 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3646 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3647 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3648 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3649 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3650 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3651 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3652 | D->addAttr(::new (S.Context) |
| 3653 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 3654 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3655 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3656 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3657 | D->addAttr(::new (S.Context) |
| 3658 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3659 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3660 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3661 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3662 | D->addAttr(::new (S.Context) |
| 3663 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3664 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3665 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3666 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3667 | D->addAttr(::new (S.Context) |
| 3668 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3669 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3670 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3671 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3672 | D->addAttr(::new (S.Context) |
| 3673 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3674 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3675 | return; |
| 3676 | }; |
| 3677 | } |
| 3678 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3679 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3680 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 3681 | const int EP_ObjCMethod = 1; |
| 3682 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3683 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3684 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3685 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3686 | if (isa<ObjCMethodDecl>(D)) |
| 3687 | resultType = cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3688 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3689 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3690 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3691 | if (!resultType->isReferenceType() && |
| 3692 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3693 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3694 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3695 | << attr.getName() |
| 3696 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3697 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3698 | |
| 3699 | // Drop the attribute. |
| 3700 | return; |
| 3701 | } |
| 3702 | |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3703 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3704 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 3705 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3708 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3709 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3710 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3711 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3712 | DeclContext *DC = method->getDeclContext(); |
| 3713 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3714 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3715 | << attr.getName() << 0; |
| 3716 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3717 | return; |
| 3718 | } |
| 3719 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3720 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3721 | << attr.getName() << 1; |
| 3722 | return; |
| 3723 | } |
| 3724 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3725 | method->addAttr(::new (S.Context) |
| 3726 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 3727 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3728 | } |
| 3729 | |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3730 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 3731 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3732 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3733 | |
| 3734 | // Check whether there's a conflicting attribute already present. |
| 3735 | Attr *Existing; |
| 3736 | if (IsAudited) { |
| 3737 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 3738 | } else { |
| 3739 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 3740 | } |
| 3741 | if (Existing) { |
| 3742 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 3743 | << A.getName() |
| 3744 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 3745 | << A.getRange() << Existing->getRange(); |
| 3746 | return; |
| 3747 | } |
| 3748 | |
| 3749 | // All clear; add the attribute. |
| 3750 | if (IsAudited) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3751 | D->addAttr(::new (S.Context) |
| 3752 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 3753 | A.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3754 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3755 | D->addAttr(::new (S.Context) |
| 3756 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 3757 | A.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3758 | } |
| 3759 | } |
| 3760 | |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3761 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3762 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3763 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3764 | |
| 3765 | // In Objective-C, verify that the type names an Objective-C type. |
| 3766 | // We don't want to check this outside of ObjC because people sometimes |
| 3767 | // do crazy C declarations of Objective-C types. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3768 | if (Parm && S.getLangOpts().ObjC1) { |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3769 | // Check for an existing type with this name. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3770 | LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc, |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3771 | Sema::LookupOrdinaryName); |
| 3772 | if (S.LookupName(R, Sc)) { |
| 3773 | NamedDecl *Target = R.getFoundDecl(); |
| 3774 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 3775 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 3776 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 3777 | } |
| 3778 | } |
| 3779 | } |
| 3780 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3781 | D->addAttr(::new (S.Context) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3782 | NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3783 | Attr.getAttributeSpellingListIndex())); |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3784 | } |
| 3785 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3786 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 3787 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3788 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3789 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3790 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3791 | 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] | 3792 | return; |
| 3793 | } |
| 3794 | |
| 3795 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3796 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3797 | Attr.getAttributeSpellingListIndex())); |
| 3798 | } |
| 3799 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3800 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 3801 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3802 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3803 | |
| 3804 | if (!Parm) { |
| 3805 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3806 | return; |
| 3807 | } |
| 3808 | |
| 3809 | D->addAttr(::new (S.Context) |
| 3810 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 3811 | Attr.getAttributeSpellingListIndex())); |
| 3812 | } |
| 3813 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3814 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3815 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3816 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3817 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3818 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3819 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3820 | } |
| 3821 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3822 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3823 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3824 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3825 | QualType type = vd->getType(); |
| 3826 | |
| 3827 | if (!type->isDependentType() && |
| 3828 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3829 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3830 | << type; |
| 3831 | return; |
| 3832 | } |
| 3833 | |
| 3834 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3835 | |
| 3836 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3837 | // going to infer. |
| 3838 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3839 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3840 | |
| 3841 | switch (lifetime) { |
| 3842 | case Qualifiers::OCL_None: |
| 3843 | assert(type->isDependentType() && |
| 3844 | "didn't infer lifetime for non-dependent type?"); |
| 3845 | break; |
| 3846 | |
| 3847 | case Qualifiers::OCL_Weak: // meaningful |
| 3848 | case Qualifiers::OCL_Strong: // meaningful |
| 3849 | break; |
| 3850 | |
| 3851 | case Qualifiers::OCL_ExplicitNone: |
| 3852 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3853 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3854 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3855 | break; |
| 3856 | } |
| 3857 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3858 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3859 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 3860 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3861 | } |
| 3862 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3863 | //===----------------------------------------------------------------------===// |
| 3864 | // Microsoft specific attribute handlers. |
| 3865 | //===----------------------------------------------------------------------===// |
| 3866 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3867 | // Check if MS extensions or some other language extensions are enabled. If |
| 3868 | // not, issue a diagnostic that the given attribute is unused. |
| 3869 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 3870 | bool OtherExtension = false) { |
| 3871 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 3872 | return true; |
| 3873 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3874 | return false; |
| 3875 | } |
| 3876 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3877 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 3878 | if (!S.LangOpts.CPlusPlus) { |
| 3879 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3880 | << Attr.getName() << AttributeLangSupport::C; |
| 3881 | return; |
| 3882 | } |
| 3883 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3884 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 3885 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 3886 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 3887 | if (!isa<CXXRecordDecl>(D)) { |
| 3888 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3889 | << Attr.getName() << ExpectedClass; |
| 3890 | return; |
| 3891 | } |
| 3892 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3893 | StringRef StrRef; |
| 3894 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3895 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3896 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3897 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3898 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 3899 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3900 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 3901 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3902 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3903 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3904 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3905 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3906 | return; |
| 3907 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3908 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3909 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3910 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3911 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3912 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3913 | return; |
| 3914 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3915 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3916 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3917 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3918 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3919 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3920 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3921 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 3922 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3923 | } |
| 3924 | |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 3925 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3926 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 3927 | return; |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 3928 | |
| 3929 | AttributeList::Kind Kind = Attr.getKind(); |
| 3930 | if (Kind == AttributeList::AT_SingleInheritance) |
| 3931 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3932 | ::new (S.Context) |
| 3933 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 3934 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 3935 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 3936 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3937 | ::new (S.Context) |
| 3938 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 3939 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 3940 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 3941 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3942 | ::new (S.Context) |
| 3943 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 3944 | Attr.getAttributeSpellingListIndex())); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 3945 | } |
| 3946 | |
Aaron Ballman | 5010a76 | 2013-12-02 16:17:55 +0000 | [diff] [blame^] | 3947 | static void handleWin64Attr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3948 | if (!checkMicrosoftExt(S, Attr)) |
| 3949 | return; |
| 3950 | |
Aaron Ballman | 5010a76 | 2013-12-02 16:17:55 +0000 | [diff] [blame^] | 3951 | D->addAttr(::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 3952 | Attr.getAttributeSpellingListIndex())); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 3953 | } |
| 3954 | |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3955 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3956 | if (!checkMicrosoftExt(S, Attr)) |
| 3957 | return; |
| 3958 | D->addAttr(::new (S.Context) |
| 3959 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 3960 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 3963 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3964 | if (!checkMicrosoftExt(S, Attr)) |
| 3965 | return; |
| 3966 | // Check linkage after possibly merging declaratinos. See |
| 3967 | // checkAttributesAfterMerging(). |
| 3968 | D->addAttr(::new (S.Context) |
| 3969 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 3970 | Attr.getAttributeSpellingListIndex())); |
| 3971 | } |
| 3972 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3973 | /// Handles semantic checking for features that are common to all attributes, |
| 3974 | /// such as checking whether a parameter was properly specified, or the correct |
| 3975 | /// number of arguments were passed, etc. |
| 3976 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 3977 | const AttributeList &Attr) { |
| 3978 | // Several attributes carry different semantics than the parsing requires, so |
| 3979 | // those are opted out of the common handling. |
| 3980 | // |
| 3981 | // We also bail on unknown and ignored attributes because those are handled |
| 3982 | // as part of the target-specific handling logic. |
| 3983 | if (Attr.hasCustomParsing() || |
| 3984 | Attr.getKind() == AttributeList::UnknownAttribute || |
| 3985 | Attr.getKind() == AttributeList::IgnoredAttribute) |
| 3986 | return false; |
| 3987 | |
| 3988 | // If there are no optional arguments, then checking for the argument count |
| 3989 | // is trivial. |
| 3990 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 3991 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 3992 | return true; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3993 | |
| 3994 | // Check whether the attribute appertains to the given subject. |
| 3995 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 3996 | return true; |
| 3997 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3998 | return false; |
| 3999 | } |
| 4000 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4001 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4002 | // Top Level Sema Entry Points |
| 4003 | //===----------------------------------------------------------------------===// |
| 4004 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4005 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4006 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4007 | /// silently ignore it if a GNU attribute. |
| 4008 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4009 | const AttributeList &Attr, |
| 4010 | bool IncludeCXX11Attributes) { |
| 4011 | if (Attr.isInvalid()) |
| 4012 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4013 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4014 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4015 | // instead. |
| 4016 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4017 | return; |
| 4018 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4019 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4020 | return; |
| 4021 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4022 | switch (Attr.getKind()) { |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 4023 | case AttributeList::AT_IBAction: |
| 4024 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4025 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4026 | case AttributeList::AT_IBOutletCollection: |
| 4027 | handleIBOutletCollection(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4028 | case AttributeList::AT_AddressSpace: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4029 | case AttributeList::AT_ObjCGC: |
| 4030 | case AttributeList::AT_VectorSize: |
| 4031 | case AttributeList::AT_NeonVectorType: |
| 4032 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4033 | case AttributeList::AT_Ptr32: |
| 4034 | case AttributeList::AT_Ptr64: |
| 4035 | case AttributeList::AT_SPtr: |
| 4036 | case AttributeList::AT_UPtr: |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4037 | // Ignore these, these are type attributes, handled by |
| 4038 | // ProcessTypeAttributes. |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4039 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4040 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4041 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4042 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4043 | case AttributeList::AT_AlwaysInline: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4044 | handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4045 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4046 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4047 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4048 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4049 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4050 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4051 | handleDependencyAttr(S, scope, D, Attr); |
| 4052 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4053 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4054 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4055 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4056 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 4057 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4058 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4059 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4060 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4061 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4062 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4063 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4064 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4065 | case AttributeList::AT_MinSize: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4066 | handleSimpleAttribute<MinSizeAttr>(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4067 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4068 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4069 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4070 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | 9a22621 | 2013-08-28 23:13:26 +0000 | [diff] [blame] | 4071 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4072 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4073 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4074 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4075 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4076 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4077 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4078 | case AttributeList::AT_MayAlias: |
| 4079 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break; |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4080 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4081 | case AttributeList::AT_NoCommon: |
| 4082 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4083 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4084 | case AttributeList::AT_Overloadable: |
| 4085 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 4086 | case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4087 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4088 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4089 | case AttributeList::AT_Naked: |
| 4090 | handleSimpleAttribute<NakedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4091 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4092 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4093 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4094 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4095 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4096 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4097 | handleObjCOwnershipAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4098 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4099 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4100 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4101 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4102 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4103 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4104 | case AttributeList::AT_ObjCRequiresSuper: |
| 4105 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4106 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4107 | case AttributeList::AT_NSBridged: |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4108 | handleNSBridgedAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4109 | |
| 4110 | case AttributeList::AT_ObjCBridge: |
| 4111 | handleObjCBridgeAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4112 | |
| 4113 | case AttributeList::AT_ObjCBridgeMutable: |
| 4114 | handleObjCBridgeMutableAttr(S, scope, D, Attr); break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4115 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4116 | case AttributeList::AT_CFAuditedTransfer: |
| 4117 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4118 | handleCFTransferAttr(S, D, Attr); break; |
| 4119 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4120 | // Checker-specific. |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4121 | case AttributeList::AT_CFConsumed: |
| 4122 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4123 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4124 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4125 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4126 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4127 | case AttributeList::AT_NSReturnsNotRetained: |
| 4128 | case AttributeList::AT_CFReturnsNotRetained: |
| 4129 | case AttributeList::AT_NSReturnsRetained: |
| 4130 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4131 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4132 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4133 | case AttributeList::AT_WorkGroupSizeHint: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4134 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4135 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4136 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4137 | case AttributeList::AT_VecTypeHint: |
| 4138 | handleVecTypeHint(S, D, Attr); break; |
| 4139 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4140 | case AttributeList::AT_InitPriority: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4141 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4142 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4143 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4144 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4145 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4146 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4147 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4148 | case AttributeList::AT_ArcWeakrefUnavailable: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4149 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4150 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4151 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break; |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 4152 | case AttributeList::AT_ObjCSuppressProtocol: |
| 4153 | handleObjCSuppresProtocolAttr(S, D, Attr); |
| 4154 | break; |
| 4155 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4156 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4157 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4158 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4159 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4160 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4161 | case AttributeList::AT_Visibility: |
| 4162 | handleVisibilityAttr(S, D, Attr, false); |
| 4163 | break; |
| 4164 | case AttributeList::AT_TypeVisibility: |
| 4165 | handleVisibilityAttr(S, D, Attr, true); |
| 4166 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4167 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 6a42b5a | 2013-11-27 16:59:17 +0000 | [diff] [blame] | 4168 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4169 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4170 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4171 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4172 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4173 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4174 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4175 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4176 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4177 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4178 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4179 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4180 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4181 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4182 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4183 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4184 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4185 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4186 | case AttributeList::AT_Pure: |
| 4187 | handleSimpleAttribute<PureAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4188 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4189 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4190 | case AttributeList::AT_NoInline: |
| 4191 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4192 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4193 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | b4f3134 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4194 | // Just ignore |
| 4195 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4196 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4197 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4198 | case AttributeList::AT_StdCall: |
| 4199 | case AttributeList::AT_CDecl: |
| 4200 | case AttributeList::AT_FastCall: |
| 4201 | case AttributeList::AT_ThisCall: |
| 4202 | case AttributeList::AT_Pascal: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4203 | case AttributeList::AT_MSABI: |
| 4204 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4205 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4206 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4207 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4208 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4209 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4210 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4211 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4212 | case AttributeList::AT_OpenCLImageAccess: |
| 4213 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4214 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4215 | |
| 4216 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4217 | case AttributeList::AT_MsStruct: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4218 | handleSimpleAttribute<MsStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4219 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4220 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4221 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4222 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4223 | case AttributeList::AT_SingleInheritance: |
| 4224 | case AttributeList::AT_MultipleInheritance: |
| 4225 | case AttributeList::AT_VirtualInheritance: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4226 | handleInheritanceAttr(S, D, Attr); |
| 4227 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4228 | case AttributeList::AT_Win64: |
Aaron Ballman | 5010a76 | 2013-12-02 16:17:55 +0000 | [diff] [blame^] | 4229 | handleWin64Attr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4230 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4231 | handleForceInlineAttr(S, D, Attr); |
| 4232 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4233 | case AttributeList::AT_SelectAny: |
| 4234 | handleSelectAnyAttr(S, D, Attr); |
| 4235 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4236 | |
| 4237 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4238 | case AttributeList::AT_AssertExclusiveLock: |
| 4239 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4240 | break; |
| 4241 | case AttributeList::AT_AssertSharedLock: |
| 4242 | handleAssertSharedLockAttr(S, D, Attr); |
| 4243 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4244 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 4245 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4246 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4247 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4248 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4249 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4250 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4251 | case AttributeList::AT_NoSanitizeAddress: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4252 | handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4253 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4254 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4255 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4256 | break; |
| 4257 | case AttributeList::AT_NoSanitizeThread: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4258 | handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4259 | break; |
| 4260 | case AttributeList::AT_NoSanitizeMemory: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4261 | handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4262 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4263 | case AttributeList::AT_Lockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4264 | handleSimpleAttribute<LockableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4265 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4266 | handleGuardedByAttr(S, D, Attr); |
| 4267 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4268 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4269 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4270 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4271 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4272 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4273 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4274 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4275 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4276 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4277 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4278 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4279 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4280 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4281 | handleLockReturnedAttr(S, D, Attr); |
| 4282 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4283 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4284 | handleLocksExcludedAttr(S, D, Attr); |
| 4285 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4286 | case AttributeList::AT_SharedLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4287 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4288 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4289 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4290 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4291 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4292 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4293 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4294 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4295 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4296 | handleUnlockFunAttr(S, D, Attr); |
| 4297 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4298 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4299 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4300 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4301 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4302 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4303 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4304 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4305 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4306 | case AttributeList::AT_Consumable: |
| 4307 | handleConsumableAttr(S, D, Attr); |
| 4308 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4309 | case AttributeList::AT_CallableWhen: |
| 4310 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4311 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4312 | case AttributeList::AT_ParamTypestate: |
| 4313 | handleParamTypestateAttr(S, D, Attr); |
| 4314 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4315 | case AttributeList::AT_ReturnTypestate: |
| 4316 | handleReturnTypestateAttr(S, D, Attr); |
| 4317 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4318 | case AttributeList::AT_SetTypestate: |
| 4319 | handleSetTypestateAttr(S, D, Attr); |
| 4320 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4321 | case AttributeList::AT_TestTypestate: |
| 4322 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4323 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4324 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4325 | // Type safety attributes. |
| 4326 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4327 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4328 | break; |
| 4329 | case AttributeList::AT_TypeTagForDatatype: |
| 4330 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4331 | break; |
| 4332 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4333 | default: |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4334 | // Ask target about the attribute. |
| 4335 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4336 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4337 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4338 | diag::warn_unhandled_ms_attribute_ignored : |
| 4339 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4340 | break; |
| 4341 | } |
| 4342 | } |
| 4343 | |
| 4344 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4345 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4346 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4347 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4348 | bool IncludeCXX11Attributes) { |
| 4349 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4350 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4351 | |
| 4352 | // GCC accepts |
| 4353 | // static int a9 __attribute__((weakref)); |
| 4354 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4355 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4356 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4357 | cast<NamedDecl>(D)->getNameAsString(); |
| 4358 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4359 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4360 | } |
| 4361 | } |
| 4362 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4363 | // Annotation attributes are the only attributes allowed after an access |
| 4364 | // specifier. |
| 4365 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4366 | const AttributeList *AttrList) { |
| 4367 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4368 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4369 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4370 | } else { |
| 4371 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4372 | return true; |
| 4373 | } |
| 4374 | } |
| 4375 | |
| 4376 | return false; |
| 4377 | } |
| 4378 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4379 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4380 | /// contains any decl attributes that we should warn about. |
| 4381 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4382 | for ( ; A; A = A->getNext()) { |
| 4383 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4384 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4385 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4386 | |
| 4387 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4388 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4389 | << A->getName() << A->getRange(); |
| 4390 | } else { |
| 4391 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4392 | << A->getName() << A->getRange(); |
| 4393 | } |
| 4394 | } |
| 4395 | } |
| 4396 | |
| 4397 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4398 | /// used to build a declaration, complain about any decl attributes |
| 4399 | /// which might be lying around on it. |
| 4400 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4401 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4402 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4403 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4404 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4405 | } |
| 4406 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4407 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4408 | /// \#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] | 4409 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4410 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4411 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4412 | NamedDecl *NewD = 0; |
| 4413 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4414 | FunctionDecl *NewFD; |
| 4415 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4416 | // FIXME: Mangling? |
| 4417 | // FIXME: Is the qualifier info correct? |
| 4418 | // FIXME: Is the DeclContext correct? |
| 4419 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4420 | Loc, Loc, DeclarationName(II), |
| 4421 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4422 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4423 | FD->hasPrototype(), |
| 4424 | false/*isConstexprSpecified*/); |
| 4425 | NewD = NewFD; |
| 4426 | |
| 4427 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4428 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4429 | |
| 4430 | // Fake up parameter variables; they are declared as if this were |
| 4431 | // a typedef. |
| 4432 | QualType FDTy = FD->getType(); |
| 4433 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4434 | SmallVector<ParmVarDecl*, 16> Params; |
| 4435 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4436 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4437 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4438 | Param->setScopeInfo(0, Params.size()); |
| 4439 | Params.push_back(Param); |
| 4440 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4441 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4442 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4443 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4444 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4445 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4446 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4447 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4448 | if (VD->getQualifier()) { |
| 4449 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4450 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4451 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4452 | } |
| 4453 | return NewD; |
| 4454 | } |
| 4455 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4456 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4457 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4458 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4459 | if (W.getUsed()) return; // only do this once |
| 4460 | W.setUsed(true); |
| 4461 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4462 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4463 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4464 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 4465 | NDId->getName())); |
| 4466 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4467 | WeakTopLevelDecl.push_back(NewD); |
| 4468 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4469 | // to insert Decl at TU scope, sorry. |
| 4470 | DeclContext *SavedContext = CurContext; |
| 4471 | CurContext = Context.getTranslationUnitDecl(); |
| 4472 | PushOnScopeChains(NewD, S); |
| 4473 | CurContext = SavedContext; |
| 4474 | } else { // just add weak to existing |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4475 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4476 | } |
| 4477 | } |
| 4478 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 4479 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 4480 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4481 | // have to do this. |
| 4482 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4483 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4484 | NamedDecl *ND = NULL; |
| 4485 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 4486 | if (VD->isExternC()) |
| 4487 | ND = VD; |
| 4488 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 4489 | if (FD->isExternC()) |
| 4490 | ND = FD; |
| 4491 | if (ND) { |
| 4492 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4493 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4494 | = WeakUndeclaredIdentifiers.find(Id); |
| 4495 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 4496 | WeakInfo W = I->second; |
| 4497 | DeclApplyPragmaWeak(S, ND, W); |
| 4498 | WeakUndeclaredIdentifiers[Id] = W; |
| 4499 | } |
| 4500 | } |
| 4501 | } |
| 4502 | } |
| 4503 | } |
| 4504 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4505 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4506 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4507 | /// 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] | 4508 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4509 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4510 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4511 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4512 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4513 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4514 | // position to the decl itself. This handles cases like: |
| 4515 | // int *__attr__(x)** D; |
| 4516 | // when X is a decl attribute. |
| 4517 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4518 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4519 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4520 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4521 | // Finally, apply any attributes on the decl itself. |
| 4522 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4523 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4524 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4525 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4526 | /// Is the given declaration allowed to use a forbidden type? |
| 4527 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4528 | // Private ivars are always okay. Unfortunately, people don't |
| 4529 | // always properly make their ivars private, even in system headers. |
| 4530 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4531 | // Function declarations in sys headers will be marked unavailable. |
| 4532 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4533 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4534 | return false; |
| 4535 | |
| 4536 | // Require it to be declared in a system header. |
| 4537 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4538 | } |
| 4539 | |
| 4540 | /// Handle a delayed forbidden-type diagnostic. |
| 4541 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4542 | Decl *decl) { |
| 4543 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 4544 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 4545 | "this system declaration uses an unsupported type")); |
| 4546 | return; |
| 4547 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4548 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4549 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4550 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4551 | // kind of forbidden type messages on unavailable functions. |
| 4552 | if (FD->hasAttr<UnavailableAttr>() && |
| 4553 | diag.getForbiddenTypeDiagnostic() == |
| 4554 | diag::err_arc_array_param_no_ownership) { |
| 4555 | diag.Triggered = true; |
| 4556 | return; |
| 4557 | } |
| 4558 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4559 | |
| 4560 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4561 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4562 | diag.Triggered = true; |
| 4563 | } |
| 4564 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4565 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4566 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4567 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4568 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4569 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4570 | // When delaying diagnostics to run in the context of a parsed |
| 4571 | // declaration, we only want to actually emit anything if parsing |
| 4572 | // succeeds. |
| 4573 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4574 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4575 | // We emit all the active diagnostics in this pool or any of its |
| 4576 | // parents. In general, we'll get one pool for the decl spec |
| 4577 | // and a child pool for each declarator; in a decl group like: |
| 4578 | // deprecated_typedef foo, *bar, baz(); |
| 4579 | // only the declarator pops will be passed decls. This is correct; |
| 4580 | // we really do need to consider delayed diagnostics from the decl spec |
| 4581 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4582 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4583 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4584 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4585 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4586 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4587 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4588 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4589 | continue; |
| 4590 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4591 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4592 | case DelayedDiagnostic::Deprecation: |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4593 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 4594 | if (!decl->isInvalidDecl()) |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4595 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4596 | break; |
| 4597 | |
| 4598 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4599 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4600 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4601 | |
| 4602 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4603 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4604 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4605 | } |
| 4606 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4607 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4608 | } |
| 4609 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4610 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4611 | /// been delayed in the current context instead of in the given pool. |
| 4612 | /// Essentially, this just moves them to the current pool. |
| 4613 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4614 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4615 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4616 | curPool->steal(pool); |
| 4617 | } |
| 4618 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4619 | static bool isDeclDeprecated(Decl *D) { |
| 4620 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4621 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4622 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4623 | // A category implicitly has the availability of the interface. |
| 4624 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4625 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4626 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4627 | return false; |
| 4628 | } |
| 4629 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4630 | static void |
| 4631 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 4632 | SourceLocation Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4633 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4634 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4635 | DeclarationName Name = D->getDeclName(); |
| 4636 | if (!Message.empty()) { |
| 4637 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 4638 | S.Diag(D->getLocation(), |
| 4639 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4640 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4641 | if (ObjCPropery) |
| 4642 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 4643 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4644 | } else if (!UnknownObjCClass) { |
| 4645 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 4646 | S.Diag(D->getLocation(), |
| 4647 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4648 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4649 | if (ObjCPropery) |
| 4650 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 4651 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4652 | } else { |
| 4653 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 4654 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4655 | } |
| 4656 | } |
| 4657 | |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 4658 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4659 | Decl *Ctx) { |
| 4660 | if (isDeclDeprecated(Ctx)) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4661 | return; |
| 4662 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4663 | DD.Triggered = true; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4664 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 4665 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4666 | DD.getUnknownObjCClass(), |
| 4667 | DD.getObjCProperty()); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4670 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4671 | SourceLocation Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4672 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4673 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4674 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4675 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4676 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 4677 | UnknownObjCClass, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4678 | ObjCProperty, |
Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4679 | Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4680 | return; |
| 4681 | } |
| 4682 | |
| 4683 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 4684 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4685 | return; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4686 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4687 | } |