Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1 | //===--- SemaDeclAttr.cpp - Declaration Attribute Handling ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements decl-related attribute processing. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 15 | #include "TargetAttributesSema.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 17 | #include "clang/AST/CXXInheritance.h" |
John McCall | 28a0cf7 | 2010-08-25 07:42:41 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclObjC.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | 56fdb6a | 2008-08-11 06:23:49 +0000 | [diff] [blame] | 21 | #include "clang/AST/Expr.h" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 22 | #include "clang/AST/Mangle.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 23 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 24 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 25 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 26 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 27 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 28 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 29 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 30 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 32 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 33 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 34 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 35 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 36 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 37 | C, |
| 38 | Cpp, |
| 39 | ObjC |
| 40 | }; |
| 41 | } |
| 42 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 43 | //===----------------------------------------------------------------------===// |
| 44 | // Helper functions |
| 45 | //===----------------------------------------------------------------------===// |
| 46 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 47 | static const FunctionType *getFunctionType(const Decl *D, |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 48 | bool blocksToo = true) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 49 | QualType Ty; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 50 | if (const ValueDecl *decl = dyn_cast<ValueDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 51 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 52 | else if (const FieldDecl *decl = dyn_cast<FieldDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 53 | Ty = decl->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 54 | else if (const TypedefNameDecl* decl = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 55 | Ty = decl->getUnderlyingType(); |
| 56 | else |
| 57 | return 0; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 59 | if (Ty->isFunctionPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 60 | Ty = Ty->getAs<PointerType>()->getPointeeType(); |
Fariborz Jahanian | 28c433d | 2009-05-18 17:39:25 +0000 | [diff] [blame] | 61 | else if (blocksToo && Ty->isBlockPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 62 | Ty = Ty->getAs<BlockPointerType>()->getPointeeType(); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 63 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 64 | return Ty->getAs<FunctionType>(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 67 | // FIXME: We should provide an abstraction around a method or function |
| 68 | // to provide the following bits of information. |
| 69 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 70 | /// isFunction - Return true if the given decl has function |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 71 | /// type (function or function-typed variable). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 72 | static bool isFunction(const Decl *D) { |
| 73 | return getFunctionType(D, false) != NULL; |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 77 | /// type (function or function-typed variable) or an Objective-C |
| 78 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 79 | static bool isFunctionOrMethod(const Decl *D) { |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 80 | return isFunction(D) || isa<ObjCMethodDecl>(D); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 83 | /// isFunctionOrMethodOrBlock - Return true if the given decl has function |
| 84 | /// type (function or function-typed variable) or an Objective-C |
| 85 | /// method or a block. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 86 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 87 | if (isFunctionOrMethod(D)) |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 88 | return true; |
| 89 | // check for block is more involved. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 90 | if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 91 | QualType Ty = V->getType(); |
| 92 | return Ty->isBlockPointerType(); |
| 93 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 94 | return isa<BlockDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 95 | } |
| 96 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 97 | /// Return true if the given decl has a declarator that should have |
| 98 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 99 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 100 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 101 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 102 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 105 | /// hasFunctionProto - Return true if the given decl has a argument |
| 106 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 107 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 108 | static bool hasFunctionProto(const Decl *D) { |
| 109 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 110 | return isa<FunctionProtoType>(FnTy); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 111 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 112 | assert(isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D)); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /// getFunctionOrMethodNumArgs - Return number of function or method |
| 118 | /// arguments. It is an error to call this on a K&R function (use |
| 119 | /// hasFunctionProto first). |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 120 | static unsigned getFunctionOrMethodNumArgs(const Decl *D) { |
| 121 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 122 | return cast<FunctionProtoType>(FnTy)->getNumArgs(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 123 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 124 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 125 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 128 | static QualType getFunctionOrMethodArgType(const Decl *D, unsigned Idx) { |
| 129 | if (const FunctionType *FnTy = getFunctionType(D)) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 130 | return cast<FunctionProtoType>(FnTy)->getArgType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 131 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 132 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 133 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 134 | return cast<ObjCMethodDecl>(D)->param_begin()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 137 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
| 138 | if (const FunctionType *FnTy = getFunctionType(D)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 139 | return cast<FunctionProtoType>(FnTy)->getResultType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 140 | return cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 143 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
| 144 | if (const FunctionType *FnTy = getFunctionType(D)) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 145 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 146 | return proto->isVariadic(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 147 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 8af4f40 | 2010-04-29 16:48:58 +0000 | [diff] [blame] | 148 | return BD->isVariadic(); |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 149 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 150 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 154 | static bool isInstanceMethod(const Decl *D) { |
| 155 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 156 | return MethodDecl->isInstance(); |
| 157 | return false; |
| 158 | } |
| 159 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 160 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 161 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 162 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 163 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 164 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 165 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 166 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 167 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 168 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 169 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 170 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 171 | // FIXME: Should we walk the chain of classes? |
| 172 | return ClsName == &Ctx.Idents.get("NSString") || |
| 173 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 174 | } |
| 175 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 176 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 177 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 178 | if (!PT) |
| 179 | return false; |
| 180 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 181 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 182 | if (!RT) |
| 183 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 184 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 185 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 186 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 187 | return false; |
| 188 | |
| 189 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 190 | } |
| 191 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 192 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 193 | // FIXME: Include the type in the argument list. |
| 194 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 195 | } |
| 196 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 197 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 198 | /// output an error. |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 199 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 200 | unsigned Num) { |
| 201 | if (getNumAttributeArgs(Attr) != Num) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 202 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 203 | << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | |
| 207 | return true; |
| 208 | } |
| 209 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 210 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 211 | /// \brief Check if the attribute has at least as many args as Num. May |
| 212 | /// output an error. |
| 213 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 214 | unsigned Num) { |
| 215 | if (getNumAttributeArgs(Attr) < Num) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 216 | S.Diag(Attr.getLoc(), diag::err_attribute_too_few_arguments) << Num; |
| 217 | return false; |
| 218 | } |
| 219 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 220 | return true; |
| 221 | } |
| 222 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 223 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 224 | /// expression and return success or failure. May output an error. |
| 225 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 226 | const Expr *Expr, uint32_t &Val, |
| 227 | unsigned Idx = UINT_MAX) { |
| 228 | llvm::APSInt I(32); |
| 229 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 230 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 231 | if (Idx != UINT_MAX) |
| 232 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 233 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 234 | << Expr->getSourceRange(); |
| 235 | else |
| 236 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 237 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 238 | << Expr->getSourceRange(); |
| 239 | return false; |
| 240 | } |
| 241 | Val = (uint32_t)I.getZExtValue(); |
| 242 | return true; |
| 243 | } |
| 244 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 245 | /// \brief Check if IdxExpr is a valid argument index for a function or |
| 246 | /// instance method D. May output an error. |
| 247 | /// |
| 248 | /// \returns true if IdxExpr is a valid index. |
| 249 | static bool checkFunctionOrMethodArgumentIndex(Sema &S, const Decl *D, |
| 250 | StringRef AttrName, |
| 251 | SourceLocation AttrLoc, |
| 252 | unsigned AttrArgNum, |
| 253 | const Expr *IdxExpr, |
| 254 | uint64_t &Idx) |
| 255 | { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 256 | assert(isFunctionOrMethod(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 257 | |
| 258 | // In C++ the implicit 'this' function parameter also counts. |
| 259 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 260 | bool HP = hasFunctionProto(D); |
| 261 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 262 | bool IV = HP && isFunctionOrMethodVariadic(D); |
| 263 | unsigned NumArgs = (HP ? getFunctionOrMethodNumArgs(D) : 0) + |
| 264 | HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 265 | |
| 266 | llvm::APSInt IdxInt; |
| 267 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 268 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 269 | std::string Name = std::string("'") + AttrName.str() + std::string("'"); |
| 270 | S.Diag(AttrLoc, diag::err_attribute_argument_n_type) << Name.c_str() |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 271 | << AttrArgNum << AANT_ArgumentIntegerConstant << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 272 | return false; |
| 273 | } |
| 274 | |
| 275 | Idx = IdxInt.getLimitedValue(); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 276 | if (Idx < 1 || (!IV && Idx > NumArgs)) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 277 | S.Diag(AttrLoc, diag::err_attribute_argument_out_of_bounds) |
| 278 | << AttrName << AttrArgNum << IdxExpr->getSourceRange(); |
| 279 | return false; |
| 280 | } |
| 281 | Idx--; // Convert to zero-based. |
| 282 | if (HasImplicitThisParam) { |
| 283 | if (Idx == 0) { |
| 284 | S.Diag(AttrLoc, |
| 285 | diag::err_attribute_invalid_implicit_this_argument) |
| 286 | << AttrName << IdxExpr->getSourceRange(); |
| 287 | return false; |
| 288 | } |
| 289 | --Idx; |
| 290 | } |
| 291 | |
| 292 | return true; |
| 293 | } |
| 294 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 295 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 296 | /// If not emit an error and return false. If the argument is an identifier it |
| 297 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 298 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 299 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 300 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 301 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 302 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 303 | if (Attr.isArgIdent(ArgNum)) { |
| 304 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 305 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 306 | << Attr.getName() << AANT_ArgumentString |
| 307 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 308 | << FixItHint::CreateInsertion(PP.getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 309 | Str = Loc->Ident->getName(); |
| 310 | if (ArgLocation) |
| 311 | *ArgLocation = Loc->Loc; |
| 312 | return true; |
| 313 | } |
| 314 | |
| 315 | // Now check for an actual string literal. |
| 316 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 317 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 318 | if (ArgLocation) |
| 319 | *ArgLocation = ArgExpr->getLocStart(); |
| 320 | |
| 321 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 322 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 323 | << Attr.getName() << AANT_ArgumentString; |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | Str = Literal->getString(); |
| 328 | return true; |
| 329 | } |
| 330 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 331 | /// \brief Applies the given attribute to the Decl without performing any |
| 332 | /// additional semantic checking. |
| 333 | template <typename AttrType> |
| 334 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 335 | const AttributeList &Attr) { |
| 336 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 337 | Attr.getAttributeSpellingListIndex())); |
| 338 | } |
| 339 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 340 | /// |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 341 | /// \brief Check if passed in Decl is a field or potentially shared global var |
| 342 | /// \return true if the Decl is a field or potentially shared global variable |
| 343 | /// |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 344 | static bool mayBeSharedVariable(const Decl *D) { |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 345 | if (const VarDecl *vd = dyn_cast<VarDecl>(D)) |
Richard Smith | fd3834f | 2013-04-13 02:43:54 +0000 | [diff] [blame] | 346 | return vd->hasGlobalStorage() && !vd->getTLSKind(); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 347 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 348 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 351 | /// \brief Check if the passed-in expression is of type int or bool. |
| 352 | static bool isIntOrBool(Expr *Exp) { |
| 353 | QualType QT = Exp->getType(); |
| 354 | return QT->isBooleanType() || QT->isIntegerType(); |
| 355 | } |
| 356 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 357 | |
| 358 | // Check to see if the type is a smart pointer of some kind. We assume |
| 359 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 360 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
| 361 | DeclContextLookupConstResult Res1 = RT->getDecl()->lookup( |
| 362 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 363 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 364 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 365 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 366 | DeclContextLookupConstResult Res2 = RT->getDecl()->lookup( |
| 367 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 368 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 369 | return false; |
| 370 | |
| 371 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 374 | /// \brief Check if passed in Decl is a pointer type. |
| 375 | /// Note that this function may produce an error message. |
| 376 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 377 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 378 | const AttributeList &Attr) { |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 379 | if (const ValueDecl *vd = dyn_cast<ValueDecl>(D)) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 380 | QualType QT = vd->getType(); |
Benjamin Kramer | 3c05b7c | 2011-08-02 04:50:49 +0000 | [diff] [blame] | 381 | if (QT->isAnyPointerType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 382 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 383 | |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 384 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 385 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 386 | // (We don't want to force template instantiation if we can avoid it, |
| 387 | // since that would alter the order in which templates are instantiated.) |
| 388 | if (RT->isIncompleteType()) |
| 389 | return true; |
| 390 | |
| 391 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 392 | return true; |
| 393 | } |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 394 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 395 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 396 | << Attr.getName()->getName() << QT; |
| 397 | } else { |
| 398 | S.Diag(Attr.getLoc(), diag::err_attribute_can_be_applied_only_to_value_decl) |
| 399 | << Attr.getName(); |
| 400 | } |
| 401 | return false; |
| 402 | } |
| 403 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 404 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 405 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 406 | static const RecordType *getRecordType(QualType QT) { |
| 407 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 408 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 409 | |
| 410 | // Now check if we point to record type. |
| 411 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 412 | return PT->getPointeeType()->getAs<RecordType>(); |
| 413 | |
| 414 | return 0; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 415 | } |
| 416 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 417 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 418 | static bool checkBaseClassIsLockableCallback(const CXXBaseSpecifier *Specifier, |
| 419 | CXXBasePath &Path, void *Unused) { |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 420 | const RecordType *RT = Specifier->getType()->getAs<RecordType>(); |
| 421 | if (RT->getDecl()->getAttr<LockableAttr>()) |
| 422 | return true; |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 427 | /// \brief Thread Safety Analysis: Checks that the passed in RecordType |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 428 | /// resolves to a lockable object. |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 429 | static void checkForLockableRecord(Sema &S, Decl *D, const AttributeList &Attr, |
| 430 | QualType Ty) { |
| 431 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 432 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 433 | // Warn if could not get record type for this argument. |
Benjamin Kramer | 2667afa | 2011-09-03 03:30:59 +0000 | [diff] [blame] | 434 | if (!RT) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 435 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_class) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 436 | << Attr.getName() << Ty.getAsString(); |
| 437 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 438 | } |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 439 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 440 | // 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] | 441 | if (RT->isIncompleteType()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 442 | return; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 443 | |
| 444 | // Allow smart pointers to be used as lockable objects. |
| 445 | // FIXME -- Check the type that the smart pointer points to. |
| 446 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 447 | return; |
| 448 | |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 449 | // Check if the type is lockable. |
| 450 | RecordDecl *RD = RT->getDecl(); |
| 451 | if (RD->getAttr<LockableAttr>()) |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 452 | return; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 453 | |
| 454 | // Else check if any base classes are lockable. |
| 455 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 456 | CXXBasePaths BPaths(false, false); |
| 457 | if (CRD->lookupInBases(checkBaseClassIsLockableCallback, 0, BPaths)) |
| 458 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 459 | } |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 460 | |
| 461 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 462 | << Attr.getName() << Ty.getAsString(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 465 | /// \brief Thread Safety Analysis: Checks that all attribute arguments, starting |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 466 | /// from Sidx, resolve to a lockable object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 467 | /// \param Sidx The attribute argument index to start checking with. |
| 468 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 469 | /// parameter list. |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 470 | static void checkAttrArgsAreLockableObjs(Sema &S, Decl *D, |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 471 | const AttributeList &Attr, |
| 472 | SmallVectorImpl<Expr*> &Args, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 473 | int Sidx = 0, |
| 474 | bool ParamIdxOk = false) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 475 | for(unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 476 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 477 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 478 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 479 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 480 | Args.push_back(ArgExp); |
| 481 | continue; |
| 482 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 483 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 484 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 485 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 486 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 487 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 488 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 489 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 490 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 491 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 492 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 493 | // We allow constant strings to be used as a placeholder for expressions |
| 494 | // that are not valid C++ syntax, but warn that they are ignored. |
| 495 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 496 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 497 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 498 | continue; |
| 499 | } |
| 500 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 501 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 502 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 503 | // A pointer to member expression of the form &MyClass::mu is treated |
| 504 | // specially -- we need to look at the type of the member. |
| 505 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 506 | if (UOp->getOpcode() == UO_AddrOf) |
| 507 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 508 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 509 | ArgTy = DRE->getDecl()->getType(); |
| 510 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 511 | // First see if we can just cast to record type, or point to record type. |
| 512 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 513 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 514 | // Now check if we index into a record type function param. |
| 515 | if(!RT && ParamIdxOk) { |
| 516 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 517 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 518 | if(FD && IL) { |
| 519 | unsigned int NumParams = FD->getNumParams(); |
| 520 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 521 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 522 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 523 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 524 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 525 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 526 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 527 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 528 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 532 | checkForLockableRecord(S, D, Attr, ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 533 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 534 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 535 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 538 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 539 | // Attribute Implementations |
| 540 | //===----------------------------------------------------------------------===// |
| 541 | |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 542 | // FIXME: All this manual attribute parsing code is gross. At the |
| 543 | // least add some helper functions to check most argument patterns (# |
| 544 | // and types of args). |
| 545 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 546 | static bool checkGuardedVarAttrCommon(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 547 | const AttributeList &Attr) { |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 548 | // D must be either a member field or global (potentially shared) variable. |
| 549 | if (!mayBeSharedVariable(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 550 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 551 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 552 | return false; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 555 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 558 | static void handleGuardedVarAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 559 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 560 | return; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 561 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 562 | D->addAttr(::new (S.Context) |
| 563 | GuardedVarAttr(Attr.getRange(), S.Context, |
| 564 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 567 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 568 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 569 | if (!checkGuardedVarAttrCommon(S, D, Attr)) |
| 570 | return; |
| 571 | |
| 572 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 573 | return; |
| 574 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 575 | D->addAttr(::new (S.Context) |
| 576 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 577 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 580 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 581 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 582 | Expr* &Arg) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 583 | // D must be either a member field or global (potentially shared) variable. |
| 584 | if (!mayBeSharedVariable(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 585 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 586 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 587 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 588 | } |
| 589 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 590 | SmallVector<Expr*, 1> Args; |
| 591 | // check that all arguments are lockable objects |
| 592 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 593 | unsigned Size = Args.size(); |
| 594 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 595 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 596 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 597 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 598 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 599 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 602 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 603 | Expr *Arg = 0; |
| 604 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 605 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 606 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 607 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg)); |
| 608 | } |
| 609 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 610 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 611 | const AttributeList &Attr) { |
| 612 | Expr *Arg = 0; |
| 613 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 614 | return; |
| 615 | |
| 616 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 617 | return; |
| 618 | |
| 619 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
| 620 | S.Context, Arg)); |
| 621 | } |
| 622 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 623 | static bool checkLockableAttrCommon(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 624 | const AttributeList &Attr) { |
Caitlin Sadowski | 086fb95 | 2011-09-16 00:35:54 +0000 | [diff] [blame] | 625 | // FIXME: Lockable structs for C code. |
David Blaikie | 021221d | 2013-07-29 18:24:03 +0000 | [diff] [blame] | 626 | if (!isa<RecordDecl>(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 627 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 628 | << Attr.getName() << ExpectedStructOrUnionOrClass; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 629 | return false; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 632 | return true; |
| 633 | } |
| 634 | |
| 635 | static void handleLockableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 636 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 637 | return; |
| 638 | |
| 639 | D->addAttr(::new (S.Context) LockableAttr(Attr.getRange(), S.Context)); |
| 640 | } |
| 641 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 642 | static void handleScopedLockableAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 643 | const AttributeList &Attr) { |
| 644 | if (!checkLockableAttrCommon(S, D, Attr)) |
| 645 | return; |
| 646 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 647 | D->addAttr(::new (S.Context) |
| 648 | ScopedLockableAttr(Attr.getRange(), S.Context, |
| 649 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 652 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 653 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 654 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 655 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 656 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 657 | |
| 658 | // D must be either a member field or global (potentially shared) variable. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 659 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 660 | if (!VD || !mayBeSharedVariable(D)) { |
Aaron Ballman | 07e2764 | 2013-11-20 21:41:42 +0000 | [diff] [blame] | 661 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 662 | << Attr.getName() << ExpectedFieldOrGlobalVar; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 663 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 664 | } |
| 665 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 666 | // Check that this attribute only applies to lockable types. |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 667 | QualType QT = VD->getType(); |
| 668 | if (!QT->isDependentType()) { |
| 669 | const RecordType *RT = getRecordType(QT); |
| 670 | if (!RT || !RT->getDecl()->getAttr<LockableAttr>()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 671 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 672 | << Attr.getName(); |
| 673 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 677 | // Check that all arguments are lockable objects. |
| 678 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 679 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 680 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 681 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 682 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 685 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 686 | const AttributeList &Attr) { |
| 687 | SmallVector<Expr*, 1> Args; |
| 688 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 689 | return; |
| 690 | |
| 691 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 692 | D->addAttr(::new (S.Context) |
| 693 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 694 | StartArg, Args.size(), |
| 695 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 698 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 699 | const AttributeList &Attr) { |
| 700 | SmallVector<Expr*, 1> Args; |
| 701 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 702 | return; |
| 703 | |
| 704 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 705 | D->addAttr(::new (S.Context) |
| 706 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 707 | StartArg, Args.size(), |
| 708 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 709 | } |
| 710 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 711 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 712 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 713 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 714 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 715 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 716 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 717 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 718 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 719 | } |
| 720 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 721 | static void handleSharedLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 722 | const AttributeList &Attr) { |
| 723 | SmallVector<Expr*, 1> Args; |
| 724 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 725 | return; |
| 726 | |
| 727 | unsigned Size = Args.size(); |
| 728 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 729 | D->addAttr(::new (S.Context) |
| 730 | SharedLockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 731 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 734 | static void handleExclusiveLockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 735 | const AttributeList &Attr) { |
| 736 | SmallVector<Expr*, 1> Args; |
| 737 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 738 | return; |
| 739 | |
| 740 | unsigned Size = Args.size(); |
| 741 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 742 | D->addAttr(::new (S.Context) |
| 743 | ExclusiveLockFunctionAttr(Attr.getRange(), S.Context, |
| 744 | StartArg, Size, |
| 745 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 746 | } |
| 747 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 748 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 749 | const AttributeList &Attr) { |
| 750 | SmallVector<Expr*, 1> Args; |
| 751 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 752 | return; |
| 753 | |
| 754 | unsigned Size = Args.size(); |
| 755 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 756 | D->addAttr(::new (S.Context) |
| 757 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 758 | Attr.getAttributeSpellingListIndex())); |
| 759 | } |
| 760 | |
| 761 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 762 | const AttributeList &Attr) { |
| 763 | SmallVector<Expr*, 1> Args; |
| 764 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 765 | return; |
| 766 | |
| 767 | unsigned Size = Args.size(); |
| 768 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 769 | D->addAttr(::new (S.Context) |
| 770 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 771 | StartArg, Size, |
| 772 | Attr.getAttributeSpellingListIndex())); |
| 773 | } |
| 774 | |
| 775 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 776 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 777 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 778 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 779 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 780 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 781 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 782 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 783 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 784 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 785 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 789 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 790 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 791 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 794 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 795 | const AttributeList &Attr) { |
| 796 | SmallVector<Expr*, 2> Args; |
| 797 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 798 | return; |
| 799 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 800 | D->addAttr(::new (S.Context) |
| 801 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 802 | Attr.getArgAsExpr(0), |
| 803 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 804 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 807 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 808 | const AttributeList &Attr) { |
| 809 | SmallVector<Expr*, 2> Args; |
| 810 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 811 | return; |
| 812 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 813 | D->addAttr(::new (S.Context) |
| 814 | ExclusiveTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 815 | Attr.getArgAsExpr(0), |
| 816 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 817 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 820 | static bool checkLocksRequiredCommon(Sema &S, Decl *D, |
| 821 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 822 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 823 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 824 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 825 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 826 | // check that all arguments are lockable objects |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 827 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 828 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 829 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 830 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 831 | return true; |
| 832 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 833 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 834 | static void handleExclusiveLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 835 | const AttributeList &Attr) { |
| 836 | SmallVector<Expr*, 1> Args; |
| 837 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 838 | return; |
| 839 | |
| 840 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 841 | D->addAttr(::new (S.Context) |
| 842 | ExclusiveLocksRequiredAttr(Attr.getRange(), S.Context, |
| 843 | StartArg, Args.size(), |
| 844 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 847 | static void handleSharedLocksRequiredAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 848 | const AttributeList &Attr) { |
| 849 | SmallVector<Expr*, 1> Args; |
| 850 | if (!checkLocksRequiredCommon(S, D, Attr, Args)) |
| 851 | return; |
| 852 | |
| 853 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 854 | D->addAttr(::new (S.Context) |
| 855 | SharedLocksRequiredAttr(Attr.getRange(), S.Context, |
| 856 | StartArg, Args.size(), |
| 857 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 860 | static void handleUnlockFunAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 861 | const AttributeList &Attr) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 862 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 863 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 864 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 865 | checkAttrArgsAreLockableObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 866 | unsigned Size = Args.size(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 867 | Expr **StartArg = Size == 0 ? 0 : &Args[0]; |
| 868 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 869 | D->addAttr(::new (S.Context) |
| 870 | UnlockFunctionAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 871 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 875 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 876 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 877 | SmallVector<Expr*, 1> Args; |
| 878 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
| 879 | unsigned Size = Args.size(); |
| 880 | if (Size == 0) |
| 881 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 882 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 883 | D->addAttr(::new (S.Context) |
| 884 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 885 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 889 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 890 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 891 | return; |
| 892 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 893 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 894 | SmallVector<Expr*, 1> Args; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 895 | checkAttrArgsAreLockableObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 896 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 897 | if (Size == 0) |
| 898 | return; |
| 899 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 900 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 901 | D->addAttr(::new (S.Context) |
| 902 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 903 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 904 | } |
| 905 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 906 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 907 | ConsumableAttr::ConsumedState DefaultState; |
| 908 | |
| 909 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 910 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 911 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 912 | DefaultState)) { |
| 913 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 914 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 915 | return; |
| 916 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 917 | } else { |
| 918 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 919 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 920 | return; |
| 921 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 922 | |
| 923 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 924 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 925 | Attr.getAttributeSpellingListIndex())); |
| 926 | } |
| 927 | |
| 928 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 929 | const AttributeList &Attr) { |
| 930 | ASTContext &CurrContext = S.getASTContext(); |
| 931 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 932 | |
| 933 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 934 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 935 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 936 | RD->getNameAsString(); |
| 937 | |
| 938 | return false; |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | return true; |
| 943 | } |
| 944 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 945 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 946 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 947 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 948 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 949 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 950 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 951 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 952 | return; |
| 953 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 954 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 955 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 956 | CallableWhenAttr::ConsumedState CallableState; |
| 957 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 958 | StringRef StateString; |
| 959 | SourceLocation Loc; |
| 960 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 961 | return; |
| 962 | |
| 963 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 964 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 965 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 966 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 967 | return; |
| 968 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 969 | |
| 970 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 971 | } |
| 972 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 973 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 974 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 975 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 976 | } |
| 977 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 978 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 979 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 980 | const AttributeList &Attr) { |
| 981 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 982 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 983 | ParamTypestateAttr::ConsumedState ParamState; |
| 984 | |
| 985 | if (Attr.isArgIdent(0)) { |
| 986 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 987 | StringRef StateString = Ident->Ident->getName(); |
| 988 | |
| 989 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 990 | ParamState)) { |
| 991 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 992 | << Attr.getName() << StateString; |
| 993 | return; |
| 994 | } |
| 995 | } else { |
| 996 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 997 | Attr.getName() << AANT_ArgumentIdentifier; |
| 998 | return; |
| 999 | } |
| 1000 | |
| 1001 | // FIXME: This check is currently being done in the analysis. It can be |
| 1002 | // enabled here only after the parser propagates attributes at |
| 1003 | // template specialization definition, not declaration. |
| 1004 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 1005 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1006 | // |
| 1007 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1008 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1009 | // ReturnType.getAsString(); |
| 1010 | // return; |
| 1011 | //} |
| 1012 | |
| 1013 | D->addAttr(::new (S.Context) |
| 1014 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 1015 | Attr.getAttributeSpellingListIndex())); |
| 1016 | } |
| 1017 | |
| 1018 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1019 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 1020 | const AttributeList &Attr) { |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1021 | if (!checkAttributeNumArgs(S, Attr, 1)) return; |
| 1022 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1023 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 1024 | |
| 1025 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1026 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1027 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1028 | ReturnState)) { |
| 1029 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1030 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1031 | return; |
| 1032 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1033 | } else { |
| 1034 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1035 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1036 | return; |
| 1037 | } |
| 1038 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1039 | // FIXME: This check is currently being done in the analysis. It can be |
| 1040 | // enabled here only after the parser propagates attributes at |
| 1041 | // template specialization definition, not declaration. |
| 1042 | //QualType ReturnType; |
| 1043 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1044 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1045 | // ReturnType = Param->getType(); |
| 1046 | // |
| 1047 | //} else if (const CXXConstructorDecl *Constructor = |
| 1048 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1049 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1050 | // |
| 1051 | //} else { |
| 1052 | // |
| 1053 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1054 | //} |
| 1055 | // |
| 1056 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1057 | // |
| 1058 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1059 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1060 | // ReturnType.getAsString(); |
| 1061 | // return; |
| 1062 | //} |
| 1063 | |
| 1064 | D->addAttr(::new (S.Context) |
| 1065 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1066 | Attr.getAttributeSpellingListIndex())); |
| 1067 | } |
| 1068 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1069 | |
| 1070 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1071 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1072 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1073 | |
| 1074 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1075 | return; |
| 1076 | |
| 1077 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1078 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1079 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1080 | StringRef Param = Ident->Ident->getName(); |
| 1081 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1082 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1083 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1084 | return; |
| 1085 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1086 | } else { |
| 1087 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1088 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1089 | return; |
| 1090 | } |
| 1091 | |
| 1092 | D->addAttr(::new (S.Context) |
| 1093 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1094 | Attr.getAttributeSpellingListIndex())); |
| 1095 | } |
| 1096 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1097 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1098 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1099 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 1100 | return; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1101 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1102 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1103 | return; |
| 1104 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1105 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1106 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1107 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1108 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1109 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1110 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1111 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1112 | return; |
| 1113 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1114 | } else { |
| 1115 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1116 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1121 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1122 | Attr.getAttributeSpellingListIndex())); |
| 1123 | } |
| 1124 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1125 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1126 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1127 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1128 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1131 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1132 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 1133 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context)); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1134 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1135 | // If the alignment is less than or equal to 8 bits, the packed attribute |
| 1136 | // has no effect. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1137 | if (!FD->getType()->isDependentType() && |
| 1138 | !FD->getType()->isIncompleteType() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1139 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1140 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
Chris Lattner | e3d20d9 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1141 | << Attr.getName() << FD->getType(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1142 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1143 | FD->addAttr(::new (S.Context) |
| 1144 | PackedAttr(Attr.getRange(), S.Context, |
| 1145 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1146 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1147 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1150 | static void handleIBAction(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1151 | // The IBAction attributes only apply to instance methods. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1152 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1153 | if (MD->isInstanceMethod()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1154 | D->addAttr(::new (S.Context) |
| 1155 | IBActionAttr(Attr.getRange(), S.Context, |
| 1156 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1157 | return; |
| 1158 | } |
| 1159 | |
Ted Kremenek | d68ec81 | 2011-02-04 06:54:16 +0000 | [diff] [blame] | 1160 | S.Diag(Attr.getLoc(), diag::warn_attribute_ibaction) << Attr.getName(); |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1163 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1164 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1165 | // variables or properties of Objective-C classes. The outlet must also |
| 1166 | // have an object reference type. |
| 1167 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1168 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1169 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1170 | << Attr.getName() << VD->getType() << 0; |
| 1171 | return false; |
| 1172 | } |
| 1173 | } |
| 1174 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1175 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1176 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1177 | << Attr.getName() << PD->getType() << 1; |
| 1178 | return false; |
| 1179 | } |
| 1180 | } |
| 1181 | else { |
| 1182 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1183 | return false; |
| 1184 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1185 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1186 | return true; |
| 1187 | } |
| 1188 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1189 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1190 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1191 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1192 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1193 | D->addAttr(::new (S.Context) |
| 1194 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1195 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1198 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1199 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1200 | |
| 1201 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1202 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1203 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1204 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1205 | return; |
| 1206 | } |
| 1207 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1208 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1209 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1210 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1211 | ParsedType PT; |
| 1212 | |
| 1213 | if (Attr.hasParsedType()) |
| 1214 | PT = Attr.getTypeArg(); |
| 1215 | else { |
| 1216 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1217 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1218 | if (!PT) { |
| 1219 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1220 | return; |
| 1221 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1222 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1223 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1224 | TypeSourceInfo *QTLoc = 0; |
| 1225 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1226 | if (!QTLoc) |
| 1227 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1228 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1229 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1230 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1231 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1232 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1233 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1234 | S.Diag(Attr.getLoc(), |
| 1235 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1236 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1237 | return; |
| 1238 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1239 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1240 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1241 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1242 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1245 | static void possibleTransparentUnionPointerType(QualType &T) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1246 | if (const RecordType *UT = T->getAsUnionType()) |
| 1247 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1248 | RecordDecl *UD = UT->getDecl(); |
| 1249 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 1250 | itend = UD->field_end(); it != itend; ++it) { |
| 1251 | QualType QT = it->getType(); |
| 1252 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) { |
| 1253 | T = QT; |
| 1254 | return; |
| 1255 | } |
| 1256 | } |
| 1257 | } |
| 1258 | } |
| 1259 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1260 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nuno Lopes | e881ce2 | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1261 | if (!isFunctionOrMethod(D)) { |
| 1262 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1263 | << Attr.getName() << ExpectedFunctionOrMethod; |
Nuno Lopes | e881ce2 | 2012-06-18 16:39:04 +0000 | [diff] [blame] | 1264 | return; |
| 1265 | } |
| 1266 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1267 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1268 | return; |
| 1269 | |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1270 | SmallVector<unsigned, 8> SizeArgs; |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1271 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1272 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1273 | uint64_t Idx; |
| 1274 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1275 | Attr.getLoc(), i + 1, Ex, Idx)) |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1276 | return; |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1277 | |
| 1278 | // check if the function argument is of an integer type |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1279 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1280 | if (!T->isIntegerType()) { |
Aaron Ballman | 9d69509 | 2013-07-30 14:10:17 +0000 | [diff] [blame] | 1281 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1282 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 1283 | << Ex->getSourceRange(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1284 | return; |
| 1285 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1286 | SizeArgs.push_back(Idx); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | // check if the function returns a pointer |
| 1290 | if (!getFunctionType(D)->getResultType()->isAnyPointerType()) { |
| 1291 | S.Diag(Attr.getLoc(), diag::warn_ns_attribute_wrong_return_type) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1292 | << Attr.getName() << 0 /*function*/<< 1 /*pointer*/ << D->getSourceRange(); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1295 | D->addAttr(::new (S.Context) |
| 1296 | AllocSizeAttr(Attr.getRange(), S.Context, |
| 1297 | SizeArgs.data(), SizeArgs.size(), |
| 1298 | Attr.getAttributeSpellingListIndex())); |
Nuno Lopes | 5c7ad16 | 2012-05-24 00:22:00 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1301 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1302 | // GCC ignores the nonnull attribute on K&R style function prototypes, so we |
| 1303 | // ignore it as well |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1304 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1305 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1306 | << Attr.getName() << ExpectedFunction; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1307 | return; |
| 1308 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1309 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1310 | SmallVector<unsigned, 8> NonNullArgs; |
| 1311 | for (unsigned i = 0; i < Attr.getNumArgs(); ++i) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1312 | Expr *Ex = Attr.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1313 | uint64_t Idx; |
| 1314 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 1315 | Attr.getLoc(), i + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1316 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1317 | |
| 1318 | // Is the function argument a pointer type? |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1319 | QualType T = getFunctionOrMethodArgType(D, Idx).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1320 | possibleTransparentUnionPointerType(T); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1321 | |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1322 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) { |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1323 | // FIXME: Should also highlight argument in decl. |
Douglas Gregor | 62157e5 | 2010-08-12 18:48:43 +0000 | [diff] [blame] | 1324 | S.Diag(Attr.getLoc(), diag::warn_nonnull_pointers_only) |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1325 | << "nonnull" << Ex->getSourceRange(); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1326 | continue; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1327 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1328 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1329 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1330 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1331 | |
| 1332 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
| 1333 | // arguments have a nonnull attribute. |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1334 | if (NonNullArgs.empty()) { |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1335 | for (unsigned i = 0, e = getFunctionOrMethodNumArgs(D); i != e; ++i) { |
| 1336 | QualType T = getFunctionOrMethodArgType(D, i).getNonReferenceType(); |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 1337 | possibleTransparentUnionPointerType(T); |
Ted Kremenek | d4adebb | 2009-07-15 23:23:54 +0000 | [diff] [blame] | 1338 | if (T->isAnyPointerType() || T->isBlockPointerType()) |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1339 | NonNullArgs.push_back(i); |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1340 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1341 | |
Ted Kremenek | 22813f4 | 2010-10-21 18:49:36 +0000 | [diff] [blame] | 1342 | // No pointer arguments? |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1343 | if (NonNullArgs.empty()) { |
| 1344 | // Warn the trivial case only if attribute is not coming from a |
| 1345 | // macro instantiation. |
| 1346 | if (Attr.getLoc().isFileID()) |
| 1347 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1348 | return; |
Fariborz Jahanian | cb67d7b | 2010-09-27 19:05:51 +0000 | [diff] [blame] | 1349 | } |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1350 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1351 | |
Nick Lewycky | e112151 | 2013-01-24 01:12:16 +0000 | [diff] [blame] | 1352 | unsigned *start = &NonNullArgs[0]; |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1353 | unsigned size = NonNullArgs.size(); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1354 | llvm::array_pod_sort(start, start + size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1355 | D->addAttr(::new (S.Context) |
| 1356 | NonNullAttr(Attr.getRange(), S.Context, start, size, |
| 1357 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1360 | static const char *ownershipKindToDiagName(OwnershipAttr::OwnershipKind K) { |
| 1361 | switch (K) { |
| 1362 | case OwnershipAttr::Holds: return "'ownership_holds'"; |
| 1363 | case OwnershipAttr::Takes: return "'ownership_takes'"; |
| 1364 | case OwnershipAttr::Returns: return "'ownership_returns'"; |
| 1365 | } |
| 1366 | llvm_unreachable("unknown ownership"); |
| 1367 | } |
| 1368 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1369 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1370 | // This attribute must be applied to a function declaration. The first |
| 1371 | // argument to the attribute must be an identifier, the name of the resource, |
| 1372 | // for example: malloc. The following arguments must be argument indexes, the |
| 1373 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1374 | // 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] | 1375 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1376 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1377 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1378 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1379 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1380 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1381 | return; |
| 1382 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1383 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1384 | // Figure out our Kind. |
| 1385 | OwnershipAttr::OwnershipKind K = |
| 1386 | OwnershipAttr(AL.getLoc(), S.Context, 0, 0, 0, |
| 1387 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1388 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1389 | // Check arguments. |
| 1390 | switch (K) { |
| 1391 | case OwnershipAttr::Takes: |
| 1392 | case OwnershipAttr::Holds: |
| 1393 | if (AL.getNumArgs() < 2) { |
| 1394 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) << 2; |
| 1395 | return; |
| 1396 | } |
| 1397 | break; |
| 1398 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1399 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1400 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1401 | return; |
| 1402 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1403 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1406 | if (!isFunction(D) || !hasFunctionProto(D)) { |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1407 | S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1408 | << AL.getName() << ExpectedFunction; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1409 | return; |
| 1410 | } |
| 1411 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1412 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1413 | |
| 1414 | // Normalize the argument, __foo__ becomes foo. |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1415 | StringRef ModuleName = Module->getName(); |
| 1416 | if (ModuleName.startswith("__") && ModuleName.endswith("__") && |
| 1417 | ModuleName.size() > 4) { |
| 1418 | ModuleName = ModuleName.drop_front(2).drop_back(2); |
| 1419 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1420 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1421 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1422 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1423 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1424 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1425 | uint64_t Idx; |
| 1426 | if (!checkFunctionOrMethodArgumentIndex(S, D, AL.getName()->getName(), |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1427 | AL.getLoc(), i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1428 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1429 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1430 | // Is the function argument a pointer type? |
| 1431 | QualType T = getFunctionOrMethodArgType(D, Idx); |
| 1432 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1433 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1434 | case OwnershipAttr::Takes: |
| 1435 | case OwnershipAttr::Holds: |
| 1436 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1437 | Err = 0; |
| 1438 | break; |
| 1439 | case OwnershipAttr::Returns: |
| 1440 | if (!T->isIntegerType()) |
| 1441 | Err = 1; |
| 1442 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1443 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1444 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1445 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1446 | << Ex->getSourceRange(); |
| 1447 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1448 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Check we don't have a conflict with another ownership attribute. |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1451 | for (specific_attr_iterator<OwnershipAttr> |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1452 | i = D->specific_attr_begin<OwnershipAttr>(), |
| 1453 | e = D->specific_attr_end<OwnershipAttr>(); i != e; ++i) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1454 | // FIXME: A returns attribute should conflict with any returns attribute |
| 1455 | // with a different index too. |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1456 | if ((*i)->getOwnKind() != K && (*i)->args_end() != |
| 1457 | std::find((*i)->args_begin(), (*i)->args_end(), Idx)) { |
| 1458 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
| 1459 | << AL.getName() << ownershipKindToDiagName((*i)->getOwnKind()); |
| 1460 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1463 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | unsigned* start = OwnershipArgs.data(); |
| 1467 | unsigned size = OwnershipArgs.size(); |
| 1468 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1469 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1470 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1471 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1472 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1475 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1476 | // Check the attribute arguments. |
| 1477 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1478 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1479 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1480 | return; |
| 1481 | } |
| 1482 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1483 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1484 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1485 | // gcc rejects |
| 1486 | // class c { |
| 1487 | // static int a __attribute__((weakref ("v2"))); |
| 1488 | // static int b() __attribute__((weakref ("f3"))); |
| 1489 | // }; |
| 1490 | // and ignores the attributes of |
| 1491 | // void f(void) { |
| 1492 | // static int a __attribute__((weakref ("v2"))); |
| 1493 | // } |
| 1494 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1495 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1496 | if (!Ctx->isFileContext()) { |
| 1497 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) << |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1498 | nd->getNameAsString(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1499 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
| 1502 | // The GCC manual says |
| 1503 | // |
| 1504 | // At present, a declaration to which `weakref' is attached can only |
| 1505 | // be `static'. |
| 1506 | // |
| 1507 | // It also says |
| 1508 | // |
| 1509 | // Without a TARGET, |
| 1510 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1511 | // equivalent to `weak'. |
| 1512 | // |
| 1513 | // gcc 4.4.1 will accept |
| 1514 | // int a7 __attribute__((weakref)); |
| 1515 | // as |
| 1516 | // int a7 __attribute__((weak)); |
| 1517 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1518 | // it if this behaviour is actually used. |
| 1519 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1520 | // GCC rejects |
| 1521 | // static ((alias ("y"), weakref)). |
| 1522 | // Should we? How to check that weakref is before or after alias? |
| 1523 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1524 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1525 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1526 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1527 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1528 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1529 | // GCC will accept anything as the argument of weakref. Should we |
| 1530 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1531 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1532 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1533 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1534 | D->addAttr(::new (S.Context) |
| 1535 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1536 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1539 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1540 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1541 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1542 | return; |
| 1543 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1544 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1545 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1546 | return; |
| 1547 | } |
| 1548 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1549 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1550 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1551 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1552 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1553 | } |
| 1554 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1555 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1556 | if (D->hasAttr<HotAttr>()) { |
| 1557 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1558 | << Attr.getName() << "hot"; |
| 1559 | return; |
| 1560 | } |
| 1561 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1562 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1563 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
| 1566 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1567 | if (D->hasAttr<ColdAttr>()) { |
| 1568 | S.Diag(Attr.getLoc(), diag::err_attributes_are_not_compatible) |
| 1569 | << Attr.getName() << "cold"; |
| 1570 | return; |
| 1571 | } |
| 1572 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1573 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1574 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1577 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1578 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1579 | StringRef Model; |
| 1580 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1581 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1582 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1583 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1584 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1585 | if (!cast<VarDecl>(D)->getTLSKind()) { |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1586 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1587 | << Attr.getName() << ExpectedTLSVar; |
| 1588 | return; |
| 1589 | } |
| 1590 | |
| 1591 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1592 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1593 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1594 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1595 | return; |
| 1596 | } |
| 1597 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1598 | D->addAttr(::new (S.Context) |
| 1599 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1600 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1601 | } |
| 1602 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1603 | static void handleMallocAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1604 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | QualType RetTy = FD->getResultType(); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1606 | if (RetTy->isAnyPointerType() || RetTy->isBlockPointerType()) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1607 | D->addAttr(::new (S.Context) |
| 1608 | MallocAttr(Attr.getRange(), S.Context, |
| 1609 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1610 | return; |
| 1611 | } |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Ted Kremenek | 08479ae | 2009-08-15 00:51:46 +0000 | [diff] [blame] | 1614 | S.Diag(Attr.getLoc(), diag::warn_attribute_malloc_pointer_only); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1617 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1618 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1619 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 1620 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1621 | return; |
| 1622 | } |
| 1623 | |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1624 | D->addAttr(::new (S.Context) CommonAttr(Attr.getRange(), S.Context, |
| 1625 | Attr.getAttributeSpellingListIndex())); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1628 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1629 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1630 | |
| 1631 | if (S.CheckNoReturnAttr(attr)) return; |
| 1632 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1633 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1634 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1635 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1636 | return; |
| 1637 | } |
| 1638 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1639 | D->addAttr(::new (S.Context) |
| 1640 | NoReturnAttr(attr.getRange(), S.Context, |
| 1641 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
| 1644 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1645 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1646 | attr.setInvalid(); |
| 1647 | return true; |
| 1648 | } |
| 1649 | |
| 1650 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1653 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1654 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1655 | |
| 1656 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1657 | // because 'analyzer_noreturn' does not impact the type. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1658 | if (!isFunctionOrMethod(D) && !isa<BlockDecl>(D)) { |
| 1659 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1660 | if (VD == 0 || (!VD->getType()->isBlockPointerType() |
| 1661 | && !VD->getType()->isFunctionPointerType())) { |
| 1662 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1663 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1664 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1665 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1666 | return; |
| 1667 | } |
| 1668 | } |
| 1669 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1670 | D->addAttr(::new (S.Context) |
| 1671 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1672 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1675 | static void handleCXX11NoReturnAttr(Sema &S, Decl *D, |
| 1676 | const AttributeList &Attr) { |
| 1677 | // C++11 [dcl.attr.noreturn]p1: |
| 1678 | // The attribute may be applied to the declarator-id in a function |
| 1679 | // declaration. |
| 1680 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
| 1681 | if (!FD) { |
| 1682 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 1683 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 1684 | return; |
| 1685 | } |
| 1686 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1687 | D->addAttr(::new (S.Context) |
| 1688 | CXX11NoReturnAttr(Attr.getRange(), S.Context, |
| 1689 | Attr.getAttributeSpellingListIndex())); |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1692 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1693 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1694 | /* |
| 1695 | Returning a Vector Class in Registers |
| 1696 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1697 | According to the PPU ABI specifications, a class with a single member of |
| 1698 | vector type is returned in memory when used as the return value of a function. |
| 1699 | This results in inefficient code when implementing vector classes. To return |
| 1700 | the value in a single vector register, add the vecreturn attribute to the |
| 1701 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1702 | |
| 1703 | Example: |
| 1704 | |
| 1705 | struct Vector |
| 1706 | { |
| 1707 | __vector float xyzw; |
| 1708 | } __attribute__((vecreturn)); |
| 1709 | |
| 1710 | Vector Add(Vector lhs, Vector rhs) |
| 1711 | { |
| 1712 | Vector result; |
| 1713 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1714 | return result; // This will be returned in a register |
| 1715 | } |
| 1716 | */ |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1717 | if (D->getAttr<VecReturnAttr>()) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1718 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << "vecreturn"; |
| 1719 | return; |
| 1720 | } |
| 1721 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1722 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1723 | int count = 0; |
| 1724 | |
| 1725 | if (!isa<CXXRecordDecl>(record)) { |
| 1726 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1727 | return; |
| 1728 | } |
| 1729 | |
| 1730 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1731 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1732 | return; |
| 1733 | } |
| 1734 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1735 | for (RecordDecl::field_iterator iter = record->field_begin(); |
| 1736 | iter != record->field_end(); iter++) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1737 | if ((count == 1) || !iter->getType()->isVectorType()) { |
| 1738 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1739 | return; |
| 1740 | } |
| 1741 | count++; |
| 1742 | } |
| 1743 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1744 | D->addAttr(::new (S.Context) |
| 1745 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1746 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1749 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1750 | const AttributeList &Attr) { |
| 1751 | if (isa<ParmVarDecl>(D)) { |
| 1752 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1753 | // parameter of a function declaration or lambda. |
| 1754 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1755 | S.Diag(Attr.getLoc(), |
| 1756 | diag::err_carries_dependency_param_not_function_decl); |
| 1757 | return; |
| 1758 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1759 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1760 | |
| 1761 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1762 | Attr.getRange(), S.Context, |
| 1763 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1766 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1767 | if (!isa<VarDecl>(D) && !isa<ObjCIvarDecl>(D) && !isFunctionOrMethod(D) && |
Daniel Jasper | 429c134 | 2012-06-13 18:31:09 +0000 | [diff] [blame] | 1768 | !isa<TypeDecl>(D) && !isa<LabelDecl>(D) && !isa<FieldDecl>(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 1769 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1770 | << Attr.getName() << ExpectedVariableFunctionOrLabel; |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1771 | return; |
| 1772 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1773 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1774 | D->addAttr(::new (S.Context) |
| 1775 | UnusedAttr(Attr.getRange(), S.Context, |
| 1776 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 39c59a8 | 2008-07-25 04:39:19 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1779 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1780 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1781 | if (VD->hasLocalStorage()) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1782 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used"; |
| 1783 | return; |
| 1784 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1785 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1786 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1787 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1788 | return; |
| 1789 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1790 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1791 | D->addAttr(::new (S.Context) |
| 1792 | UsedAttr(Attr.getRange(), S.Context, |
| 1793 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1796 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1797 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1798 | if (Attr.getNumArgs() > 1) { |
| 1799 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1800 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1801 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1802 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1803 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1804 | if (Attr.getNumArgs() > 0 && |
| 1805 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1806 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1807 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1808 | D->addAttr(::new (S.Context) |
| 1809 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1810 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1813 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1814 | // check the attribute arguments. |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1815 | if (Attr.getNumArgs() > 1) { |
| 1816 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1817 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1818 | } |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1819 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1820 | uint32_t priority = ConstructorAttr::DefaultPriority; |
| 1821 | if (Attr.getNumArgs() > 0 && |
| 1822 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1823 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1824 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1825 | D->addAttr(::new (S.Context) |
| 1826 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1827 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1830 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1831 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1832 | const AttributeList &Attr) { |
Chris Lattner | 190aa10 | 2011-02-24 05:42:24 +0000 | [diff] [blame] | 1833 | unsigned NumArgs = Attr.getNumArgs(); |
| 1834 | if (NumArgs > 1) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 1835 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1836 | return; |
| 1837 | } |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1838 | |
| 1839 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1840 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1841 | if (NumArgs == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1842 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1843 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1844 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1845 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1848 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
| 1849 | const AttributeList &Attr) { |
Ted Kremenek | 7559b47 | 2013-11-23 22:29:11 +0000 | [diff] [blame] | 1850 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1851 | |
| 1852 | if (!Parm) { |
| 1853 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 1; |
| 1854 | return; |
| 1855 | } |
| 1856 | |
| 1857 | D->addAttr(::new (S.Context) |
| 1858 | ObjCSuppressProtocolAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 1859 | Attr.getAttributeSpellingListIndex())); |
| 1860 | } |
| 1861 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1862 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1863 | IdentifierInfo *Platform, |
| 1864 | VersionTuple Introduced, |
| 1865 | VersionTuple Deprecated, |
| 1866 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1867 | StringRef PlatformName |
| 1868 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1869 | if (PlatformName.empty()) |
| 1870 | PlatformName = Platform->getName(); |
| 1871 | |
| 1872 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1873 | // of these steps are needed). |
| 1874 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1875 | !(Introduced <= Deprecated)) { |
| 1876 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1877 | << 1 << PlatformName << Deprecated.getAsString() |
| 1878 | << 0 << Introduced.getAsString(); |
| 1879 | return true; |
| 1880 | } |
| 1881 | |
| 1882 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1883 | !(Introduced <= Obsoleted)) { |
| 1884 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1885 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1886 | << 0 << Introduced.getAsString(); |
| 1887 | return true; |
| 1888 | } |
| 1889 | |
| 1890 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1891 | !(Deprecated <= Obsoleted)) { |
| 1892 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1893 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1894 | << 1 << Deprecated.getAsString(); |
| 1895 | return true; |
| 1896 | } |
| 1897 | |
| 1898 | return false; |
| 1899 | } |
| 1900 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1901 | /// \brief Check whether the two versions match. |
| 1902 | /// |
| 1903 | /// If either version tuple is empty, then they are assumed to match. If |
| 1904 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1905 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1906 | bool BeforeIsOkay) { |
| 1907 | if (X.empty() || Y.empty()) |
| 1908 | return true; |
| 1909 | |
| 1910 | if (X == Y) |
| 1911 | return true; |
| 1912 | |
| 1913 | if (BeforeIsOkay && X < Y) |
| 1914 | return true; |
| 1915 | |
| 1916 | return false; |
| 1917 | } |
| 1918 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1919 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1920 | IdentifierInfo *Platform, |
| 1921 | VersionTuple Introduced, |
| 1922 | VersionTuple Deprecated, |
| 1923 | VersionTuple Obsoleted, |
| 1924 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1925 | StringRef Message, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1926 | bool Override, |
| 1927 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1928 | VersionTuple MergedIntroduced = Introduced; |
| 1929 | VersionTuple MergedDeprecated = Deprecated; |
| 1930 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1931 | bool FoundAny = false; |
| 1932 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1933 | if (D->hasAttrs()) { |
| 1934 | AttrVec &Attrs = D->getAttrs(); |
| 1935 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1936 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1937 | if (!OldAA) { |
| 1938 | ++i; |
| 1939 | continue; |
| 1940 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1941 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1942 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1943 | if (OldPlatform != Platform) { |
| 1944 | ++i; |
| 1945 | continue; |
| 1946 | } |
| 1947 | |
| 1948 | FoundAny = true; |
| 1949 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1950 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1951 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1952 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1953 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1954 | if (!versionsMatch(OldIntroduced, Introduced, Override) || |
| 1955 | !versionsMatch(Deprecated, OldDeprecated, Override) || |
| 1956 | !versionsMatch(Obsoleted, OldObsoleted, Override) || |
| 1957 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | 43dc0c7 | 2013-01-16 00:54:48 +0000 | [diff] [blame] | 1958 | (Override && !OldIsUnavailable && IsUnavailable))) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1959 | if (Override) { |
| 1960 | int Which = -1; |
| 1961 | VersionTuple FirstVersion; |
| 1962 | VersionTuple SecondVersion; |
| 1963 | if (!versionsMatch(OldIntroduced, Introduced, Override)) { |
| 1964 | Which = 0; |
| 1965 | FirstVersion = OldIntroduced; |
| 1966 | SecondVersion = Introduced; |
| 1967 | } else if (!versionsMatch(Deprecated, OldDeprecated, Override)) { |
| 1968 | Which = 1; |
| 1969 | FirstVersion = Deprecated; |
| 1970 | SecondVersion = OldDeprecated; |
| 1971 | } else if (!versionsMatch(Obsoleted, OldObsoleted, Override)) { |
| 1972 | Which = 2; |
| 1973 | FirstVersion = Obsoleted; |
| 1974 | SecondVersion = OldObsoleted; |
| 1975 | } |
| 1976 | |
| 1977 | if (Which == -1) { |
| 1978 | Diag(OldAA->getLocation(), |
| 1979 | diag::warn_mismatched_availability_override_unavail) |
| 1980 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1981 | } else { |
| 1982 | Diag(OldAA->getLocation(), |
| 1983 | diag::warn_mismatched_availability_override) |
| 1984 | << Which |
| 1985 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 1986 | << FirstVersion.getAsString() << SecondVersion.getAsString(); |
| 1987 | } |
| 1988 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 1989 | } else { |
| 1990 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 1991 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 1992 | } |
| 1993 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1994 | Attrs.erase(Attrs.begin() + i); |
| 1995 | --e; |
| 1996 | continue; |
| 1997 | } |
| 1998 | |
| 1999 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2000 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2001 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2002 | |
| 2003 | if (MergedIntroduced2.empty()) |
| 2004 | MergedIntroduced2 = OldIntroduced; |
| 2005 | if (MergedDeprecated2.empty()) |
| 2006 | MergedDeprecated2 = OldDeprecated; |
| 2007 | if (MergedObsoleted2.empty()) |
| 2008 | MergedObsoleted2 = OldObsoleted; |
| 2009 | |
| 2010 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2011 | MergedIntroduced2, MergedDeprecated2, |
| 2012 | MergedObsoleted2)) { |
| 2013 | Attrs.erase(Attrs.begin() + i); |
| 2014 | --e; |
| 2015 | continue; |
| 2016 | } |
| 2017 | |
| 2018 | MergedIntroduced = MergedIntroduced2; |
| 2019 | MergedDeprecated = MergedDeprecated2; |
| 2020 | MergedObsoleted = MergedObsoleted2; |
| 2021 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2022 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | if (FoundAny && |
| 2026 | MergedIntroduced == Introduced && |
| 2027 | MergedDeprecated == Deprecated && |
| 2028 | MergedObsoleted == Obsoleted) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2029 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2030 | |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2031 | // Only create a new attribute if !Override, but we want to do |
| 2032 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2033 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2034 | MergedDeprecated, MergedObsoleted) && |
| 2035 | !Override) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2036 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2037 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2038 | Obsoleted, IsUnavailable, Message, |
| 2039 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2040 | } |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2041 | return NULL; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2044 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2045 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2046 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2047 | return; |
| 2048 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2049 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2050 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2051 | IdentifierInfo *II = Platform->Ident; |
| 2052 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2053 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2054 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2055 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2056 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2057 | if (!ND) { |
| 2058 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2059 | return; |
| 2060 | } |
| 2061 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2062 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2063 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2064 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2065 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2066 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 2067 | if (const StringLiteral *SE = |
| 2068 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2069 | Str = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2070 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2071 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2072 | Introduced.Version, |
| 2073 | Deprecated.Version, |
| 2074 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2075 | IsUnavailable, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2076 | /*Override=*/false, |
| 2077 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2078 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2079 | D->addAttr(NewAttr); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2080 | } |
| 2081 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2082 | template <class T> |
| 2083 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2084 | typename T::VisibilityType value, |
| 2085 | unsigned attrSpellingListIndex) { |
| 2086 | T *existingAttr = D->getAttr<T>(); |
| 2087 | if (existingAttr) { |
| 2088 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2089 | if (existingValue == value) |
| 2090 | return NULL; |
| 2091 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2092 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2093 | D->dropAttr<T>(); |
| 2094 | } |
| 2095 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2096 | } |
| 2097 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2098 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2099 | VisibilityAttr::VisibilityType Vis, |
| 2100 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2101 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2102 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2103 | } |
| 2104 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2105 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2106 | TypeVisibilityAttr::VisibilityType Vis, |
| 2107 | unsigned AttrSpellingListIndex) { |
| 2108 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2109 | AttrSpellingListIndex); |
| 2110 | } |
| 2111 | |
| 2112 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2113 | bool isTypeVisibility) { |
| 2114 | // Visibility attributes don't mean anything on a typedef. |
| 2115 | if (isa<TypedefNameDecl>(D)) { |
| 2116 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2117 | << Attr.getName(); |
| 2118 | return; |
| 2119 | } |
| 2120 | |
| 2121 | // 'type_visibility' can only go on a type or namespace. |
| 2122 | if (isTypeVisibility && |
| 2123 | !(isa<TagDecl>(D) || |
| 2124 | isa<ObjCInterfaceDecl>(D) || |
| 2125 | isa<NamespaceDecl>(D))) { |
| 2126 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2127 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2128 | return; |
| 2129 | } |
| 2130 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2131 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2132 | StringRef TypeStr; |
| 2133 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2134 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2135 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2136 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2137 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2138 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2139 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2140 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2141 | return; |
| 2142 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2143 | |
| 2144 | // Complain about attempts to use protected visibility on targets |
| 2145 | // (like Darwin) that don't support it. |
| 2146 | if (type == VisibilityAttr::Protected && |
| 2147 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2148 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2149 | type = VisibilityAttr::Default; |
| 2150 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2151 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2152 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2153 | clang::Attr *newAttr; |
| 2154 | if (isTypeVisibility) { |
| 2155 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2156 | (TypeVisibilityAttr::VisibilityType) type, |
| 2157 | Index); |
| 2158 | } else { |
| 2159 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2160 | } |
| 2161 | if (newAttr) |
| 2162 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2165 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2166 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2167 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2168 | if (!Attr.isArgIdent(0)) { |
| 2169 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2170 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2171 | return; |
| 2172 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2173 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2174 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2175 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2176 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2177 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2178 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2179 | return; |
| 2180 | } |
| 2181 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2182 | if (F == ObjCMethodFamilyAttr::OMF_init && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2183 | !method->getResultType()->isObjCObjectPointerType()) { |
| 2184 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
| 2185 | << method->getResultType(); |
| 2186 | // Ignore the attribute. |
| 2187 | return; |
| 2188 | } |
| 2189 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2190 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2191 | S.Context, F)); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2194 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2195 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2196 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2197 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2198 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2199 | return; |
| 2200 | } |
| 2201 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2202 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2203 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2204 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2205 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2206 | return; |
| 2207 | } |
| 2208 | } |
| 2209 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2210 | // It is okay to include this attribute on properties, e.g.: |
| 2211 | // |
| 2212 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2213 | // |
| 2214 | // In this case it follows tradition and suppresses an error in the above |
| 2215 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2216 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2217 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2218 | D->addAttr(::new (S.Context) |
| 2219 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2220 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2223 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2224 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2225 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2226 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2227 | return; |
| 2228 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2229 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2230 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2231 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2232 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2233 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2234 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2235 | return; |
| 2236 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2237 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2238 | D->addAttr(::new (S.Context) |
| 2239 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2240 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2243 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2244 | // check the attribute arguments. |
| 2245 | if (Attr.getNumArgs() > 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 2246 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2247 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2250 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2251 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2252 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2253 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2254 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2255 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2256 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2257 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2258 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2259 | return; |
| 2260 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2261 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2262 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2263 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2264 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2265 | return; |
| 2266 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2267 | |
| 2268 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2271 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2272 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2273 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2274 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2275 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2276 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2277 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2278 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2279 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2280 | return; |
| 2281 | } |
| 2282 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2283 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2284 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2285 | // FIXME: This error message could be improved, it would be nice |
| 2286 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2287 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2288 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2289 | return; |
| 2290 | } |
| 2291 | } |
| 2292 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2293 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2294 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2295 | if (isa<FunctionNoProtoType>(FT)) { |
| 2296 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2297 | return; |
| 2298 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2299 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2300 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2301 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2302 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2303 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2304 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2305 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2306 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2307 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2308 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2309 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2310 | if (!BD->isVariadic()) { |
| 2311 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2312 | return; |
| 2313 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2314 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2315 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2316 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2317 | const FunctionType *FT = Ty->isFunctionPointerType() ? getFunctionType(D) |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2318 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2319 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2320 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2321 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2322 | return; |
| 2323 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2324 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2325 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2326 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2327 | return; |
| 2328 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2329 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2330 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2331 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2332 | return; |
| 2333 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2334 | D->addAttr(::new (S.Context) |
| 2335 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2336 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2339 | static void handleWarnUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 2340 | if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 2341 | RD->addAttr(::new (S.Context) WarnUnusedAttr(Attr.getRange(), S.Context)); |
| 2342 | else |
| 2343 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2344 | } |
| 2345 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2346 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Kaelyn Uhrain | 8681f9d | 2012-11-12 23:48:05 +0000 | [diff] [blame] | 2347 | if (!isFunction(D) && !isa<ObjCMethodDecl>(D) && !isa<CXXRecordDecl>(D)) { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2348 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Kaelyn Uhrain | 3d699e0 | 2012-11-13 00:18:47 +0000 | [diff] [blame] | 2349 | << Attr.getName() << ExpectedFunctionMethodOrClass; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2350 | return; |
| 2351 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2352 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2353 | if (isFunction(D) && getFunctionType(D)->getResultType()->isVoidType()) { |
| 2354 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2355 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2356 | return; |
| 2357 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2358 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 2359 | if (MD->getResultType()->isVoidType()) { |
| 2360 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2361 | << Attr.getName() << 1; |
| 2362 | return; |
| 2363 | } |
| 2364 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2365 | D->addAttr(::new (S.Context) |
| 2366 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2367 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2370 | static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2371 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) { |
Fariborz Jahanian | 47f9a73 | 2011-10-21 22:27:12 +0000 | [diff] [blame] | 2372 | if (isa<CXXRecordDecl>(D)) { |
| 2373 | D->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); |
| 2374 | return; |
| 2375 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2376 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2377 | << Attr.getName() << ExpectedVariableOrFunction; |
Fariborz Jahanian | 41136ee | 2009-07-16 01:12:24 +0000 | [diff] [blame] | 2378 | return; |
| 2379 | } |
| 2380 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2381 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 2382 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2383 | nd->addAttr(::new (S.Context) |
| 2384 | WeakAttr(Attr.getRange(), S.Context, |
| 2385 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2386 | } |
| 2387 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2388 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2389 | // weak_import only applies to variable & function declarations. |
| 2390 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2391 | if (!D->canBeWeakImported(isDef)) { |
| 2392 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2393 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2394 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2395 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2396 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2397 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2398 | // Nothing to warn about here. |
| 2399 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2400 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2401 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2402 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2403 | return; |
| 2404 | } |
| 2405 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2406 | D->addAttr(::new (S.Context) |
| 2407 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2408 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2409 | } |
| 2410 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2411 | // Handles reqd_work_group_size and work_group_size_hint. |
| 2412 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2413 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2414 | uint32_t WGSize[3]; |
| 2415 | for (unsigned i = 0; i < 3; ++i) |
| 2416 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(i), WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2417 | return; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2418 | |
| 2419 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize |
| 2420 | && D->hasAttr<ReqdWorkGroupSizeAttr>()) { |
| 2421 | ReqdWorkGroupSizeAttr *A = D->getAttr<ReqdWorkGroupSizeAttr>(); |
| 2422 | if (!(A->getXDim() == WGSize[0] && |
| 2423 | A->getYDim() == WGSize[1] && |
| 2424 | A->getZDim() == WGSize[2])) { |
| 2425 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2426 | Attr.getName(); |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | if (Attr.getKind() == AttributeList::AT_WorkGroupSizeHint |
| 2431 | && D->hasAttr<WorkGroupSizeHintAttr>()) { |
| 2432 | WorkGroupSizeHintAttr *A = D->getAttr<WorkGroupSizeHintAttr>(); |
| 2433 | if (!(A->getXDim() == WGSize[0] && |
| 2434 | A->getYDim() == WGSize[1] && |
| 2435 | A->getZDim() == WGSize[2])) { |
| 2436 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << |
| 2437 | Attr.getName(); |
| 2438 | } |
| 2439 | } |
| 2440 | |
| 2441 | if (Attr.getKind() == AttributeList::AT_ReqdWorkGroupSize) |
| 2442 | D->addAttr(::new (S.Context) |
| 2443 | ReqdWorkGroupSizeAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2444 | WGSize[0], WGSize[1], WGSize[2], |
| 2445 | Attr.getAttributeSpellingListIndex())); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2446 | else |
| 2447 | D->addAttr(::new (S.Context) |
| 2448 | WorkGroupSizeHintAttr(Attr.getRange(), S.Context, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2449 | WGSize[0], WGSize[1], WGSize[2], |
| 2450 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2453 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2454 | assert(Attr.getKind() == AttributeList::AT_VecTypeHint); |
| 2455 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2456 | if (!Attr.hasParsedType()) { |
| 2457 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2458 | << Attr.getName() << 1; |
| 2459 | return; |
| 2460 | } |
| 2461 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2462 | TypeSourceInfo *ParmTSI = 0; |
| 2463 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2464 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2465 | |
| 2466 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2467 | (ParmType->isBooleanType() || |
| 2468 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2469 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2470 | << ParmType; |
| 2471 | return; |
| 2472 | } |
| 2473 | |
| 2474 | if (Attr.getKind() == AttributeList::AT_VecTypeHint && |
| 2475 | D->hasAttr<VecTypeHintAttr>()) { |
| 2476 | VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>(); |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2477 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2478 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2479 | return; |
| 2480 | } |
| 2481 | } |
| 2482 | |
| 2483 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2484 | ParmTSI)); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2485 | } |
| 2486 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2487 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2488 | StringRef Name, |
| 2489 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2490 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2491 | if (ExistingAttr->getName() == Name) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2492 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2493 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2494 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2495 | return NULL; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2496 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2497 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2498 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2501 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2502 | // Make sure that there is a string literal as the sections's single |
| 2503 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2504 | StringRef Str; |
| 2505 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2506 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2507 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2508 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2509 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2510 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2511 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2512 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2513 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2514 | return; |
| 2515 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2516 | |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2517 | // This attribute cannot be applied to local variables. |
| 2518 | if (isa<VarDecl>(D) && cast<VarDecl>(D)->hasLocalStorage()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2519 | S.Diag(LiteralLoc, diag::err_attribute_section_local_variable); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2520 | return; |
| 2521 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2522 | |
| 2523 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2524 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2525 | if (NewAttr) |
| 2526 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2529 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2530 | static void handleNothrowAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2531 | if (NoThrowAttr *Existing = D->getAttr<NoThrowAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2532 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2533 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2534 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2535 | D->addAttr(::new (S.Context) |
| 2536 | NoThrowAttr(Attr.getRange(), S.Context, |
| 2537 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2538 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2539 | } |
| 2540 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2541 | static void handleConstAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2542 | if (ConstAttr *Existing = D->getAttr<ConstAttr>()) { |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2543 | if (Existing->getLocation().isInvalid()) |
Argyrios Kyrtzidis | 635a9b4 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 2544 | Existing->setRange(Attr.getRange()); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2545 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2546 | D->addAttr(::new (S.Context) |
| 2547 | ConstAttr(Attr.getRange(), S.Context, |
| 2548 | Attr.getAttributeSpellingListIndex() )); |
Douglas Gregor | 8833683 | 2011-06-15 05:45:11 +0000 | [diff] [blame] | 2549 | } |
Anders Carlsson | b831628 | 2008-10-05 23:32:53 +0000 | [diff] [blame] | 2550 | } |
| 2551 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2552 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2553 | VarDecl *VD = cast<VarDecl>(D); |
| 2554 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2555 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2556 | return; |
| 2557 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2558 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2559 | Expr *E = Attr.getArgAsExpr(0); |
| 2560 | SourceLocation Loc = E->getExprLoc(); |
| 2561 | FunctionDecl *FD = 0; |
| 2562 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2563 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2564 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2565 | // will warn the user. |
| 2566 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2567 | if (DRE->hasQualifier()) |
| 2568 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2569 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2570 | NI = DRE->getNameInfo(); |
| 2571 | if (!FD) { |
| 2572 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2573 | << NI.getName(); |
| 2574 | return; |
| 2575 | } |
| 2576 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2577 | if (ULE->hasExplicitTemplateArgs()) |
| 2578 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2579 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2580 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2581 | if (!FD) { |
| 2582 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2583 | << NI.getName(); |
| 2584 | if (ULE->getType() == S.Context.OverloadTy) |
| 2585 | S.NoteAllOverloadCandidates(ULE); |
| 2586 | return; |
| 2587 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2588 | } else { |
| 2589 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2590 | return; |
| 2591 | } |
| 2592 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2593 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2594 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2595 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2596 | return; |
| 2597 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2598 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2599 | // We're currently more strict than GCC about what function types we accept. |
| 2600 | // If this ever proves to be a problem it should be easy to fix. |
| 2601 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2602 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2603 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2604 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2605 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2606 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2607 | return; |
| 2608 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2609 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2610 | D->addAttr(::new (S.Context) |
| 2611 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2612 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2613 | } |
| 2614 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2615 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2616 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2617 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2618 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2619 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2620 | << Attr.getName() << ExpectedFunction; |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2621 | return; |
| 2622 | } |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2623 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2624 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2625 | uint64_t ArgIdx; |
| 2626 | if (!checkFunctionOrMethodArgumentIndex(S, D, Attr.getName()->getName(), |
| 2627 | Attr.getLoc(), 1, IdxExpr, ArgIdx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2628 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2629 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2630 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2631 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2632 | |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2633 | bool not_nsstring_type = !isNSStringType(Ty, S.Context); |
| 2634 | if (not_nsstring_type && |
| 2635 | !isCFStringType(Ty, S.Context) && |
| 2636 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2637 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2638 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2639 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2640 | << (not_nsstring_type ? "a string type" : "an NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2641 | << IdxExpr->getSourceRange(); |
| 2642 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2643 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2644 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2645 | if (!isNSStringType(Ty, S.Context) && |
| 2646 | !isCFStringType(Ty, S.Context) && |
| 2647 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2648 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2649 | // FIXME: Should highlight the actual expression that has the wrong type. |
| 2650 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2651 | << (not_nsstring_type ? "string type" : "NSString") |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2652 | << IdxExpr->getSourceRange(); |
| 2653 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2656 | // We cannot use the ArgIdx returned from checkFunctionOrMethodArgumentIndex |
| 2657 | // because that has corrected for the implicit this parameter, and is zero- |
| 2658 | // based. The attribute expects what the user wrote explicitly. |
| 2659 | llvm::APSInt Val; |
| 2660 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2661 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2662 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2663 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2664 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2667 | enum FormatAttrKind { |
| 2668 | CFStringFormat, |
| 2669 | NSStringFormat, |
| 2670 | StrftimeFormat, |
| 2671 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2672 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2673 | InvalidFormat |
| 2674 | }; |
| 2675 | |
| 2676 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2677 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2678 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2679 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2680 | // Check for formats that get handled specially. |
| 2681 | .Case("NSString", NSStringFormat) |
| 2682 | .Case("CFString", CFStringFormat) |
| 2683 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2684 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2685 | // Otherwise, check for supported formats. |
| 2686 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2687 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2688 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2689 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2690 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2691 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2692 | } |
| 2693 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2694 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2695 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2696 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2697 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2698 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2699 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2700 | return; |
| 2701 | } |
| 2702 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2703 | if (!isa<VarDecl>(D) || S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2704 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2705 | Attr.setInvalid(); |
| 2706 | return; |
| 2707 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2708 | QualType T = dyn_cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2709 | if (S.Context.getAsArrayType(T)) |
| 2710 | T = S.Context.getBaseElementType(T); |
| 2711 | if (!T->getAs<RecordType>()) { |
| 2712 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2713 | Attr.setInvalid(); |
| 2714 | return; |
| 2715 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2716 | |
| 2717 | Expr *E = Attr.getArgAsExpr(0); |
| 2718 | uint32_t prioritynum; |
| 2719 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2720 | Attr.setInvalid(); |
| 2721 | return; |
| 2722 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2723 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2724 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2725 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2726 | << E->getSourceRange(); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2727 | Attr.setInvalid(); |
| 2728 | return; |
| 2729 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2730 | D->addAttr(::new (S.Context) |
| 2731 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2732 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2733 | } |
| 2734 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2735 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2736 | IdentifierInfo *Format, int FormatIdx, |
| 2737 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2738 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2739 | // Check whether we already have an equivalent format attribute. |
| 2740 | for (specific_attr_iterator<FormatAttr> |
| 2741 | i = D->specific_attr_begin<FormatAttr>(), |
| 2742 | e = D->specific_attr_end<FormatAttr>(); |
| 2743 | i != e ; ++i) { |
| 2744 | FormatAttr *f = *i; |
| 2745 | if (f->getType() == Format && |
| 2746 | f->getFormatIdx() == FormatIdx && |
| 2747 | f->getFirstArg() == FirstArg) { |
| 2748 | // If we don't have a valid location for this attribute, adopt the |
| 2749 | // location. |
| 2750 | if (f->getLocation().isInvalid()) |
| 2751 | f->setRange(Range); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2752 | return NULL; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2753 | } |
| 2754 | } |
| 2755 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2756 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2757 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2760 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2761 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2762 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2763 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2764 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2765 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2766 | return; |
| 2767 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2768 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2769 | if (!isFunctionOrMethodOrBlock(D) || !hasFunctionProto(D)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2770 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2771 | << Attr.getName() << ExpectedFunction; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2772 | return; |
| 2773 | } |
| 2774 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2775 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2776 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2777 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 2778 | unsigned NumArgs = getFunctionOrMethodNumArgs(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2779 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2780 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2781 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2782 | |
| 2783 | // Normalize the argument, __foo__ becomes foo. |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2784 | if (Format.startswith("__") && Format.endswith("__")) { |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2785 | Format = Format.substr(2, Format.size() - 4); |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2786 | // If we've modified the string name, we need a new identifier for it. |
| 2787 | II = &S.Context.Idents.get(Format); |
| 2788 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2789 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2790 | // Check for supported formats. |
| 2791 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2792 | |
| 2793 | if (Kind == IgnoredFormat) |
| 2794 | return; |
| 2795 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2796 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2797 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2798 | << "format" << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2799 | return; |
| 2800 | } |
| 2801 | |
| 2802 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2803 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2804 | uint32_t Idx; |
| 2805 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2806 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2807 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2808 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2809 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2810 | << "format" << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2811 | return; |
| 2812 | } |
| 2813 | |
| 2814 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2815 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2816 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2817 | if (HasImplicitThisParam) { |
| 2818 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2819 | S.Diag(Attr.getLoc(), |
| 2820 | diag::err_format_attribute_implicit_this_format_string) |
| 2821 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2822 | return; |
| 2823 | } |
| 2824 | ArgIdx--; |
| 2825 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2826 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2827 | // make sure the format string is really a string |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2828 | QualType Ty = getFunctionOrMethodArgType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2829 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2830 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2831 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2832 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2833 | << "a CFString" << IdxExpr->getSourceRange(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2834 | return; |
| 2835 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2836 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2837 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2838 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2839 | if (!isNSStringType(Ty, S.Context)) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2840 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2841 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2842 | << "an NSString" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2843 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2844 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2845 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2846 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2847 | // FIXME: Should highlight the actual expression that has the wrong type. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2848 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
| 2849 | << "a string type" << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2850 | return; |
| 2851 | } |
| 2852 | |
| 2853 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2854 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2855 | uint32_t FirstArg; |
| 2856 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2857 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2858 | |
| 2859 | // check if the function is variadic if the 3rd argument non-zero |
| 2860 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2861 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2862 | ++NumArgs; // +1 for ... |
| 2863 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2864 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2865 | return; |
| 2866 | } |
| 2867 | } |
| 2868 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2869 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2870 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2871 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2872 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2873 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2874 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2875 | return; |
| 2876 | } |
| 2877 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2878 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2879 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2880 | << "format" << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2881 | return; |
| 2882 | } |
| 2883 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2884 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2885 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2886 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2887 | if (NewAttr) |
| 2888 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2889 | } |
| 2890 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2891 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2892 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2893 | // Try to find the underlying union declaration. |
| 2894 | RecordDecl *RD = 0; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2895 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2896 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2897 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2898 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2899 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2900 | |
| 2901 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2902 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2903 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2904 | return; |
| 2905 | } |
| 2906 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2907 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2908 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2909 | diag::warn_transparent_union_attribute_not_definition); |
| 2910 | return; |
| 2911 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2912 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 2913 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 2914 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2915 | if (Field == FieldEnd) { |
| 2916 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 2917 | return; |
| 2918 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2919 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 2920 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2921 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2922 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2923 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 2924 | diag::warn_transparent_union_attribute_floating) |
| 2925 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2926 | return; |
| 2927 | } |
| 2928 | |
| 2929 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 2930 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 2931 | for (; Field != FieldEnd; ++Field) { |
| 2932 | QualType FieldType = Field->getType(); |
| 2933 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
| 2934 | S.Context.getTypeAlign(FieldType) != FirstAlign) { |
| 2935 | // Warn if we drop the attribute. |
| 2936 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2937 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2938 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2939 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2940 | diag::warn_transparent_union_attribute_field_size_align) |
| 2941 | << isSize << Field->getDeclName() << FieldBits; |
| 2942 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2943 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2944 | diag::note_transparent_union_first_field_size_align) |
| 2945 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 2946 | return; |
| 2947 | } |
| 2948 | } |
| 2949 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2950 | RD->addAttr(::new (S.Context) |
| 2951 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 2952 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2953 | } |
| 2954 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2955 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2956 | // Make sure that there is a string literal as the annotation's single |
| 2957 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2958 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2959 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2960 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2961 | |
| 2962 | // Don't duplicate annotations that are already set. |
| 2963 | for (specific_attr_iterator<AnnotateAttr> |
| 2964 | i = D->specific_attr_begin<AnnotateAttr>(), |
| 2965 | e = D->specific_attr_end<AnnotateAttr>(); i != e; ++i) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2966 | if ((*i)->getAnnotation() == Str) |
| 2967 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 2968 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2969 | |
| 2970 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2971 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2972 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2973 | } |
| 2974 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2975 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2976 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 2977 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 2978 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2979 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2980 | return; |
| 2981 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 2982 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 2983 | if (Attr.getNumArgs() == 0) { |
| 2984 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
| 2985 | true, 0, Attr.getAttributeSpellingListIndex())); |
| 2986 | return; |
| 2987 | } |
| 2988 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2989 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2990 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 2991 | S.Diag(Attr.getEllipsisLoc(), |
| 2992 | diag::err_pack_expansion_without_parameter_packs); |
| 2993 | return; |
| 2994 | } |
| 2995 | |
| 2996 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 2997 | return; |
| 2998 | |
| 2999 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3000 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3004 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3005 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3006 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3007 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3008 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3009 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3010 | // C++11 [dcl.align]p1: |
| 3011 | // An alignment-specifier may be applied to a variable or to a class |
| 3012 | // data member, but it shall not be applied to a bit-field, a function |
| 3013 | // parameter, the formal parameter of a catch clause, or a variable |
| 3014 | // declared with the register storage class specifier. An |
| 3015 | // alignment-specifier may also be applied to the declaration of a class |
| 3016 | // or enumeration type. |
| 3017 | // C11 6.7.5/2: |
| 3018 | // An alignment attribute shall not be specified in a declaration of |
| 3019 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3020 | // object declared with the register storage-class specifier. |
| 3021 | int DiagKind = -1; |
| 3022 | if (isa<ParmVarDecl>(D)) { |
| 3023 | DiagKind = 0; |
| 3024 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3025 | if (VD->getStorageClass() == SC_Register) |
| 3026 | DiagKind = 1; |
| 3027 | if (VD->isExceptionVariable()) |
| 3028 | DiagKind = 2; |
| 3029 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3030 | if (FD->isBitField()) |
| 3031 | DiagKind = 3; |
| 3032 | } else if (!isa<TagDecl>(D)) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3033 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) |
| 3034 | << (TmpAttr.isC11() ? "'_Alignas'" : "'alignas'") |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3035 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3036 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3037 | return; |
| 3038 | } |
| 3039 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3040 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Richard Smith | bc8caaf | 2013-02-22 04:55:39 +0000 | [diff] [blame] | 3041 | << TmpAttr.isC11() << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3042 | return; |
| 3043 | } |
| 3044 | } |
| 3045 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3046 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3047 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3048 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3049 | AA->setPackExpansion(IsPackExpansion); |
| 3050 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3051 | return; |
| 3052 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3053 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3054 | // FIXME: Cache the number on the Attr object? |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3055 | llvm::APSInt Alignment(32); |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3056 | ExprResult ICE |
| 3057 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3058 | diag::err_aligned_attribute_argument_not_int, |
| 3059 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3060 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3061 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3062 | |
| 3063 | // C++11 [dcl.align]p2: |
| 3064 | // -- if the constant expression evaluates to zero, the alignment |
| 3065 | // specifier shall have no effect |
| 3066 | // C11 6.7.5p6: |
| 3067 | // An alignment specification of zero has no effect. |
| 3068 | if (!(TmpAttr.isAlignas() && !Alignment) && |
| 3069 | !llvm::isPowerOf2_64(Alignment.getZExtValue())) { |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3070 | Diag(AttrLoc, diag::err_attribute_aligned_not_power_of_two) |
| 3071 | << E->getSourceRange(); |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3072 | return; |
| 3073 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3074 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3075 | if (TmpAttr.isDeclspec()) { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3076 | // We've already verified it's a power of 2, now let's make sure it's |
| 3077 | // 8192 or less. |
| 3078 | if (Alignment.getZExtValue() > 8192) { |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3079 | Diag(AttrLoc, diag::err_attribute_aligned_greater_than_8192) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3080 | << E->getSourceRange(); |
| 3081 | return; |
| 3082 | } |
| 3083 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3084 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3085 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
| 3086 | ICE.take(), SpellingListIndex); |
| 3087 | AA->setPackExpansion(IsPackExpansion); |
| 3088 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3089 | } |
| 3090 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3091 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3092 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3093 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3094 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3095 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3096 | SpellingListIndex); |
| 3097 | AA->setPackExpansion(IsPackExpansion); |
| 3098 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3099 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3100 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3101 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3102 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3103 | |
| 3104 | QualType Ty; |
| 3105 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3106 | Ty = VD->getType(); |
| 3107 | else |
| 3108 | Ty = Context.getTagDeclType(cast<TagDecl>(D)); |
Richard Smith | 3653b7e | 2013-02-22 09:21:42 +0000 | [diff] [blame] | 3109 | if (Ty->isDependentType() || Ty->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3110 | return; |
| 3111 | |
| 3112 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3113 | // The combined effect of all alignment attributes in a declaration shall |
| 3114 | // not specify an alignment that is less strict than the alignment that |
| 3115 | // would otherwise be required for the entity being declared. |
| 3116 | AlignedAttr *AlignasAttr = 0; |
| 3117 | unsigned Align = 0; |
| 3118 | for (specific_attr_iterator<AlignedAttr> |
| 3119 | I = D->specific_attr_begin<AlignedAttr>(), |
| 3120 | E = D->specific_attr_end<AlignedAttr>(); I != E; ++I) { |
| 3121 | if (I->isAlignmentDependent()) |
| 3122 | return; |
| 3123 | if (I->isAlignas()) |
| 3124 | AlignasAttr = *I; |
| 3125 | Align = std::max(Align, I->getAlignment(Context)); |
| 3126 | } |
| 3127 | |
| 3128 | if (AlignasAttr && Align) { |
| 3129 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
| 3130 | CharUnits NaturalAlign = Context.getTypeAlignInChars(Ty); |
| 3131 | if (NaturalAlign > RequestedAlign) |
| 3132 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
| 3133 | << Ty << (unsigned)NaturalAlign.getQuantity(); |
| 3134 | } |
| 3135 | } |
| 3136 | |
Chandler Carruth | 3ed22c3 | 2011-07-01 23:49:16 +0000 | [diff] [blame] | 3137 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3138 | /// type. |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3139 | /// |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3140 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3141 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3142 | /// HImode, not an intermediate pointer. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3143 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3144 | // This attribute isn't documented, but glibc uses it. It changes |
| 3145 | // the width of an int or unsigned int to the specified size. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3146 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 9744ffd | 2013-07-30 14:29:12 +0000 | [diff] [blame] | 3147 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3148 | << AANT_ArgumentIdentifier; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3149 | return; |
| 3150 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3151 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3152 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
| 3153 | StringRef Str = Name->getName(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3154 | |
| 3155 | // Normalize the attribute name, __foo__ becomes foo. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3156 | if (Str.startswith("__") && Str.endswith("__")) |
| 3157 | Str = Str.substr(2, Str.size() - 4); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3158 | |
| 3159 | unsigned DestWidth = 0; |
| 3160 | bool IntegerMode = true; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3161 | bool ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3162 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3163 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3164 | switch (Str[0]) { |
| 3165 | case 'Q': DestWidth = 8; break; |
| 3166 | case 'H': DestWidth = 16; break; |
| 3167 | case 'S': DestWidth = 32; break; |
| 3168 | case 'D': DestWidth = 64; break; |
| 3169 | case 'X': DestWidth = 96; break; |
| 3170 | case 'T': DestWidth = 128; break; |
| 3171 | } |
| 3172 | if (Str[1] == 'F') { |
| 3173 | IntegerMode = false; |
| 3174 | } else if (Str[1] == 'C') { |
| 3175 | IntegerMode = false; |
| 3176 | ComplexMode = true; |
| 3177 | } else if (Str[1] != 'I') { |
| 3178 | DestWidth = 0; |
| 3179 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3180 | break; |
| 3181 | case 4: |
| 3182 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3183 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3184 | if (Str == "word") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3185 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3186 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3187 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3188 | break; |
| 3189 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3190 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3191 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3192 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3193 | case 11: |
| 3194 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3195 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3196 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3197 | } |
| 3198 | |
| 3199 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3200 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3201 | OldTy = TD->getUnderlyingType(); |
| 3202 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3203 | OldTy = VD->getType(); |
| 3204 | else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3205 | S.Diag(D->getLocation(), diag::err_attr_wrong_decl) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3206 | << "mode" << Attr.getRange(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3207 | return; |
| 3208 | } |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3209 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3210 | if (!OldTy->getAs<BuiltinType>() && !OldTy->isComplexType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3211 | S.Diag(Attr.getLoc(), diag::err_mode_not_primitive); |
| 3212 | else if (IntegerMode) { |
Douglas Gregor | b90df60 | 2010-06-16 00:17:44 +0000 | [diff] [blame] | 3213 | if (!OldTy->isIntegralOrEnumerationType()) |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3214 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3215 | } else if (ComplexMode) { |
| 3216 | if (!OldTy->isComplexType()) |
| 3217 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3218 | } else { |
| 3219 | if (!OldTy->isFloatingType()) |
| 3220 | S.Diag(Attr.getLoc(), diag::err_mode_wrong_type); |
| 3221 | } |
| 3222 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3223 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3224 | // and friends, at least with glibc. |
Eli Friedman | 1efaaea | 2009-02-13 02:31:07 +0000 | [diff] [blame] | 3225 | // FIXME: Make sure floating-point mappings are accurate |
| 3226 | // FIXME: Support XF and TF types |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3227 | if (!DestWidth) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3228 | S.Diag(Attr.getLoc(), diag::err_unknown_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3229 | return; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | QualType NewTy; |
| 3233 | |
| 3234 | if (IntegerMode) |
| 3235 | NewTy = S.Context.getIntTypeForBitwidth(DestWidth, |
| 3236 | OldTy->isSignedIntegerType()); |
| 3237 | else |
| 3238 | NewTy = S.Context.getRealTypeForBitwidth(DestWidth); |
| 3239 | |
| 3240 | if (NewTy.isNull()) { |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3241 | S.Diag(Attr.getLoc(), diag::err_unsupported_machine_mode) << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3242 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3245 | if (ComplexMode) { |
| 3246 | NewTy = S.Context.getComplexType(NewTy); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3250 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3251 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
| 3252 | else |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3253 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3254 | |
| 3255 | D->addAttr(::new (S.Context) |
| 3256 | ModeAttr(Attr.getRange(), S.Context, Name, |
| 3257 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3258 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3259 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3260 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3261 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3262 | if (!VD->hasGlobalStorage()) |
| 3263 | S.Diag(Attr.getLoc(), |
| 3264 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3265 | << Attr.getName(); |
| 3266 | } else if (!isFunctionOrMethod(D)) { |
| 3267 | S.Diag(Attr.getLoc(), |
| 3268 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3269 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3270 | return; |
| 3271 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3272 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3273 | D->addAttr(::new (S.Context) |
| 3274 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3275 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3276 | } |
| 3277 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3278 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3279 | if (S.LangOpts.CUDA) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3280 | D->addAttr(::new (S.Context) |
| 3281 | CUDAConstantAttr(Attr.getRange(), S.Context, |
| 3282 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3283 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3284 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3285 | } |
| 3286 | } |
| 3287 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3288 | static void handleDeviceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3289 | if (S.LangOpts.CUDA) { |
| 3290 | // check the attribute arguments. |
| 3291 | if (Attr.getNumArgs() != 0) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3292 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3293 | << Attr.getName() << 0; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3294 | return; |
| 3295 | } |
| 3296 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3297 | D->addAttr(::new (S.Context) |
| 3298 | CUDADeviceAttr(Attr.getRange(), S.Context, |
| 3299 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3300 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3301 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3302 | } |
| 3303 | } |
| 3304 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3305 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3306 | if (S.LangOpts.CUDA) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3307 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3308 | if (!FD->getResultType()->isVoidType()) { |
Abramo Bagnara | 6d81063 | 2010-12-14 22:11:44 +0000 | [diff] [blame] | 3309 | TypeLoc TL = FD->getTypeSourceInfo()->getTypeLoc().IgnoreParens(); |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3310 | if (FunctionTypeLoc FTL = TL.getAs<FunctionTypeLoc>()) { |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3311 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3312 | << FD->getType() |
David Blaikie | 6adc78e | 2013-02-18 22:06:02 +0000 | [diff] [blame] | 3313 | << FixItHint::CreateReplacement(FTL.getResultLoc().getSourceRange(), |
Peter Collingbourne | e8cfaf4 | 2010-12-12 23:02:57 +0000 | [diff] [blame] | 3314 | "void"); |
| 3315 | } else { |
| 3316 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
| 3317 | << FD->getType(); |
| 3318 | } |
| 3319 | return; |
| 3320 | } |
| 3321 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3322 | D->addAttr(::new (S.Context) |
| 3323 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
| 3324 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3325 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3326 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3327 | } |
| 3328 | } |
| 3329 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3330 | static void handleHostAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3331 | if (S.LangOpts.CUDA) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3332 | D->addAttr(::new (S.Context) |
| 3333 | CUDAHostAttr(Attr.getRange(), S.Context, |
| 3334 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3335 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3336 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3337 | } |
| 3338 | } |
| 3339 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3340 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3341 | if (S.LangOpts.CUDA) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3342 | D->addAttr(::new (S.Context) |
| 3343 | CUDASharedAttr(Attr.getRange(), S.Context, |
| 3344 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3345 | } else { |
Aaron Ballman | ecf81c0 | 2013-11-19 22:18:24 +0000 | [diff] [blame] | 3346 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3347 | } |
| 3348 | } |
| 3349 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3350 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3351 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3352 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3353 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3354 | return; |
| 3355 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3356 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3357 | D->addAttr(::new (S.Context) |
| 3358 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3359 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3362 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3363 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3364 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3365 | const FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3366 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3367 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3368 | CallingConv CC; |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3369 | if (S.CheckCallingConvAttr(Attr, CC, FD)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3370 | return; |
| 3371 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3372 | if (!isa<ObjCMethodDecl>(D)) { |
| 3373 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3374 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3375 | return; |
| 3376 | } |
| 3377 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3378 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3379 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3380 | D->addAttr(::new (S.Context) |
| 3381 | FastCallAttr(Attr.getRange(), S.Context, |
| 3382 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3383 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3384 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3385 | D->addAttr(::new (S.Context) |
| 3386 | StdCallAttr(Attr.getRange(), S.Context, |
| 3387 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3388 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3389 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3390 | D->addAttr(::new (S.Context) |
| 3391 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3392 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3393 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3394 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3395 | D->addAttr(::new (S.Context) |
| 3396 | CDeclAttr(Attr.getRange(), S.Context, |
| 3397 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3398 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3399 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3400 | D->addAttr(::new (S.Context) |
| 3401 | PascalAttr(Attr.getRange(), S.Context, |
| 3402 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3403 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3404 | case AttributeList::AT_MSABI: |
| 3405 | D->addAttr(::new (S.Context) |
| 3406 | MSABIAttr(Attr.getRange(), S.Context, |
| 3407 | Attr.getAttributeSpellingListIndex())); |
| 3408 | return; |
| 3409 | case AttributeList::AT_SysVABI: |
| 3410 | D->addAttr(::new (S.Context) |
| 3411 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3412 | Attr.getAttributeSpellingListIndex())); |
| 3413 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3414 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3415 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3416 | switch (CC) { |
| 3417 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3418 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3419 | break; |
| 3420 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3421 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3422 | break; |
| 3423 | default: |
| 3424 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3427 | D->addAttr(::new (S.Context) |
| 3428 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3429 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3430 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3431 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3432 | case AttributeList::AT_PnaclCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3433 | D->addAttr(::new (S.Context) |
| 3434 | PnaclCallAttr(Attr.getRange(), S.Context, |
| 3435 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3436 | return; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3437 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3438 | D->addAttr(::new (S.Context) |
| 3439 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3440 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3441 | return; |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3442 | |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3443 | default: |
| 3444 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3445 | } |
| 3446 | } |
| 3447 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3448 | static void handleOpenCLImageAccessAttr(Sema &S, Decl *D, |
| 3449 | const AttributeList &Attr) { |
| 3450 | uint32_t ArgNum; |
| 3451 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), ArgNum)) |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3452 | return; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3453 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3454 | D->addAttr(::new (S.Context) OpenCLImageAccessAttr(Attr.getRange(), |
| 3455 | S.Context, ArgNum)); |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 3456 | } |
| 3457 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3458 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3459 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3460 | if (attr.isInvalid()) |
| 3461 | return true; |
| 3462 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3463 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3464 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3465 | attr.setInvalid(); |
| 3466 | return true; |
| 3467 | } |
| 3468 | |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3469 | // TODO: diagnose uses of these conventions on the wrong target. Or, better |
| 3470 | // move to TargetAttributesSema one day. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3471 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3472 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3473 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3474 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3475 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3476 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3477 | case AttributeList::AT_MSABI: |
| 3478 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3479 | CC_X86_64Win64; |
| 3480 | break; |
| 3481 | case AttributeList::AT_SysVABI: |
| 3482 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3483 | CC_C; |
| 3484 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3485 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3486 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3487 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3488 | attr.setInvalid(); |
| 3489 | return true; |
| 3490 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3491 | if (StrRef == "aapcs") { |
| 3492 | CC = CC_AAPCS; |
| 3493 | break; |
| 3494 | } else if (StrRef == "aapcs-vfp") { |
| 3495 | CC = CC_AAPCS_VFP; |
| 3496 | break; |
| 3497 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3498 | |
| 3499 | attr.setInvalid(); |
| 3500 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3501 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3502 | } |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3503 | case AttributeList::AT_PnaclCall: CC = CC_PnaclCall; break; |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3504 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3505 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3506 | } |
| 3507 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3508 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3509 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
| 3510 | if (A == TargetInfo::CCCR_Warning) { |
| 3511 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3512 | |
| 3513 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3514 | if (FD) |
| 3515 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3516 | TargetInfo::CCMT_NonMember; |
| 3517 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3520 | return false; |
| 3521 | } |
| 3522 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3523 | static void handleRegparmAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3524 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3525 | |
| 3526 | unsigned numParams; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3527 | if (S.CheckRegparmAttr(Attr, numParams)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3528 | return; |
| 3529 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3530 | if (!isa<ObjCMethodDecl>(D)) { |
| 3531 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3532 | << Attr.getName() << ExpectedFunctionOrMethod; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3533 | return; |
| 3534 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3535 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3536 | D->addAttr(::new (S.Context) |
| 3537 | RegparmAttr(Attr.getRange(), S.Context, numParams, |
| 3538 | Attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
| 3541 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3542 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3543 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3544 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3545 | return true; |
| 3546 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3547 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3548 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3549 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3550 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3551 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3552 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3553 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3554 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3555 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3556 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3559 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3560 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3561 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3562 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3563 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3566 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3567 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3568 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3569 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3570 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3571 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3574 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3575 | } |
| 3576 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3577 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, const AttributeList &Attr){ |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3578 | if (S.LangOpts.CUDA) { |
| 3579 | // check the attribute arguments. |
| 3580 | if (Attr.getNumArgs() != 1 && Attr.getNumArgs() != 2) { |
John McCall | 80ee596 | 2011-03-02 12:15:05 +0000 | [diff] [blame] | 3581 | // FIXME: 0 is not okay. |
| 3582 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) << 2; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3583 | return; |
| 3584 | } |
| 3585 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3586 | if (!isFunctionOrMethod(D)) { |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3587 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3588 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3589 | return; |
| 3590 | } |
| 3591 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3592 | uint32_t MaxThreads, MinBlocks; |
| 3593 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), MaxThreads, 1) || |
| 3594 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(1), MinBlocks, 2)) |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3595 | return; |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3596 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3597 | D->addAttr(::new (S.Context) |
| 3598 | CUDALaunchBoundsAttr(Attr.getRange(), S.Context, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3599 | MaxThreads, MinBlocks, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3600 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 3601 | } else { |
| 3602 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "launch_bounds"; |
| 3603 | } |
| 3604 | } |
| 3605 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3606 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 3607 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3608 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3609 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3610 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3611 | return; |
| 3612 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3613 | |
| 3614 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3615 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3616 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3617 | StringRef AttrName = Attr.getName()->getName(); |
| 3618 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3619 | |
| 3620 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 3621 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3622 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 3623 | return; |
| 3624 | } |
| 3625 | |
| 3626 | uint64_t ArgumentIdx; |
| 3627 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3628 | Attr.getLoc(), 2, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3629 | Attr.getArgAsExpr(1), ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3630 | return; |
| 3631 | |
| 3632 | uint64_t TypeTagIdx; |
| 3633 | if (!checkFunctionOrMethodArgumentIndex(S, D, AttrName, |
| 3634 | Attr.getLoc(), 3, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3635 | Attr.getArgAsExpr(2), TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3636 | return; |
| 3637 | |
| 3638 | bool IsPointer = (AttrName == "pointer_with_type_tag"); |
| 3639 | if (IsPointer) { |
| 3640 | // Ensure that buffer has a pointer type. |
| 3641 | QualType BufferTy = getFunctionOrMethodArgType(D, ArgumentIdx); |
| 3642 | if (!BufferTy->isPointerType()) { |
| 3643 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 3644 | << Attr.getName(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3645 | } |
| 3646 | } |
| 3647 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3648 | D->addAttr(::new (S.Context) |
| 3649 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 3650 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 3651 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 3655 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3656 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3657 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 3658 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3659 | return; |
| 3660 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3661 | |
| 3662 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 3663 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3664 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 3665 | if (!isa<VarDecl>(D)) { |
| 3666 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3667 | << Attr.getName() << ExpectedVariable; |
| 3668 | return; |
| 3669 | } |
| 3670 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3671 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3672 | TypeSourceInfo *MatchingCTypeLoc = 0; |
| 3673 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 3674 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3675 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3676 | D->addAttr(::new (S.Context) |
| 3677 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 3678 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3679 | Attr.getLayoutCompatible(), |
| 3680 | Attr.getMustBeNull(), |
| 3681 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 3682 | } |
| 3683 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3684 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3685 | // Checker-specific attribute handlers. |
| 3686 | //===----------------------------------------------------------------------===// |
| 3687 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3688 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3689 | return type->isDependentType() || |
| 3690 | type->isObjCObjectPointerType() || |
| 3691 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3692 | } |
| 3693 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 3694 | return type->isDependentType() || |
| 3695 | type->isPointerType() || |
| 3696 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3697 | } |
| 3698 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3699 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3700 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3701 | bool typeOK, cf; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3702 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3703 | if (Attr.getKind() == AttributeList::AT_NSConsumed) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3704 | typeOK = isValidSubjectOfNSAttribute(S, param->getType()); |
| 3705 | cf = false; |
| 3706 | } else { |
| 3707 | typeOK = isValidSubjectOfCFAttribute(S, param->getType()); |
| 3708 | cf = true; |
| 3709 | } |
| 3710 | |
| 3711 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3712 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3713 | << Attr.getRange() << Attr.getName() << cf; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3714 | return; |
| 3715 | } |
| 3716 | |
| 3717 | if (cf) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3718 | param->addAttr(::new (S.Context) |
| 3719 | CFConsumedAttr(Attr.getRange(), S.Context, |
| 3720 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3721 | else |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3722 | param->addAttr(::new (S.Context) |
| 3723 | NSConsumedAttr(Attr.getRange(), S.Context, |
| 3724 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3727 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 3728 | const AttributeList &Attr) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3729 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3730 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3731 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3732 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3733 | returnType = MD->getResultType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3734 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3735 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3736 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 3737 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 3738 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3739 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3740 | returnType = FD->getResultType(); |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3741 | else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3742 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3743 | << Attr.getRange() << Attr.getName() |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3744 | << ExpectedFunctionOrMethod; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3745 | return; |
| 3746 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3747 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3748 | bool typeOK; |
| 3749 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3750 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3751 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3752 | case AttributeList::AT_NSReturnsAutoreleased: |
| 3753 | case AttributeList::AT_NSReturnsRetained: |
| 3754 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3755 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 3756 | cf = false; |
| 3757 | break; |
| 3758 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3759 | case AttributeList::AT_CFReturnsRetained: |
| 3760 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3761 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 3762 | cf = true; |
| 3763 | break; |
| 3764 | } |
| 3765 | |
| 3766 | if (!typeOK) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3767 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 3768 | << Attr.getRange() << Attr.getName() << isa<ObjCMethodDecl>(D) << cf; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3769 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 3770 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3771 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3772 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3773 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3774 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3775 | case AttributeList::AT_NSReturnsAutoreleased: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3776 | D->addAttr(::new (S.Context) |
| 3777 | NSReturnsAutoreleasedAttr(Attr.getRange(), S.Context, |
| 3778 | Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 3779 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3780 | case AttributeList::AT_CFReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3781 | D->addAttr(::new (S.Context) |
| 3782 | CFReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3783 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3784 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3785 | case AttributeList::AT_NSReturnsNotRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3786 | D->addAttr(::new (S.Context) |
| 3787 | NSReturnsNotRetainedAttr(Attr.getRange(), S.Context, |
| 3788 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 3789 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3790 | case AttributeList::AT_CFReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3791 | D->addAttr(::new (S.Context) |
| 3792 | CFReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3793 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3794 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3795 | case AttributeList::AT_NSReturnsRetained: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3796 | D->addAttr(::new (S.Context) |
| 3797 | NSReturnsRetainedAttr(Attr.getRange(), S.Context, |
| 3798 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 3799 | return; |
| 3800 | }; |
| 3801 | } |
| 3802 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3803 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 3804 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 3805 | const int EP_ObjCMethod = 1; |
| 3806 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3807 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3808 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3809 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3810 | if (isa<ObjCMethodDecl>(D)) |
| 3811 | resultType = cast<ObjCMethodDecl>(D)->getResultType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3812 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3813 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3814 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 3815 | if (!resultType->isReferenceType() && |
| 3816 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3817 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3818 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3819 | << attr.getName() |
| 3820 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 3821 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3822 | |
| 3823 | // Drop the attribute. |
| 3824 | return; |
| 3825 | } |
| 3826 | |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 3827 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3828 | ObjCReturnsInnerPointerAttr(attr.getRange(), S.Context, |
| 3829 | attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 3830 | } |
| 3831 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3832 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 3833 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3834 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3835 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3836 | DeclContext *DC = method->getDeclContext(); |
| 3837 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 3838 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3839 | << attr.getName() << 0; |
| 3840 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 3841 | return; |
| 3842 | } |
| 3843 | if (method->getMethodFamily() == OMF_dealloc) { |
| 3844 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 3845 | << attr.getName() << 1; |
| 3846 | return; |
| 3847 | } |
| 3848 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3849 | method->addAttr(::new (S.Context) |
| 3850 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 3851 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 3852 | } |
| 3853 | |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3854 | /// Handle cf_audited_transfer and cf_unknown_transfer. |
| 3855 | static void handleCFTransferAttr(Sema &S, Decl *D, const AttributeList &A) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3856 | bool IsAudited = (A.getKind() == AttributeList::AT_CFAuditedTransfer); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3857 | |
| 3858 | // Check whether there's a conflicting attribute already present. |
| 3859 | Attr *Existing; |
| 3860 | if (IsAudited) { |
| 3861 | Existing = D->getAttr<CFUnknownTransferAttr>(); |
| 3862 | } else { |
| 3863 | Existing = D->getAttr<CFAuditedTransferAttr>(); |
| 3864 | } |
| 3865 | if (Existing) { |
| 3866 | S.Diag(D->getLocStart(), diag::err_attributes_are_not_compatible) |
| 3867 | << A.getName() |
| 3868 | << (IsAudited ? "cf_unknown_transfer" : "cf_audited_transfer") |
| 3869 | << A.getRange() << Existing->getRange(); |
| 3870 | return; |
| 3871 | } |
| 3872 | |
| 3873 | // All clear; add the attribute. |
| 3874 | if (IsAudited) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3875 | D->addAttr(::new (S.Context) |
| 3876 | CFAuditedTransferAttr(A.getRange(), S.Context, |
| 3877 | A.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3878 | } else { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3879 | D->addAttr(::new (S.Context) |
| 3880 | CFUnknownTransferAttr(A.getRange(), S.Context, |
| 3881 | A.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 3882 | } |
| 3883 | } |
| 3884 | |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3885 | static void handleNSBridgedAttr(Sema &S, Scope *Sc, Decl *D, |
| 3886 | const AttributeList &Attr) { |
| 3887 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 3888 | if (!RD || RD->isUnion()) { |
| 3889 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3890 | << Attr.getRange() << Attr.getName() << ExpectedStruct; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3893 | IdentifierLoc *Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3894 | |
| 3895 | // In Objective-C, verify that the type names an Objective-C type. |
| 3896 | // We don't want to check this outside of ObjC because people sometimes |
| 3897 | // do crazy C declarations of Objective-C types. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3898 | if (Parm && S.getLangOpts().ObjC1) { |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3899 | // Check for an existing type with this name. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3900 | LookupResult R(S, DeclarationName(Parm->Ident), Parm->Loc, |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3901 | Sema::LookupOrdinaryName); |
| 3902 | if (S.LookupName(R, Sc)) { |
| 3903 | NamedDecl *Target = R.getFoundDecl(); |
| 3904 | if (Target && !isa<ObjCInterfaceDecl>(Target)) { |
| 3905 | S.Diag(D->getLocStart(), diag::err_ns_bridged_not_interface); |
| 3906 | S.Diag(Target->getLocStart(), diag::note_declared_at); |
| 3907 | } |
| 3908 | } |
| 3909 | } |
| 3910 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3911 | D->addAttr(::new (S.Context) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3912 | NSBridgedAttr(Attr.getRange(), S.Context, Parm ? Parm->Ident : 0, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3913 | Attr.getAttributeSpellingListIndex())); |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3916 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 3917 | const AttributeList &Attr) { |
Fariborz Jahanian | db3d855 | 2013-11-19 00:09:48 +0000 | [diff] [blame] | 3918 | if (!isa<RecordDecl>(D)) { |
Fariborz Jahanian | f720f86 | 2013-11-19 17:42:25 +0000 | [diff] [blame] | 3919 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3920 | << Attr.getName() |
| 3921 | << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass |
| 3922 | : ExpectedStructOrUnion); |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3923 | return; |
| 3924 | } |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3925 | |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3926 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3927 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3928 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3929 | 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] | 3930 | return; |
| 3931 | } |
| 3932 | |
| 3933 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 3934 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 3935 | Attr.getAttributeSpellingListIndex())); |
| 3936 | } |
| 3937 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3938 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 3939 | const AttributeList &Attr) { |
| 3940 | if (!isa<RecordDecl>(D)) { |
| 3941 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 3942 | << Attr.getName() |
| 3943 | << (S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass |
| 3944 | : ExpectedStructOrUnion); |
| 3945 | return; |
| 3946 | } |
| 3947 | |
Fariborz Jahanian | 2651ac5 | 2013-11-22 00:02:22 +0000 | [diff] [blame] | 3948 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : 0; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 3949 | |
| 3950 | if (!Parm) { |
| 3951 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 3952 | return; |
| 3953 | } |
| 3954 | |
| 3955 | D->addAttr(::new (S.Context) |
| 3956 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 3957 | Attr.getAttributeSpellingListIndex())); |
| 3958 | } |
| 3959 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3960 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 3961 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3962 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3963 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3964 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 3965 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3966 | } |
| 3967 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3968 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 3969 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3970 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3971 | QualType type = vd->getType(); |
| 3972 | |
| 3973 | if (!type->isDependentType() && |
| 3974 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3975 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3976 | << type; |
| 3977 | return; |
| 3978 | } |
| 3979 | |
| 3980 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 3981 | |
| 3982 | // If we have no lifetime yet, check the lifetime we're presumably |
| 3983 | // going to infer. |
| 3984 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 3985 | lifetime = type->getObjCARCImplicitLifetime(); |
| 3986 | |
| 3987 | switch (lifetime) { |
| 3988 | case Qualifiers::OCL_None: |
| 3989 | assert(type->isDependentType() && |
| 3990 | "didn't infer lifetime for non-dependent type?"); |
| 3991 | break; |
| 3992 | |
| 3993 | case Qualifiers::OCL_Weak: // meaningful |
| 3994 | case Qualifiers::OCL_Strong: // meaningful |
| 3995 | break; |
| 3996 | |
| 3997 | case Qualifiers::OCL_ExplicitNone: |
| 3998 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3999 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4000 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4001 | break; |
| 4002 | } |
| 4003 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4004 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4005 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4006 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4009 | //===----------------------------------------------------------------------===// |
| 4010 | // Microsoft specific attribute handlers. |
| 4011 | //===----------------------------------------------------------------------===// |
| 4012 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4013 | // Check if MS extensions or some other language extensions are enabled. If |
| 4014 | // not, issue a diagnostic that the given attribute is unused. |
| 4015 | static bool checkMicrosoftExt(Sema &S, const AttributeList &Attr, |
| 4016 | bool OtherExtension = false) { |
| 4017 | if (S.LangOpts.MicrosoftExt || OtherExtension) |
| 4018 | return true; |
| 4019 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 4020 | return false; |
| 4021 | } |
| 4022 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4023 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 4024 | if (!S.LangOpts.CPlusPlus) { |
| 4025 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4026 | << Attr.getName() << AttributeLangSupport::C; |
| 4027 | return; |
| 4028 | } |
| 4029 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4030 | if (!checkMicrosoftExt(S, Attr, S.LangOpts.Borland)) |
| 4031 | return; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 4032 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 4033 | if (!isa<CXXRecordDecl>(D)) { |
| 4034 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4035 | << Attr.getName() << ExpectedClass; |
| 4036 | return; |
| 4037 | } |
| 4038 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4039 | StringRef StrRef; |
| 4040 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4041 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4042 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4043 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4044 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4045 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4046 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4047 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4048 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4049 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4050 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4051 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4052 | return; |
| 4053 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4054 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4055 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4056 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4057 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4058 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4059 | return; |
| 4060 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4061 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4062 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4063 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4064 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4065 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4066 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4067 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 4068 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4069 | } |
| 4070 | |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4071 | static void handleInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4072 | if (!checkMicrosoftExt(S, Attr)) |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4073 | return; |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4074 | |
| 4075 | AttributeList::Kind Kind = Attr.getKind(); |
| 4076 | if (Kind == AttributeList::AT_SingleInheritance) |
| 4077 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4078 | ::new (S.Context) |
| 4079 | SingleInheritanceAttr(Attr.getRange(), S.Context, |
| 4080 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4081 | else if (Kind == AttributeList::AT_MultipleInheritance) |
| 4082 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4083 | ::new (S.Context) |
| 4084 | MultipleInheritanceAttr(Attr.getRange(), S.Context, |
| 4085 | Attr.getAttributeSpellingListIndex())); |
Nico Weber | efa45b2 | 2012-11-07 21:31:36 +0000 | [diff] [blame] | 4086 | else if (Kind == AttributeList::AT_VirtualInheritance) |
| 4087 | D->addAttr( |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4088 | ::new (S.Context) |
| 4089 | VirtualInheritanceAttr(Attr.getRange(), S.Context, |
| 4090 | Attr.getAttributeSpellingListIndex())); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | static void handlePortabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4094 | if (!checkMicrosoftExt(S, Attr)) |
| 4095 | return; |
| 4096 | |
| 4097 | AttributeList::Kind Kind = Attr.getKind(); |
Aaron Ballman | cc14f3a | 2013-11-22 21:49:04 +0000 | [diff] [blame] | 4098 | if (Kind == AttributeList::AT_Win64) |
| 4099 | D->addAttr( |
| 4100 | ::new (S.Context) Win64Attr(Attr.getRange(), S.Context, |
| 4101 | Attr.getAttributeSpellingListIndex())); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4102 | } |
| 4103 | |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4104 | static void handleForceInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4105 | if (!checkMicrosoftExt(S, Attr)) |
| 4106 | return; |
| 4107 | D->addAttr(::new (S.Context) |
| 4108 | ForceInlineAttr(Attr.getRange(), S.Context, |
| 4109 | Attr.getAttributeSpellingListIndex())); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4110 | } |
| 4111 | |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4112 | static void handleSelectAnyAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4113 | if (!checkMicrosoftExt(S, Attr)) |
| 4114 | return; |
| 4115 | // Check linkage after possibly merging declaratinos. See |
| 4116 | // checkAttributesAfterMerging(). |
| 4117 | D->addAttr(::new (S.Context) |
| 4118 | SelectAnyAttr(Attr.getRange(), S.Context, |
| 4119 | Attr.getAttributeSpellingListIndex())); |
| 4120 | } |
| 4121 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4122 | /// Handles semantic checking for features that are common to all attributes, |
| 4123 | /// such as checking whether a parameter was properly specified, or the correct |
| 4124 | /// number of arguments were passed, etc. |
| 4125 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 4126 | const AttributeList &Attr) { |
| 4127 | // Several attributes carry different semantics than the parsing requires, so |
| 4128 | // those are opted out of the common handling. |
| 4129 | // |
| 4130 | // We also bail on unknown and ignored attributes because those are handled |
| 4131 | // as part of the target-specific handling logic. |
| 4132 | if (Attr.hasCustomParsing() || |
| 4133 | Attr.getKind() == AttributeList::UnknownAttribute || |
| 4134 | Attr.getKind() == AttributeList::IgnoredAttribute) |
| 4135 | return false; |
| 4136 | |
| 4137 | // If there are no optional arguments, then checking for the argument count |
| 4138 | // is trivial. |
| 4139 | if (Attr.getMinArgs() == Attr.getMaxArgs() && |
| 4140 | !checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 4141 | return true; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4142 | |
| 4143 | // Check whether the attribute appertains to the given subject. |
| 4144 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 4145 | return true; |
| 4146 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4147 | return false; |
| 4148 | } |
| 4149 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4150 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4151 | // Top Level Sema Entry Points |
| 4152 | //===----------------------------------------------------------------------===// |
| 4153 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4154 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 4155 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 4156 | /// silently ignore it if a GNU attribute. |
| 4157 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 4158 | const AttributeList &Attr, |
| 4159 | bool IncludeCXX11Attributes) { |
| 4160 | if (Attr.isInvalid()) |
| 4161 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4162 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4163 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 4164 | // instead. |
| 4165 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 4166 | return; |
| 4167 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 4168 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 4169 | return; |
| 4170 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4171 | switch (Attr.getKind()) { |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4172 | case AttributeList::AT_IBAction: handleIBAction(S, D, Attr); break; |
| 4173 | case AttributeList::AT_IBOutlet: handleIBOutlet(S, D, Attr); break; |
| 4174 | case AttributeList::AT_IBOutletCollection: |
| 4175 | handleIBOutletCollection(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4176 | case AttributeList::AT_AddressSpace: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4177 | case AttributeList::AT_ObjCGC: |
| 4178 | case AttributeList::AT_VectorSize: |
| 4179 | case AttributeList::AT_NeonVectorType: |
| 4180 | case AttributeList::AT_NeonPolyVectorType: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4181 | case AttributeList::AT_Ptr32: |
| 4182 | case AttributeList::AT_Ptr64: |
| 4183 | case AttributeList::AT_SPtr: |
| 4184 | case AttributeList::AT_UPtr: |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4185 | // Ignore these, these are type attributes, handled by |
| 4186 | // ProcessTypeAttributes. |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4187 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4188 | case AttributeList::AT_Alias: handleAliasAttr (S, D, Attr); break; |
| 4189 | case AttributeList::AT_Aligned: handleAlignedAttr (S, D, Attr); break; |
| 4190 | case AttributeList::AT_AllocSize: handleAllocSizeAttr (S, D, Attr); break; |
| 4191 | case AttributeList::AT_AlwaysInline: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4192 | handleSimpleAttribute<AlwaysInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4193 | case AttributeList::AT_AnalyzerNoReturn: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4194 | handleAnalyzerNoReturnAttr (S, D, Attr); break; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 4195 | case AttributeList::AT_TLSModel: handleTLSModelAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4196 | case AttributeList::AT_Annotate: handleAnnotateAttr (S, D, Attr); break; |
| 4197 | case AttributeList::AT_Availability:handleAvailabilityAttr(S, D, Attr); break; |
| 4198 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4199 | handleDependencyAttr(S, scope, D, Attr); |
| 4200 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4201 | case AttributeList::AT_Common: handleCommonAttr (S, D, Attr); break; |
| 4202 | case AttributeList::AT_CUDAConstant:handleConstantAttr (S, D, Attr); break; |
| 4203 | case AttributeList::AT_Constructor: handleConstructorAttr (S, D, Attr); break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4204 | case AttributeList::AT_CXX11NoReturn: |
| 4205 | handleCXX11NoReturnAttr(S, D, Attr); |
| 4206 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4207 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4208 | handleAttrWithMessage<DeprecatedAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4209 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4210 | case AttributeList::AT_Destructor: handleDestructorAttr (S, D, Attr); break; |
| 4211 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4212 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4213 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4214 | case AttributeList::AT_MinSize: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4215 | handleSimpleAttribute<MinSizeAttr>(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 4216 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4217 | case AttributeList::AT_Format: handleFormatAttr (S, D, Attr); break; |
| 4218 | case AttributeList::AT_FormatArg: handleFormatArgAttr (S, D, Attr); break; |
| 4219 | case AttributeList::AT_CUDAGlobal: handleGlobalAttr (S, D, Attr); break; |
Aaron Ballman | 9a22621 | 2013-08-28 23:13:26 +0000 | [diff] [blame] | 4220 | case AttributeList::AT_CUDADevice: handleDeviceAttr (S, D, Attr); break; |
| 4221 | case AttributeList::AT_CUDAHost: handleHostAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4222 | case AttributeList::AT_GNUInline: handleGNUInlineAttr (S, D, Attr); break; |
| 4223 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4224 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4225 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4226 | case AttributeList::AT_Malloc: handleMallocAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4227 | case AttributeList::AT_MayAlias: |
| 4228 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); break; |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4229 | case AttributeList::AT_Mode: handleModeAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4230 | case AttributeList::AT_NoCommon: |
| 4231 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4232 | case AttributeList::AT_NonNull: handleNonNullAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4233 | case AttributeList::AT_Overloadable: |
| 4234 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); break; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 4235 | case AttributeList::AT_Ownership: handleOwnershipAttr (S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4236 | case AttributeList::AT_Cold: handleColdAttr (S, D, Attr); break; |
| 4237 | case AttributeList::AT_Hot: handleHotAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4238 | case AttributeList::AT_Naked: |
| 4239 | handleSimpleAttribute<NakedAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4240 | case AttributeList::AT_NoReturn: handleNoReturnAttr (S, D, Attr); break; |
| 4241 | case AttributeList::AT_NoThrow: handleNothrowAttr (S, D, Attr); break; |
| 4242 | case AttributeList::AT_CUDAShared: handleSharedAttr (S, D, Attr); break; |
| 4243 | case AttributeList::AT_VecReturn: handleVecReturnAttr (S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4244 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4245 | case AttributeList::AT_ObjCOwnership: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4246 | handleObjCOwnershipAttr(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4247 | case AttributeList::AT_ObjCPreciseLifetime: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4248 | handleObjCPreciseLifetimeAttr(S, D, Attr); break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4249 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4250 | case AttributeList::AT_ObjCReturnsInnerPointer: |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4251 | handleObjCReturnsInnerPointerAttr(S, D, Attr); break; |
| 4252 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4253 | case AttributeList::AT_ObjCRequiresSuper: |
| 4254 | handleObjCRequiresSuperAttr(S, D, Attr); break; |
| 4255 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4256 | case AttributeList::AT_NSBridged: |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4257 | handleNSBridgedAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4258 | |
| 4259 | case AttributeList::AT_ObjCBridge: |
| 4260 | handleObjCBridgeAttr(S, scope, D, Attr); break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4261 | |
| 4262 | case AttributeList::AT_ObjCBridgeMutable: |
| 4263 | handleObjCBridgeMutableAttr(S, scope, D, Attr); break; |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 4264 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4265 | case AttributeList::AT_CFAuditedTransfer: |
| 4266 | case AttributeList::AT_CFUnknownTransfer: |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4267 | handleCFTransferAttr(S, D, Attr); break; |
| 4268 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4269 | // Checker-specific. |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4270 | case AttributeList::AT_CFConsumed: |
| 4271 | case AttributeList::AT_NSConsumed: handleNSConsumedAttr (S, D, Attr); break; |
| 4272 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4273 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); break; |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4274 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4275 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4276 | case AttributeList::AT_NSReturnsNotRetained: |
| 4277 | case AttributeList::AT_CFReturnsNotRetained: |
| 4278 | case AttributeList::AT_NSReturnsRetained: |
| 4279 | case AttributeList::AT_CFReturnsRetained: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4280 | handleNSReturnsRetainedAttr(S, D, Attr); break; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4281 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4282 | case AttributeList::AT_WorkGroupSizeHint: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4283 | case AttributeList::AT_ReqdWorkGroupSize: |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 4284 | handleWorkGroupSize(S, D, Attr); break; |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 4285 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 4286 | case AttributeList::AT_VecTypeHint: |
| 4287 | handleVecTypeHint(S, D, Attr); break; |
| 4288 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4289 | case AttributeList::AT_InitPriority: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4290 | handleInitPriorityAttr(S, D, Attr); break; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 4291 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4292 | case AttributeList::AT_Packed: handlePackedAttr (S, D, Attr); break; |
| 4293 | case AttributeList::AT_Section: handleSectionAttr (S, D, Attr); break; |
| 4294 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 4295 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 4296 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4297 | case AttributeList::AT_ArcWeakrefUnavailable: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4298 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4299 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4300 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); break; |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 4301 | case AttributeList::AT_ObjCSuppressProtocol: |
| 4302 | handleObjCSuppresProtocolAttr(S, D, Attr); |
| 4303 | break; |
| 4304 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4305 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4306 | case AttributeList::AT_Unused: handleUnusedAttr (S, D, Attr); break; |
| 4307 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4308 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4309 | case AttributeList::AT_Used: handleUsedAttr (S, D, Attr); break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 4310 | case AttributeList::AT_Visibility: |
| 4311 | handleVisibilityAttr(S, D, Attr, false); |
| 4312 | break; |
| 4313 | case AttributeList::AT_TypeVisibility: |
| 4314 | handleVisibilityAttr(S, D, Attr, true); |
| 4315 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 4316 | case AttributeList::AT_WarnUnused: |
| 4317 | handleWarnUnusedAttr(S, D, Attr); |
| 4318 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4319 | case AttributeList::AT_WarnUnusedResult: handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 4320 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4321 | case AttributeList::AT_Weak: handleWeakAttr (S, D, Attr); break; |
| 4322 | case AttributeList::AT_WeakRef: handleWeakRefAttr (S, D, Attr); break; |
| 4323 | case AttributeList::AT_WeakImport: handleWeakImportAttr (S, D, Attr); break; |
| 4324 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4325 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4326 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4327 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4328 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4329 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4330 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 4331 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4332 | case AttributeList::AT_ObjCNSObject:handleObjCNSObject (S, D, Attr); break; |
| 4333 | case AttributeList::AT_Blocks: handleBlocksAttr (S, D, Attr); break; |
| 4334 | case AttributeList::AT_Sentinel: handleSentinelAttr (S, D, Attr); break; |
| 4335 | case AttributeList::AT_Const: handleConstAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4336 | case AttributeList::AT_Pure: |
| 4337 | handleSimpleAttribute<PureAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4338 | case AttributeList::AT_Cleanup: handleCleanupAttr (S, D, Attr); break; |
| 4339 | case AttributeList::AT_NoDebug: handleNoDebugAttr (S, D, Attr); break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4340 | case AttributeList::AT_NoInline: |
| 4341 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4342 | case AttributeList::AT_Regparm: handleRegparmAttr (S, D, Attr); break; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4343 | case AttributeList::IgnoredAttribute: |
Anders Carlsson | b4f3134 | 2009-02-13 08:16:43 +0000 | [diff] [blame] | 4344 | // Just ignore |
| 4345 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4346 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4347 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4348 | case AttributeList::AT_StdCall: |
| 4349 | case AttributeList::AT_CDecl: |
| 4350 | case AttributeList::AT_FastCall: |
| 4351 | case AttributeList::AT_ThisCall: |
| 4352 | case AttributeList::AT_Pascal: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4353 | case AttributeList::AT_MSABI: |
| 4354 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4355 | case AttributeList::AT_Pcs: |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4356 | case AttributeList::AT_PnaclCall: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4357 | case AttributeList::AT_IntelOclBicc: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4358 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 4359 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4360 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4361 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); break; |
Guy Benyei | fb36ede | 2013-03-24 13:58:12 +0000 | [diff] [blame] | 4362 | case AttributeList::AT_OpenCLImageAccess: |
| 4363 | handleOpenCLImageAccessAttr(S, D, Attr); |
| 4364 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4365 | |
| 4366 | // Microsoft attributes: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4367 | case AttributeList::AT_MsStruct: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4368 | handleSimpleAttribute<MsStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4369 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4370 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4371 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4372 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4373 | case AttributeList::AT_SingleInheritance: |
| 4374 | case AttributeList::AT_MultipleInheritance: |
| 4375 | case AttributeList::AT_VirtualInheritance: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4376 | handleInheritanceAttr(S, D, Attr); |
| 4377 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4378 | case AttributeList::AT_Win64: |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 4379 | handlePortabilityAttr(S, D, Attr); |
| 4380 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4381 | case AttributeList::AT_ForceInline: |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 4382 | handleForceInlineAttr(S, D, Attr); |
| 4383 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 4384 | case AttributeList::AT_SelectAny: |
| 4385 | handleSelectAnyAttr(S, D, Attr); |
| 4386 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4387 | |
| 4388 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 4389 | case AttributeList::AT_AssertExclusiveLock: |
| 4390 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 4391 | break; |
| 4392 | case AttributeList::AT_AssertSharedLock: |
| 4393 | handleAssertSharedLockAttr(S, D, Attr); |
| 4394 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4395 | case AttributeList::AT_GuardedVar: |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4396 | handleGuardedVarAttr(S, D, Attr); |
| 4397 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4398 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4399 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4400 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4401 | case AttributeList::AT_ScopedLockable: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4402 | handleScopedLockableAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4403 | break; |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4404 | case AttributeList::AT_NoSanitizeAddress: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4405 | handleSimpleAttribute<NoSanitizeAddressAttr>(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 4406 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4407 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4408 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4409 | break; |
| 4410 | case AttributeList::AT_NoSanitizeThread: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4411 | handleSimpleAttribute<NoSanitizeThreadAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 4412 | break; |
| 4413 | case AttributeList::AT_NoSanitizeMemory: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame^] | 4414 | handleSimpleAttribute<NoSanitizeMemoryAttr>(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4415 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4416 | case AttributeList::AT_Lockable: |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4417 | handleLockableAttr(S, D, Attr); |
| 4418 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4419 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4420 | handleGuardedByAttr(S, D, Attr); |
| 4421 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4422 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4423 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4424 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4425 | case AttributeList::AT_ExclusiveLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4426 | handleExclusiveLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4427 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4428 | case AttributeList::AT_ExclusiveLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4429 | handleExclusiveLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4430 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4431 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4432 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4433 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4434 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4435 | handleLockReturnedAttr(S, D, Attr); |
| 4436 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4437 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4438 | handleLocksExcludedAttr(S, D, Attr); |
| 4439 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4440 | case AttributeList::AT_SharedLockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4441 | handleSharedLockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4442 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4443 | case AttributeList::AT_SharedLocksRequired: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4444 | handleSharedLocksRequiredAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4445 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4446 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4447 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4448 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4449 | case AttributeList::AT_UnlockFunction: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4450 | handleUnlockFunAttr(S, D, Attr); |
| 4451 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4452 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4453 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4454 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4455 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 4456 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 4457 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 4458 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 4459 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 4460 | case AttributeList::AT_Consumable: |
| 4461 | handleConsumableAttr(S, D, Attr); |
| 4462 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 4463 | case AttributeList::AT_CallableWhen: |
| 4464 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4465 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 4466 | case AttributeList::AT_ParamTypestate: |
| 4467 | handleParamTypestateAttr(S, D, Attr); |
| 4468 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 4469 | case AttributeList::AT_ReturnTypestate: |
| 4470 | handleReturnTypestateAttr(S, D, Attr); |
| 4471 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4472 | case AttributeList::AT_SetTypestate: |
| 4473 | handleSetTypestateAttr(S, D, Attr); |
| 4474 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 4475 | case AttributeList::AT_TestTypestate: |
| 4476 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 4477 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 4478 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4479 | // Type safety attributes. |
| 4480 | case AttributeList::AT_ArgumentWithTypeTag: |
| 4481 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 4482 | break; |
| 4483 | case AttributeList::AT_TypeTagForDatatype: |
| 4484 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 4485 | break; |
| 4486 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4487 | default: |
Anton Korobeynikov | 55bcea1 | 2010-01-10 12:58:08 +0000 | [diff] [blame] | 4488 | // Ask target about the attribute. |
| 4489 | const TargetAttributesSema &TargetAttrs = S.getTargetAttributesSema(); |
| 4490 | if (!TargetAttrs.ProcessDeclAttribute(scope, D, Attr, S)) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 4491 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() ? |
| 4492 | diag::warn_unhandled_ms_attribute_ignored : |
| 4493 | diag::warn_unknown_attribute_ignored) << Attr.getName(); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4494 | break; |
| 4495 | } |
| 4496 | } |
| 4497 | |
| 4498 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 4499 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 4500 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 4501 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 4502 | bool IncludeCXX11Attributes) { |
| 4503 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4504 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4505 | |
| 4506 | // GCC accepts |
| 4507 | // static int a9 __attribute__((weakref)); |
| 4508 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4509 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4510 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) << |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 4511 | cast<NamedDecl>(D)->getNameAsString(); |
| 4512 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 4513 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 4514 | } |
| 4515 | } |
| 4516 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4517 | // Annotation attributes are the only attributes allowed after an access |
| 4518 | // specifier. |
| 4519 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 4520 | const AttributeList *AttrList) { |
| 4521 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4522 | if (l->getKind() == AttributeList::AT_Annotate) { |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 4523 | handleAnnotateAttr(*this, ASDecl, *l); |
| 4524 | } else { |
| 4525 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 4526 | return true; |
| 4527 | } |
| 4528 | } |
| 4529 | |
| 4530 | return false; |
| 4531 | } |
| 4532 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4533 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 4534 | /// contains any decl attributes that we should warn about. |
| 4535 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 4536 | for ( ; A; A = A->getNext()) { |
| 4537 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 4538 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4539 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 4540 | |
| 4541 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 4542 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 4543 | << A->getName() << A->getRange(); |
| 4544 | } else { |
| 4545 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 4546 | << A->getName() << A->getRange(); |
| 4547 | } |
| 4548 | } |
| 4549 | } |
| 4550 | |
| 4551 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 4552 | /// used to build a declaration, complain about any decl attributes |
| 4553 | /// which might be lying around on it. |
| 4554 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 4555 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 4556 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 4557 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 4558 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 4559 | } |
| 4560 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4561 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4562 | /// \#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] | 4563 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 4564 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4565 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4566 | NamedDecl *NewD = 0; |
| 4567 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4568 | FunctionDecl *NewFD; |
| 4569 | // FIXME: Missing call to CheckFunctionDeclaration(). |
| 4570 | // FIXME: Mangling? |
| 4571 | // FIXME: Is the qualifier info correct? |
| 4572 | // FIXME: Is the DeclContext correct? |
| 4573 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 4574 | Loc, Loc, DeclarationName(II), |
| 4575 | FD->getType(), FD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4576 | SC_None, false/*isInlineSpecified*/, |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4577 | FD->hasPrototype(), |
| 4578 | false/*isConstexprSpecified*/); |
| 4579 | NewD = NewFD; |
| 4580 | |
| 4581 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4582 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4583 | |
| 4584 | // Fake up parameter variables; they are declared as if this were |
| 4585 | // a typedef. |
| 4586 | QualType FDTy = FD->getType(); |
| 4587 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 4588 | SmallVector<ParmVarDecl*, 16> Params; |
| 4589 | for (FunctionProtoType::arg_type_iterator AI = FT->arg_type_begin(), |
| 4590 | AE = FT->arg_type_end(); AI != AE; ++AI) { |
| 4591 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, *AI); |
| 4592 | Param->setScopeInfo(0, Params.size()); |
| 4593 | Params.push_back(Param); |
| 4594 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 4595 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4596 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4597 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 4598 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 4599 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 4600 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 4601 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4602 | if (VD->getQualifier()) { |
| 4603 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 4604 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 4605 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4606 | } |
| 4607 | return NewD; |
| 4608 | } |
| 4609 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 4610 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4611 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 4612 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4613 | if (W.getUsed()) return; // only do this once |
| 4614 | W.setUsed(true); |
| 4615 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 4616 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 4617 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4618 | NewD->addAttr(::new (Context) AliasAttr(W.getLocation(), Context, |
| 4619 | NDId->getName())); |
| 4620 | NewD->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 4621 | WeakTopLevelDecl.push_back(NewD); |
| 4622 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 4623 | // to insert Decl at TU scope, sorry. |
| 4624 | DeclContext *SavedContext = CurContext; |
| 4625 | CurContext = Context.getTranslationUnitDecl(); |
| 4626 | PushOnScopeChains(NewD, S); |
| 4627 | CurContext = SavedContext; |
| 4628 | } else { // just add weak to existing |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 4629 | ND->addAttr(::new (Context) WeakAttr(W.getLocation(), Context)); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 4630 | } |
| 4631 | } |
| 4632 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 4633 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 4634 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 4635 | // have to do this. |
| 4636 | LoadExternalWeakUndeclaredIdentifiers(); |
| 4637 | if (!WeakUndeclaredIdentifiers.empty()) { |
| 4638 | NamedDecl *ND = NULL; |
| 4639 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 4640 | if (VD->isExternC()) |
| 4641 | ND = VD; |
| 4642 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 4643 | if (FD->isExternC()) |
| 4644 | ND = FD; |
| 4645 | if (ND) { |
| 4646 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
| 4647 | llvm::DenseMap<IdentifierInfo*,WeakInfo>::iterator I |
| 4648 | = WeakUndeclaredIdentifiers.find(Id); |
| 4649 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 4650 | WeakInfo W = I->second; |
| 4651 | DeclApplyPragmaWeak(S, ND, W); |
| 4652 | WeakUndeclaredIdentifiers[Id] = W; |
| 4653 | } |
| 4654 | } |
| 4655 | } |
| 4656 | } |
| 4657 | } |
| 4658 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4659 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 4660 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 4661 | /// 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] | 4662 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4663 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4664 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4665 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4666 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4667 | // Walk the declarator structure, applying decl attributes that were in a type |
| 4668 | // position to the decl itself. This handles cases like: |
| 4669 | // int *__attr__(x)** D; |
| 4670 | // when X is a decl attribute. |
| 4671 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 4672 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4673 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4674 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4675 | // Finally, apply any attributes on the decl itself. |
| 4676 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 4677 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4678 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4679 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4680 | /// Is the given declaration allowed to use a forbidden type? |
| 4681 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl) { |
| 4682 | // Private ivars are always okay. Unfortunately, people don't |
| 4683 | // always properly make their ivars private, even in system headers. |
| 4684 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 4685 | // Function declarations in sys headers will be marked unavailable. |
| 4686 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 4687 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4688 | return false; |
| 4689 | |
| 4690 | // Require it to be declared in a system header. |
| 4691 | return S.Context.getSourceManager().isInSystemHeader(decl->getLocation()); |
| 4692 | } |
| 4693 | |
| 4694 | /// Handle a delayed forbidden-type diagnostic. |
| 4695 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 4696 | Decl *decl) { |
| 4697 | if (decl && isForbiddenTypeAllowed(S, decl)) { |
| 4698 | decl->addAttr(new (S.Context) UnavailableAttr(diag.Loc, S.Context, |
| 4699 | "this system declaration uses an unsupported type")); |
| 4700 | return; |
| 4701 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4702 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4703 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 4704 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 4705 | // kind of forbidden type messages on unavailable functions. |
| 4706 | if (FD->hasAttr<UnavailableAttr>() && |
| 4707 | diag.getForbiddenTypeDiagnostic() == |
| 4708 | diag::err_arc_array_param_no_ownership) { |
| 4709 | diag.Triggered = true; |
| 4710 | return; |
| 4711 | } |
| 4712 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4713 | |
| 4714 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 4715 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 4716 | diag.Triggered = true; |
| 4717 | } |
| 4718 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4719 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 4720 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4721 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4722 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4723 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4724 | // When delaying diagnostics to run in the context of a parsed |
| 4725 | // declaration, we only want to actually emit anything if parsing |
| 4726 | // succeeds. |
| 4727 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4728 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4729 | // We emit all the active diagnostics in this pool or any of its |
| 4730 | // parents. In general, we'll get one pool for the decl spec |
| 4731 | // and a child pool for each declarator; in a decl group like: |
| 4732 | // deprecated_typedef foo, *bar, baz(); |
| 4733 | // only the declarator pops will be passed decls. This is correct; |
| 4734 | // we really do need to consider delayed diagnostics from the decl spec |
| 4735 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4736 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4737 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4738 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4739 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 4740 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 4741 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4742 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4743 | continue; |
| 4744 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4745 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4746 | case DelayedDiagnostic::Deprecation: |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 4747 | // Don't bother giving deprecation diagnostics if the decl is invalid. |
| 4748 | if (!decl->isInvalidDecl()) |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4749 | HandleDelayedDeprecationCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4750 | break; |
| 4751 | |
| 4752 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4753 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4754 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4755 | |
| 4756 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4757 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4758 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4759 | } |
| 4760 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 4761 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 4764 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 4765 | /// been delayed in the current context instead of in the given pool. |
| 4766 | /// Essentially, this just moves them to the current pool. |
| 4767 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 4768 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 4769 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 4770 | curPool->steal(pool); |
| 4771 | } |
| 4772 | |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4773 | static bool isDeclDeprecated(Decl *D) { |
| 4774 | do { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 4775 | if (D->isDeprecated()) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4776 | return true; |
Argyrios Kyrtzidis | c281c96 | 2011-10-06 23:23:27 +0000 | [diff] [blame] | 4777 | // A category implicitly has the availability of the interface. |
| 4778 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
| 4779 | return CatD->getClassInterface()->isDeprecated(); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4780 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 4781 | return false; |
| 4782 | } |
| 4783 | |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4784 | static void |
| 4785 | DoEmitDeprecationWarning(Sema &S, const NamedDecl *D, StringRef Message, |
| 4786 | SourceLocation Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4787 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4788 | const ObjCPropertyDecl *ObjCPropery) { |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4789 | DeclarationName Name = D->getDeclName(); |
| 4790 | if (!Message.empty()) { |
| 4791 | S.Diag(Loc, diag::warn_deprecated_message) << Name << Message; |
| 4792 | S.Diag(D->getLocation(), |
| 4793 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4794 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4795 | if (ObjCPropery) |
| 4796 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 4797 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4798 | } else if (!UnknownObjCClass) { |
| 4799 | S.Diag(Loc, diag::warn_deprecated) << D->getDeclName(); |
| 4800 | S.Diag(D->getLocation(), |
| 4801 | isa<ObjCMethodDecl>(D) ? diag::note_method_declared_at |
| 4802 | : diag::note_previous_decl) << Name; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4803 | if (ObjCPropery) |
| 4804 | S.Diag(ObjCPropery->getLocation(), diag::note_property_attribute) |
| 4805 | << ObjCPropery->getDeclName() << 0; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4806 | } else { |
| 4807 | S.Diag(Loc, diag::warn_deprecated_fwdclass_message) << Name; |
| 4808 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 4809 | } |
| 4810 | } |
| 4811 | |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 4812 | void Sema::HandleDelayedDeprecationCheck(DelayedDiagnostic &DD, |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4813 | Decl *Ctx) { |
| 4814 | if (isDeclDeprecated(Ctx)) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4815 | return; |
| 4816 | |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 4817 | DD.Triggered = true; |
Eli Friedman | 971bfa1 | 2012-08-08 21:52:41 +0000 | [diff] [blame] | 4818 | DoEmitDeprecationWarning(*this, DD.getDeprecationDecl(), |
| 4819 | DD.getDeprecationMessage(), DD.Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4820 | DD.getUnknownObjCClass(), |
| 4821 | DD.getObjCProperty()); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4822 | } |
| 4823 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4824 | void Sema::EmitDeprecationWarning(NamedDecl *D, StringRef Message, |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 4825 | SourceLocation Loc, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4826 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 4827 | const ObjCPropertyDecl *ObjCProperty) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4828 | // Delay if we're currently parsing a declaration. |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 4829 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4830 | DelayedDiagnostics.add(DelayedDiagnostic::makeDeprecation(Loc, D, |
| 4831 | UnknownObjCClass, |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4832 | ObjCProperty, |
Fariborz Jahanian | 7923ef4 | 2012-03-02 21:50:02 +0000 | [diff] [blame] | 4833 | Message)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4834 | return; |
| 4835 | } |
| 4836 | |
| 4837 | // Otherwise, don't warn if our current context is deprecated. |
Argyrios Kyrtzidis | 9321ad3 | 2011-10-06 23:23:20 +0000 | [diff] [blame] | 4838 | if (isDeclDeprecated(cast<Decl>(getCurLexicalContext()))) |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4839 | return; |
Fariborz Jahanian | 974c948 | 2012-09-21 20:46:37 +0000 | [diff] [blame] | 4840 | DoEmitDeprecationWarning(*this, D, Message, Loc, UnknownObjCClass, ObjCProperty); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4841 | } |