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" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 16 | #include "clang/AST/CXXInheritance.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 20 | #include "clang/AST/Expr.h" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 21 | #include "clang/AST/Mangle.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 22 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 23 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 25 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 26 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 28 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 31 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 32 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 33 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 34 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 35 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 36 | C, |
| 37 | Cpp, |
| 38 | ObjC |
| 39 | }; |
| 40 | } |
| 41 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | // Helper functions |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 46 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 47 | bool blocksToo = true) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 48 | QualType Ty; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 49 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 50 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 51 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 52 | Ty = decl->getUnderlyingType(); |
| 53 | else |
| 54 | return 0; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 56 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 57 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 28c433d | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 58 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 59 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 60 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 61 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 64 | // FIXME: We should provide an abstraction around a method or function |
| 65 | // to provide the following bits of information. |
| 66 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 67 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 68 | /// type (function or function-typed variable). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 69 | static bool isFunction(const Decl *D) { |
| 70 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 74 | /// type (function or function-typed variable) or an Objective-C |
| 75 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 76 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 77 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 80 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 81 | /// type (function or function-typed variable) or an Objective-C |
| 82 | /// method or a block. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 83 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 84 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 85 | return true; |
| 86 | // check for block is more involved. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 87 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 88 | QualType Ty = V->getType(); |
| 89 | return Ty->isBlockPointerType(); |
| 90 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 91 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 92 | } |
| 93 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 94 | /// Return true if the given decl has a declarator that should have |
| 95 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 96 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 97 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 98 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 99 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 102 | /// hasFunctionProto - Return true if the given decl has a argument |
| 103 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 104 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 105 | static bool hasFunctionProto(const Decl *D) { |
| 106 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 107 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 108 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 109 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 115 | /// arguments. It is an error to call this on a K&R function (use |
| 116 | /// hasFunctionProto first). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 117 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 118 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 119 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 120 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 121 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 122 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 125 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 126 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 127 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 128 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 129 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 130 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 131 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 134 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 135 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 136 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 137 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 140 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 141 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 142 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 143 | return proto->isVariadic(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 144 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 145 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 146 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 147 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 151 | static bool isInstanceMethod(const Decl *D) { |
| 152 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 153 | return MethodDecl->isInstance(); |
| 154 | return false; |
| 155 | } |
| 156 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 157 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 158 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 159 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 160 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 161 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 162 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 163 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 164 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 165 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 166 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 168 | // FIXME: Should we walk the chain of classes? |
| 169 | return ClsName == &Ctx.Idents.get("NSString") || |
| 170 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 171 | } |
| 172 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 173 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 174 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 175 | if (!PT) |
| 176 | return false; |
| 177 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 178 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 179 | if (!RT) |
| 180 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 181 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 182 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 183 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 184 | return false; |
| 185 | |
| 186 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 187 | } |
| 188 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 189 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 190 | // FIXME: Include the type in the argument list. |
| 191 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 192 | } |
| 193 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 194 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 195 | /// output an error. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 196 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 197 | unsigned Num) { |
| 198 | if (getNumAttributeArgs(Attr) != Num) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 199 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 200 | << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | |
| 204 | return true; |
| 205 | } |
| 206 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 207 | /// \brief Check if the attribute has at least as many args as Num. May |
| 208 | /// output an error. |
| 209 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 210 | unsigned Num) { |
| 211 | if (getNumAttributeArgs(Attr) < Num) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 212 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) |
| 213 | << Attr.getName() << Num; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 214 | return false; |
| 215 | } |
| 216 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 217 | return true; |
| 218 | } |
| 219 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 220 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 221 | /// expression and return success or failure. May output an error. |
| 222 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 223 | const Expr *Expr, uint32_t &Val, |
| 224 | unsigned Idx = UINT_MAX) { |
| 225 | llvm::APSInt I(32); |
| 226 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 227 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 228 | if (Idx != UINT_MAX) |
| 229 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 230 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 231 | << Expr->getSourceRange(); |
| 232 | else |
| 233 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 234 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 235 | << Expr->getSourceRange(); |
| 236 | return false; |
| 237 | } |
| 238 | Val = (uint32_t)I.getZExtValue(); |
| 239 | return true; |
| 240 | } |
| 241 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 242 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 243 | /// declaration. Returns true if diagnosed. |
| 244 | template <typename AttrTy> |
| 245 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 246 | const AttributeList &Attr) { |
| 247 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 248 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 249 | << Attr.getName() << A; |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 250 | return true; |
| 251 | } |
| 252 | return false; |
| 253 | } |
| 254 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 255 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 256 | /// instance method D. May output an error. |
| 257 | /// |
| 258 | /// \returns true if IdxExpr is a valid index. |
| 259 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 260 | const AttributeList &Attr, |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 261 | unsigned AttrArgNum, |
| 262 | const Expr *IdxExpr, |
| 263 | uint64_t &Idx) |
| 264 | { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 265 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 266 | |
| 267 | // In C++ the implicit 'this' function parameter also counts. |
| 268 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 269 | bool HP = hasFunctionProto(D); |
| 270 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 271 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 272 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 273 | HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 274 | |
| 275 | llvm::APSInt IdxInt; |
| 276 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 277 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 278 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 279 | << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant |
| 280 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
| 284 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 285 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 286 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 287 | << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | Idx--; // Convert to zero-based. |
| 291 | if (HasImplicitThisParam) { |
| 292 | if (Idx == 0) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 293 | S.Diag(Attr.getLoc(), |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 294 | diag::err_attribute_invalid_implicit_this_argument) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 295 | << Attr.getName() << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 296 | return false; |
| 297 | } |
| 298 | --Idx; |
| 299 | } |
| 300 | |
| 301 | return true; |
| 302 | } |
| 303 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 304 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 305 | /// If not emit an error and return false. If the argument is an identifier it |
| 306 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 307 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 308 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 309 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 310 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 311 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 312 | if (Attr.isArgIdent(ArgNum)) { |
| 313 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 314 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 315 | << Attr.getName() << AANT_ArgumentString |
| 316 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 317 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 318 | Str = Loc->Ident->getName(); |
| 319 | if (ArgLocation) |
| 320 | *ArgLocation = Loc->Loc; |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | // Now check for an actual string literal. |
| 325 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 326 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 327 | if (ArgLocation) |
| 328 | *ArgLocation = ArgExpr->getLocStart(); |
| 329 | |
| 330 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 331 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 332 | << Attr.getName() << AANT_ArgumentString; |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | Str = Literal->getString(); |
| 337 | return true; |
| 338 | } |
| 339 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 340 | /// \brief Applies the given attribute to the Decl without performing any |
| 341 | /// additional semantic checking. |
| 342 | template <typename AttrType> |
| 343 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 344 | const AttributeList &Attr) { |
| 345 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 346 | Attr.getAttributeSpellingListIndex())); |
| 347 | } |
| 348 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 349 | /// \brief Check if the passed-in expression is of type int or bool. |
| 350 | static bool isIntOrBool(Expr *Exp) { |
| 351 | QualType QT = Exp->getType(); |
| 352 | return QT->isBooleanType() || QT->isIntegerType(); |
| 353 | } |
| 354 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 355 | |
| 356 | // Check to see if the type is a smart pointer of some kind. We assume |
| 357 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 358 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 359 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 360 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 361 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 362 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 363 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 364 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 365 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 366 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 367 | return false; |
| 368 | |
| 369 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 372 | /// \brief Check if passed in Decl is a pointer type. |
| 373 | /// Note that this function may produce an error message. |
| 374 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 375 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 376 | const AttributeList &Attr) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 377 | const ValueDecl *vd = cast<ValueDecl>(D); |
| 378 | QualType QT = vd->getType(); |
| 379 | if (QT->isAnyPointerType()) |
| 380 | return true; |
| 381 | |
| 382 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 383 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 384 | // (We don't want to force template instantiation if we can avoid it, |
| 385 | // since that would alter the order in which templates are instantiated.) |
| 386 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 387 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 388 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 389 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 390 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 391 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 392 | |
| 393 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Aaron Ballman | 6828945 | 2013-12-26 15:06:01 +0000 | [diff] [blame] | 394 | << Attr.getName() << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 395 | return false; |
| 396 | } |
| 397 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 398 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 399 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 400 | static const RecordType *getRecordType(QualType QT) { |
| 401 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 402 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 403 | |
| 404 | // Now check if we point to record type. |
| 405 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 406 | return PT->getPointeeType()->getAs<RecordType>(); |
| 407 | |
| 408 | return 0; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 409 | } |
| 410 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 411 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 412 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 413 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 414 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 415 | return RT->getDecl()->hasAttr<LockableAttr>(); |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 419 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 420 | /// resolves to a lockable object. |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 421 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 422 | QualType Ty) { |
| 423 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 424 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 425 | // Warn if could not get record type for this argument. |
Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 426 | if (!RT) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 427 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
Aaron Ballman | 1da282a | 2014-01-02 23:15:58 +0000 | [diff] [blame] | 428 | << Attr.getName() << Ty; |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 429 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 430 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 431 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 432 | // Don't check for lockable if the class hasn't been defined yet. |
DeLesley Hutchins | 3509f29 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 433 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 434 | return; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 435 | |
| 436 | // Allow smart pointers to be used as lockable objects. |
| 437 | // FIXME -- Check the type that the smart pointer points to. |
| 438 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 439 | return; |
| 440 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 441 | // Check if the type is lockable. |
| 442 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 443 | if (RD->hasAttr<LockableAttr>()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 444 | return; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 445 | |
| 446 | // Else check if any base classes are lockable. |
| 447 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 448 | CXXBasePaths BPaths(false, false); |
| 449 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 450 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 451 | } |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 452 | |
| 453 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
Aaron Ballman | 1da282a | 2014-01-02 23:15:58 +0000 | [diff] [blame] | 454 | << Attr.getName() << Ty; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 457 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 458 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 459 | /// \param Sidx The attribute argument index to start checking with. |
| 460 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 461 | /// parameter list. |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 462 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 463 | const AttributeList &Attr, |
| 464 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 465 | int Sidx = 0, |
| 466 | bool ParamIdxOk = false) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 467 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 468 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 469 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 470 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 471 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 472 | Args.push_back(ArgExp); |
| 473 | continue; |
| 474 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 475 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 476 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 477 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 478 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 479 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 480 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 481 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 482 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 483 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 484 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 485 | // We allow constant strings to be used as a placeholder for expressions |
| 486 | // that are not valid C++ syntax, but warn that they are ignored. |
| 487 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 488 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 489 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 490 | continue; |
| 491 | } |
| 492 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 493 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 494 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 495 | // A pointer to member expression of the form &MyClass::mu is treated |
| 496 | // specially -- we need to look at the type of the member. |
| 497 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 498 | if (UOp->getOpcode() == UO_AddrOf) |
| 499 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 500 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 501 | ArgTy = DRE->getDecl()->getType(); |
| 502 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 503 | // First see if we can just cast to record type, or point to record type. |
| 504 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 505 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 506 | // Now check if we index into a record type function param. |
| 507 | if(!RT && ParamIdxOk) { |
| 508 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 509 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 510 | if(FD && IL) { |
| 511 | unsigned int NumParams = FD->getNumParams(); |
| 512 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 513 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 514 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 515 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 516 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 517 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 518 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 519 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 520 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 524 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 525 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 526 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 527 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 530 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 531 | // Attribute Implementations |
| 532 | //===----------------------------------------------------------------------===// |
| 533 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 534 | // FIXME: All this manual attribute parsing code is gross. At the |
| 535 | // least add some helper functions to check most argument patterns (# |
| 536 | // and types of args). |
| 537 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 538 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 539 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 540 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 541 | return; |
| 542 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 543 | D->addAttr(::new (S.Context) |
| 544 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 545 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 548 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 549 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 550 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 551 | SmallVector<Expr*, 1> Args; |
| 552 | // check that all arguments are lockable objects |
| 553 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 554 | unsigned Size = Args.size(); |
| 555 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 556 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 557 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 558 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 559 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 560 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 563 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 564 | Expr *Arg = 0; |
| 565 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 566 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 567 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 568 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 569 | } |
| 570 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 571 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 572 | const AttributeList &Attr) { |
| 573 | Expr *Arg = 0; |
| 574 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 575 | return; |
| 576 | |
| 577 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 578 | return; |
| 579 | |
| 580 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 581 | S.Context, Arg)); |
| 582 | } |
| 583 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 584 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 585 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 586 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 587 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 588 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 589 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 590 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 591 | QualType QT = cast<ValueDecl>(D)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 592 | if (!QT->isDependentType()) { |
| 593 | const RecordType *RT = getRecordType(QT); |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 594 | if (!RT || !RT->getDecl()->hasAttr<LockableAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 595 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 596 | << Attr.getName(); |
| 597 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 601 | // Check that all arguments are lockable objects. |
| 602 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 603 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 604 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 605 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 606 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 609 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 610 | const AttributeList &Attr) { |
| 611 | SmallVector<Expr*, 1> Args; |
| 612 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 613 | return; |
| 614 | |
| 615 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 616 | D->addAttr(::new (S.Context) |
| 617 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 618 | StartArg, Args.size(), |
| 619 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 622 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 623 | const AttributeList &Attr) { |
| 624 | SmallVector<Expr*, 1> Args; |
| 625 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 626 | return; |
| 627 | |
| 628 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 629 | D->addAttr(::new (S.Context) |
| 630 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 631 | StartArg, Args.size(), |
| 632 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 635 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 636 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 637 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 638 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 639 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 640 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 641 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 642 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 645 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 646 | const AttributeList &Attr) { |
| 647 | SmallVector<Expr*, 1> Args; |
| 648 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 649 | return; |
| 650 | |
| 651 | unsigned Size = Args.size(); |
| 652 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 653 | D->addAttr(::new (S.Context) |
| 654 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 655 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 658 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 659 | const AttributeList &Attr) { |
| 660 | SmallVector<Expr*, 1> Args; |
| 661 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 662 | return; |
| 663 | |
| 664 | unsigned Size = Args.size(); |
| 665 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 666 | D->addAttr(::new (S.Context) |
| 667 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 668 | StartArg, Size, |
| 669 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 670 | } |
| 671 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 672 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 673 | const AttributeList &Attr) { |
| 674 | SmallVector<Expr*, 1> Args; |
| 675 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 676 | return; |
| 677 | |
| 678 | unsigned Size = Args.size(); |
| 679 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 680 | D->addAttr(::new (S.Context) |
| 681 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 682 | Attr.getAttributeSpellingListIndex())); |
| 683 | } |
| 684 | |
| 685 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 686 | const AttributeList &Attr) { |
| 687 | SmallVector<Expr*, 1> Args; |
| 688 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 689 | return; |
| 690 | |
| 691 | unsigned Size = Args.size(); |
| 692 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 693 | D->addAttr(::new (S.Context) |
| 694 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 695 | StartArg, Size, |
| 696 | Attr.getAttributeSpellingListIndex())); |
| 697 | } |
| 698 | |
| 699 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 700 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 701 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 702 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 703 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 704 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 705 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 706 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 707 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 708 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 709 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 713 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 714 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 715 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 718 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 719 | const AttributeList &Attr) { |
| 720 | SmallVector<Expr*, 2> Args; |
| 721 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 722 | return; |
| 723 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 724 | D->addAttr(::new (S.Context) |
| 725 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 726 | Attr.getArgAsExpr(0), |
| 727 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 728 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 731 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 732 | const AttributeList &Attr) { |
| 733 | SmallVector<Expr*, 2> Args; |
| 734 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 735 | return; |
| 736 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 737 | D->addAttr(::new (S.Context) |
| 738 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 739 | Attr.getArgAsExpr(0), |
| 740 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 741 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 744 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 745 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 746 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 747 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 748 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 749 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 750 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 751 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 752 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 753 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 754 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 755 | return true; |
| 756 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 757 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 758 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 759 | const AttributeList &Attr) { |
| 760 | SmallVector<Expr*, 1> Args; |
| 761 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 762 | return; |
| 763 | |
| 764 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 765 | D->addAttr(::new (S.Context) |
| 766 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 767 | StartArg, Args.size(), |
| 768 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 771 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 772 | const AttributeList &Attr) { |
| 773 | SmallVector<Expr*, 1> Args; |
| 774 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 775 | return; |
| 776 | |
| 777 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 778 | D->addAttr(::new (S.Context) |
| 779 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 780 | StartArg, Args.size(), |
| 781 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 784 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 785 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 786 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 787 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 788 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 789 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 790 | unsigned Size = Args.size(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 791 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 792 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 793 | D->addAttr(::new (S.Context) |
| 794 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 795 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 799 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 800 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 801 | SmallVector<Expr*, 1> Args; |
| 802 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 803 | unsigned Size = Args.size(); |
| 804 | if (Size == 0) |
| 805 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 806 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 807 | D->addAttr(::new (S.Context) |
| 808 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 809 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 813 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 814 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 815 | return; |
| 816 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 817 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 818 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 819 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 820 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 821 | if (Size == 0) |
| 822 | return; |
| 823 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 824 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 825 | D->addAttr(::new (S.Context) |
| 826 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 827 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 830 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 831 | Expr *Cond = Attr.getArgAsExpr(0); |
| 832 | if (!Cond->isTypeDependent()) { |
| 833 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 834 | if (Converted.isInvalid()) |
| 835 | return; |
| 836 | Cond = Converted.take(); |
| 837 | } |
| 838 | |
| 839 | StringRef Msg; |
| 840 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
| 841 | return; |
| 842 | |
| 843 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 844 | if (!Cond->isValueDependent() && |
| 845 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 846 | Diags)) { |
| 847 | S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr); |
| 848 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 849 | S.Diag(Diags[I].first, Diags[I].second); |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | D->addAttr(::new (S.Context) |
| 854 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 855 | Attr.getAttributeSpellingListIndex())); |
| 856 | } |
| 857 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 858 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 859 | ConsumableAttr::ConsumedState DefaultState; |
| 860 | |
| 861 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 862 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 863 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 864 | DefaultState)) { |
| 865 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 866 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 867 | return; |
| 868 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 869 | } else { |
| 870 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 871 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 872 | return; |
| 873 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 874 | |
| 875 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 876 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 877 | Attr.getAttributeSpellingListIndex())); |
| 878 | } |
| 879 | |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 880 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 881 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 882 | const AttributeList &Attr) { |
| 883 | ASTContext &CurrContext = S.getASTContext(); |
| 884 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 885 | |
| 886 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 887 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 888 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 889 | RD->getNameAsString(); |
| 890 | |
| 891 | return false; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | return true; |
| 896 | } |
| 897 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 898 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 899 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 900 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 901 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 902 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 903 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 904 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 905 | return; |
| 906 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 907 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 908 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 909 | CallableWhenAttr::ConsumedState CallableState; |
| 910 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 911 | StringRef StateString; |
| 912 | SourceLocation Loc; |
| 913 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 914 | return; |
| 915 | |
| 916 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 917 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 918 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 919 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 920 | return; |
| 921 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 922 | |
| 923 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 924 | } |
| 925 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 926 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 927 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 928 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 929 | } |
| 930 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 931 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 932 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 933 | const AttributeList &Attr) { |
| 934 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 935 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 936 | ParamTypestateAttr::ConsumedState ParamState; |
| 937 | |
| 938 | if (Attr.isArgIdent(0)) { |
| 939 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 940 | StringRef StateString = Ident->Ident->getName(); |
| 941 | |
| 942 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 943 | ParamState)) { |
| 944 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 945 | << Attr.getName() << StateString; |
| 946 | return; |
| 947 | } |
| 948 | } else { |
| 949 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 950 | Attr.getName() << AANT_ArgumentIdentifier; |
| 951 | return; |
| 952 | } |
| 953 | |
| 954 | // FIXME: This check is currently being done in the analysis. It can be |
| 955 | // enabled here only after the parser propagates attributes at |
| 956 | // template specialization definition, not declaration. |
| 957 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 958 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 959 | // |
| 960 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 961 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 962 | // ReturnType.getAsString(); |
| 963 | // return; |
| 964 | //} |
| 965 | |
| 966 | D->addAttr(::new (S.Context) |
| 967 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 968 | Attr.getAttributeSpellingListIndex())); |
| 969 | } |
| 970 | |
| 971 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 972 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 973 | const AttributeList &Attr) { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 974 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 975 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 976 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 977 | |
| 978 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 979 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 980 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 981 | ReturnState)) { |
| 982 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 983 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 984 | return; |
| 985 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 986 | } else { |
| 987 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 988 | Attr.getName() << AANT_ArgumentIdentifier; |
| 989 | return; |
| 990 | } |
| 991 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 992 | // FIXME: This check is currently being done in the analysis. It can be |
| 993 | // enabled here only after the parser propagates attributes at |
| 994 | // template specialization definition, not declaration. |
| 995 | //QualType ReturnType; |
| 996 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 997 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 998 | // ReturnType = Param->getType(); |
| 999 | // |
| 1000 | //} else if (const CXXConstructorDecl *Constructor = |
| 1001 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1002 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1003 | // |
| 1004 | //} else { |
| 1005 | // |
| 1006 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1007 | //} |
| 1008 | // |
| 1009 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1010 | // |
| 1011 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1012 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1013 | // ReturnType.getAsString(); |
| 1014 | // return; |
| 1015 | //} |
| 1016 | |
| 1017 | D->addAttr(::new (S.Context) |
| 1018 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1019 | Attr.getAttributeSpellingListIndex())); |
| 1020 | } |
| 1021 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1022 | |
| 1023 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1024 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1025 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1026 | |
| 1027 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1028 | return; |
| 1029 | |
| 1030 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1031 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1032 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1033 | StringRef Param = Ident->Ident->getName(); |
| 1034 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1035 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1036 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1037 | return; |
| 1038 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1039 | } else { |
| 1040 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1041 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1042 | return; |
| 1043 | } |
| 1044 | |
| 1045 | D->addAttr(::new (S.Context) |
| 1046 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1047 | Attr.getAttributeSpellingListIndex())); |
| 1048 | } |
| 1049 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1050 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1051 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1052 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1053 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1054 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1055 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1056 | return; |
| 1057 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1058 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1059 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1060 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1061 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1062 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1063 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1064 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1065 | return; |
| 1066 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1067 | } else { |
| 1068 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1069 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1070 | return; |
| 1071 | } |
| 1072 | |
| 1073 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1074 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1075 | Attr.getAttributeSpellingListIndex())); |
| 1076 | } |
| 1077 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1078 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1079 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1080 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1081 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1084 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1085 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1086 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1087 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1088 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1089 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1090 | if (!FD->getType()->isDependentType() && |
| 1091 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1092 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1093 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1094 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1095 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1096 | FD->addAttr(::new (S.Context) |
| 1097 | PackedAttr(Attr.getRange(), S.Context, |
| 1098 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1099 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1100 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1103 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1104 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1105 | // variables or properties of Objective-C classes. The outlet must also |
| 1106 | // have an object reference type. |
| 1107 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1108 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1109 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1110 | << Attr.getName() << VD->getType() << 0; |
| 1111 | return false; |
| 1112 | } |
| 1113 | } |
| 1114 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1115 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1116 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1117 | << Attr.getName() << PD->getType() << 1; |
| 1118 | return false; |
| 1119 | } |
| 1120 | } |
| 1121 | else { |
| 1122 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1123 | return false; |
| 1124 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1125 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1126 | return true; |
| 1127 | } |
| 1128 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1129 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1130 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1131 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1132 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1133 | D->addAttr(::new (S.Context) |
| 1134 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1135 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1138 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1139 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1140 | |
| 1141 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1142 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1143 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1144 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1145 | return; |
| 1146 | } |
| 1147 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1148 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1149 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1150 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1151 | ParsedType PT; |
| 1152 | |
| 1153 | if (Attr.hasParsedType()) |
| 1154 | PT = Attr.getTypeArg(); |
| 1155 | else { |
| 1156 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1157 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1158 | if (!PT) { |
| 1159 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1160 | return; |
| 1161 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1162 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1163 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1164 | TypeSourceInfo *QTLoc = 0; |
| 1165 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1166 | if (!QTLoc) |
| 1167 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1168 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1169 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1170 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1171 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1172 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1173 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1174 | S.Diag(Attr.getLoc(), |
| 1175 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1176 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1177 | return; |
| 1178 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1179 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1180 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1181 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1182 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1185 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1186 | if (const RecordType *UT = T->getAsUnionType()) |
| 1187 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1188 | RecordDecl *UD = UT->getDecl(); |
| 1189 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1190 | itend = UD->field_end(); it != itend; ++it) { |
| 1191 | QualType QT = it->getType(); |
| 1192 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1193 | T = QT; |
| 1194 | return; |
| 1195 | } |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1200 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1201 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1202 | // ignore it as well |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1203 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1204 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1205 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1206 | return; |
| 1207 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1208 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1209 | SmallVector<unsigned, 8> NonNullArgs; |
| 1210 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1211 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1212 | uint64_t Idx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 1213 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, i + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1214 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1215 | |
| 1216 | // Is the function argument a pointer type? |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1217 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1218 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1219 | |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1220 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1221 | // FIXME: Should also highlight argument in decl. |
Aaron Ballman | cedaaea | 2013-12-26 17:07:49 +0000 | [diff] [blame] | 1222 | S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only) |
| 1223 | << Attr.getName() << Ex->getSourceRange(); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1224 | continue; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1225 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1226 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1227 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1228 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1229 | |
| 1230 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1231 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1232 | if (NonNullArgs.empty()) { |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1233 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1234 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1235 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1236 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1237 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1238 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1239 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1240 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1241 | if (NonNullArgs.empty()) { |
| 1242 | // Warn the trivial case only if attribute is not coming from a |
| 1243 | // macro instantiation. |
| 1244 | if (Attr.getLoc().isFileID()) |
| 1245 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1246 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1247 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1248 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1249 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1250 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1251 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1252 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1253 | D->addAttr(::new (S.Context) |
| 1254 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1255 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1258 | static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) { |
| 1259 | switch (K) { |
| 1260 | case OwnershipAttr::Holds: return "'ownership_holds'"; |
| 1261 | case OwnershipAttr::Takes: return "'ownership_takes'"; |
| 1262 | case OwnershipAttr::Returns: return "'ownership_returns'"; |
| 1263 | } |
| 1264 | llvm_unreachable("unknown ownership"); |
| 1265 | } |
| 1266 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1267 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1268 | // This attribute must be applied to a function declaration. The first |
| 1269 | // argument to the attribute must be an identifier, the name of the resource, |
| 1270 | // for example: malloc. The following arguments must be argument indexes, the |
| 1271 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1272 | // 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] | 1273 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1274 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1275 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1276 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1277 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1278 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1279 | return; |
| 1280 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1281 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1282 | // Figure out our Kind. |
| 1283 | OwnershipAttr::OwnershipKind K = |
| 1284 | OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0, |
| 1285 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1286 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1287 | // Check arguments. |
| 1288 | switch (K) { |
| 1289 | case OwnershipAttr::Takes: |
| 1290 | case OwnershipAttr::Holds: |
| 1291 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1292 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1293 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1294 | return; |
| 1295 | } |
| 1296 | break; |
| 1297 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1298 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1299 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1300 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1301 | return; |
| 1302 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1303 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1306 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1307 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1308 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1309 | return; |
| 1310 | } |
| 1311 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1312 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1313 | |
| 1314 | // Normalize the argument, __foo__ becomes foo. |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1315 | StringRef ModuleName = Module->getName(); |
| 1316 | if (ModuleName.startswith("__") && ModuleName.endswith("__") && |
| 1317 | ModuleName.size() > 4) { |
| 1318 | ModuleName = ModuleName.drop_front(2).drop_back(2); |
| 1319 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1320 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1321 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1322 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1323 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1324 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1325 | uint64_t Idx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 1326 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1327 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1328 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1329 | // Is the function argument a pointer type? |
| 1330 | QualType T = getFunctionOrMethodArgType(D, Idx); |
| 1331 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1332 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1333 | case OwnershipAttr::Takes: |
| 1334 | case OwnershipAttr::Holds: |
| 1335 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1336 | Err = 0; |
| 1337 | break; |
| 1338 | case OwnershipAttr::Returns: |
| 1339 | if (!T->isIntegerType()) |
| 1340 | Err = 1; |
| 1341 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1342 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1343 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1344 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1345 | << Ex->getSourceRange(); |
| 1346 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1347 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1348 | |
| 1349 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1350 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1351 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1352 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1353 | // FIXME: A returns attribute should conflict with any returns attribute |
| 1354 | // with a different index too. |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1355 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1356 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1357 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1358 | << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind()); |
| 1359 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1360 | } |
| 1361 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1362 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | unsigned* start = OwnershipArgs.data(); |
| 1366 | unsigned size = OwnershipArgs.size(); |
| 1367 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1368 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1369 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1370 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1371 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1374 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1375 | // Check the attribute arguments. |
| 1376 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1377 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1378 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1379 | return; |
| 1380 | } |
| 1381 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1382 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1383 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1384 | // gcc rejects |
| 1385 | // class c { |
| 1386 | // static int a __attribute__((weakref ("v2"))); |
| 1387 | // static int b() __attribute__((weakref ("f3"))); |
| 1388 | // }; |
| 1389 | // and ignores the attributes of |
| 1390 | // void f(void) { |
| 1391 | // static int a __attribute__((weakref ("v2"))); |
| 1392 | // } |
| 1393 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1394 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1395 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1396 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1397 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1398 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | // The GCC manual says |
| 1402 | // |
| 1403 | // At present, a declaration to which `weakref' is attached can only |
| 1404 | // be `static'. |
| 1405 | // |
| 1406 | // It also says |
| 1407 | // |
| 1408 | // Without a TARGET, |
| 1409 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1410 | // equivalent to `weak'. |
| 1411 | // |
| 1412 | // gcc 4.4.1 will accept |
| 1413 | // int a7 __attribute__((weakref)); |
| 1414 | // as |
| 1415 | // int a7 __attribute__((weak)); |
| 1416 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1417 | // it if this behaviour is actually used. |
| 1418 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1419 | // GCC rejects |
| 1420 | // static ((alias ("y"), weakref)). |
| 1421 | // Should we? How to check that weakref is before or after alias? |
| 1422 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1423 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1424 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1425 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1426 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1427 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1428 | // GCC will accept anything as the argument of weakref. Should we |
| 1429 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1430 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1431 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1432 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1433 | D->addAttr(::new (S.Context) |
| 1434 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1435 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1438 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1439 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1440 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1441 | return; |
| 1442 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1443 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1444 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1445 | return; |
| 1446 | } |
| 1447 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1448 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1449 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1450 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1451 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1454 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1455 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1456 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1457 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1458 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1459 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 1463 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr)) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1464 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1465 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1466 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1467 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1470 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1471 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1472 | StringRef Model; |
| 1473 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1474 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1475 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1476 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1477 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1478 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1479 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1480 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1481 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1482 | return; |
| 1483 | } |
| 1484 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1485 | D->addAttr(::new (S.Context) |
| 1486 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1487 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1490 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1491 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1492 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1493 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1494 | D->addAttr(::new (S.Context) |
| 1495 | MallocAttr(Attr.getRange(), S.Context, |
| 1496 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1497 | return; |
| 1498 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1501 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1504 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1505 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1506 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1507 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1508 | return; |
| 1509 | } |
| 1510 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1511 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context, |
| 1512 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1515 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1516 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1517 | |
| 1518 | if (S.CheckNoReturnAttr(attr)) return; |
| 1519 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1520 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1521 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1522 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1523 | return; |
| 1524 | } |
| 1525 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1526 | D->addAttr(::new (S.Context) |
| 1527 | NoReturnAttr(attr.getRange(), S.Context, |
| 1528 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1532 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1533 | attr.setInvalid(); |
| 1534 | return true; |
| 1535 | } |
| 1536 | |
| 1537 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1538 | } |
| 1539 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1540 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1541 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1542 | |
| 1543 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1544 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1545 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1546 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1547 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1548 | && !VD->getType()->isFunctionPointerType())) { |
| 1549 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1550 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1551 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1552 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1553 | return; |
| 1554 | } |
| 1555 | } |
| 1556 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1557 | D->addAttr(::new (S.Context) |
| 1558 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1559 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1562 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1563 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1564 | /* |
| 1565 | Returning a Vector Class in Registers |
| 1566 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1567 | According to the PPU ABI specifications, a class with a single member of |
| 1568 | vector type is returned in memory when used as the return value of a function. |
| 1569 | This results in inefficient code when implementing vector classes. To return |
| 1570 | the value in a single vector register, add the vecreturn attribute to the |
| 1571 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1572 | |
| 1573 | Example: |
| 1574 | |
| 1575 | struct Vector |
| 1576 | { |
| 1577 | __vector float xyzw; |
| 1578 | } __attribute__((vecreturn)); |
| 1579 | |
| 1580 | Vector Add(Vector lhs, Vector rhs) |
| 1581 | { |
| 1582 | Vector result; |
| 1583 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1584 | return result; // This will be returned in a register |
| 1585 | } |
| 1586 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1587 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1588 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1589 | return; |
| 1590 | } |
| 1591 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1592 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1593 | int count = 0; |
| 1594 | |
| 1595 | if (!isa<CXXRecordDecl>(record)) { |
| 1596 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1597 | return; |
| 1598 | } |
| 1599 | |
| 1600 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1601 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1602 | return; |
| 1603 | } |
| 1604 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1605 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1606 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1607 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1608 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1609 | return; |
| 1610 | } |
| 1611 | count++; |
| 1612 | } |
| 1613 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1614 | D->addAttr(::new (S.Context) |
| 1615 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1616 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1619 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1620 | const AttributeList &Attr) { |
| 1621 | if (isa<ParmVarDecl>(D)) { |
| 1622 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1623 | // parameter of a function declaration or lambda. |
| 1624 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1625 | S.Diag(Attr.getLoc(), |
| 1626 | diag::err_carries_dependency_param_not_function_decl); |
| 1627 | return; |
| 1628 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1629 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1630 | |
| 1631 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1632 | Attr.getRange(), S.Context, |
| 1633 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1636 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1637 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 429c134 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1638 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1639 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1640 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1641 | return; |
| 1642 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1643 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1644 | D->addAttr(::new (S.Context) |
| 1645 | UnusedAttr(Attr.getRange(), S.Context, |
| 1646 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1649 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1650 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1651 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1652 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1653 | return; |
| 1654 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1655 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1656 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1657 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1658 | return; |
| 1659 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1660 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1661 | D->addAttr(::new (S.Context) |
| 1662 | UsedAttr(Attr.getRange(), S.Context, |
| 1663 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1666 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1667 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1668 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1669 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1670 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1671 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1672 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1673 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1674 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1675 | if (Attr.getNumArgs() > 0 && |
| 1676 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1677 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1678 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1679 | D->addAttr(::new (S.Context) |
| 1680 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1681 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1684 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1685 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1686 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1687 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1688 | << Attr.getName() << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1689 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1690 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1691 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1692 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1693 | if (Attr.getNumArgs() > 0 && |
| 1694 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1695 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1696 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1697 | D->addAttr(::new (S.Context) |
| 1698 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1699 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1702 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1703 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1704 | const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1705 | unsigned NumArgs = Attr.getNumArgs(); |
| 1706 | if (NumArgs > 1) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1707 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 1708 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1709 | return; |
| 1710 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1711 | |
| 1712 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1713 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1714 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1715 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1716 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1717 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1718 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1721 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
| 1722 | const AttributeList &Attr) { |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1723 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1724 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1725 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1728 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1729 | IdentifierInfo *Platform, |
| 1730 | VersionTuple Introduced, |
| 1731 | VersionTuple Deprecated, |
| 1732 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1733 | StringRef PlatformName |
| 1734 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1735 | if (PlatformName.empty()) |
| 1736 | PlatformName = Platform->getName(); |
| 1737 | |
| 1738 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1739 | // of these steps are needed). |
| 1740 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1741 | !(Introduced <= Deprecated)) { |
| 1742 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1743 | << 1 << PlatformName << Deprecated.getAsString() |
| 1744 | << 0 << Introduced.getAsString(); |
| 1745 | return true; |
| 1746 | } |
| 1747 | |
| 1748 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1749 | !(Introduced <= Obsoleted)) { |
| 1750 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1751 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1752 | << 0 << Introduced.getAsString(); |
| 1753 | return true; |
| 1754 | } |
| 1755 | |
| 1756 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1757 | !(Deprecated <= Obsoleted)) { |
| 1758 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1759 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1760 | << 1 << Deprecated.getAsString(); |
| 1761 | return true; |
| 1762 | } |
| 1763 | |
| 1764 | return false; |
| 1765 | } |
| 1766 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1767 | /// \brief Check whether the two versions match. |
| 1768 | /// |
| 1769 | /// If either version tuple is empty, then they are assumed to match. If |
| 1770 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1771 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1772 | bool BeforeIsOkay) { |
| 1773 | if (X.empty() || Y.empty()) |
| 1774 | return true; |
| 1775 | |
| 1776 | if (X == Y) |
| 1777 | return true; |
| 1778 | |
| 1779 | if (BeforeIsOkay && X < Y) |
| 1780 | return true; |
| 1781 | |
| 1782 | return false; |
| 1783 | } |
| 1784 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1785 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1786 | IdentifierInfo *Platform, |
| 1787 | VersionTuple Introduced, |
| 1788 | VersionTuple Deprecated, |
| 1789 | VersionTuple Obsoleted, |
| 1790 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1791 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1792 | bool Override, |
| 1793 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1794 | VersionTuple MergedIntroduced = Introduced; |
| 1795 | VersionTuple MergedDeprecated = Deprecated; |
| 1796 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1797 | bool FoundAny = false; |
| 1798 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1799 | if (D->hasAttrs()) { |
| 1800 | AttrVec &Attrs = D->getAttrs(); |
| 1801 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1802 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1803 | if (!OldAA) { |
| 1804 | ++i; |
| 1805 | continue; |
| 1806 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1807 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1808 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1809 | if (OldPlatform != Platform) { |
| 1810 | ++i; |
| 1811 | continue; |
| 1812 | } |
| 1813 | |
| 1814 | FoundAny = true; |
| 1815 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1816 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1817 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1818 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1819 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1820 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 1821 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 1822 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 1823 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 1824 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1825 | if (Override) { |
| 1826 | int Which = -1; |
| 1827 | VersionTuple FirstVersion; |
| 1828 | VersionTuple SecondVersion; |
| 1829 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 1830 | Which = 0; |
| 1831 | FirstVersion = OldIntroduced; |
| 1832 | SecondVersion = Introduced; |
| 1833 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 1834 | Which = 1; |
| 1835 | FirstVersion = Deprecated; |
| 1836 | SecondVersion = OldDeprecated; |
| 1837 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 1838 | Which = 2; |
| 1839 | FirstVersion = Obsoleted; |
| 1840 | SecondVersion = OldObsoleted; |
| 1841 | } |
| 1842 | |
| 1843 | if (Which == -1) { |
| 1844 | Diag(OldAA->getLocation(), |
| 1845 | diag::warn_mismatched_availability_override_unavail) |
| 1846 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1847 | } else { |
| 1848 | Diag(OldAA->getLocation(), |
| 1849 | diag::warn_mismatched_availability_override) |
| 1850 | << Which |
| 1851 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1852 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 1853 | } |
| 1854 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1855 | } else { |
| 1856 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1857 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1858 | } |
| 1859 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1860 | Attrs.erase(Attrs.begin() + i); |
| 1861 | --e; |
| 1862 | continue; |
| 1863 | } |
| 1864 | |
| 1865 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 1866 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 1867 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 1868 | |
| 1869 | if (MergedIntroduced2.empty()) |
| 1870 | MergedIntroduced2 = OldIntroduced; |
| 1871 | if (MergedDeprecated2.empty()) |
| 1872 | MergedDeprecated2 = OldDeprecated; |
| 1873 | if (MergedObsoleted2.empty()) |
| 1874 | MergedObsoleted2 = OldObsoleted; |
| 1875 | |
| 1876 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 1877 | MergedIntroduced2, MergedDeprecated2, |
| 1878 | MergedObsoleted2)) { |
| 1879 | Attrs.erase(Attrs.begin() + i); |
| 1880 | --e; |
| 1881 | continue; |
| 1882 | } |
| 1883 | |
| 1884 | MergedIntroduced = MergedIntroduced2; |
| 1885 | MergedDeprecated = MergedDeprecated2; |
| 1886 | MergedObsoleted = MergedObsoleted2; |
| 1887 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1888 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | if (FoundAny && |
| 1892 | MergedIntroduced == Introduced && |
| 1893 | MergedDeprecated == Deprecated && |
| 1894 | MergedObsoleted == Obsoleted) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1895 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1896 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1897 | // Only create a new attribute if !Override, but we want to do |
| 1898 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1899 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 1900 | MergedDeprecated, MergedObsoleted) && |
| 1901 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1902 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 1903 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1904 | Obsoleted, IsUnavailable, Message, |
| 1905 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1906 | } |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1907 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1910 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 1911 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1912 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1913 | return; |
| 1914 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1915 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 1916 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1917 | IdentifierInfo *II = Platform->Ident; |
| 1918 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 1919 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 1920 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1921 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 1922 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1923 | if (!ND) { |
| 1924 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1925 | return; |
| 1926 | } |
| 1927 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1928 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 1929 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 1930 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 1931 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1932 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 1933 | if (const StringLiteral *SE = |
| 1934 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 1935 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1936 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1937 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1938 | Introduced.Version, |
| 1939 | Deprecated.Version, |
| 1940 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1941 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1942 | /*Override=*/false, |
| 1943 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 1944 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1945 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1948 | template <class T> |
| 1949 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 1950 | typename T::VisibilityType value, |
| 1951 | unsigned attrSpellingListIndex) { |
| 1952 | T *existingAttr = D->getAttr<T>(); |
| 1953 | if (existingAttr) { |
| 1954 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 1955 | if (existingValue == value) |
| 1956 | return NULL; |
| 1957 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 1958 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 1959 | D->dropAttr<T>(); |
| 1960 | } |
| 1961 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 1962 | } |
| 1963 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1964 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1965 | VisibilityAttr::VisibilityType Vis, |
| 1966 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1967 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 1968 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 1971 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 1972 | TypeVisibilityAttr::VisibilityType Vis, |
| 1973 | unsigned AttrSpellingListIndex) { |
| 1974 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 1975 | AttrSpellingListIndex); |
| 1976 | } |
| 1977 | |
| 1978 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 1979 | bool isTypeVisibility) { |
| 1980 | // Visibility attributes don't mean anything on a typedef. |
| 1981 | if (isa<TypedefNameDecl>(D)) { |
| 1982 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 1983 | << Attr.getName(); |
| 1984 | return; |
| 1985 | } |
| 1986 | |
| 1987 | // 'type_visibility' can only go on a type or namespace. |
| 1988 | if (isTypeVisibility && |
| 1989 | !(isa<TagDecl>(D) || |
| 1990 | isa<ObjCInterfaceDecl>(D) || |
| 1991 | isa<NamespaceDecl>(D))) { |
| 1992 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 1993 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 1994 | return; |
| 1995 | } |
| 1996 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 1997 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1998 | StringRef TypeStr; |
| 1999 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2000 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2001 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2002 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2003 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2004 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2005 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2006 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2007 | return; |
| 2008 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2009 | |
| 2010 | // Complain about attempts to use protected visibility on targets |
| 2011 | // (like Darwin) that don't support it. |
| 2012 | if (type == VisibilityAttr::Protected && |
| 2013 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2014 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2015 | type = VisibilityAttr::Default; |
| 2016 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2017 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2018 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2019 | clang::Attr *newAttr; |
| 2020 | if (isTypeVisibility) { |
| 2021 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2022 | (TypeVisibilityAttr::VisibilityType) type, |
| 2023 | Index); |
| 2024 | } else { |
| 2025 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2026 | } |
| 2027 | if (newAttr) |
| 2028 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2029 | } |
| 2030 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2031 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2032 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2033 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2034 | if (!Attr.isArgIdent(0)) { |
| 2035 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2036 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2037 | return; |
| 2038 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2039 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2040 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2041 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2042 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2043 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2044 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2045 | return; |
| 2046 | } |
| 2047 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2048 | if (F == ObjCMethodFamilyAttr::OMF_init && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2049 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2050 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2051 | << method->getResultType(); |
| 2052 | // Ignore the attribute. |
| 2053 | return; |
| 2054 | } |
| 2055 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2056 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2057 | S.Context, F)); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2058 | } |
| 2059 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2060 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2061 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2062 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2063 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2064 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2065 | return; |
| 2066 | } |
| 2067 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2068 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2069 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2070 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2071 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2072 | return; |
| 2073 | } |
| 2074 | } |
| 2075 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2076 | // It is okay to include this attribute on properties, e.g.: |
| 2077 | // |
| 2078 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2079 | // |
| 2080 | // In this case it follows tradition and suppresses an error in the above |
| 2081 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2082 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2083 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2084 | D->addAttr(::new (S.Context) |
| 2085 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2086 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2087 | } |
| 2088 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2089 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2090 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2091 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2092 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2093 | return; |
| 2094 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2095 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2096 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2097 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2098 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2099 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2100 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2101 | return; |
| 2102 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2103 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2104 | D->addAttr(::new (S.Context) |
| 2105 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2106 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2109 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2110 | // check the attribute arguments. |
| 2111 | if (Attr.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 2112 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 2113 | << Attr.getName() << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2114 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2115 | } |
| 2116 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2117 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2118 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2119 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2120 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2121 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2122 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2123 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2124 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2125 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2126 | return; |
| 2127 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2128 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2129 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2130 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2131 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2132 | return; |
| 2133 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2134 | |
| 2135 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2138 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2139 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2140 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2141 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2142 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2143 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2144 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2145 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2146 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2147 | return; |
| 2148 | } |
| 2149 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2150 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2151 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2152 | // FIXME: This error message could be improved, it would be nice |
| 2153 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2154 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2155 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2156 | return; |
| 2157 | } |
| 2158 | } |
| 2159 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2160 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2161 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2162 | if (isa<FunctionNoProtoType>(FT)) { |
| 2163 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2164 | return; |
| 2165 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2166 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2167 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2168 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2169 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2170 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2171 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2172 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2173 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2174 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2175 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2176 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2177 | if (!BD->isVariadic()) { |
| 2178 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2179 | return; |
| 2180 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2181 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2182 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2183 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2184 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2185 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2186 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2187 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2188 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2189 | return; |
| 2190 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2191 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2192 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2193 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2194 | return; |
| 2195 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2196 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2197 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2198 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2199 | return; |
| 2200 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2201 | D->addAttr(::new (S.Context) |
| 2202 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2203 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2204 | } |
| 2205 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2206 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2207 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2208 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | 3d699e0 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2209 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2210 | return; |
| 2211 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2212 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2213 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2214 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2215 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2216 | return; |
| 2217 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2218 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2219 | if (MD->getResultType()->isVoidType()) { |
| 2220 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2221 | << Attr.getName() << 1; |
| 2222 | return; |
| 2223 | } |
| 2224 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2225 | D->addAttr(::new (S.Context) |
| 2226 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2227 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2230 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2231 | // weak_import only applies to variable & function declarations. |
| 2232 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2233 | if (!D->canBeWeakImported(isDef)) { |
| 2234 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2235 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2236 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2237 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2238 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2239 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2240 | // Nothing to warn about here. |
| 2241 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2242 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2243 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2244 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2245 | return; |
| 2246 | } |
| 2247 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2248 | D->addAttr(::new (S.Context) |
| 2249 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2250 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2253 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2254 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2255 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2256 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2257 | uint32_t WGSize[3]; |
| 2258 | for (unsigned i = 0; i < 3; ++i) |
| 2259 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2260 | return; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2261 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2262 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2263 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2264 | Existing->getYDim() == WGSize[1] && |
| 2265 | Existing->getZDim() == WGSize[2])) |
| 2266 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2267 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2268 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2269 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2270 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2273 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2274 | if (!Attr.hasParsedType()) { |
| 2275 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2276 | << Attr.getName() << 1; |
| 2277 | return; |
| 2278 | } |
| 2279 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2280 | TypeSourceInfo *ParmTSI = 0; |
| 2281 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2282 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2283 | |
| 2284 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2285 | (ParmType->isBooleanType() || |
| 2286 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2287 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2288 | << ParmType; |
| 2289 | return; |
| 2290 | } |
| 2291 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2292 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2293 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2294 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2295 | return; |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2300 | ParmTSI)); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2303 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2304 | StringRef Name, |
| 2305 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2306 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2307 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2308 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2309 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2310 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2311 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2312 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2313 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2314 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2317 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2318 | // Make sure that there is a string literal as the sections's single |
| 2319 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2320 | StringRef Str; |
| 2321 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2322 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2323 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2324 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2325 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2326 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2327 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2328 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2329 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2330 | return; |
| 2331 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2332 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2333 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2334 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2335 | if (NewAttr) |
| 2336 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2339 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2340 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2341 | VarDecl *VD = cast<VarDecl>(D); |
| 2342 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2343 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2344 | return; |
| 2345 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2346 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2347 | Expr *E = Attr.getArgAsExpr(0); |
| 2348 | SourceLocation Loc = E->getExprLoc(); |
| 2349 | FunctionDecl *FD = 0; |
| 2350 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2351 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2352 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2353 | // will warn the user. |
| 2354 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2355 | if (DRE->hasQualifier()) |
| 2356 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2357 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2358 | NI = DRE->getNameInfo(); |
| 2359 | if (!FD) { |
| 2360 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2361 | << NI.getName(); |
| 2362 | return; |
| 2363 | } |
| 2364 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2365 | if (ULE->hasExplicitTemplateArgs()) |
| 2366 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2367 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2368 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2369 | if (!FD) { |
| 2370 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2371 | << NI.getName(); |
| 2372 | if (ULE->getType() == S.Context.OverloadTy) |
| 2373 | S.NoteAllOverloadCandidates(ULE); |
| 2374 | return; |
| 2375 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2376 | } else { |
| 2377 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2378 | return; |
| 2379 | } |
| 2380 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2381 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2382 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2383 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2384 | return; |
| 2385 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2386 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2387 | // We're currently more strict than GCC about what function types we accept. |
| 2388 | // If this ever proves to be a problem it should be easy to fix. |
| 2389 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2390 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2391 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2392 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2393 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2394 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2395 | return; |
| 2396 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2397 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2398 | D->addAttr(::new (S.Context) |
| 2399 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2400 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2403 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2404 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2405 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2406 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2407 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2408 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2409 | return; |
| 2410 | } |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2411 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2412 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2413 | uint64_t ArgIdx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2414 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2415 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2416 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2417 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2418 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2419 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2420 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2421 | if (not_nsstring_type && |
| 2422 | !isCFStringType(Ty, S.Context) && |
| 2423 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2424 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2425 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2426 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2427 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2428 | << IdxExpr->getSourceRange(); |
| 2429 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2430 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2431 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2432 | if (!isNSStringType(Ty, S.Context) && |
| 2433 | !isCFStringType(Ty, S.Context) && |
| 2434 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2435 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2436 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2437 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2438 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2439 | << IdxExpr->getSourceRange(); |
| 2440 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2443 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2444 | // because that has corrected for the implicit this parameter, and is zero- |
| 2445 | // based. The attribute expects what the user wrote explicitly. |
| 2446 | llvm::APSInt Val; |
| 2447 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2448 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2449 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2450 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2451 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2454 | enum FormatAttrKind { |
| 2455 | CFStringFormat, |
| 2456 | NSStringFormat, |
| 2457 | StrftimeFormat, |
| 2458 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2459 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2460 | InvalidFormat |
| 2461 | }; |
| 2462 | |
| 2463 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2464 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2465 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2466 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2467 | // Check for formats that get handled specially. |
| 2468 | .Case("NSString", NSStringFormat) |
| 2469 | .Case("CFString", CFStringFormat) |
| 2470 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2471 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2472 | // Otherwise, check for supported formats. |
| 2473 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2474 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2475 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2476 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2477 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2478 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2479 | } |
| 2480 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2481 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2482 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2483 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2484 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2485 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2486 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2487 | return; |
| 2488 | } |
| 2489 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2490 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2491 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2492 | Attr.setInvalid(); |
| 2493 | return; |
| 2494 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2495 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2496 | if (S.Context.getAsArrayType(T)) |
| 2497 | T = S.Context.getBaseElementType(T); |
| 2498 | if (!T->getAs<RecordType>()) { |
| 2499 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2500 | Attr.setInvalid(); |
| 2501 | return; |
| 2502 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2503 | |
| 2504 | Expr *E = Attr.getArgAsExpr(0); |
| 2505 | uint32_t prioritynum; |
| 2506 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2507 | Attr.setInvalid(); |
| 2508 | return; |
| 2509 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2510 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2511 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2512 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2513 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2514 | Attr.setInvalid(); |
| 2515 | return; |
| 2516 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2517 | D->addAttr(::new (S.Context) |
| 2518 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2519 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2522 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2523 | IdentifierInfo *Format, int FormatIdx, |
| 2524 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2525 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2526 | // Check whether we already have an equivalent format attribute. |
| 2527 | for (specific_attr_iterator<FormatAttr> |
| 2528 | i = D->specific_attr_begin<FormatAttr>(), |
| 2529 | e = D->specific_attr_end<FormatAttr>(); |
| 2530 | i != e ; ++i) { |
| 2531 | FormatAttr *f = *i; |
| 2532 | if (f->getType() == Format && |
| 2533 | f->getFormatIdx() == FormatIdx && |
| 2534 | f->getFirstArg() == FirstArg) { |
| 2535 | // If we don't have a valid location for this attribute, adopt the |
| 2536 | // location. |
| 2537 | if (f->getLocation().isInvalid()) |
| 2538 | f->setRange(Range); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2539 | return NULL; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2540 | } |
| 2541 | } |
| 2542 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2543 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2544 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2547 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2548 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2549 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2550 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2551 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2552 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2553 | return; |
| 2554 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2555 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2556 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2557 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2558 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2559 | return; |
| 2560 | } |
| 2561 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2562 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2563 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2564 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2565 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2566 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2567 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2568 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2569 | |
| 2570 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2571 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2572 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2573 | // If we've modified the string name, we need a new identifier for it. |
| 2574 | II = &S.Context.Idents.get(Format); |
| 2575 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2576 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2577 | // Check for supported formats. |
| 2578 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2579 | |
| 2580 | if (Kind == IgnoredFormat) |
| 2581 | return; |
| 2582 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2583 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2584 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 2585 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2586 | return; |
| 2587 | } |
| 2588 | |
| 2589 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2590 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2591 | uint32_t Idx; |
| 2592 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2593 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2594 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2595 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2596 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2597 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2598 | return; |
| 2599 | } |
| 2600 | |
| 2601 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2602 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2603 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2604 | if (HasImplicitThisParam) { |
| 2605 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2606 | S.Diag(Attr.getLoc(), |
| 2607 | diag::err_format_attribute_implicit_this_format_string) |
| 2608 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2609 | return; |
| 2610 | } |
| 2611 | ArgIdx--; |
| 2612 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2614 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2615 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2616 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2617 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2618 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2619 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2620 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2621 | return; |
| 2622 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2623 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2624 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2625 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2626 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2627 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2628 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2629 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2630 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2631 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2632 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2633 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2634 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2635 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2636 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2637 | return; |
| 2638 | } |
| 2639 | |
| 2640 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2641 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2642 | uint32_t FirstArg; |
| 2643 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2644 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2645 | |
| 2646 | // check if the function is variadic if the 3rd argument non-zero |
| 2647 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2648 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2649 | ++NumArgs; // +1 for ... |
| 2650 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2651 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2652 | return; |
| 2653 | } |
| 2654 | } |
| 2655 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2656 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2657 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2658 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2659 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2660 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2661 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2662 | return; |
| 2663 | } |
| 2664 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2665 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2666 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2667 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2668 | return; |
| 2669 | } |
| 2670 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2671 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2672 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2673 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2674 | if (NewAttr) |
| 2675 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2678 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2679 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2680 | // Try to find the underlying union declaration. |
| 2681 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2682 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2683 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2684 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2685 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2686 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2687 | |
| 2688 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2689 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2690 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2691 | return; |
| 2692 | } |
| 2693 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2694 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2695 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2696 | diag::warn_transparent_union_attribute_not_definition); |
| 2697 | return; |
| 2698 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2699 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2700 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2701 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2702 | if (Field == FieldEnd) { |
| 2703 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2704 | return; |
| 2705 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2706 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2707 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2708 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2709 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2710 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2711 | diag::warn_transparent_union_attribute_floating) |
| 2712 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2713 | return; |
| 2714 | } |
| 2715 | |
| 2716 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2717 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2718 | for (; Field != FieldEnd; ++Field) { |
| 2719 | QualType FieldType = Field->getType(); |
| 2720 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 2721 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 2722 | // Warn if we drop the attribute. |
| 2723 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2724 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2725 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2726 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2727 | diag::warn_transparent_union_attribute_field_size_align) |
| 2728 | << isSize << Field->getDeclName() << FieldBits; |
| 2729 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2730 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2731 | diag::note_transparent_union_first_field_size_align) |
| 2732 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2733 | return; |
| 2734 | } |
| 2735 | } |
| 2736 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2737 | RD->addAttr(::new (S.Context) |
| 2738 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2739 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2742 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2743 | // Make sure that there is a string literal as the annotation's single |
| 2744 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2745 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2746 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2747 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2748 | |
| 2749 | // Don't duplicate annotations that are already set. |
| 2750 | for (specific_attr_iterator<AnnotateAttr> |
| 2751 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 2752 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2753 | if ((*i)->getAnnotation() == Str) |
| 2754 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2755 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2756 | |
| 2757 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2758 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2759 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2762 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2763 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2764 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2765 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2766 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2767 | return; |
| 2768 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2769 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2770 | if (Attr.getNumArgs() == 0) { |
| 2771 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 2772 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 2773 | return; |
| 2774 | } |
| 2775 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2776 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2777 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 2778 | S.Diag(Attr.getEllipsisLoc(), |
| 2779 | diag::err_pack_expansion_without_parameter_packs); |
| 2780 | return; |
| 2781 | } |
| 2782 | |
| 2783 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 2784 | return; |
| 2785 | |
| 2786 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 2787 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2791 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2792 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 2793 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 2794 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2795 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2796 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2797 | // C++11 [dcl.align]p1: |
| 2798 | // An alignment-specifier may be applied to a variable or to a class |
| 2799 | // data member, but it shall not be applied to a bit-field, a function |
| 2800 | // parameter, the formal parameter of a catch clause, or a variable |
| 2801 | // declared with the register storage class specifier. An |
| 2802 | // alignment-specifier may also be applied to the declaration of a class |
| 2803 | // or enumeration type. |
| 2804 | // C11 6.7.5/2: |
| 2805 | // An alignment attribute shall not be specified in a declaration of |
| 2806 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 2807 | // object declared with the register storage-class specifier. |
| 2808 | int DiagKind = -1; |
| 2809 | if (isa<ParmVarDecl>(D)) { |
| 2810 | DiagKind = 0; |
| 2811 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2812 | if (VD->getStorageClass() == SC_Register) |
| 2813 | DiagKind = 1; |
| 2814 | if (VD->isExceptionVariable()) |
| 2815 | DiagKind = 2; |
| 2816 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 2817 | if (FD->isBitField()) |
| 2818 | DiagKind = 3; |
| 2819 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2820 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 2821 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 2822 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2823 | return; |
| 2824 | } |
| 2825 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2826 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 2827 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 2828 | return; |
| 2829 | } |
| 2830 | } |
| 2831 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2832 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 2833 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2834 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 2835 | AA->setPackExpansion(IsPackExpansion); |
| 2836 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2837 | return; |
| 2838 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2839 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2840 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2841 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 2842 | ExprResult ICE |
| 2843 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 2844 | diag::err_aligned_attribute_argument_not_int, |
| 2845 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 2846 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 2847 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2848 | |
| 2849 | // C++11 [dcl.align]p2: |
| 2850 | // -- if the constant expression evaluates to zero, the alignment |
| 2851 | // specifier shall have no effect |
| 2852 | // C11 6.7.5p6: |
| 2853 | // An alignment specification of zero has no effect. |
| 2854 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 2855 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 2856 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 2857 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2858 | return; |
| 2859 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2860 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2861 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2862 | // We've already verified it's a power of 2, now let's make sure it's |
| 2863 | // 8192 or less. |
| 2864 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2865 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2866 | << E->getSourceRange(); |
| 2867 | return; |
| 2868 | } |
| 2869 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 2870 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2871 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 2872 | ICE.take(), SpellingListIndex); |
| 2873 | AA->setPackExpansion(IsPackExpansion); |
| 2874 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2875 | } |
| 2876 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 2877 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2878 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2879 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 2880 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2881 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 2882 | SpellingListIndex); |
| 2883 | AA->setPackExpansion(IsPackExpansion); |
| 2884 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2885 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2886 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2887 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 2888 | assert(D->hasAttrs() && "no attributes on decl"); |
| 2889 | |
| 2890 | QualType Ty; |
| 2891 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2892 | Ty = VD->getType(); |
| 2893 | else |
| 2894 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 3653b7e | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 2895 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2896 | return; |
| 2897 | |
| 2898 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 2899 | // The combined effect of all alignment attributes in a declaration shall |
| 2900 | // not specify an alignment that is less strict than the alignment that |
| 2901 | // would otherwise be required for the entity being declared. |
| 2902 | AlignedAttr *AlignasAttr = 0; |
| 2903 | unsigned Align = 0; |
| 2904 | for (specific_attr_iterator<AlignedAttr> |
| 2905 | I = D->specific_attr_begin<AlignedAttr>(), |
| 2906 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 2907 | if (I->isAlignmentDependent()) |
| 2908 | return; |
| 2909 | if (I->isAlignas()) |
| 2910 | AlignasAttr = *I; |
| 2911 | Align = std::max(Align, I->getAlignment(Context)); |
| 2912 | } |
| 2913 | |
| 2914 | if (AlignasAttr && Align) { |
| 2915 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 2916 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 2917 | if (NaturalAlign > RequestedAlign) |
| 2918 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 2919 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 2920 | } |
| 2921 | } |
| 2922 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 2923 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2924 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2925 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2926 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 2927 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 2928 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2929 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2930 | // This attribute isn't documented, but glibc uses it. It changes |
| 2931 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2932 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 2933 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 2934 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2935 | return; |
| 2936 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2937 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2938 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 2939 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2940 | |
| 2941 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2942 | if (Str.startswith("__") && Str.endswith("__")) |
| 2943 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2944 | |
| 2945 | unsigned DestWidth = 0; |
| 2946 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2947 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2948 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2949 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2950 | switch (Str[0]) { |
| 2951 | case 'Q': DestWidth = 8; break; |
| 2952 | case 'H': DestWidth = 16; break; |
| 2953 | case 'S': DestWidth = 32; break; |
| 2954 | case 'D': DestWidth = 64; break; |
| 2955 | case 'X': DestWidth = 96; break; |
| 2956 | case 'T': DestWidth = 128; break; |
| 2957 | } |
| 2958 | if (Str[1] == 'F') { |
| 2959 | IntegerMode = false; |
| 2960 | } else if (Str[1] == 'C') { |
| 2961 | IntegerMode = false; |
| 2962 | ComplexMode = true; |
| 2963 | } else if (Str[1] != 'I') { |
| 2964 | DestWidth = 0; |
| 2965 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2966 | break; |
| 2967 | case 4: |
| 2968 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 2969 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2970 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2971 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2972 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2973 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2974 | break; |
| 2975 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 2976 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2977 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2978 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2979 | case 11: |
| 2980 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 2981 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 2982 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2983 | } |
| 2984 | |
| 2985 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2986 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2987 | OldTy = TD->getUnderlyingType(); |
| 2988 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 2989 | OldTy = VD->getType(); |
| 2990 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2991 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Aaron Ballman | 2dfb03f | 2013-12-27 16:30:46 +0000 | [diff] [blame] | 2992 | << Attr.getName() << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 2993 | return; |
| 2994 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2995 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2996 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 2997 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 2998 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 2999 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3000 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3001 | } else if (ComplexMode) { |
| 3002 | if (!OldTy->isComplexType()) |
| 3003 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3004 | } else { |
| 3005 | if (!OldTy->isFloatingType()) |
| 3006 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3007 | } |
| 3008 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3009 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3010 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3011 | // FIXME: Make sure floating-point mappings are accurate |
| 3012 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3013 | if (!DestWidth) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 3014 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3015 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | QualType NewTy; |
| 3019 | |
| 3020 | if (IntegerMode) |
| 3021 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 3022 | OldTy->isSignedIntegerType()); |
| 3023 | else |
| 3024 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 3025 | |
| 3026 | if (NewTy.isNull()) { |
Aaron Ballman | 0390908 | 2013-12-23 15:23:11 +0000 | [diff] [blame] | 3027 | S.Diag(Attr.getLoc(), diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3028 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3029 | } |
| 3030 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3031 | if (ComplexMode) { |
| 3032 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3036 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3037 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3038 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3039 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3040 | |
| 3041 | D->addAttr(::new (S.Context) |
| 3042 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3043 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3044 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3045 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3046 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3047 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3048 | if (!VD->hasGlobalStorage()) |
| 3049 | S.Diag(Attr.getLoc(), |
| 3050 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3051 | << Attr.getName(); |
| 3052 | } else if (!isFunctionOrMethod(D)) { |
| 3053 | S.Diag(Attr.getLoc(), |
| 3054 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3055 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3056 | return; |
| 3057 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3058 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3059 | D->addAttr(::new (S.Context) |
| 3060 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3061 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3064 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3065 | FunctionDecl *FD = cast<FunctionDecl>(D); |
| 3066 | if (!FD->getResultType()->isVoidType()) { |
| 3067 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
| 3068 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
| 3069 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3070 | << FD->getType() |
| 3071 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
| 3072 | "void"); |
| 3073 | } else { |
| 3074 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3075 | << FD->getType(); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3076 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3077 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3078 | } |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3079 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3080 | D->addAttr(::new (S.Context) |
| 3081 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3082 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3083 | } |
| 3084 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3085 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3086 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3087 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3088 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3089 | return; |
| 3090 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3091 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3092 | D->addAttr(::new (S.Context) |
| 3093 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3094 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3095 | } |
| 3096 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3097 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3098 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3099 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3100 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3101 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3102 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3103 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3104 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3105 | return; |
| 3106 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3107 | if (!isa<ObjCMethodDecl>(D)) { |
| 3108 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3109 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3110 | return; |
| 3111 | } |
| 3112 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3113 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3114 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3115 | D->addAttr(::new (S.Context) |
| 3116 | FastCallAttr(Attr.getRange(), S.Context, |
| 3117 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3118 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3119 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3120 | D->addAttr(::new (S.Context) |
| 3121 | StdCallAttr(Attr.getRange(), S.Context, |
| 3122 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3123 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3124 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3125 | D->addAttr(::new (S.Context) |
| 3126 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3127 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3128 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3129 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3130 | D->addAttr(::new (S.Context) |
| 3131 | CDeclAttr(Attr.getRange(), S.Context, |
| 3132 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3133 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3134 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3135 | D->addAttr(::new (S.Context) |
| 3136 | PascalAttr(Attr.getRange(), S.Context, |
| 3137 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3138 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3139 | case AttributeList::AT_MSABI: |
| 3140 | D->addAttr(::new (S.Context) |
| 3141 | MSABIAttr(Attr.getRange(), S.Context, |
| 3142 | Attr.getAttributeSpellingListIndex())); |
| 3143 | return; |
| 3144 | case AttributeList::AT_SysVABI: |
| 3145 | D->addAttr(::new (S.Context) |
| 3146 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3147 | Attr.getAttributeSpellingListIndex())); |
| 3148 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3149 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3150 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3151 | switch (CC) { |
| 3152 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3153 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3154 | break; |
| 3155 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3156 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3157 | break; |
| 3158 | default: |
| 3159 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3162 | D->addAttr(::new (S.Context) |
| 3163 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3164 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3165 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3166 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3167 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3168 | D->addAttr(::new (S.Context) |
| 3169 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3170 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3171 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3172 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3173 | D->addAttr(::new (S.Context) |
| 3174 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3175 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3176 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3177 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3178 | default: |
| 3179 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3180 | } |
| 3181 | } |
| 3182 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3183 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3184 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3185 | if (attr.isInvalid()) |
| 3186 | return true; |
| 3187 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3188 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3189 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3190 | attr.setInvalid(); |
| 3191 | return true; |
| 3192 | } |
| 3193 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3194 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3195 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3196 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3197 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3198 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3199 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3200 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3201 | case AttributeList::AT_MSABI: |
| 3202 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3203 | CC_X86_64Win64; |
| 3204 | break; |
| 3205 | case AttributeList::AT_SysVABI: |
| 3206 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3207 | CC_C; |
| 3208 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3209 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3210 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3211 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3212 | attr.setInvalid(); |
| 3213 | return true; |
| 3214 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3215 | if (StrRef == "aapcs") { |
| 3216 | CC = CC_AAPCS; |
| 3217 | break; |
| 3218 | } else if (StrRef == "aapcs-vfp") { |
| 3219 | CC = CC_AAPCS_VFP; |
| 3220 | break; |
| 3221 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3222 | |
| 3223 | attr.setInvalid(); |
| 3224 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3225 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3226 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3227 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3228 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3229 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3232 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3233 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3234 | if (A == TargetInfo::CCCR_Warning) { |
| 3235 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3236 | |
| 3237 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3238 | if (FD) |
| 3239 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3240 | TargetInfo::CCMT_NonMember; |
| 3241 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3244 | return false; |
| 3245 | } |
| 3246 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3247 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3248 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3249 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3250 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3251 | return true; |
| 3252 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3253 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3254 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3255 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3256 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3257 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3258 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3259 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3260 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3261 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3262 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3265 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3266 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3267 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3268 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3269 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3270 | } |
| 3271 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3272 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3273 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3274 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3275 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3276 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3277 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3280 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3283 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 3284 | const AttributeList &Attr) { |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3285 | // check the attribute arguments. |
| 3286 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
| 3287 | // FIXME: 0 is not okay. |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 3288 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3289 | << Attr.getName() << 2; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3290 | return; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3291 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3292 | |
| 3293 | if (!isFunctionOrMethod(D)) { |
| 3294 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3295 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3296 | return; |
| 3297 | } |
| 3298 | |
Aaron Ballman | 6603993 | 2013-12-19 00:41:31 +0000 | [diff] [blame] | 3299 | uint32_t MaxThreads, MinBlocks = 0; |
| 3300 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1)) |
| 3301 | return; |
| 3302 | if (Attr.getNumArgs() > 1 && !checkUInt32Argument(S, Attr, |
| 3303 | Attr.getArgAsExpr(1), |
| 3304 | MinBlocks, 2)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3305 | return; |
| 3306 | |
| 3307 | D->addAttr(::new (S.Context) |
| 3308 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
| 3309 | MaxThreads, MinBlocks, |
| 3310 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3313 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3314 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3315 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3316 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3317 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3318 | return; |
| 3319 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3320 | |
| 3321 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3322 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3323 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3324 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3325 | |
| 3326 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3327 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3328 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3329 | return; |
| 3330 | } |
| 3331 | |
| 3332 | uint64_t ArgumentIdx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3333 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 3334 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3335 | return; |
| 3336 | |
| 3337 | uint64_t TypeTagIdx; |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3338 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 3339 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3340 | return; |
| 3341 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3342 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3343 | if (IsPointer) { |
| 3344 | // Ensure that buffer has a pointer type. |
| 3345 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 3346 | if (!BufferTy->isPointerType()) { |
| 3347 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3348 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3349 | } |
| 3350 | } |
| 3351 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3352 | D->addAttr(::new (S.Context) |
| 3353 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3354 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3355 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3359 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3360 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3361 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3362 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3363 | return; |
| 3364 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3365 | |
| 3366 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3367 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3368 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3369 | if (!isa<VarDecl>(D)) { |
| 3370 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3371 | << Attr.getName() << ExpectedVariable; |
| 3372 | return; |
| 3373 | } |
| 3374 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3375 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3376 | TypeSourceInfo *MatchingCTypeLoc = 0; |
| 3377 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3378 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3379 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3380 | D->addAttr(::new (S.Context) |
| 3381 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3382 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3383 | Attr.getLayoutCompatible(), |
| 3384 | Attr.getMustBeNull(), |
| 3385 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3386 | } |
| 3387 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3388 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3389 | // Checker-specific attribute handlers. |
| 3390 | //===----------------------------------------------------------------------===// |
| 3391 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3392 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3393 | return type->isDependentType() || |
| 3394 | type->isObjCObjectPointerType() || |
| 3395 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3396 | } |
| 3397 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3398 | return type->isDependentType() || |
| 3399 | type->isPointerType() || |
| 3400 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3401 | } |
| 3402 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3403 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3404 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3405 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3406 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3407 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3408 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3409 | cf = false; |
| 3410 | } else { |
| 3411 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3412 | cf = true; |
| 3413 | } |
| 3414 | |
| 3415 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3416 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3417 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3418 | return; |
| 3419 | } |
| 3420 | |
| 3421 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3422 | param->addAttr(::new (S.Context) |
| 3423 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3424 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3425 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3426 | param->addAttr(::new (S.Context) |
| 3427 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3428 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3431 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3432 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3433 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3434 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3435 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3436 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3437 | returnType = MD->getResultType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3438 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3439 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3440 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3441 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3442 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3443 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3444 | returnType = FD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3445 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3446 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3447 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3448 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3449 | return; |
| 3450 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3451 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3452 | bool typeOK; |
| 3453 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3454 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3455 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3456 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3457 | case AttributeList::AT_NSReturnsRetained: |
| 3458 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3459 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3460 | cf = false; |
| 3461 | break; |
| 3462 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3463 | case AttributeList::AT_CFReturnsRetained: |
| 3464 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3465 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3466 | cf = true; |
| 3467 | break; |
| 3468 | } |
| 3469 | |
| 3470 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3471 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3472 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3473 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3474 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3475 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3476 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3477 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3478 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3479 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3480 | D->addAttr(::new (S.Context) |
| 3481 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 3482 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3483 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3484 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3485 | D->addAttr(::new (S.Context) |
| 3486 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3487 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3488 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3489 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3490 | D->addAttr(::new (S.Context) |
| 3491 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3492 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3493 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3494 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3495 | D->addAttr(::new (S.Context) |
| 3496 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3497 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3498 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3499 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3500 | D->addAttr(::new (S.Context) |
| 3501 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3502 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3503 | return; |
| 3504 | }; |
| 3505 | } |
| 3506 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3507 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3508 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 3509 | const int EP_ObjCMethod = 1; |
| 3510 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3511 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3512 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3513 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3514 | if (isa<ObjCMethodDecl>(D)) |
| 3515 | resultType = cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3516 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3517 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3518 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3519 | if (!resultType->isReferenceType() && |
| 3520 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3521 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3522 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3523 | << attr.getName() |
| 3524 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3525 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3526 | |
| 3527 | // Drop the attribute. |
| 3528 | return; |
| 3529 | } |
| 3530 | |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3531 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3532 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 3533 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3536 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3537 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3538 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3539 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3540 | DeclContext *DC = method->getDeclContext(); |
| 3541 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3542 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3543 | << attr.getName() << 0; |
| 3544 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3545 | return; |
| 3546 | } |
| 3547 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3548 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3549 | << attr.getName() << 1; |
| 3550 | return; |
| 3551 | } |
| 3552 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3553 | method->addAttr(::new (S.Context) |
| 3554 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 3555 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3556 | } |
| 3557 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3558 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 3559 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3560 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr)) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3561 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3562 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3563 | D->addAttr(::new (S.Context) |
| 3564 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 3565 | Attr.getAttributeSpellingListIndex())); |
| 3566 | } |
| 3567 | |
| 3568 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 3569 | const AttributeList &Attr) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 3570 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr)) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 3571 | return; |
| 3572 | |
| 3573 | D->addAttr(::new (S.Context) |
| 3574 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 3575 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3578 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 3579 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3580 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3581 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3582 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3583 | 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] | 3584 | return; |
| 3585 | } |
| 3586 | |
| 3587 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3588 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3589 | Attr.getAttributeSpellingListIndex())); |
| 3590 | } |
| 3591 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3592 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 3593 | const AttributeList &Attr) { |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3594 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3595 | |
| 3596 | if (!Parm) { |
| 3597 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3598 | return; |
| 3599 | } |
| 3600 | |
| 3601 | D->addAttr(::new (S.Context) |
| 3602 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 3603 | Attr.getAttributeSpellingListIndex())); |
| 3604 | } |
| 3605 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 3606 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3607 | const AttributeList &Attr) { |
| 3608 | IdentifierInfo *RelatedClass = |
| 3609 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : 0; |
| 3610 | if (!RelatedClass) { |
| 3611 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3612 | return; |
| 3613 | } |
| 3614 | IdentifierInfo *ClassMethod = |
| 3615 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : 0; |
| 3616 | IdentifierInfo *InstanceMethod = |
| 3617 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : 0; |
| 3618 | D->addAttr(::new (S.Context) |
| 3619 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 3620 | ClassMethod, InstanceMethod, |
| 3621 | Attr.getAttributeSpellingListIndex())); |
| 3622 | } |
| 3623 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3624 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 3625 | const AttributeList &Attr) { |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3626 | ObjCInterfaceDecl *IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 3627 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 3628 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 3629 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 3630 | Attr.getAttributeSpellingListIndex())); |
| 3631 | } |
| 3632 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3633 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3634 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3635 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3636 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3637 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3638 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3639 | } |
| 3640 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3641 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3642 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3643 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3644 | QualType type = vd->getType(); |
| 3645 | |
| 3646 | if (!type->isDependentType() && |
| 3647 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3648 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3649 | << type; |
| 3650 | return; |
| 3651 | } |
| 3652 | |
| 3653 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3654 | |
| 3655 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3656 | // going to infer. |
| 3657 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3658 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3659 | |
| 3660 | switch (lifetime) { |
| 3661 | case Qualifiers::OCL_None: |
| 3662 | assert(type->isDependentType() && |
| 3663 | "didn't infer lifetime for non-dependent type?"); |
| 3664 | break; |
| 3665 | |
| 3666 | case Qualifiers::OCL_Weak: // meaningful |
| 3667 | case Qualifiers::OCL_Strong: // meaningful |
| 3668 | break; |
| 3669 | |
| 3670 | case Qualifiers::OCL_ExplicitNone: |
| 3671 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3672 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3673 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 3674 | break; |
| 3675 | } |
| 3676 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3677 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3678 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 3679 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3682 | //===----------------------------------------------------------------------===// |
| 3683 | // Microsoft specific attribute handlers. |
| 3684 | //===----------------------------------------------------------------------===// |
| 3685 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3686 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 3687 | if (!S.LangOpts.CPlusPlus) { |
| 3688 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 3689 | << Attr.getName() << AttributeLangSupport::C; |
| 3690 | return; |
| 3691 | } |
| 3692 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 3693 | if (!isa<CXXRecordDecl>(D)) { |
| 3694 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3695 | << Attr.getName() << ExpectedClass; |
| 3696 | return; |
| 3697 | } |
| 3698 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3699 | StringRef StrRef; |
| 3700 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3701 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3702 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3703 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3704 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 3705 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3706 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 3707 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3708 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3709 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3710 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3711 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3712 | return; |
| 3713 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 3714 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3715 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3716 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3717 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3718 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3719 | return; |
| 3720 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3721 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3722 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3723 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 3724 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 3725 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 3726 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 3727 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 3728 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3731 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 3732 | const AttributeList &Attr) { |
| 3733 | // Check the attribute arguments. |
| 3734 | if (Attr.getNumArgs() > 1) { |
| 3735 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 3736 | << Attr.getName() << 1; |
| 3737 | return; |
| 3738 | } |
| 3739 | |
| 3740 | StringRef Str; |
| 3741 | SourceLocation ArgLoc; |
| 3742 | |
| 3743 | if (Attr.getNumArgs() == 0) |
| 3744 | Str = ""; |
| 3745 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 3746 | return; |
| 3747 | |
| 3748 | ARMInterruptAttr::InterruptType Kind; |
| 3749 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 3750 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 3751 | << Attr.getName() << Str << ArgLoc; |
| 3752 | return; |
| 3753 | } |
| 3754 | |
| 3755 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3756 | D->addAttr(::new (S.Context) |
| 3757 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 3758 | } |
| 3759 | |
| 3760 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 3761 | const AttributeList &Attr) { |
| 3762 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3763 | return; |
| 3764 | |
| 3765 | if (!Attr.isArgExpr(0)) { |
| 3766 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3767 | << AANT_ArgumentIntegerConstant; |
| 3768 | return; |
| 3769 | } |
| 3770 | |
| 3771 | // FIXME: Check for decl - it should be void ()(void). |
| 3772 | |
| 3773 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 3774 | llvm::APSInt NumParams(32); |
| 3775 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 3776 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 3777 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 3778 | << NumParamsExpr->getSourceRange(); |
| 3779 | return; |
| 3780 | } |
| 3781 | |
| 3782 | unsigned Num = NumParams.getLimitedValue(255); |
| 3783 | if ((Num & 1) || Num > 30) { |
| 3784 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 3785 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 3786 | << NumParamsExpr->getSourceRange(); |
| 3787 | return; |
| 3788 | } |
| 3789 | |
| 3790 | D->addAttr(::new (S.Context) MSP430InterruptAttr(Attr.getLoc(), S.Context, Num)); |
| 3791 | D->addAttr(::new (S.Context) UsedAttr(Attr.getLoc(), S.Context)); |
| 3792 | } |
| 3793 | |
| 3794 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3795 | // Dispatch the interrupt attribute based on the current target. |
| 3796 | if (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::msp430) |
| 3797 | handleMSP430InterruptAttr(S, D, Attr); |
| 3798 | else |
| 3799 | handleARMInterruptAttr(S, D, Attr); |
| 3800 | } |
| 3801 | |
| 3802 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 3803 | const AttributeList& Attr) { |
| 3804 | // If we try to apply it to a function pointer, don't warn, but don't |
| 3805 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 3806 | // special about calling a force_align_arg_pointer function. |
| 3807 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 3808 | if (VD && VD->getType()->isFunctionPointerType()) |
| 3809 | return; |
| 3810 | // Also don't warn on function pointer typedefs. |
| 3811 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 3812 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 3813 | TD->getUnderlyingType()->isFunctionType())) |
| 3814 | return; |
| 3815 | // Attribute can only be applied to function types. |
| 3816 | if (!isa<FunctionDecl>(D)) { |
| 3817 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3818 | << Attr.getName() << /* function */0; |
| 3819 | return; |
| 3820 | } |
| 3821 | |
| 3822 | D->addAttr(::new (S.Context) X86ForceAlignArgPointerAttr(Attr.getRange(), |
| 3823 | S.Context)); |
| 3824 | } |
| 3825 | |
| 3826 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 3827 | unsigned AttrSpellingListIndex) { |
| 3828 | if (D->hasAttr<DLLExportAttr>()) { |
| 3829 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "dllimport"; |
| 3830 | return NULL; |
| 3831 | } |
| 3832 | |
| 3833 | if (D->hasAttr<DLLImportAttr>()) |
| 3834 | return NULL; |
| 3835 | |
| 3836 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3837 | if (VD->hasDefinition()) { |
| 3838 | // dllimport cannot be applied to definitions. |
| 3839 | Diag(D->getLocation(), diag::warn_attribute_invalid_on_definition) |
| 3840 | << "dllimport"; |
| 3841 | return NULL; |
| 3842 | } |
| 3843 | } |
| 3844 | |
| 3845 | return ::new (Context)DLLImportAttr(Range, Context, |
| 3846 | AttrSpellingListIndex); |
| 3847 | } |
| 3848 | |
| 3849 | static void handleDLLImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3850 | // Attribute can be applied only to functions or variables. |
| 3851 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 3852 | if (!FD && !isa<VarDecl>(D)) { |
| 3853 | // Apparently Visual C++ thinks it is okay to not emit a warning |
| 3854 | // in this case, so only emit a warning when -fms-extensions is not |
| 3855 | // specified. |
| 3856 | if (!S.getLangOpts().MicrosoftExt) |
| 3857 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3858 | << Attr.getName() << 2 /*variable and function*/; |
| 3859 | return; |
| 3860 | } |
| 3861 | |
| 3862 | // Currently, the dllimport attribute is ignored for inlined functions. |
| 3863 | // Warning is emitted. |
| 3864 | if (FD && FD->isInlineSpecified()) { |
| 3865 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3866 | return; |
| 3867 | } |
| 3868 | |
| 3869 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3870 | DLLImportAttr *NewAttr = S.mergeDLLImportAttr(D, Attr.getRange(), Index); |
| 3871 | if (NewAttr) |
| 3872 | D->addAttr(NewAttr); |
| 3873 | } |
| 3874 | |
| 3875 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 3876 | unsigned AttrSpellingListIndex) { |
| 3877 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
| 3878 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << "dllimport"; |
| 3879 | D->dropAttr<DLLImportAttr>(); |
| 3880 | } |
| 3881 | |
| 3882 | if (D->hasAttr<DLLExportAttr>()) |
| 3883 | return NULL; |
| 3884 | |
| 3885 | return ::new (Context)DLLExportAttr(Range, Context, |
| 3886 | AttrSpellingListIndex); |
| 3887 | } |
| 3888 | |
| 3889 | static void handleDLLExportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3890 | // Currently, the dllexport attribute is ignored for inlined functions, unless |
| 3891 | // the -fkeep-inline-functions flag has been used. Warning is emitted; |
| 3892 | if (isa<FunctionDecl>(D) && cast<FunctionDecl>(D)->isInlineSpecified()) { |
| 3893 | // FIXME: ... unless the -fkeep-inline-functions flag has been used. |
| 3894 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3895 | return; |
| 3896 | } |
| 3897 | |
| 3898 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3899 | DLLExportAttr *NewAttr = S.mergeDLLExportAttr(D, Attr.getRange(), Index); |
| 3900 | if (NewAttr) |
| 3901 | D->addAttr(NewAttr); |
| 3902 | } |
| 3903 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3904 | /// Handles semantic checking for features that are common to all attributes, |
| 3905 | /// such as checking whether a parameter was properly specified, or the correct |
| 3906 | /// number of arguments were passed, etc. |
| 3907 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 3908 | const AttributeList &Attr) { |
| 3909 | // Several attributes carry different semantics than the parsing requires, so |
| 3910 | // those are opted out of the common handling. |
| 3911 | // |
| 3912 | // We also bail on unknown and ignored attributes because those are handled |
| 3913 | // as part of the target-specific handling logic. |
| 3914 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3915 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3916 | return false; |
| 3917 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3918 | // Check whether the attribute requires specific language extensions to be |
| 3919 | // enabled. |
| 3920 | if (!Attr.diagnoseLangOpts(S)) |
| 3921 | return true; |
| 3922 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3923 | // If there are no optional arguments, then checking for the argument count |
| 3924 | // is trivial. |
| 3925 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 3926 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 3927 | return true; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3928 | |
| 3929 | // Check whether the attribute appertains to the given subject. |
| 3930 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 3931 | return true; |
| 3932 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3933 | return false; |
| 3934 | } |
| 3935 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3936 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3937 | // Top Level Sema Entry Points |
| 3938 | //===----------------------------------------------------------------------===// |
| 3939 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3940 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 3941 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 3942 | /// silently ignore it if a GNU attribute. |
| 3943 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 3944 | const AttributeList &Attr, |
| 3945 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3946 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3947 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3948 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 3949 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 3950 | // instead. |
| 3951 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 3952 | return; |
| 3953 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3954 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 3955 | // which do not apply to the current target architecture are treated as |
| 3956 | // though they were unknown attributes. |
| 3957 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
| 3958 | !Attr.existsInTarget(S.Context.getTargetInfo().getTriple())) { |
| 3959 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 3960 | diag::warn_unhandled_ms_attribute_ignored : |
| 3961 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
| 3962 | return; |
| 3963 | } |
| 3964 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 3965 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 3966 | return; |
| 3967 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3968 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3969 | default: |
| 3970 | // Type attributes are handled elsewhere; silently move on. |
| 3971 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
| 3972 | break; |
| 3973 | case AttributeList::AT_Interrupt: |
| 3974 | handleInterruptAttr(S, D, Attr); break; |
| 3975 | case AttributeList::AT_X86ForceAlignArgPointer: |
| 3976 | handleX86ForceAlignArgPointerAttr(S, D, Attr); break; |
| 3977 | case AttributeList::AT_DLLExport: |
| 3978 | handleDLLExportAttr(S, D, Attr); break; |
| 3979 | case AttributeList::AT_DLLImport: |
| 3980 | handleDLLImportAttr(S, D, Attr); break; |
| 3981 | case AttributeList::AT_Mips16: |
| 3982 | handleSimpleAttribute<Mips16Attr>(S, D, Attr); break; |
| 3983 | case AttributeList::AT_NoMips16: |
| 3984 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 3985 | case AttributeList::AT_IBAction: |
| 3986 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 3987 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 3988 | case AttributeList::AT_IBOutletCollection: |
| 3989 | handleIBOutletCollection(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3990 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 3991 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3992 | case AttributeList::AT_AlwaysInline: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 3993 | handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3994 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3995 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 3996 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3997 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 3998 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 3999 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4000 | handleDependencyAttr(S, scope, D, Attr); |
| 4001 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4002 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4003 | case AttributeList::AT_CUDAConstant: |
| 4004 | handleSimpleAttribute<CUDAConstantAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4005 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4006 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 3a8e2d9 | 2013-11-27 18:53:58 +0000 | [diff] [blame] | 4007 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4008 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4009 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4010 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4011 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 4012 | case AttributeList::AT_EnableIf: handleEnableIfAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4013 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4014 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4015 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4016 | case AttributeList::AT_MinSize: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4017 | handleSimpleAttribute<MinSizeAttr>(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4018 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4019 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4020 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4021 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 4022 | case AttributeList::AT_CUDADevice: |
| 4023 | handleSimpleAttribute<CUDADeviceAttr>(S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4024 | case AttributeList::AT_CUDAHost: |
| 4025 | handleSimpleAttribute<CUDAHostAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4026 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4027 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4028 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4029 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4030 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4031 | case AttributeList::AT_MayAlias: |
| 4032 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break; |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4033 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4034 | case AttributeList::AT_NoCommon: |
| 4035 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4036 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4037 | case AttributeList::AT_Overloadable: |
| 4038 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 4039 | case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4040 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4041 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4042 | case AttributeList::AT_Naked: |
| 4043 | handleSimpleAttribute<NakedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4044 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4045 | case AttributeList::AT_NoThrow: |
| 4046 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4047 | case AttributeList::AT_CUDAShared: |
| 4048 | handleSimpleAttribute<CUDASharedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4049 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4050 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4051 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4052 | handleObjCOwnershipAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4053 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4054 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4055 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4056 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4057 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4058 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4059 | case AttributeList::AT_ObjCRequiresSuper: |
| 4060 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4061 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4062 | case AttributeList::AT_ObjCBridge: |
| 4063 | handleObjCBridgeAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4064 | |
| 4065 | case AttributeList::AT_ObjCBridgeMutable: |
| 4066 | handleObjCBridgeMutableAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4067 | |
| 4068 | case AttributeList::AT_ObjCBridgeRelated: |
| 4069 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4070 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4071 | case AttributeList::AT_ObjCDesignatedInitializer: |
| 4072 | handleObjCDesignatedInitializer(S, D, Attr); break; |
| 4073 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4074 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4075 | handleCFAuditedTransferAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4076 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4077 | handleCFUnknownTransferAttr(S, D, Attr); break; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4078 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4079 | // Checker-specific. |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4080 | case AttributeList::AT_CFConsumed: |
| 4081 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4082 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4083 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4084 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4085 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4086 | case AttributeList::AT_NSReturnsNotRetained: |
| 4087 | case AttributeList::AT_CFReturnsNotRetained: |
| 4088 | case AttributeList::AT_NSReturnsRetained: |
| 4089 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4090 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4091 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 4092 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4093 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 4094 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4095 | case AttributeList::AT_VecTypeHint: |
| 4096 | handleVecTypeHint(S, D, Attr); break; |
| 4097 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4098 | case AttributeList::AT_InitPriority: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4099 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4100 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4101 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4102 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4103 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4104 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4105 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4106 | case AttributeList::AT_ArcWeakrefUnavailable: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4107 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4108 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4109 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 4110 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 4111 | handleObjCSuppresProtocolAttr(S, D, Attr); |
| 4112 | break; |
| 4113 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4114 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4115 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4116 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4117 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4118 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4119 | case AttributeList::AT_Visibility: |
| 4120 | handleVisibilityAttr(S, D, Attr, false); |
| 4121 | break; |
| 4122 | case AttributeList::AT_TypeVisibility: |
| 4123 | handleVisibilityAttr(S, D, Attr, true); |
| 4124 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4125 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 6a42b5a | 2013-11-27 16:59:17 +0000 | [diff] [blame] | 4126 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4127 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4128 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 4129 | case AttributeList::AT_Weak: |
| 4130 | handleSimpleAttribute<WeakAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4131 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4132 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4133 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4134 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4135 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4136 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4137 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4138 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4139 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4140 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4141 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4142 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4143 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 4144 | case AttributeList::AT_Const: |
| 4145 | handleSimpleAttribute<ConstAttr>(S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4146 | case AttributeList::AT_Pure: |
| 4147 | handleSimpleAttribute<PureAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4148 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4149 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4150 | case AttributeList::AT_NoInline: |
| 4151 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4152 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4153 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4154 | case AttributeList::AT_StdCall: |
| 4155 | case AttributeList::AT_CDecl: |
| 4156 | case AttributeList::AT_FastCall: |
| 4157 | case AttributeList::AT_ThisCall: |
| 4158 | case AttributeList::AT_Pascal: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4159 | case AttributeList::AT_MSABI: |
| 4160 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4161 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4162 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4163 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4164 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4165 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4166 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4167 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4168 | case AttributeList::AT_OpenCLImageAccess: |
Aaron Ballman | 2689133 | 2014-01-14 17:41:53 +0000 | [diff] [blame] | 4169 | handleSimpleAttribute<OpenCLImageAccessAttr>(S, D, Attr); break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4170 | |
| 4171 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4172 | case AttributeList::AT_MsStruct: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4173 | handleSimpleAttribute<MsStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4174 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4175 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4176 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4177 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 4178 | case AttributeList::AT_MSInheritance: |
| 4179 | handleSimpleAttribute<MSInheritanceAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4180 | case AttributeList::AT_ForceInline: |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4181 | handleSimpleAttribute<ForceInlineAttr>(S, D, Attr); break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4182 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4183 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4184 | |
| 4185 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4186 | case AttributeList::AT_AssertExclusiveLock: |
| 4187 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4188 | break; |
| 4189 | case AttributeList::AT_AssertSharedLock: |
| 4190 | handleAssertSharedLockAttr(S, D, Attr); |
| 4191 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4192 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 4193 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4194 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4195 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4196 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4197 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4198 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4199 | case AttributeList::AT_NoSanitizeAddress: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4200 | handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4201 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4202 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4203 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4204 | break; |
| 4205 | case AttributeList::AT_NoSanitizeThread: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4206 | handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4207 | break; |
| 4208 | case AttributeList::AT_NoSanitizeMemory: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 4209 | handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4210 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4211 | case AttributeList::AT_Lockable: |
Aaron Ballman | 57ede3b | 2013-11-27 19:35:27 +0000 | [diff] [blame] | 4212 | handleSimpleAttribute<LockableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4213 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4214 | handleGuardedByAttr(S, D, Attr); |
| 4215 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4216 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4217 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4218 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4219 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4220 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4221 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4222 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4223 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4224 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4225 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4226 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4227 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4228 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4229 | handleLockReturnedAttr(S, D, Attr); |
| 4230 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4231 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4232 | handleLocksExcludedAttr(S, D, Attr); |
| 4233 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4234 | case AttributeList::AT_SharedLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4235 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4236 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4237 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4238 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4239 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4240 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4241 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4242 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4243 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4244 | handleUnlockFunAttr(S, D, Attr); |
| 4245 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4246 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4247 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4248 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4249 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4250 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4251 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4252 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4253 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4254 | case AttributeList::AT_Consumable: |
| 4255 | handleConsumableAttr(S, D, Attr); |
| 4256 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 4257 | case AttributeList::AT_ConsumableAutoCast: |
| 4258 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); break; |
| 4259 | break; |
| 4260 | case AttributeList::AT_ConsumableSetOnRead: |
| 4261 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); break; |
| 4262 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4263 | case AttributeList::AT_CallableWhen: |
| 4264 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4265 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4266 | case AttributeList::AT_ParamTypestate: |
| 4267 | handleParamTypestateAttr(S, D, Attr); |
| 4268 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4269 | case AttributeList::AT_ReturnTypestate: |
| 4270 | handleReturnTypestateAttr(S, D, Attr); |
| 4271 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4272 | case AttributeList::AT_SetTypestate: |
| 4273 | handleSetTypestateAttr(S, D, Attr); |
| 4274 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4275 | case AttributeList::AT_TestTypestate: |
| 4276 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4277 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4278 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4279 | // Type safety attributes. |
| 4280 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4281 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4282 | break; |
| 4283 | case AttributeList::AT_TypeTagForDatatype: |
| 4284 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4285 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4286 | } |
| 4287 | } |
| 4288 | |
| 4289 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4290 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4291 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4292 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4293 | bool IncludeCXX11Attributes) { |
| 4294 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4295 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4296 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4297 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4298 | // GCC accepts |
| 4299 | // static int a9 __attribute__((weakref)); |
| 4300 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4301 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 4302 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 4303 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4304 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4305 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4306 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4307 | |
| 4308 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 4309 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4310 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
| 4311 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4312 | D->setInvalidDecl(); |
| 4313 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4314 | if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
| 4315 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4316 | D->setInvalidDecl(); |
| 4317 | } |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 4318 | if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
| 4319 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 4320 | D->setInvalidDecl(); |
| 4321 | } |
| 4322 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4325 | // Annotation attributes are the only attributes allowed after an access |
| 4326 | // specifier. |
| 4327 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4328 | const AttributeList *AttrList) { |
| 4329 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4330 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4331 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4332 | } else { |
| 4333 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4334 | return true; |
| 4335 | } |
| 4336 | } |
| 4337 | |
| 4338 | return false; |
| 4339 | } |
| 4340 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4341 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4342 | /// contains any decl attributes that we should warn about. |
| 4343 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4344 | for ( ; A; A = A->getNext()) { |
| 4345 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4346 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4347 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4348 | |
| 4349 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4350 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4351 | << A->getName() << A->getRange(); |
| 4352 | } else { |
| 4353 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4354 | << A->getName() << A->getRange(); |
| 4355 | } |
| 4356 | } |
| 4357 | } |
| 4358 | |
| 4359 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4360 | /// used to build a declaration, complain about any decl attributes |
| 4361 | /// which might be lying around on it. |
| 4362 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4363 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4364 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4365 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4366 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4367 | } |
| 4368 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4369 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4370 | /// \#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] | 4371 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4372 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4373 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4374 | NamedDecl *NewD = 0; |
| 4375 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4376 | FunctionDecl *NewFD; |
| 4377 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4378 | // FIXME: Mangling? |
| 4379 | // FIXME: Is the qualifier info correct? |
| 4380 | // FIXME: Is the DeclContext correct? |
| 4381 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4382 | Loc, Loc, DeclarationName(II), |
| 4383 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4384 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4385 | FD->hasPrototype(), |
| 4386 | false/*isConstexprSpecified*/); |
| 4387 | NewD = NewFD; |
| 4388 | |
| 4389 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4390 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4391 | |
| 4392 | // Fake up parameter variables; they are declared as if this were |
| 4393 | // a typedef. |
| 4394 | QualType FDTy = FD->getType(); |
| 4395 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4396 | SmallVector<ParmVarDecl*, 16> Params; |
| 4397 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4398 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4399 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4400 | Param->setScopeInfo(0, Params.size()); |
| 4401 | Params.push_back(Param); |
| 4402 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4403 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4404 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4405 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4406 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4407 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4408 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4409 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4410 | if (VD->getQualifier()) { |
| 4411 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4412 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4413 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4414 | } |
| 4415 | return NewD; |
| 4416 | } |
| 4417 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4418 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4419 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4420 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4421 | if (W.getUsed()) return; // only do this once |
| 4422 | W.setUsed(true); |
| 4423 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4424 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4425 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4426 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 4427 | NDId->getName())); |
| 4428 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4429 | WeakTopLevelDecl.push_back(NewD); |
| 4430 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4431 | // to insert Decl at TU scope, sorry. |
| 4432 | DeclContext *SavedContext = CurContext; |
| 4433 | CurContext = Context.getTranslationUnitDecl(); |
| 4434 | PushOnScopeChains(NewD, S); |
| 4435 | CurContext = SavedContext; |
| 4436 | } else { // just add weak to existing |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4437 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4438 | } |
| 4439 | } |
| 4440 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 4441 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 4442 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4443 | // have to do this. |
| 4444 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4445 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4446 | NamedDecl *ND = NULL; |
| 4447 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 4448 | if (VD->isExternC()) |
| 4449 | ND = VD; |
| 4450 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 4451 | if (FD->isExternC()) |
| 4452 | ND = FD; |
| 4453 | if (ND) { |
| 4454 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4455 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4456 | = WeakUndeclaredIdentifiers.find(Id); |
| 4457 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 4458 | WeakInfo W = I->second; |
| 4459 | DeclApplyPragmaWeak(S, ND, W); |
| 4460 | WeakUndeclaredIdentifiers[Id] = W; |
| 4461 | } |
| 4462 | } |
| 4463 | } |
| 4464 | } |
| 4465 | } |
| 4466 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4467 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4468 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4469 | /// 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] | 4470 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4471 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4472 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4473 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4474 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4475 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4476 | // position to the decl itself. This handles cases like: |
| 4477 | // int *__attr__(x)** D; |
| 4478 | // when X is a decl attribute. |
| 4479 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4480 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4481 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4482 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4483 | // Finally, apply any attributes on the decl itself. |
| 4484 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4485 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4486 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4487 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4488 | /// Is the given declaration allowed to use a forbidden type? |
| 4489 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4490 | // Private ivars are always okay. Unfortunately, people don't |
| 4491 | // always properly make their ivars private, even in system headers. |
| 4492 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4493 | // Function declarations in sys headers will be marked unavailable. |
| 4494 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4495 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4496 | return false; |
| 4497 | |
| 4498 | // Require it to be declared in a system header. |
| 4499 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4500 | } |
| 4501 | |
| 4502 | /// Handle a delayed forbidden-type diagnostic. |
| 4503 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4504 | Decl *decl) { |
| 4505 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 4506 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 4507 | "this system declaration uses an unsupported type")); |
| 4508 | return; |
| 4509 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4510 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4511 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4512 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4513 | // kind of forbidden type messages on unavailable functions. |
| 4514 | if (FD->hasAttr<UnavailableAttr>() && |
| 4515 | diag.getForbiddenTypeDiagnostic() == |
| 4516 | diag::err_arc_array_param_no_ownership) { |
| 4517 | diag.Triggered = true; |
| 4518 | return; |
| 4519 | } |
| 4520 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4521 | |
| 4522 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4523 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4524 | diag.Triggered = true; |
| 4525 | } |
| 4526 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4527 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4528 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4529 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4530 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4531 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4532 | // When delaying diagnostics to run in the context of a parsed |
| 4533 | // declaration, we only want to actually emit anything if parsing |
| 4534 | // succeeds. |
| 4535 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4536 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4537 | // We emit all the active diagnostics in this pool or any of its |
| 4538 | // parents. In general, we'll get one pool for the decl spec |
| 4539 | // and a child pool for each declarator; in a decl group like: |
| 4540 | // deprecated_typedef foo, *bar, baz(); |
| 4541 | // only the declarator pops will be passed decls. This is correct; |
| 4542 | // we really do need to consider delayed diagnostics from the decl spec |
| 4543 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4544 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4545 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4546 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4547 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4548 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4549 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4550 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4551 | continue; |
| 4552 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4553 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4554 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4555 | case DelayedDiagnostic::Unavailable: |
| 4556 | // Don't bother giving deprecation/unavailable diagnostics if |
| 4557 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4558 | if (!decl->isInvalidDecl()) |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4559 | HandleDelayedAvailabilityCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4560 | break; |
| 4561 | |
| 4562 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4563 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4564 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4565 | |
| 4566 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4567 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4568 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4569 | } |
| 4570 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4571 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4574 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4575 | /// been delayed in the current context instead of in the given pool. |
| 4576 | /// Essentially, this just moves them to the current pool. |
| 4577 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4578 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4579 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4580 | curPool->steal(pool); |
| 4581 | } |
| 4582 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4583 | static bool isDeclDeprecated(Decl *D) { |
| 4584 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4585 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4586 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4587 | // A category implicitly has the availability of the interface. |
| 4588 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4589 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4590 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4591 | return false; |
| 4592 | } |
| 4593 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4594 | static bool isDeclUnavailable(Decl *D) { |
| 4595 | do { |
| 4596 | if (D->isUnavailable()) |
| 4597 | return true; |
| 4598 | // A category implicitly has the availability of the interface. |
| 4599 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4600 | return CatD->getClassInterface()->isUnavailable(); |
| 4601 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4602 | return false; |
| 4603 | } |
| 4604 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4605 | static void |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4606 | DoEmitAvailabilityWarning(Sema &S, |
| 4607 | DelayedDiagnostic::DDKind K, |
| 4608 | Decl *Ctx, |
| 4609 | const NamedDecl *D, |
| 4610 | StringRef Message, |
| 4611 | SourceLocation Loc, |
| 4612 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4613 | const ObjCPropertyDecl *ObjCProperty) { |
| 4614 | |
| 4615 | // Diagnostics for deprecated or unavailable. |
| 4616 | unsigned diag, diag_message, diag_fwdclass_message; |
| 4617 | |
| 4618 | // Matches 'diag::note_property_attribute' options. |
| 4619 | unsigned property_note_select; |
| 4620 | |
| 4621 | // Matches diag::note_availability_specified_here. |
| 4622 | unsigned available_here_select_kind; |
| 4623 | |
| 4624 | // Don't warn if our current context is deprecated or unavailable. |
| 4625 | switch (K) { |
| 4626 | case DelayedDiagnostic::Deprecation: |
| 4627 | if (isDeclDeprecated(Ctx)) |
| 4628 | return; |
| 4629 | diag = diag::warn_deprecated; |
| 4630 | diag_message = diag::warn_deprecated_message; |
| 4631 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 4632 | property_note_select = /* deprecated */ 0; |
| 4633 | available_here_select_kind = /* deprecated */ 2; |
| 4634 | break; |
| 4635 | |
| 4636 | case DelayedDiagnostic::Unavailable: |
| 4637 | if (isDeclUnavailable(Ctx)) |
| 4638 | return; |
| 4639 | diag = diag::err_unavailable; |
| 4640 | diag_message = diag::err_unavailable_message; |
| 4641 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 4642 | property_note_select = /* unavailable */ 1; |
| 4643 | available_here_select_kind = /* unavailable */ 0; |
| 4644 | break; |
| 4645 | |
| 4646 | default: |
| 4647 | llvm_unreachable("Neither a deprecation or unavailable kind"); |
| 4648 | } |
| 4649 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4650 | DeclarationName Name = D->getDeclName(); |
| 4651 | if (!Message.empty()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4652 | S.Diag(Loc, diag_message) << Name << Message; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4653 | if (ObjCProperty) |
| 4654 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4655 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4656 | } else if (!UnknownObjCClass) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4657 | S.Diag(Loc, diag) << Name; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4658 | if (ObjCProperty) |
| 4659 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 4660 | << ObjCProperty->getDeclName() << property_note_select; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4661 | } else { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4662 | S.Diag(Loc, diag_fwdclass_message) << Name; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4663 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4664 | } |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4665 | |
| 4666 | S.Diag(D->getLocation(), diag::note_availability_specified_here) |
| 4667 | << D << available_here_select_kind; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4668 | } |
| 4669 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4670 | void Sema::HandleDelayedAvailabilityCheck(DelayedDiagnostic &DD, |
| 4671 | Decl *Ctx) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4672 | DD.Triggered = true; |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4673 | DoEmitAvailabilityWarning(*this, |
| 4674 | (DelayedDiagnostic::DDKind) DD.Kind, |
| 4675 | Ctx, |
| 4676 | DD.getDeprecationDecl(), |
| 4677 | DD.getDeprecationMessage(), |
| 4678 | DD.Loc, |
| 4679 | DD.getUnknownObjCClass(), |
| 4680 | DD.getObjCProperty()); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4681 | } |
| 4682 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4683 | void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD, |
| 4684 | NamedDecl *D, StringRef Message, |
| 4685 | SourceLocation Loc, |
| 4686 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4687 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4688 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4689 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4690 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability(AD, Loc, D, |
| 4691 | UnknownObjCClass, |
| 4692 | ObjCProperty, |
| 4693 | Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4694 | return; |
| 4695 | } |
| 4696 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 4697 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
| 4698 | DelayedDiagnostic::DDKind K; |
| 4699 | switch (AD) { |
| 4700 | case AD_Deprecation: |
| 4701 | K = DelayedDiagnostic::Deprecation; |
| 4702 | break; |
| 4703 | case AD_Unavailable: |
| 4704 | K = DelayedDiagnostic::Unavailable; |
| 4705 | break; |
| 4706 | } |
| 4707 | |
| 4708 | DoEmitAvailabilityWarning(*this, K, Ctx, D, Message, Loc, |
| 4709 | UnknownObjCClass, ObjCProperty); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4710 | } |