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" |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTConsumer.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" |
Benjamin Kramer | f3ca2698 | 2014-05-10 16:31:55 +0000 | [diff] [blame] | 22 | #include "clang/AST/ExprCXX.h" |
Rafael Espindola | db77c4a | 2013-02-26 19:13:56 +0000 | [diff] [blame] | 23 | #include "clang/AST/Mangle.h" |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 24 | #include "clang/AST/ASTMutationListener.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 25 | #include "clang/Basic/CharInfo.h" |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 26 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 27 | #include "clang/Basic/TargetInfo.h" |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 28 | #include "clang/Lex/Preprocessor.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 29 | #include "clang/Sema/DeclSpec.h" |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 30 | #include "clang/Sema/DelayedDiagnostic.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 31 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 32 | #include "clang/Sema/Scope.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/StringExtras.h" |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 36 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 37 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 38 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 39 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 40 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 41 | C, |
| 42 | Cpp, |
| 43 | ObjC |
| 44 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 45 | } // end namespace AttributeLangSupport |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 46 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 47 | //===----------------------------------------------------------------------===// |
| 48 | // Helper functions |
| 49 | //===----------------------------------------------------------------------===// |
| 50 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 51 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 52 | /// type (function or function-typed variable) or an Objective-C |
| 53 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 54 | static bool isFunctionOrMethod(const Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 55 | return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 56 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 57 | |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 58 | /// \brief Return true if the given decl has function type (function or |
| 59 | /// function-typed variable) or an Objective-C method or a block. |
| 60 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 61 | return isFunctionOrMethod(D) || isa<BlockDecl>(D); |
| 62 | } |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 63 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 64 | /// Return true if the given decl has a declarator that should have |
| 65 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 66 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 67 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 68 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 69 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 72 | /// hasFunctionProto - Return true if the given decl has a argument |
| 73 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 74 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 75 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 76 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 77 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 78 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 81 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 82 | /// parameters. It is an error to call this on a K&R function (use |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 83 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 84 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 85 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 86 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 87 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 88 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 89 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 92 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 93 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 94 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 95 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 96 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 97 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 98 | return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 101 | static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) { |
| 102 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 103 | return FD->getParamDecl(Idx)->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 104 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 105 | return MD->parameters()[Idx]->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 106 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 107 | return BD->getParamDecl(Idx)->getSourceRange(); |
| 108 | return SourceRange(); |
| 109 | } |
| 110 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 111 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 112 | if (const FunctionType *FnTy = D->getFunctionType()) |
Aaron Ballman | 6288d06 | 2014-07-11 16:31:29 +0000 | [diff] [blame] | 113 | return cast<FunctionType>(FnTy)->getReturnType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 114 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 117 | static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) { |
| 118 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 119 | return FD->getReturnTypeSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 120 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 121 | return MD->getReturnTypeSourceRange(); |
| 122 | return SourceRange(); |
| 123 | } |
| 124 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 125 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 126 | if (const FunctionType *FnTy = D->getFunctionType()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 127 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 128 | return proto->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 129 | } |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 130 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| 131 | return BD->isVariadic(); |
| 132 | |
| 133 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 136 | static bool isInstanceMethod(const Decl *D) { |
| 137 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 138 | return MethodDecl->isInstance(); |
| 139 | return false; |
| 140 | } |
| 141 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 142 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 143 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 144 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 145 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 146 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 147 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 148 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 149 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 150 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 151 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 153 | // FIXME: Should we walk the chain of classes? |
| 154 | return ClsName == &Ctx.Idents.get("NSString") || |
| 155 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 156 | } |
| 157 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 158 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 159 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 160 | if (!PT) |
| 161 | return false; |
| 162 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 163 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 164 | if (!RT) |
| 165 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 166 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 167 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 168 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 169 | return false; |
| 170 | |
| 171 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 172 | } |
| 173 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 174 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 175 | // FIXME: Include the type in the argument list. |
| 176 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 177 | } |
| 178 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 179 | template <typename Compare> |
| 180 | static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr, |
| 181 | unsigned Num, unsigned Diag, |
| 182 | Compare Comp) { |
| 183 | if (Comp(getNumAttributeArgs(Attr), Num)) { |
| 184 | S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | |
| 188 | return true; |
| 189 | } |
| 190 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 191 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 192 | /// output an error. |
| 193 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 194 | unsigned Num) { |
| 195 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 196 | diag::err_attribute_wrong_number_arguments, |
| 197 | std::not_equal_to<unsigned>()); |
| 198 | } |
| 199 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 200 | /// \brief Check if the attribute has at least as many args as Num. May |
| 201 | /// output an error. |
| 202 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 203 | unsigned Num) { |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 204 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 205 | diag::err_attribute_too_few_arguments, |
| 206 | std::less<unsigned>()); |
| 207 | } |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 208 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 209 | /// \brief Check if the attribute has at most as many args as Num. May |
| 210 | /// output an error. |
| 211 | static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr, |
| 212 | unsigned Num) { |
| 213 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 214 | diag::err_attribute_too_many_arguments, |
| 215 | std::greater<unsigned>()); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 218 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 219 | /// expression and return success or failure. May output an error. |
| 220 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 221 | const Expr *Expr, uint32_t &Val, |
| 222 | unsigned Idx = UINT_MAX) { |
| 223 | llvm::APSInt I(32); |
| 224 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 225 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 226 | if (Idx != UINT_MAX) |
| 227 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 228 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 229 | << Expr->getSourceRange(); |
| 230 | else |
| 231 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 232 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 233 | << Expr->getSourceRange(); |
| 234 | return false; |
| 235 | } |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 236 | |
| 237 | if (!I.isIntN(32)) { |
Aaron Ballman | 31f4231 | 2014-07-24 14:51:23 +0000 | [diff] [blame] | 238 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 239 | << I.toString(10, false) << 32 << /* Unsigned */ 1; |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 243 | Val = (uint32_t)I.getZExtValue(); |
| 244 | return true; |
| 245 | } |
| 246 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 247 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 248 | /// declaration. Returns true if diagnosed. |
| 249 | template <typename AttrTy> |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 250 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range, |
| 251 | IdentifierInfo *Ident) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 252 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 253 | S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident |
| 254 | << A; |
| 255 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 256 | return true; |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 261 | /// \brief Check if IdxExpr is a valid parameter index for a function or |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 262 | /// instance method D. May output an error. |
| 263 | /// |
| 264 | /// \returns true if IdxExpr is a valid index. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 265 | static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D, |
| 266 | const AttributeList &Attr, |
| 267 | unsigned AttrArgNum, |
| 268 | const Expr *IdxExpr, |
| 269 | uint64_t &Idx) { |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 270 | assert(isFunctionOrMethodOrBlock(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 271 | |
| 272 | // In C++ the implicit 'this' function parameter also counts. |
| 273 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 274 | bool HP = hasFunctionProto(D); |
| 275 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 276 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 277 | unsigned NumParams = |
| 278 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 279 | |
| 280 | llvm::APSInt IdxInt; |
| 281 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 282 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 283 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 284 | << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant |
| 285 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 286 | return false; |
| 287 | } |
| 288 | |
| 289 | Idx = IdxInt.getLimitedValue(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 290 | if (Idx < 1 || (!IV && Idx > NumParams)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 291 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 292 | << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 293 | return false; |
| 294 | } |
| 295 | Idx--; // Convert to zero-based. |
| 296 | if (HasImplicitThisParam) { |
| 297 | if (Idx == 0) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 298 | S.Diag(Attr.getLoc(), |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 299 | diag::err_attribute_invalid_implicit_this_argument) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 300 | << Attr.getName() << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 301 | return false; |
| 302 | } |
| 303 | --Idx; |
| 304 | } |
| 305 | |
| 306 | return true; |
| 307 | } |
| 308 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 309 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 310 | /// If not emit an error and return false. If the argument is an identifier it |
| 311 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 312 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 313 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 314 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 315 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 316 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 317 | if (Attr.isArgIdent(ArgNum)) { |
| 318 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 319 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 320 | << Attr.getName() << AANT_ArgumentString |
| 321 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 322 | << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 323 | Str = Loc->Ident->getName(); |
| 324 | if (ArgLocation) |
| 325 | *ArgLocation = Loc->Loc; |
| 326 | return true; |
| 327 | } |
| 328 | |
| 329 | // Now check for an actual string literal. |
| 330 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 331 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 332 | if (ArgLocation) |
| 333 | *ArgLocation = ArgExpr->getLocStart(); |
| 334 | |
| 335 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 336 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 337 | << Attr.getName() << AANT_ArgumentString; |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | Str = Literal->getString(); |
| 342 | return true; |
| 343 | } |
| 344 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 345 | /// \brief Applies the given attribute to the Decl without performing any |
| 346 | /// additional semantic checking. |
| 347 | template <typename AttrType> |
| 348 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 349 | const AttributeList &Attr) { |
| 350 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 351 | Attr.getAttributeSpellingListIndex())); |
| 352 | } |
| 353 | |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 354 | template <typename AttrType> |
| 355 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
| 356 | const AttributeList &Attr) { |
| 357 | handleSimpleAttribute<AttrType>(S, D, Attr); |
| 358 | } |
| 359 | |
| 360 | /// \brief Applies the given attribute to the Decl so long as the Decl doesn't |
| 361 | /// already have one of the given incompatible attributes. |
| 362 | template <typename AttrType, typename IncompatibleAttrType, |
| 363 | typename... IncompatibleAttrTypes> |
| 364 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
| 365 | const AttributeList &Attr) { |
| 366 | if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, Attr.getRange(), |
| 367 | Attr.getName())) |
| 368 | return; |
| 369 | handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D, |
| 370 | Attr); |
| 371 | } |
| 372 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 373 | /// \brief Check if the passed-in expression is of type int or bool. |
| 374 | static bool isIntOrBool(Expr *Exp) { |
| 375 | QualType QT = Exp->getType(); |
| 376 | return QT->isBooleanType() || QT->isIntegerType(); |
| 377 | } |
| 378 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 379 | |
| 380 | // Check to see if the type is a smart pointer of some kind. We assume |
| 381 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 382 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 383 | DeclContextLookupResult Res1 = RT->getDecl()->lookup( |
| 384 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 385 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 386 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 387 | |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 388 | DeclContextLookupResult Res2 = RT->getDecl()->lookup( |
| 389 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 390 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 391 | return false; |
| 392 | |
| 393 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 396 | /// \brief Check if passed in Decl is a pointer type. |
| 397 | /// Note that this function may produce an error message. |
| 398 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 399 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 400 | const AttributeList &Attr) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 401 | const ValueDecl *vd = cast<ValueDecl>(D); |
| 402 | QualType QT = vd->getType(); |
| 403 | if (QT->isAnyPointerType()) |
| 404 | return true; |
| 405 | |
| 406 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 407 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 408 | // (We don't want to force template instantiation if we can avoid it, |
| 409 | // since that would alter the order in which templates are instantiated.) |
| 410 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 411 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 412 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 413 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 414 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 415 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 416 | |
| 417 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Aaron Ballman | 6828945 | 2013-12-26 15:06:01 +0000 | [diff] [blame] | 418 | << Attr.getName() << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 419 | return false; |
| 420 | } |
| 421 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 422 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 423 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 424 | static const RecordType *getRecordType(QualType QT) { |
| 425 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 426 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 427 | |
| 428 | // Now check if we point to record type. |
| 429 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 430 | return PT->getPointeeType()->getAs<RecordType>(); |
| 431 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 432 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 433 | } |
| 434 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 435 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 436 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 437 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 438 | if (!RT) |
| 439 | return false; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 440 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 441 | // Don't check for the capability if the class hasn't been defined yet. |
DeLesley Hutchins | 3509f29 | 2012-02-16 17:15:51 +0000 | [diff] [blame] | 442 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 443 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 444 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 445 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 446 | // FIXME -- Check the type that the smart pointer points to. |
| 447 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 448 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 449 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 450 | // Check if the record itself has a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 451 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 452 | if (RD->hasAttr<CapabilityAttr>()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 453 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 454 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 455 | // Else check if any base classes have a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 456 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 457 | CXXBasePaths BPaths(false, false); |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 458 | if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) { |
| 459 | const auto *Type = BS->getType()->getAs<RecordType>(); |
| 460 | return Type->getDecl()->hasAttr<CapabilityAttr>(); |
| 461 | }, BPaths)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 462 | return true; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 463 | } |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 464 | return false; |
| 465 | } |
| 466 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 467 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 468 | const auto *TD = Ty->getAs<TypedefType>(); |
| 469 | if (!TD) |
| 470 | return false; |
| 471 | |
| 472 | TypedefNameDecl *TN = TD->getDecl(); |
| 473 | if (!TN) |
| 474 | return false; |
| 475 | |
| 476 | return TN->hasAttr<CapabilityAttr>(); |
| 477 | } |
| 478 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 479 | static bool typeHasCapability(Sema &S, QualType Ty) { |
| 480 | if (checkTypedefTypeForCapability(Ty)) |
| 481 | return true; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 482 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 483 | if (checkRecordTypeForCapability(S, Ty)) |
| 484 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 485 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 486 | return false; |
| 487 | } |
| 488 | |
| 489 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 490 | // Capability expressions are simple expressions involving the boolean logic |
| 491 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 492 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 493 | // is a capability or not. |
| 494 | |
| 495 | if (const auto *E = dyn_cast<DeclRefExpr>(Ex)) |
| 496 | return typeHasCapability(S, E->getType()); |
| 497 | else if (const auto *E = dyn_cast<CastExpr>(Ex)) |
| 498 | return isCapabilityExpr(S, E->getSubExpr()); |
| 499 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 500 | return isCapabilityExpr(S, E->getSubExpr()); |
| 501 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
| 502 | if (E->getOpcode() == UO_LNot) |
| 503 | return isCapabilityExpr(S, E->getSubExpr()); |
| 504 | return false; |
| 505 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 506 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 507 | return isCapabilityExpr(S, E->getLHS()) && |
| 508 | isCapabilityExpr(S, E->getRHS()); |
| 509 | return false; |
| 510 | } |
| 511 | |
| 512 | return false; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 515 | /// \brief Checks that all attribute arguments, starting from Sidx, resolve to |
| 516 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 517 | /// \param Sidx The attribute argument index to start checking with. |
| 518 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 519 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 520 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
| 521 | const AttributeList &Attr, |
| 522 | SmallVectorImpl<Expr *> &Args, |
| 523 | int Sidx = 0, |
| 524 | bool ParamIdxOk = false) { |
| 525 | for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 526 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 527 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 528 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 529 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 530 | Args.push_back(ArgExp); |
| 531 | continue; |
| 532 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 533 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 534 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 535 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 536 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 537 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 538 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 539 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 540 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 541 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 542 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 543 | // We allow constant strings to be used as a placeholder for expressions |
| 544 | // that are not valid C++ syntax, but warn that they are ignored. |
| 545 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 546 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 547 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 548 | continue; |
| 549 | } |
| 550 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 551 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 552 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 553 | // A pointer to member expression of the form &MyClass::mu is treated |
| 554 | // specially -- we need to look at the type of the member. |
| 555 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 556 | if (UOp->getOpcode() == UO_AddrOf) |
| 557 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 558 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 559 | ArgTy = DRE->getDecl()->getType(); |
| 560 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 561 | // First see if we can just cast to record type, or pointer to record type. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 562 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 563 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 564 | // Now check if we index into a record type function param. |
| 565 | if(!RT && ParamIdxOk) { |
| 566 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 567 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 568 | if(FD && IL) { |
| 569 | unsigned int NumParams = FD->getNumParams(); |
| 570 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 571 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 572 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 573 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 574 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 575 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 576 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 577 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 578 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 582 | // If the type does not have a capability, see if the components of the |
| 583 | // expression have capabilities. This allows for writing C code where the |
| 584 | // capability may be on the type, and the expression is a capability |
| 585 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 586 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
| 587 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 588 | << Attr.getName() << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 589 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 590 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 591 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 594 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 595 | // Attribute Implementations |
| 596 | //===----------------------------------------------------------------------===// |
| 597 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 598 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 599 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 600 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 601 | return; |
| 602 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 603 | D->addAttr(::new (S.Context) |
| 604 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 605 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 608 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 609 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 610 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 611 | SmallVector<Expr*, 1> Args; |
| 612 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 613 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 614 | unsigned Size = Args.size(); |
| 615 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 616 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 617 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 618 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 619 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 620 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 623 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 624 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 625 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 626 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 627 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 628 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg, |
| 629 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 632 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 633 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 634 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 635 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 636 | return; |
| 637 | |
| 638 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 639 | return; |
| 640 | |
| 641 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 642 | S.Context, Arg, |
| 643 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 646 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 647 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 648 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 649 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 650 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 651 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 652 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 653 | QualType QT = cast<ValueDecl>(D)->getType(); |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 654 | if (!QT->isDependentType() && !typeHasCapability(S, QT)) { |
| 655 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
| 656 | << Attr.getName(); |
| 657 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 658 | } |
| 659 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 660 | // Check that all arguments are lockable objects. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 661 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 662 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 663 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 664 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 665 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 668 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 669 | const AttributeList &Attr) { |
| 670 | SmallVector<Expr*, 1> Args; |
| 671 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 672 | return; |
| 673 | |
| 674 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 675 | D->addAttr(::new (S.Context) |
| 676 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 677 | StartArg, Args.size(), |
| 678 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 681 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 682 | const AttributeList &Attr) { |
| 683 | SmallVector<Expr*, 1> Args; |
| 684 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 685 | return; |
| 686 | |
| 687 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 688 | D->addAttr(::new (S.Context) |
| 689 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 690 | StartArg, Args.size(), |
| 691 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 694 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 695 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 696 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 697 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 698 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 699 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 700 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 701 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 702 | } |
| 703 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 704 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 705 | const AttributeList &Attr) { |
| 706 | SmallVector<Expr*, 1> Args; |
| 707 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 708 | return; |
| 709 | |
| 710 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 711 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 712 | D->addAttr(::new (S.Context) |
| 713 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 714 | Attr.getAttributeSpellingListIndex())); |
| 715 | } |
| 716 | |
| 717 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 718 | const AttributeList &Attr) { |
| 719 | SmallVector<Expr*, 1> Args; |
| 720 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 721 | return; |
| 722 | |
| 723 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 724 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 725 | D->addAttr(::new (S.Context) |
| 726 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 727 | StartArg, Size, |
| 728 | Attr.getAttributeSpellingListIndex())); |
| 729 | } |
| 730 | |
| 731 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 732 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 733 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 734 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 735 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 736 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 737 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 738 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 739 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 740 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 741 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 745 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 746 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 747 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 750 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 751 | const AttributeList &Attr) { |
| 752 | SmallVector<Expr*, 2> Args; |
| 753 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 754 | return; |
| 755 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 756 | D->addAttr(::new (S.Context) |
| 757 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 758 | Attr.getArgAsExpr(0), |
| 759 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 760 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 763 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 764 | const AttributeList &Attr) { |
| 765 | SmallVector<Expr*, 2> Args; |
| 766 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 767 | return; |
| 768 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 769 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
| 770 | Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(), |
| 771 | Args.size(), Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 774 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 775 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 776 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 777 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 778 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 779 | unsigned Size = Args.size(); |
| 780 | if (Size == 0) |
| 781 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 782 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 783 | D->addAttr(::new (S.Context) |
| 784 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 785 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 789 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 790 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 791 | return; |
| 792 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 793 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 794 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 795 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 796 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 797 | if (Size == 0) |
| 798 | return; |
| 799 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 800 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 801 | D->addAttr(::new (S.Context) |
| 802 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 803 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 806 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Saleem Abdulrasool | 046ba5b | 2016-02-18 06:49:31 +0000 | [diff] [blame] | 807 | S.Diag(Attr.getLoc(), diag::ext_clang_enable_if); |
| 808 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 809 | Expr *Cond = Attr.getArgAsExpr(0); |
| 810 | if (!Cond->isTypeDependent()) { |
| 811 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 812 | if (Converted.isInvalid()) |
| 813 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 814 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | StringRef Msg; |
| 818 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
| 819 | return; |
| 820 | |
| 821 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 822 | if (!Cond->isValueDependent() && |
| 823 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 824 | Diags)) { |
| 825 | S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr); |
| 826 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 827 | S.Diag(Diags[I].first, Diags[I].second); |
| 828 | return; |
| 829 | } |
| 830 | |
| 831 | D->addAttr(::new (S.Context) |
| 832 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 833 | Attr.getAttributeSpellingListIndex())); |
| 834 | } |
| 835 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 836 | static void handlePassObjectSizeAttr(Sema &S, Decl *D, |
| 837 | const AttributeList &Attr) { |
| 838 | if (D->hasAttr<PassObjectSizeAttr>()) { |
| 839 | S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter) |
| 840 | << Attr.getName(); |
| 841 | return; |
| 842 | } |
| 843 | |
| 844 | Expr *E = Attr.getArgAsExpr(0); |
| 845 | uint32_t Type; |
| 846 | if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1)) |
| 847 | return; |
| 848 | |
| 849 | // pass_object_size's argument is passed in as the second argument of |
| 850 | // __builtin_object_size. So, it has the same constraints as that second |
| 851 | // argument; namely, it must be in the range [0, 3]. |
| 852 | if (Type > 3) { |
| 853 | S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range) |
| 854 | << Attr.getName() << 0 << 3 << E->getSourceRange(); |
| 855 | return; |
| 856 | } |
| 857 | |
| 858 | // pass_object_size is only supported on constant pointer parameters; as a |
| 859 | // kindness to users, we allow the parameter to be non-const for declarations. |
| 860 | // At this point, we have no clue if `D` belongs to a function declaration or |
| 861 | // definition, so we defer the constness check until later. |
| 862 | if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) { |
| 863 | S.Diag(D->getLocStart(), diag::err_attribute_pointers_only) |
| 864 | << Attr.getName() << 1; |
| 865 | return; |
| 866 | } |
| 867 | |
| 868 | D->addAttr(::new (S.Context) |
| 869 | PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type, |
| 870 | Attr.getAttributeSpellingListIndex())); |
| 871 | } |
| 872 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 873 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 874 | ConsumableAttr::ConsumedState DefaultState; |
| 875 | |
| 876 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 877 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 878 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 879 | DefaultState)) { |
| 880 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 881 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 882 | return; |
| 883 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 884 | } else { |
| 885 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 886 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 887 | return; |
| 888 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 889 | |
| 890 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 891 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 892 | Attr.getAttributeSpellingListIndex())); |
| 893 | } |
| 894 | |
| 895 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 896 | const AttributeList &Attr) { |
| 897 | ASTContext &CurrContext = S.getASTContext(); |
| 898 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 899 | |
| 900 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 901 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 902 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 903 | RD->getNameAsString(); |
| 904 | |
| 905 | return false; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | return true; |
| 910 | } |
| 911 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 912 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 913 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 914 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 915 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 916 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 917 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 918 | return; |
| 919 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 920 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 921 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 922 | CallableWhenAttr::ConsumedState CallableState; |
| 923 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 924 | StringRef StateString; |
| 925 | SourceLocation Loc; |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 926 | if (Attr.isArgIdent(ArgIndex)) { |
| 927 | IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex); |
| 928 | StateString = Ident->Ident->getName(); |
| 929 | Loc = Ident->Loc; |
| 930 | } else { |
| 931 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 932 | return; |
| 933 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 934 | |
| 935 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 936 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 937 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 938 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 939 | return; |
| 940 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 941 | |
| 942 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 943 | } |
| 944 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 945 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 946 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 947 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 948 | } |
| 949 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 950 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 951 | const AttributeList &Attr) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 952 | ParamTypestateAttr::ConsumedState ParamState; |
| 953 | |
| 954 | if (Attr.isArgIdent(0)) { |
| 955 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 956 | StringRef StateString = Ident->Ident->getName(); |
| 957 | |
| 958 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 959 | ParamState)) { |
| 960 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 961 | << Attr.getName() << StateString; |
| 962 | return; |
| 963 | } |
| 964 | } else { |
| 965 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 966 | Attr.getName() << AANT_ArgumentIdentifier; |
| 967 | return; |
| 968 | } |
| 969 | |
| 970 | // FIXME: This check is currently being done in the analysis. It can be |
| 971 | // enabled here only after the parser propagates attributes at |
| 972 | // template specialization definition, not declaration. |
| 973 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 974 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 975 | // |
| 976 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 977 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 978 | // ReturnType.getAsString(); |
| 979 | // return; |
| 980 | //} |
| 981 | |
| 982 | D->addAttr(::new (S.Context) |
| 983 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 984 | Attr.getAttributeSpellingListIndex())); |
| 985 | } |
| 986 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 987 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 988 | const AttributeList &Attr) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 989 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 990 | |
| 991 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 992 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 993 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 994 | ReturnState)) { |
| 995 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 996 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 997 | return; |
| 998 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 999 | } else { |
| 1000 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1001 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1002 | return; |
| 1003 | } |
| 1004 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1005 | // FIXME: This check is currently being done in the analysis. It can be |
| 1006 | // enabled here only after the parser propagates attributes at |
| 1007 | // template specialization definition, not declaration. |
| 1008 | //QualType ReturnType; |
| 1009 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1010 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1011 | // ReturnType = Param->getType(); |
| 1012 | // |
| 1013 | //} else if (const CXXConstructorDecl *Constructor = |
| 1014 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1015 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1016 | // |
| 1017 | //} else { |
| 1018 | // |
| 1019 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1020 | //} |
| 1021 | // |
| 1022 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1023 | // |
| 1024 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1025 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1026 | // ReturnType.getAsString(); |
| 1027 | // return; |
| 1028 | //} |
| 1029 | |
| 1030 | D->addAttr(::new (S.Context) |
| 1031 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1032 | Attr.getAttributeSpellingListIndex())); |
| 1033 | } |
| 1034 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1035 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1036 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1037 | return; |
| 1038 | |
| 1039 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1040 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1041 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1042 | StringRef Param = Ident->Ident->getName(); |
| 1043 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1044 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1045 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1046 | return; |
| 1047 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1048 | } else { |
| 1049 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1050 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1051 | return; |
| 1052 | } |
| 1053 | |
| 1054 | D->addAttr(::new (S.Context) |
| 1055 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1056 | Attr.getAttributeSpellingListIndex())); |
| 1057 | } |
| 1058 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1059 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1060 | const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1061 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1062 | return; |
| 1063 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1064 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1065 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1066 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1067 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1068 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1069 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1070 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1071 | return; |
| 1072 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1073 | } else { |
| 1074 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1075 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1080 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1081 | Attr.getAttributeSpellingListIndex())); |
| 1082 | } |
| 1083 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1084 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1085 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1086 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1087 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1090 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1091 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1092 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1093 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1094 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1095 | // Report warning about changed offset in the newer compiler versions. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1096 | if (!FD->getType()->isDependentType() && |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1097 | !FD->getType()->isIncompleteType() && FD->isBitField() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1098 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1099 | S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield); |
| 1100 | |
| 1101 | FD->addAttr(::new (S.Context) PackedAttr( |
| 1102 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1103 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1104 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1107 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1108 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1109 | // variables or properties of Objective-C classes. The outlet must also |
| 1110 | // have an object reference type. |
| 1111 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1112 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1113 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1114 | << Attr.getName() << VD->getType() << 0; |
| 1115 | return false; |
| 1116 | } |
| 1117 | } |
| 1118 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1119 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1120 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1121 | << Attr.getName() << PD->getType() << 1; |
| 1122 | return false; |
| 1123 | } |
| 1124 | } |
| 1125 | else { |
| 1126 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1127 | return false; |
| 1128 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1129 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1130 | return true; |
| 1131 | } |
| 1132 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1133 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1134 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1135 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1136 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1137 | D->addAttr(::new (S.Context) |
| 1138 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1139 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1140 | } |
| 1141 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1142 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1143 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1144 | |
| 1145 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1146 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1147 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1148 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1149 | return; |
| 1150 | } |
| 1151 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1152 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1153 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1154 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1155 | ParsedType PT; |
| 1156 | |
| 1157 | if (Attr.hasParsedType()) |
| 1158 | PT = Attr.getTypeArg(); |
| 1159 | else { |
| 1160 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1161 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1162 | if (!PT) { |
| 1163 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1164 | return; |
| 1165 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1166 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1167 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1168 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1169 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1170 | if (!QTLoc) |
| 1171 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1172 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1173 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1174 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1175 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1176 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1177 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1178 | S.Diag(Attr.getLoc(), |
| 1179 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1180 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1181 | return; |
| 1182 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1183 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1184 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1185 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1186 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1189 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1190 | if (RefOkay) { |
| 1191 | if (T->isReferenceType()) |
| 1192 | return true; |
| 1193 | } else { |
| 1194 | T = T.getNonReferenceType(); |
| 1195 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1196 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1197 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1198 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1199 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1200 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1201 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1202 | for (const auto *I : UD->fields()) { |
| 1203 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1204 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1205 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1206 | } |
| 1207 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
| 1210 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1213 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1214 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1215 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1216 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1217 | if (!S.isValidPointerAttrType(T)) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1218 | if (isReturnValue) |
| 1219 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1220 | << Attr.getName() << AttrParmRange << TypeRange; |
| 1221 | else |
| 1222 | S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only) |
| 1223 | << Attr.getName() << AttrParmRange << TypeRange << 0; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1224 | return false; |
| 1225 | } |
| 1226 | return true; |
| 1227 | } |
| 1228 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1229 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1230 | SmallVector<unsigned, 8> NonNullArgs; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1231 | for (unsigned I = 0; I < Attr.getNumArgs(); ++I) { |
| 1232 | Expr *Ex = Attr.getArgAsExpr(I); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1233 | uint64_t Idx; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1234 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1235 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1236 | |
| 1237 | // Is the function argument a pointer type? |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1238 | if (Idx < getFunctionOrMethodNumParams(D) && |
| 1239 | !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1240 | Ex->getSourceRange(), |
| 1241 | getFunctionOrMethodParamRange(D, Idx))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1242 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1243 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1244 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1245 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1246 | |
| 1247 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1248 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1249 | // check if the attribute came from a macro expansion or a template |
| 1250 | // instantiation. |
| 1251 | if (NonNullArgs.empty() && Attr.getLoc().isFileID() && |
| 1252 | S.ActiveTemplateInstantiations.empty()) { |
| 1253 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1254 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1255 | I != E && !AnyPointers; ++I) { |
| 1256 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1257 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1258 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1259 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1260 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1261 | if (!AnyPointers) |
| 1262 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1263 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1264 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1265 | unsigned *Start = NonNullArgs.data(); |
| 1266 | unsigned Size = NonNullArgs.size(); |
| 1267 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1268 | D->addAttr(::new (S.Context) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1269 | NonNullAttr(Attr.getRange(), S.Context, Start, Size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1270 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1273 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1274 | const AttributeList &Attr) { |
| 1275 | if (Attr.getNumArgs() > 0) { |
| 1276 | if (D->getFunctionType()) { |
| 1277 | handleNonNullAttr(S, D, Attr); |
| 1278 | } else { |
| 1279 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1280 | << D->getSourceRange(); |
| 1281 | } |
| 1282 | return; |
| 1283 | } |
| 1284 | |
| 1285 | // Is the argument a pointer type? |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1286 | if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(), |
| 1287 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1288 | return; |
| 1289 | |
| 1290 | D->addAttr(::new (S.Context) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1291 | NonNullAttr(Attr.getRange(), S.Context, nullptr, 0, |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1292 | Attr.getAttributeSpellingListIndex())); |
| 1293 | } |
| 1294 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1295 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1296 | const AttributeList &Attr) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1297 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1298 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1299 | if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1300 | /* isReturnValue */ true)) |
| 1301 | return; |
| 1302 | |
| 1303 | D->addAttr(::new (S.Context) |
| 1304 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1305 | Attr.getAttributeSpellingListIndex())); |
| 1306 | } |
| 1307 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1308 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, |
| 1309 | const AttributeList &Attr) { |
| 1310 | Expr *E = Attr.getArgAsExpr(0), |
| 1311 | *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr; |
| 1312 | S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE, |
| 1313 | Attr.getAttributeSpellingListIndex()); |
| 1314 | } |
| 1315 | |
| 1316 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1317 | Expr *OE, unsigned SpellingListIndex) { |
| 1318 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1319 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1320 | |
| 1321 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1322 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1323 | |
| 1324 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1325 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1326 | << &TmpAttr << AttrRange << SR; |
| 1327 | return; |
| 1328 | } |
| 1329 | |
| 1330 | if (!E->isValueDependent()) { |
| 1331 | llvm::APSInt I(64); |
| 1332 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1333 | if (OE) |
| 1334 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1335 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1336 | << E->getSourceRange(); |
| 1337 | else |
| 1338 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1339 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1340 | << E->getSourceRange(); |
| 1341 | return; |
| 1342 | } |
| 1343 | |
| 1344 | if (!I.isPowerOf2()) { |
| 1345 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1346 | << E->getSourceRange(); |
| 1347 | return; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | if (OE) { |
| 1352 | if (!OE->isValueDependent()) { |
| 1353 | llvm::APSInt I(64); |
| 1354 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1355 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1356 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1357 | << OE->getSourceRange(); |
| 1358 | return; |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | D->addAttr(::new (Context) |
| 1364 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1365 | } |
| 1366 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1367 | /// Normalize the attribute, __foo__ becomes foo. |
| 1368 | /// Returns true if normalization was applied. |
| 1369 | static bool normalizeName(StringRef &AttrName) { |
Aaron Ballman | 6269236 | 2015-10-09 13:53:24 +0000 | [diff] [blame] | 1370 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1371 | AttrName.endswith("__")) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1372 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1373 | return true; |
| 1374 | } |
| 1375 | return false; |
| 1376 | } |
| 1377 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1378 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1379 | // This attribute must be applied to a function declaration. The first |
| 1380 | // argument to the attribute must be an identifier, the name of the resource, |
| 1381 | // for example: malloc. The following arguments must be argument indexes, the |
| 1382 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1383 | // 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] | 1384 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1385 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1386 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1387 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1388 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1389 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1390 | return; |
| 1391 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1392 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1393 | // Figure out our Kind. |
| 1394 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1395 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1396 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1397 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1398 | // Check arguments. |
| 1399 | switch (K) { |
| 1400 | case OwnershipAttr::Takes: |
| 1401 | case OwnershipAttr::Holds: |
| 1402 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1403 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1404 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1405 | return; |
| 1406 | } |
| 1407 | break; |
| 1408 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1409 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1410 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1411 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1412 | return; |
| 1413 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1414 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1415 | } |
| 1416 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1417 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1418 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1419 | StringRef ModuleName = Module->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1420 | if (normalizeName(ModuleName)) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1421 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1422 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1423 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1424 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1425 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1426 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1427 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1428 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1429 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1430 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1431 | // Is the function argument a pointer type? |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1432 | QualType T = getFunctionOrMethodParamType(D, Idx); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1433 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1434 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1435 | case OwnershipAttr::Takes: |
| 1436 | case OwnershipAttr::Holds: |
| 1437 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1438 | Err = 0; |
| 1439 | break; |
| 1440 | case OwnershipAttr::Returns: |
| 1441 | if (!T->isIntegerType()) |
| 1442 | Err = 1; |
| 1443 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1444 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1445 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1446 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1447 | << Ex->getSourceRange(); |
| 1448 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1449 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1450 | |
| 1451 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1452 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1453 | // Cannot have two ownership attributes of different kinds for the same |
| 1454 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1455 | if (I->getOwnKind() != K && I->args_end() != |
| 1456 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1457 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1458 | << AL.getName() << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1459 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1460 | } else if (K == OwnershipAttr::Returns && |
| 1461 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1462 | // A returns attribute conflicts with any other returns attribute using |
| 1463 | // a different index. Note, diagnostic reporting is 1-based, but stored |
| 1464 | // argument indexes are 0-based. |
| 1465 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1466 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
| 1467 | << *(I->args_begin()) + 1; |
| 1468 | if (I->args_size()) |
| 1469 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
| 1470 | << (unsigned)Idx + 1 << Ex->getSourceRange(); |
| 1471 | return; |
| 1472 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1473 | } |
| 1474 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1475 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | unsigned* start = OwnershipArgs.data(); |
| 1479 | unsigned size = OwnershipArgs.size(); |
| 1480 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1481 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1482 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1483 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1484 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1487 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1488 | // Check the attribute arguments. |
| 1489 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1490 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1491 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1492 | return; |
| 1493 | } |
| 1494 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1495 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1496 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1497 | // gcc rejects |
| 1498 | // class c { |
| 1499 | // static int a __attribute__((weakref ("v2"))); |
| 1500 | // static int b() __attribute__((weakref ("f3"))); |
| 1501 | // }; |
| 1502 | // and ignores the attributes of |
| 1503 | // void f(void) { |
| 1504 | // static int a __attribute__((weakref ("v2"))); |
| 1505 | // } |
| 1506 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1507 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1508 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1509 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1510 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1511 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | // The GCC manual says |
| 1515 | // |
| 1516 | // At present, a declaration to which `weakref' is attached can only |
| 1517 | // be `static'. |
| 1518 | // |
| 1519 | // It also says |
| 1520 | // |
| 1521 | // Without a TARGET, |
| 1522 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1523 | // equivalent to `weak'. |
| 1524 | // |
| 1525 | // gcc 4.4.1 will accept |
| 1526 | // int a7 __attribute__((weakref)); |
| 1527 | // as |
| 1528 | // int a7 __attribute__((weak)); |
| 1529 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1530 | // it if this behaviour is actually used. |
| 1531 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1532 | // GCC rejects |
| 1533 | // static ((alias ("y"), weakref)). |
| 1534 | // Should we? How to check that weakref is before or after alias? |
| 1535 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1536 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1537 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1538 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1539 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1540 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1541 | // GCC will accept anything as the argument of weakref. Should we |
| 1542 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1543 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1544 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1545 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1546 | D->addAttr(::new (S.Context) |
| 1547 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1548 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1551 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1552 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1553 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1554 | return; |
| 1555 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1556 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1557 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1558 | return; |
| 1559 | } |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1560 | if (S.Context.getTargetInfo().getTriple().isNVPTX()) { |
| 1561 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_nvptx); |
| 1562 | } |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1563 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1564 | // Aliases should be on declarations, not definitions. |
| 1565 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1566 | if (FD->isThisDeclarationADefinition()) { |
| 1567 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD; |
| 1568 | return; |
| 1569 | } |
| 1570 | } else { |
| 1571 | const auto *VD = cast<VarDecl>(D); |
| 1572 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
| 1573 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD; |
| 1574 | return; |
| 1575 | } |
| 1576 | } |
| 1577 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1578 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1579 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1580 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1581 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1584 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1585 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1586 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1587 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1588 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1589 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1593 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1594 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1595 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1596 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1597 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1600 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1601 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1602 | StringRef Model; |
| 1603 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1604 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1605 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1606 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1607 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1608 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1609 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1610 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1611 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1612 | return; |
| 1613 | } |
| 1614 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1615 | D->addAttr(::new (S.Context) |
| 1616 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1617 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1620 | static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1621 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1622 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1623 | D->addAttr(::new (S.Context) RestrictAttr( |
| 1624 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1625 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1628 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1629 | << Attr.getName() << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1630 | } |
| 1631 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1632 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1633 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1634 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1635 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1636 | return; |
| 1637 | } |
| 1638 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1639 | if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(), |
| 1640 | Attr.getAttributeSpellingListIndex())) |
| 1641 | D->addAttr(CA); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 1644 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1645 | if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(), |
| 1646 | Attr.getName())) |
| 1647 | return; |
| 1648 | |
| 1649 | D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context, |
| 1650 | Attr.getAttributeSpellingListIndex())); |
| 1651 | } |
| 1652 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1653 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1654 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1655 | |
| 1656 | if (S.CheckNoReturnAttr(attr)) return; |
| 1657 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1658 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1659 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1660 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1661 | return; |
| 1662 | } |
| 1663 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1664 | D->addAttr(::new (S.Context) |
| 1665 | NoReturnAttr(attr.getRange(), S.Context, |
| 1666 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1670 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1671 | attr.setInvalid(); |
| 1672 | return true; |
| 1673 | } |
| 1674 | |
| 1675 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1676 | } |
| 1677 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1678 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1679 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1680 | |
| 1681 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1682 | // because 'analyzer_noreturn' does not impact the type. |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 1683 | if (!isFunctionOrMethodOrBlock(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1684 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1685 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 1686 | !VD->getType()->isFunctionPointerType())) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1687 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1688 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 1689 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1690 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1691 | return; |
| 1692 | } |
| 1693 | } |
| 1694 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1695 | D->addAttr(::new (S.Context) |
| 1696 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1697 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1700 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1701 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1702 | /* |
| 1703 | Returning a Vector Class in Registers |
| 1704 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1705 | According to the PPU ABI specifications, a class with a single member of |
| 1706 | vector type is returned in memory when used as the return value of a function. |
| 1707 | This results in inefficient code when implementing vector classes. To return |
| 1708 | the value in a single vector register, add the vecreturn attribute to the |
| 1709 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1710 | |
| 1711 | Example: |
| 1712 | |
| 1713 | struct Vector |
| 1714 | { |
| 1715 | __vector float xyzw; |
| 1716 | } __attribute__((vecreturn)); |
| 1717 | |
| 1718 | Vector Add(Vector lhs, Vector rhs) |
| 1719 | { |
| 1720 | Vector result; |
| 1721 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1722 | return result; // This will be returned in a register |
| 1723 | } |
| 1724 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1725 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1726 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1727 | return; |
| 1728 | } |
| 1729 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1730 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1731 | int count = 0; |
| 1732 | |
| 1733 | if (!isa<CXXRecordDecl>(record)) { |
| 1734 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1735 | return; |
| 1736 | } |
| 1737 | |
| 1738 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1739 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1740 | return; |
| 1741 | } |
| 1742 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1743 | for (const auto *I : record->fields()) { |
| 1744 | if ((count == 1) || !I->getType()->isVectorType()) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1745 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1746 | return; |
| 1747 | } |
| 1748 | count++; |
| 1749 | } |
| 1750 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1751 | D->addAttr(::new (S.Context) |
| 1752 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1753 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1756 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1757 | const AttributeList &Attr) { |
| 1758 | if (isa<ParmVarDecl>(D)) { |
| 1759 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1760 | // parameter of a function declaration or lambda. |
| 1761 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1762 | S.Diag(Attr.getLoc(), |
| 1763 | diag::err_carries_dependency_param_not_function_decl); |
| 1764 | return; |
| 1765 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1766 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1767 | |
| 1768 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1769 | Attr.getRange(), S.Context, |
| 1770 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 1773 | static void handleNotTailCalledAttr(Sema &S, Decl *D, |
| 1774 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1775 | if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(), |
| 1776 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 1777 | return; |
| 1778 | |
| 1779 | D->addAttr(::new (S.Context) NotTailCalledAttr( |
| 1780 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1781 | } |
| 1782 | |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 1783 | static void handleDisableTailCallsAttr(Sema &S, Decl *D, |
| 1784 | const AttributeList &Attr) { |
| 1785 | if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(), |
| 1786 | Attr.getName())) |
| 1787 | return; |
| 1788 | |
| 1789 | D->addAttr(::new (S.Context) DisableTailCallsAttr( |
| 1790 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1791 | } |
| 1792 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1793 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1794 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1795 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1796 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1797 | return; |
| 1798 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1799 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1800 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1801 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1802 | return; |
| 1803 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1804 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1805 | D->addAttr(::new (S.Context) |
| 1806 | UsedAttr(Attr.getRange(), S.Context, |
| 1807 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 1810 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1811 | bool IsCXX1zAttr = Attr.isCXX11Attribute() && !Attr.getScopeName(); |
| 1812 | |
| 1813 | if (IsCXX1zAttr && isa<VarDecl>(D)) { |
| 1814 | // The C++1z spelling of this attribute cannot be applied to a static data |
| 1815 | // member per [dcl.attr.unused]p2. |
| 1816 | if (cast<VarDecl>(D)->isStaticDataMember()) { |
| 1817 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1818 | << Attr.getName() << ExpectedForMaybeUnused; |
| 1819 | return; |
| 1820 | } |
| 1821 | } |
| 1822 | |
| 1823 | // If this is spelled as the standard C++1z attribute, but not in C++1z, warn |
| 1824 | // about using it as an extension. |
| 1825 | if (!S.getLangOpts().CPlusPlus1z && IsCXX1zAttr) |
| 1826 | S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName(); |
| 1827 | |
| 1828 | D->addAttr(::new (S.Context) UnusedAttr( |
| 1829 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1830 | } |
| 1831 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1832 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1833 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1834 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1835 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1836 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1837 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1838 | D->addAttr(::new (S.Context) |
| 1839 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1840 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1841 | } |
| 1842 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1843 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 1844 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1845 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1846 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1847 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1848 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1849 | D->addAttr(::new (S.Context) |
| 1850 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1851 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1854 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1855 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1856 | const AttributeList &Attr) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1857 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1858 | StringRef Str; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1859 | if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1860 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1861 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1862 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1863 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1864 | } |
| 1865 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1866 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1867 | const AttributeList &Attr) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1868 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 1869 | S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
| 1870 | << Attr.getName() << Attr.getRange(); |
| 1871 | return; |
| 1872 | } |
| 1873 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1874 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1875 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1876 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1879 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1880 | IdentifierInfo *Platform, |
| 1881 | VersionTuple Introduced, |
| 1882 | VersionTuple Deprecated, |
| 1883 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1884 | StringRef PlatformName |
| 1885 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1886 | if (PlatformName.empty()) |
| 1887 | PlatformName = Platform->getName(); |
| 1888 | |
| 1889 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1890 | // of these steps are needed). |
| 1891 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1892 | !(Introduced <= Deprecated)) { |
| 1893 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1894 | << 1 << PlatformName << Deprecated.getAsString() |
| 1895 | << 0 << Introduced.getAsString(); |
| 1896 | return true; |
| 1897 | } |
| 1898 | |
| 1899 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1900 | !(Introduced <= Obsoleted)) { |
| 1901 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1902 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1903 | << 0 << Introduced.getAsString(); |
| 1904 | return true; |
| 1905 | } |
| 1906 | |
| 1907 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1908 | !(Deprecated <= Obsoleted)) { |
| 1909 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1910 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1911 | << 1 << Deprecated.getAsString(); |
| 1912 | return true; |
| 1913 | } |
| 1914 | |
| 1915 | return false; |
| 1916 | } |
| 1917 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1918 | /// \brief Check whether the two versions match. |
| 1919 | /// |
| 1920 | /// If either version tuple is empty, then they are assumed to match. If |
| 1921 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1922 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1923 | bool BeforeIsOkay) { |
| 1924 | if (X.empty() || Y.empty()) |
| 1925 | return true; |
| 1926 | |
| 1927 | if (X == Y) |
| 1928 | return true; |
| 1929 | |
| 1930 | if (BeforeIsOkay && X < Y) |
| 1931 | return true; |
| 1932 | |
| 1933 | return false; |
| 1934 | } |
| 1935 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1936 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1937 | IdentifierInfo *Platform, |
| 1938 | VersionTuple Introduced, |
| 1939 | VersionTuple Deprecated, |
| 1940 | VersionTuple Obsoleted, |
| 1941 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1942 | StringRef Message, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 1943 | bool IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame^] | 1944 | StringRef Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1945 | AvailabilityMergeKind AMK, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1946 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1947 | VersionTuple MergedIntroduced = Introduced; |
| 1948 | VersionTuple MergedDeprecated = Deprecated; |
| 1949 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1950 | bool FoundAny = false; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1951 | bool OverrideOrImpl = false; |
| 1952 | switch (AMK) { |
| 1953 | case AMK_None: |
| 1954 | case AMK_Redeclaration: |
| 1955 | OverrideOrImpl = false; |
| 1956 | break; |
| 1957 | |
| 1958 | case AMK_Override: |
| 1959 | case AMK_ProtocolImplementation: |
| 1960 | OverrideOrImpl = true; |
| 1961 | break; |
| 1962 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1963 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1964 | if (D->hasAttrs()) { |
| 1965 | AttrVec &Attrs = D->getAttrs(); |
| 1966 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1967 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1968 | if (!OldAA) { |
| 1969 | ++i; |
| 1970 | continue; |
| 1971 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1972 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1973 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1974 | if (OldPlatform != Platform) { |
| 1975 | ++i; |
| 1976 | continue; |
| 1977 | } |
| 1978 | |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 1979 | // If there is an existing availability attribute for this platform that |
| 1980 | // is explicit and the new one is implicit use the explicit one and |
| 1981 | // discard the new implicit attribute. |
| 1982 | if (OldAA->getRange().isValid() && Range.isInvalid()) { |
| 1983 | return nullptr; |
| 1984 | } |
| 1985 | |
| 1986 | // If there is an existing attribute for this platform that is implicit |
| 1987 | // and the new attribute is explicit then erase the old one and |
| 1988 | // continue processing the attributes. |
| 1989 | if (Range.isValid() && OldAA->getRange().isInvalid()) { |
| 1990 | Attrs.erase(Attrs.begin() + i); |
| 1991 | --e; |
| 1992 | continue; |
| 1993 | } |
| 1994 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1995 | FoundAny = true; |
| 1996 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 1997 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 1998 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 1999 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2000 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2001 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 2002 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 2003 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2004 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2005 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 2006 | if (OverrideOrImpl) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2007 | int Which = -1; |
| 2008 | VersionTuple FirstVersion; |
| 2009 | VersionTuple SecondVersion; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2010 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2011 | Which = 0; |
| 2012 | FirstVersion = OldIntroduced; |
| 2013 | SecondVersion = Introduced; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2014 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2015 | Which = 1; |
| 2016 | FirstVersion = Deprecated; |
| 2017 | SecondVersion = OldDeprecated; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2018 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2019 | Which = 2; |
| 2020 | FirstVersion = Obsoleted; |
| 2021 | SecondVersion = OldObsoleted; |
| 2022 | } |
| 2023 | |
| 2024 | if (Which == -1) { |
| 2025 | Diag(OldAA->getLocation(), |
| 2026 | diag::warn_mismatched_availability_override_unavail) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2027 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2028 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2029 | } else { |
| 2030 | Diag(OldAA->getLocation(), |
| 2031 | diag::warn_mismatched_availability_override) |
| 2032 | << Which |
| 2033 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2034 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 2035 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2036 | } |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2037 | if (AMK == AMK_Override) |
| 2038 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2039 | else |
| 2040 | Diag(Range.getBegin(), diag::note_protocol_method); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2041 | } else { |
| 2042 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2043 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2044 | } |
| 2045 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2046 | Attrs.erase(Attrs.begin() + i); |
| 2047 | --e; |
| 2048 | continue; |
| 2049 | } |
| 2050 | |
| 2051 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2052 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2053 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2054 | |
| 2055 | if (MergedIntroduced2.empty()) |
| 2056 | MergedIntroduced2 = OldIntroduced; |
| 2057 | if (MergedDeprecated2.empty()) |
| 2058 | MergedDeprecated2 = OldDeprecated; |
| 2059 | if (MergedObsoleted2.empty()) |
| 2060 | MergedObsoleted2 = OldObsoleted; |
| 2061 | |
| 2062 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2063 | MergedIntroduced2, MergedDeprecated2, |
| 2064 | MergedObsoleted2)) { |
| 2065 | Attrs.erase(Attrs.begin() + i); |
| 2066 | --e; |
| 2067 | continue; |
| 2068 | } |
| 2069 | |
| 2070 | MergedIntroduced = MergedIntroduced2; |
| 2071 | MergedDeprecated = MergedDeprecated2; |
| 2072 | MergedObsoleted = MergedObsoleted2; |
| 2073 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2074 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
| 2077 | if (FoundAny && |
| 2078 | MergedIntroduced == Introduced && |
| 2079 | MergedDeprecated == Deprecated && |
| 2080 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2081 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2082 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2083 | // Only create a new attribute if !OverrideOrImpl, but we want to do |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2084 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2085 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2086 | MergedDeprecated, MergedObsoleted) && |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2087 | !OverrideOrImpl) { |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2088 | return ::new (Context) AvailabilityAttr(Range, Context, Platform, |
| 2089 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2090 | Obsoleted, IsUnavailable, Message, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame^] | 2091 | IsStrict, Replacement, |
| 2092 | AttrSpellingListIndex); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2093 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2094 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2095 | } |
| 2096 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2097 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2098 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2099 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2100 | return; |
| 2101 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2102 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2103 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2104 | IdentifierInfo *II = Platform->Ident; |
| 2105 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2106 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2107 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2108 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2109 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2110 | if (!ND) { |
| 2111 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2112 | return; |
| 2113 | } |
| 2114 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2115 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2116 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2117 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2118 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2119 | bool IsStrict = Attr.getStrictLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2120 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 2121 | if (const StringLiteral *SE = |
| 2122 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2123 | Str = SE->getString(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame^] | 2124 | StringRef Replacement; |
| 2125 | if (const StringLiteral *SE = |
| 2126 | dyn_cast_or_null<StringLiteral>(Attr.getReplacementExpr())) |
| 2127 | Replacement = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2128 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2129 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2130 | Introduced.Version, |
| 2131 | Deprecated.Version, |
| 2132 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2133 | IsUnavailable, Str, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame^] | 2134 | IsStrict, Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2135 | Sema::AMK_None, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2136 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2137 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2138 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2139 | |
| 2140 | // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning |
| 2141 | // matches before the start of the watchOS platform. |
| 2142 | if (S.Context.getTargetInfo().getTriple().isWatchOS()) { |
| 2143 | IdentifierInfo *NewII = nullptr; |
| 2144 | if (II->getName() == "ios") |
| 2145 | NewII = &S.Context.Idents.get("watchos"); |
| 2146 | else if (II->getName() == "ios_app_extension") |
| 2147 | NewII = &S.Context.Idents.get("watchos_app_extension"); |
| 2148 | |
| 2149 | if (NewII) { |
| 2150 | auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple { |
| 2151 | if (Version.empty()) |
| 2152 | return Version; |
| 2153 | auto Major = Version.getMajor(); |
| 2154 | auto NewMajor = Major >= 9 ? Major - 7 : 0; |
| 2155 | if (NewMajor >= 2) { |
| 2156 | if (Version.getMinor().hasValue()) { |
| 2157 | if (Version.getSubminor().hasValue()) |
| 2158 | return VersionTuple(NewMajor, Version.getMinor().getValue(), |
| 2159 | Version.getSubminor().getValue()); |
| 2160 | else |
| 2161 | return VersionTuple(NewMajor, Version.getMinor().getValue()); |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | return VersionTuple(2, 0); |
| 2166 | }; |
| 2167 | |
| 2168 | auto NewIntroduced = adjustWatchOSVersion(Introduced.Version); |
| 2169 | auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version); |
| 2170 | auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version); |
| 2171 | |
| 2172 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
| 2173 | SourceRange(), |
| 2174 | NewII, |
| 2175 | NewIntroduced, |
| 2176 | NewDeprecated, |
| 2177 | NewObsoleted, |
| 2178 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2179 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame^] | 2180 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2181 | Sema::AMK_None, |
| 2182 | Index); |
| 2183 | if (NewAttr) |
| 2184 | D->addAttr(NewAttr); |
| 2185 | } |
| 2186 | } else if (S.Context.getTargetInfo().getTriple().isTvOS()) { |
| 2187 | // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning |
| 2188 | // matches before the start of the tvOS platform. |
| 2189 | IdentifierInfo *NewII = nullptr; |
| 2190 | if (II->getName() == "ios") |
| 2191 | NewII = &S.Context.Idents.get("tvos"); |
| 2192 | else if (II->getName() == "ios_app_extension") |
| 2193 | NewII = &S.Context.Idents.get("tvos_app_extension"); |
| 2194 | |
| 2195 | if (NewII) { |
| 2196 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
| 2197 | SourceRange(), |
| 2198 | NewII, |
| 2199 | Introduced.Version, |
| 2200 | Deprecated.Version, |
| 2201 | Obsoleted.Version, |
| 2202 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2203 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame^] | 2204 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2205 | Sema::AMK_None, |
| 2206 | Index); |
| 2207 | if (NewAttr) |
| 2208 | D->addAttr(NewAttr); |
| 2209 | } |
| 2210 | } |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2211 | } |
| 2212 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2213 | template <class T> |
| 2214 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2215 | typename T::VisibilityType value, |
| 2216 | unsigned attrSpellingListIndex) { |
| 2217 | T *existingAttr = D->getAttr<T>(); |
| 2218 | if (existingAttr) { |
| 2219 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2220 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2221 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2222 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2223 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2224 | D->dropAttr<T>(); |
| 2225 | } |
| 2226 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2227 | } |
| 2228 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2229 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2230 | VisibilityAttr::VisibilityType Vis, |
| 2231 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2232 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2233 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2236 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2237 | TypeVisibilityAttr::VisibilityType Vis, |
| 2238 | unsigned AttrSpellingListIndex) { |
| 2239 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2240 | AttrSpellingListIndex); |
| 2241 | } |
| 2242 | |
| 2243 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2244 | bool isTypeVisibility) { |
| 2245 | // Visibility attributes don't mean anything on a typedef. |
| 2246 | if (isa<TypedefNameDecl>(D)) { |
| 2247 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2248 | << Attr.getName(); |
| 2249 | return; |
| 2250 | } |
| 2251 | |
| 2252 | // 'type_visibility' can only go on a type or namespace. |
| 2253 | if (isTypeVisibility && |
| 2254 | !(isa<TagDecl>(D) || |
| 2255 | isa<ObjCInterfaceDecl>(D) || |
| 2256 | isa<NamespaceDecl>(D))) { |
| 2257 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2258 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2259 | return; |
| 2260 | } |
| 2261 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2262 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2263 | StringRef TypeStr; |
| 2264 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2265 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2266 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2267 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2268 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2269 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2270 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2271 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2272 | return; |
| 2273 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2274 | |
| 2275 | // Complain about attempts to use protected visibility on targets |
| 2276 | // (like Darwin) that don't support it. |
| 2277 | if (type == VisibilityAttr::Protected && |
| 2278 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2279 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2280 | type = VisibilityAttr::Default; |
| 2281 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2282 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2283 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2284 | clang::Attr *newAttr; |
| 2285 | if (isTypeVisibility) { |
| 2286 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2287 | (TypeVisibilityAttr::VisibilityType) type, |
| 2288 | Index); |
| 2289 | } else { |
| 2290 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2291 | } |
| 2292 | if (newAttr) |
| 2293 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2294 | } |
| 2295 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2296 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2297 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2298 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2299 | if (!Attr.isArgIdent(0)) { |
| 2300 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2301 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2302 | return; |
| 2303 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2304 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2305 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2306 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2307 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2308 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2309 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2310 | return; |
| 2311 | } |
| 2312 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2313 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 2314 | !method->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2315 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2316 | << method->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2317 | // Ignore the attribute. |
| 2318 | return; |
| 2319 | } |
| 2320 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2321 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2322 | S.Context, F, |
| 2323 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2326 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2327 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2328 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2329 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2330 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2331 | return; |
| 2332 | } |
| 2333 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2334 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2335 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2336 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2337 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2338 | return; |
| 2339 | } |
| 2340 | } |
| 2341 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2342 | // It is okay to include this attribute on properties, e.g.: |
| 2343 | // |
| 2344 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2345 | // |
| 2346 | // In this case it follows tradition and suppresses an error in the above |
| 2347 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2348 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2349 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2350 | D->addAttr(::new (S.Context) |
| 2351 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2352 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2355 | static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2356 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 2357 | QualType T = TD->getUnderlyingType(); |
| 2358 | if (!T->isObjCObjectPointerType()) { |
| 2359 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2360 | return; |
| 2361 | } |
| 2362 | } else { |
| 2363 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2364 | return; |
| 2365 | } |
| 2366 | D->addAttr(::new (S.Context) |
| 2367 | ObjCIndependentClassAttr(Attr.getRange(), S.Context, |
| 2368 | Attr.getAttributeSpellingListIndex())); |
| 2369 | } |
| 2370 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2371 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2372 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2373 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2374 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2375 | return; |
| 2376 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2377 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2378 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2379 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2380 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2381 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2382 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2383 | return; |
| 2384 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2385 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2386 | D->addAttr(::new (S.Context) |
| 2387 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2388 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2391 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2392 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2393 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2394 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2395 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2396 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2397 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2398 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2399 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2400 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2401 | return; |
| 2402 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2403 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2404 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2405 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2406 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2407 | return; |
| 2408 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2409 | |
| 2410 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2411 | } |
| 2412 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2413 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2414 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2415 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2416 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2417 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2418 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2419 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2420 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2421 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2422 | return; |
| 2423 | } |
| 2424 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2425 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2426 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2427 | // FIXME: This error message could be improved, it would be nice |
| 2428 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2429 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2430 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2431 | return; |
| 2432 | } |
| 2433 | } |
| 2434 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2435 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2436 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2437 | if (isa<FunctionNoProtoType>(FT)) { |
| 2438 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2439 | return; |
| 2440 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2441 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2442 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2443 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2444 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2445 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2446 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2447 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2448 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2449 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2450 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2451 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2452 | if (!BD->isVariadic()) { |
| 2453 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2454 | return; |
| 2455 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2456 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2457 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2458 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2459 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2460 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2461 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2462 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2463 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2464 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2465 | return; |
| 2466 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2467 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2468 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2469 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2470 | return; |
| 2471 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2472 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2473 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2474 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2475 | return; |
| 2476 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2477 | D->addAttr(::new (S.Context) |
| 2478 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2479 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2482 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2483 | if (D->getFunctionType() && |
| 2484 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2485 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2486 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2487 | return; |
| 2488 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2489 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2490 | if (MD->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2491 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2492 | << Attr.getName() << 1; |
| 2493 | return; |
| 2494 | } |
| 2495 | |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2496 | // If this is spelled as the standard C++1z attribute, but not in C++1z, warn |
| 2497 | // about using it as an extension. |
| 2498 | if (!S.getLangOpts().CPlusPlus1z && Attr.isCXX11Attribute() && |
| 2499 | !Attr.getScopeName()) |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 2500 | S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName(); |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2501 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2502 | D->addAttr(::new (S.Context) |
| 2503 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2504 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2507 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2508 | // weak_import only applies to variable & function declarations. |
| 2509 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2510 | if (!D->canBeWeakImported(isDef)) { |
| 2511 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2512 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2513 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2514 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2515 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2516 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2517 | // Nothing to warn about here. |
| 2518 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2519 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2520 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2521 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2522 | return; |
| 2523 | } |
| 2524 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2525 | D->addAttr(::new (S.Context) |
| 2526 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2527 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2530 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2531 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2532 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2533 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2534 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2535 | for (unsigned i = 0; i < 3; ++i) { |
| 2536 | const Expr *E = Attr.getArgAsExpr(i); |
| 2537 | if (!checkUInt32Argument(S, Attr, E, WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2538 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2539 | if (WGSize[i] == 0) { |
| 2540 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero) |
| 2541 | << Attr.getName() << E->getSourceRange(); |
| 2542 | return; |
| 2543 | } |
| 2544 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2545 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2546 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2547 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2548 | Existing->getYDim() == WGSize[1] && |
| 2549 | Existing->getZDim() == WGSize[2])) |
| 2550 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2551 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2552 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2553 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2554 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2557 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2558 | if (!Attr.hasParsedType()) { |
| 2559 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2560 | << Attr.getName() << 1; |
| 2561 | return; |
| 2562 | } |
| 2563 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2564 | TypeSourceInfo *ParmTSI = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2565 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2566 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2567 | |
| 2568 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2569 | (ParmType->isBooleanType() || |
| 2570 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2571 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2572 | << ParmType; |
| 2573 | return; |
| 2574 | } |
| 2575 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2576 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2577 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2578 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2579 | return; |
| 2580 | } |
| 2581 | } |
| 2582 | |
| 2583 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2584 | ParmTSI, |
| 2585 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2588 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2589 | StringRef Name, |
| 2590 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2591 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2592 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2593 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2594 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2595 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2596 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2597 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2598 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2599 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2600 | } |
| 2601 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2602 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2603 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2604 | if (!Error.empty()) { |
| 2605 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error; |
| 2606 | return false; |
| 2607 | } |
| 2608 | return true; |
| 2609 | } |
| 2610 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2611 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2612 | // Make sure that there is a string literal as the sections's single |
| 2613 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2614 | StringRef Str; |
| 2615 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2616 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2617 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2618 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2619 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2620 | return; |
| 2621 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2622 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2623 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2624 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2625 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2626 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2627 | return; |
| 2628 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2629 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2630 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2631 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2632 | if (NewAttr) |
| 2633 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2634 | } |
| 2635 | |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2636 | // Check for things we'd like to warn about, no errors or validation for now. |
| 2637 | // TODO: Validation should use a backend target library that specifies |
| 2638 | // the allowable subtarget features and cpus. We could use something like a |
| 2639 | // TargetCodeGenInfo hook here to do validation. |
| 2640 | void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 2641 | for (auto Str : {"tune=", "fpmath="}) |
| 2642 | if (AttrStr.find(Str) != StringRef::npos) |
| 2643 | Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str; |
| 2644 | } |
| 2645 | |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2646 | static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2647 | StringRef Str; |
| 2648 | SourceLocation LiteralLoc; |
| 2649 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
| 2650 | return; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2651 | S.checkTargetAttr(LiteralLoc, Str); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2652 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2653 | TargetAttr *NewAttr = |
| 2654 | ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index); |
| 2655 | D->addAttr(NewAttr); |
| 2656 | } |
| 2657 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2658 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2659 | VarDecl *VD = cast<VarDecl>(D); |
| 2660 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2661 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2662 | return; |
| 2663 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2664 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2665 | Expr *E = Attr.getArgAsExpr(0); |
| 2666 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2667 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2668 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2669 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2670 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2671 | // will warn the user. |
| 2672 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2673 | if (DRE->hasQualifier()) |
| 2674 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2675 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2676 | NI = DRE->getNameInfo(); |
| 2677 | if (!FD) { |
| 2678 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2679 | << NI.getName(); |
| 2680 | return; |
| 2681 | } |
| 2682 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2683 | if (ULE->hasExplicitTemplateArgs()) |
| 2684 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2685 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2686 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2687 | if (!FD) { |
| 2688 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2689 | << NI.getName(); |
| 2690 | if (ULE->getType() == S.Context.OverloadTy) |
| 2691 | S.NoteAllOverloadCandidates(ULE); |
| 2692 | return; |
| 2693 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2694 | } else { |
| 2695 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2696 | return; |
| 2697 | } |
| 2698 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2699 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2700 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2701 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2702 | return; |
| 2703 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2704 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2705 | // We're currently more strict than GCC about what function types we accept. |
| 2706 | // If this ever proves to be a problem it should be easy to fix. |
| 2707 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2708 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2709 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2710 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2711 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2712 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2713 | return; |
| 2714 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2715 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2716 | D->addAttr(::new (S.Context) |
| 2717 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2718 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2721 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2722 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2723 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2724 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2725 | uint64_t Idx; |
| 2726 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2727 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2728 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2729 | // Make sure the format string is really a string. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2730 | QualType Ty = getFunctionOrMethodParamType(D, Idx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2731 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2732 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 2733 | if (NotNSStringTy && |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2734 | !isCFStringType(Ty, S.Context) && |
| 2735 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2736 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2737 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2738 | << "a string type" << IdxExpr->getSourceRange() |
| 2739 | << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2740 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2741 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2742 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2743 | if (!isNSStringType(Ty, S.Context) && |
| 2744 | !isCFStringType(Ty, S.Context) && |
| 2745 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2746 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2747 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2748 | << (NotNSStringTy ? "string type" : "NSString") |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 2749 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2750 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2751 | } |
| 2752 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2753 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2754 | // because that has corrected for the implicit this parameter, and is zero- |
| 2755 | // based. The attribute expects what the user wrote explicitly. |
| 2756 | llvm::APSInt Val; |
| 2757 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2758 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2759 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2760 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2761 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2762 | } |
| 2763 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2764 | enum FormatAttrKind { |
| 2765 | CFStringFormat, |
| 2766 | NSStringFormat, |
| 2767 | StrftimeFormat, |
| 2768 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2769 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2770 | InvalidFormat |
| 2771 | }; |
| 2772 | |
| 2773 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2774 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2775 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2776 | return llvm::StringSwitch<FormatAttrKind>(Format) |
| 2777 | // Check for formats that get handled specially. |
| 2778 | .Case("NSString", NSStringFormat) |
| 2779 | .Case("CFString", CFStringFormat) |
| 2780 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2781 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2782 | // Otherwise, check for supported formats. |
| 2783 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2784 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2785 | .Case("kprintf", SupportedFormat) // OpenBSD. |
Dimitry Andric | 6b5ed34 | 2015-02-19 22:32:33 +0000 | [diff] [blame] | 2786 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
Fariborz Jahanian | f8dce0f | 2015-02-21 00:45:58 +0000 | [diff] [blame] | 2787 | .Case("os_trace", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2788 | |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2789 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2790 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2791 | } |
| 2792 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2793 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2794 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2795 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2796 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2797 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2798 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2799 | return; |
| 2800 | } |
| 2801 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2802 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2803 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2804 | Attr.setInvalid(); |
| 2805 | return; |
| 2806 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2807 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2808 | if (S.Context.getAsArrayType(T)) |
| 2809 | T = S.Context.getBaseElementType(T); |
| 2810 | if (!T->getAs<RecordType>()) { |
| 2811 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2812 | Attr.setInvalid(); |
| 2813 | return; |
| 2814 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2815 | |
| 2816 | Expr *E = Attr.getArgAsExpr(0); |
| 2817 | uint32_t prioritynum; |
| 2818 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2819 | Attr.setInvalid(); |
| 2820 | return; |
| 2821 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2822 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2823 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2824 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2825 | << E->getSourceRange() << Attr.getName() << 101 << 65535; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2826 | Attr.setInvalid(); |
| 2827 | return; |
| 2828 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2829 | D->addAttr(::new (S.Context) |
| 2830 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2831 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2834 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2835 | IdentifierInfo *Format, int FormatIdx, |
| 2836 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2837 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2838 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2839 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 2840 | if (F->getType() == Format && |
| 2841 | F->getFormatIdx() == FormatIdx && |
| 2842 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2843 | // If we don't have a valid location for this attribute, adopt the |
| 2844 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2845 | if (F->getLocation().isInvalid()) |
| 2846 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2847 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2851 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2852 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2853 | } |
| 2854 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2855 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2856 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2857 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2858 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2859 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2860 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2861 | return; |
| 2862 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2863 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2864 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2865 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2866 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2867 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2868 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2869 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2870 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2871 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 2872 | if (normalizeName(Format)) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2873 | // If we've modified the string name, we need a new identifier for it. |
| 2874 | II = &S.Context.Idents.get(Format); |
| 2875 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2876 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2877 | // Check for supported formats. |
| 2878 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2879 | |
| 2880 | if (Kind == IgnoredFormat) |
| 2881 | return; |
| 2882 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2883 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2884 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 2885 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2886 | return; |
| 2887 | } |
| 2888 | |
| 2889 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2890 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2891 | uint32_t Idx; |
| 2892 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2893 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2894 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2895 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2896 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2897 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2898 | return; |
| 2899 | } |
| 2900 | |
| 2901 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2902 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2903 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2904 | if (HasImplicitThisParam) { |
| 2905 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2906 | S.Diag(Attr.getLoc(), |
| 2907 | diag::err_format_attribute_implicit_this_format_string) |
| 2908 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2909 | return; |
| 2910 | } |
| 2911 | ArgIdx--; |
| 2912 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2913 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2914 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2915 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2916 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2917 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2918 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2919 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2920 | << "a CFString" << IdxExpr->getSourceRange() |
| 2921 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2922 | return; |
| 2923 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2924 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2925 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2926 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2927 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2928 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2929 | << "an NSString" << IdxExpr->getSourceRange() |
| 2930 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2931 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2932 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2933 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2934 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2935 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2936 | << "a string type" << IdxExpr->getSourceRange() |
| 2937 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2938 | return; |
| 2939 | } |
| 2940 | |
| 2941 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2942 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2943 | uint32_t FirstArg; |
| 2944 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2945 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2946 | |
| 2947 | // check if the function is variadic if the 3rd argument non-zero |
| 2948 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2949 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2950 | ++NumArgs; // +1 for ... |
| 2951 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2952 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2953 | return; |
| 2954 | } |
| 2955 | } |
| 2956 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2957 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2958 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2959 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2960 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2961 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2962 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2963 | return; |
| 2964 | } |
| 2965 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2966 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2967 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2968 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2969 | return; |
| 2970 | } |
| 2971 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2972 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2973 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2974 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2975 | if (NewAttr) |
| 2976 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2979 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 2980 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2981 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2982 | RecordDecl *RD = nullptr; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2983 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2984 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 2985 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 2986 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2987 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2988 | |
| 2989 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2990 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2991 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2992 | return; |
| 2993 | } |
| 2994 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 2995 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2996 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 2997 | diag::warn_transparent_union_attribute_not_definition); |
| 2998 | return; |
| 2999 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3000 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3001 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3002 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3003 | if (Field == FieldEnd) { |
| 3004 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3005 | return; |
| 3006 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3007 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3008 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3009 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3010 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3011 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3012 | diag::warn_transparent_union_attribute_floating) |
| 3013 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3014 | return; |
| 3015 | } |
| 3016 | |
| 3017 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3018 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3019 | for (; Field != FieldEnd; ++Field) { |
| 3020 | QualType FieldType = Field->getType(); |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3021 | // FIXME: this isn't fully correct; we also need to test whether the |
| 3022 | // members of the union would all have the same calling convention as the |
| 3023 | // first member of the union. Checking just the size and alignment isn't |
| 3024 | // sufficient (consider structs passed on the stack instead of in registers |
| 3025 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3026 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3027 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3028 | // Warn if we drop the attribute. |
| 3029 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3030 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3031 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3032 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3033 | diag::warn_transparent_union_attribute_field_size_align) |
| 3034 | << isSize << Field->getDeclName() << FieldBits; |
| 3035 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3036 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3037 | diag::note_transparent_union_first_field_size_align) |
| 3038 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3039 | return; |
| 3040 | } |
| 3041 | } |
| 3042 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3043 | RD->addAttr(::new (S.Context) |
| 3044 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3045 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3046 | } |
| 3047 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3048 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3049 | // Make sure that there is a string literal as the annotation's single |
| 3050 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3051 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3052 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3053 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3054 | |
| 3055 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3056 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 3057 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3058 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3059 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3060 | |
| 3061 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3062 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3063 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3064 | } |
| 3065 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3066 | static void handleAlignValueAttr(Sema &S, Decl *D, |
| 3067 | const AttributeList &Attr) { |
| 3068 | S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 3069 | Attr.getAttributeSpellingListIndex()); |
| 3070 | } |
| 3071 | |
| 3072 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3073 | unsigned SpellingListIndex) { |
| 3074 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 3075 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3076 | |
| 3077 | QualType T; |
| 3078 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3079 | T = TD->getUnderlyingType(); |
| 3080 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3081 | T = VD->getType(); |
| 3082 | else |
| 3083 | llvm_unreachable("Unknown decl type for align_value"); |
| 3084 | |
| 3085 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 3086 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 3087 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 3088 | << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange(); |
| 3089 | return; |
| 3090 | } |
| 3091 | |
| 3092 | if (!E->isValueDependent()) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3093 | llvm::APSInt Alignment; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3094 | ExprResult ICE |
| 3095 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3096 | diag::err_align_value_attribute_argument_not_int, |
| 3097 | /*AllowFold*/ false); |
| 3098 | if (ICE.isInvalid()) |
| 3099 | return; |
| 3100 | |
| 3101 | if (!Alignment.isPowerOf2()) { |
| 3102 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3103 | << E->getSourceRange(); |
| 3104 | return; |
| 3105 | } |
| 3106 | |
| 3107 | D->addAttr(::new (Context) |
| 3108 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 3109 | SpellingListIndex)); |
| 3110 | return; |
| 3111 | } |
| 3112 | |
| 3113 | // Save dependent expressions in the AST to be instantiated. |
| 3114 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3117 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3118 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3119 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3120 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3121 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3122 | return; |
| 3123 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3124 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3125 | if (Attr.getNumArgs() == 0) { |
| 3126 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3127 | true, nullptr, Attr.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3128 | return; |
| 3129 | } |
| 3130 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3131 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3132 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3133 | S.Diag(Attr.getEllipsisLoc(), |
| 3134 | diag::err_pack_expansion_without_parameter_packs); |
| 3135 | return; |
| 3136 | } |
| 3137 | |
| 3138 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3139 | return; |
| 3140 | |
David Majnemer | 26a1e0e | 2015-04-07 02:37:09 +0000 | [diff] [blame] | 3141 | if (E->isValueDependent()) { |
| 3142 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 3143 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 3144 | S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name) |
| 3145 | << E->getSourceRange(); |
| 3146 | return; |
| 3147 | } |
| 3148 | } |
| 3149 | } |
| 3150 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3151 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3152 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
| 3155 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3156 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3157 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3158 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3159 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3160 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3161 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3162 | // C++11 [dcl.align]p1: |
| 3163 | // An alignment-specifier may be applied to a variable or to a class |
| 3164 | // data member, but it shall not be applied to a bit-field, a function |
| 3165 | // parameter, the formal parameter of a catch clause, or a variable |
| 3166 | // declared with the register storage class specifier. An |
| 3167 | // alignment-specifier may also be applied to the declaration of a class |
| 3168 | // or enumeration type. |
| 3169 | // C11 6.7.5/2: |
| 3170 | // An alignment attribute shall not be specified in a declaration of |
| 3171 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3172 | // object declared with the register storage-class specifier. |
| 3173 | int DiagKind = -1; |
| 3174 | if (isa<ParmVarDecl>(D)) { |
| 3175 | DiagKind = 0; |
| 3176 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3177 | if (VD->getStorageClass() == SC_Register) |
| 3178 | DiagKind = 1; |
| 3179 | if (VD->isExceptionVariable()) |
| 3180 | DiagKind = 2; |
| 3181 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3182 | if (FD->isBitField()) |
| 3183 | DiagKind = 3; |
| 3184 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3185 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3186 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3187 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3188 | return; |
| 3189 | } |
| 3190 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3191 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3192 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3193 | return; |
| 3194 | } |
| 3195 | } |
| 3196 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3197 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3198 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3199 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3200 | AA->setPackExpansion(IsPackExpansion); |
| 3201 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3202 | return; |
| 3203 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3204 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3205 | // FIXME: Cache the number on the Attr object? |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3206 | llvm::APSInt Alignment; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3207 | ExprResult ICE |
| 3208 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3209 | diag::err_aligned_attribute_argument_not_int, |
| 3210 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3211 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3212 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3213 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3214 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3215 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3216 | // C++11 [dcl.align]p2: |
| 3217 | // -- if the constant expression evaluates to zero, the alignment |
| 3218 | // specifier shall have no effect |
| 3219 | // C11 6.7.5p6: |
| 3220 | // An alignment specification of zero has no effect. |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3221 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3222 | if (!llvm::isPowerOf2_64(AlignVal)) { |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3223 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3224 | << E->getSourceRange(); |
| 3225 | return; |
| 3226 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3227 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3228 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3229 | // Alignment calculations can wrap around if it's greater than 2**28. |
David Majnemer | 29c69db | 2015-07-26 01:48:59 +0000 | [diff] [blame] | 3230 | unsigned MaxValidAlignment = |
| 3231 | Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192 |
| 3232 | : 268435456; |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3233 | if (AlignVal > MaxValidAlignment) { |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3234 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 3235 | << E->getSourceRange(); |
| 3236 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3237 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3238 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3239 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3240 | unsigned MaxTLSAlign = |
| 3241 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3242 | .getQuantity(); |
| 3243 | auto *VD = dyn_cast<VarDecl>(D); |
| 3244 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3245 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3246 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3247 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3248 | return; |
| 3249 | } |
| 3250 | } |
| 3251 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3252 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3253 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3254 | AA->setPackExpansion(IsPackExpansion); |
| 3255 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3258 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3259 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3260 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3261 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3262 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3263 | SpellingListIndex); |
| 3264 | AA->setPackExpansion(IsPackExpansion); |
| 3265 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3266 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3267 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3268 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3269 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3270 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3271 | QualType UnderlyingTy, DiagTy; |
| 3272 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 3273 | UnderlyingTy = DiagTy = VD->getType(); |
| 3274 | } else { |
| 3275 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
| 3276 | if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3277 | UnderlyingTy = ED->getIntegerType(); |
| 3278 | } |
| 3279 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3280 | return; |
| 3281 | |
| 3282 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3283 | // The combined effect of all alignment attributes in a declaration shall |
| 3284 | // not specify an alignment that is less strict than the alignment that |
| 3285 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3286 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3287 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3288 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3289 | if (I->isAlignmentDependent()) |
| 3290 | return; |
| 3291 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3292 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3293 | Align = std::max(Align, I->getAlignment(Context)); |
| 3294 | } |
| 3295 | |
| 3296 | if (AlignasAttr && Align) { |
| 3297 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3298 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3299 | if (NaturalAlign > RequestedAlign) |
| 3300 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3301 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3302 | } |
| 3303 | } |
| 3304 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3305 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3306 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3307 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3308 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3309 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3310 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3311 | // have to wait until the record is defined to catch any mismatches. |
| 3312 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3313 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3314 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3315 | // The unspecified model never matches what a definition could need. |
| 3316 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3317 | return false; |
| 3318 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3319 | if (BestCase) { |
| 3320 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3321 | return false; |
| 3322 | } else { |
| 3323 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3324 | return false; |
| 3325 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3326 | |
| 3327 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3328 | << 0 /*definition*/; |
| 3329 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3330 | << RD->getNameAsString(); |
| 3331 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3334 | /// parseModeAttrArg - Parses attribute mode string and returns parsed type |
| 3335 | /// attribute. |
| 3336 | static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, |
| 3337 | bool &IntegerMode, bool &ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3338 | IntegerMode = true; |
| 3339 | ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3340 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3341 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3342 | switch (Str[0]) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3343 | case 'Q': |
| 3344 | DestWidth = 8; |
| 3345 | break; |
| 3346 | case 'H': |
| 3347 | DestWidth = 16; |
| 3348 | break; |
| 3349 | case 'S': |
| 3350 | DestWidth = 32; |
| 3351 | break; |
| 3352 | case 'D': |
| 3353 | DestWidth = 64; |
| 3354 | break; |
| 3355 | case 'X': |
| 3356 | DestWidth = 96; |
| 3357 | break; |
| 3358 | case 'T': |
| 3359 | DestWidth = 128; |
| 3360 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3361 | } |
| 3362 | if (Str[1] == 'F') { |
| 3363 | IntegerMode = false; |
| 3364 | } else if (Str[1] == 'C') { |
| 3365 | IntegerMode = false; |
| 3366 | ComplexMode = true; |
| 3367 | } else if (Str[1] != 'I') { |
| 3368 | DestWidth = 0; |
| 3369 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3370 | break; |
| 3371 | case 4: |
| 3372 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3373 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3374 | if (Str == "word") |
Reid Kleckner | f27e752 | 2016-02-01 18:58:24 +0000 | [diff] [blame] | 3375 | DestWidth = S.Context.getTargetInfo().getRegisterWidth(); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3376 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3377 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3378 | break; |
| 3379 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3380 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3381 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3382 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3383 | case 11: |
| 3384 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3385 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3386 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3387 | } |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
| 3390 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
| 3391 | /// type. |
| 3392 | /// |
| 3393 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3394 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3395 | /// HImode, not an intermediate pointer. |
| 3396 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3397 | // This attribute isn't documented, but glibc uses it. It changes |
| 3398 | // the width of an int or unsigned int to the specified size. |
| 3399 | if (!Attr.isArgIdent(0)) { |
| 3400 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3401 | << AANT_ArgumentIdentifier; |
| 3402 | return; |
| 3403 | } |
| 3404 | |
| 3405 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3406 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3407 | S.AddModeAttr(Attr.getRange(), D, Name, Attr.getAttributeSpellingListIndex()); |
| 3408 | } |
| 3409 | |
| 3410 | void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name, |
| 3411 | unsigned SpellingListIndex, bool InInstantiation) { |
| 3412 | StringRef Str = Name->getName(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3413 | normalizeName(Str); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3414 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3415 | |
| 3416 | unsigned DestWidth = 0; |
| 3417 | bool IntegerMode = true; |
| 3418 | bool ComplexMode = false; |
| 3419 | llvm::APInt VectorSize(64, 0); |
| 3420 | if (Str.size() >= 4 && Str[0] == 'V') { |
| 3421 | // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2). |
| 3422 | size_t StrSize = Str.size(); |
| 3423 | size_t VectorStringLength = 0; |
| 3424 | while ((VectorStringLength + 1) < StrSize && |
| 3425 | isdigit(Str[VectorStringLength + 1])) |
| 3426 | ++VectorStringLength; |
| 3427 | if (VectorStringLength && |
| 3428 | !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) && |
| 3429 | VectorSize.isPowerOf2()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3430 | parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth, |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3431 | IntegerMode, ComplexMode); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3432 | // Avoid duplicate warning from template instantiation. |
| 3433 | if (!InInstantiation) |
| 3434 | Diag(AttrLoc, diag::warn_vector_mode_deprecated); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3435 | } else { |
| 3436 | VectorSize = 0; |
| 3437 | } |
| 3438 | } |
| 3439 | |
| 3440 | if (!VectorSize) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3441 | parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode); |
| 3442 | |
| 3443 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3444 | // and friends, at least with glibc. |
| 3445 | // FIXME: Make sure floating-point mappings are accurate |
| 3446 | // FIXME: Support XF and TF types |
| 3447 | if (!DestWidth) { |
| 3448 | Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
| 3449 | return; |
| 3450 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3451 | |
| 3452 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3453 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3454 | OldTy = TD->getUnderlyingType(); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3455 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 3456 | // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'. |
| 3457 | // Try to get type from enum declaration, default to int. |
| 3458 | OldTy = ED->getIntegerType(); |
| 3459 | if (OldTy.isNull()) |
| 3460 | OldTy = Context.IntTy; |
| 3461 | } else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3462 | OldTy = cast<ValueDecl>(D)->getType(); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3463 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3464 | if (OldTy->isDependentType()) { |
| 3465 | D->addAttr(::new (Context) |
| 3466 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
| 3467 | return; |
| 3468 | } |
| 3469 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3470 | // Base type can also be a vector type (see PR17453). |
| 3471 | // Distinguish between base type and base element type. |
| 3472 | QualType OldElemTy = OldTy; |
| 3473 | if (const VectorType *VT = OldTy->getAs<VectorType>()) |
| 3474 | OldElemTy = VT->getElementType(); |
| 3475 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3476 | // GCC allows 'mode' attribute on enumeration types (even incomplete), except |
| 3477 | // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete |
| 3478 | // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected. |
| 3479 | if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) && |
| 3480 | VectorSize.getBoolValue()) { |
| 3481 | Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange; |
| 3482 | return; |
| 3483 | } |
| 3484 | bool IntegralOrAnyEnumType = |
| 3485 | OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>(); |
| 3486 | |
| 3487 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() && |
| 3488 | !IntegralOrAnyEnumType) |
| 3489 | Diag(AttrLoc, diag::err_mode_not_primitive); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3490 | else if (IntegerMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3491 | if (!IntegralOrAnyEnumType) |
| 3492 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3493 | } else if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3494 | if (!OldElemTy->isComplexType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3495 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3496 | } else { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3497 | if (!OldElemTy->isFloatingType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3498 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3499 | } |
| 3500 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3501 | QualType NewElemTy; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3502 | |
| 3503 | if (IntegerMode) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3504 | NewElemTy = Context.getIntTypeForBitwidth(DestWidth, |
| 3505 | OldElemTy->isSignedIntegerType()); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3506 | else |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3507 | NewElemTy = Context.getRealTypeForBitwidth(DestWidth); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3508 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3509 | if (NewElemTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3510 | Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3511 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3512 | } |
| 3513 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3514 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3515 | NewElemTy = Context.getComplexType(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3516 | } |
| 3517 | |
| 3518 | QualType NewTy = NewElemTy; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3519 | if (VectorSize.getBoolValue()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3520 | NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(), |
| 3521 | VectorType::GenericVector); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3522 | } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3523 | // Complex machine mode does not support base vector types. |
| 3524 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3525 | Diag(AttrLoc, diag::err_complex_mode_vector_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3526 | return; |
| 3527 | } |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3528 | unsigned NumElements = Context.getTypeSize(OldElemTy) * |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3529 | OldVT->getNumElements() / |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3530 | Context.getTypeSize(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3531 | NewTy = |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3532 | Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | if (NewTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3536 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3537 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3541 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3542 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3543 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3544 | ED->setIntegerType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3545 | else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3546 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3547 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3548 | D->addAttr(::new (Context) |
| 3549 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3550 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3551 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3552 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Nick Lewycky | 0859707 | 2012-07-24 01:40:49 +0000 | [diff] [blame] | 3553 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3554 | if (!VD->hasGlobalStorage()) |
| 3555 | S.Diag(Attr.getLoc(), |
| 3556 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3557 | << Attr.getName(); |
| 3558 | } else if (!isFunctionOrMethod(D)) { |
| 3559 | S.Diag(Attr.getLoc(), |
| 3560 | diag::warn_attribute_requires_functions_or_static_globals) |
| 3561 | << Attr.getName(); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3562 | return; |
| 3563 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3564 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3565 | D->addAttr(::new (S.Context) |
| 3566 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3567 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3570 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3571 | IdentifierInfo *Ident, |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3572 | unsigned AttrSpellingListIndex) { |
| 3573 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3574 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3575 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3576 | return nullptr; |
| 3577 | } |
| 3578 | |
| 3579 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 3580 | return nullptr; |
| 3581 | |
| 3582 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 3583 | AttrSpellingListIndex); |
| 3584 | } |
| 3585 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3586 | CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range, |
| 3587 | IdentifierInfo *Ident, |
| 3588 | unsigned AttrSpellingListIndex) { |
| 3589 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident)) |
| 3590 | return nullptr; |
| 3591 | |
| 3592 | return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex); |
| 3593 | } |
| 3594 | |
| 3595 | InternalLinkageAttr * |
| 3596 | Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range, |
| 3597 | IdentifierInfo *Ident, |
| 3598 | unsigned AttrSpellingListIndex) { |
| 3599 | if (auto VD = dyn_cast<VarDecl>(D)) { |
| 3600 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 3601 | // ImplicitParm or VarTemplateSpecialization). |
| 3602 | if (VD->getKind() != Decl::Var) { |
| 3603 | Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type) |
| 3604 | << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 3605 | : ExpectedVariableOrFunction); |
| 3606 | return nullptr; |
| 3607 | } |
| 3608 | // Attribute does not apply to non-static local variables. |
| 3609 | if (VD->hasLocalStorage()) { |
| 3610 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 3611 | return nullptr; |
| 3612 | } |
| 3613 | } |
| 3614 | |
| 3615 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident)) |
| 3616 | return nullptr; |
| 3617 | |
| 3618 | return ::new (Context) |
| 3619 | InternalLinkageAttr(Range, Context, AttrSpellingListIndex); |
| 3620 | } |
| 3621 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3622 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 3623 | unsigned AttrSpellingListIndex) { |
| 3624 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 3625 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 3626 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3627 | return nullptr; |
| 3628 | } |
| 3629 | |
| 3630 | if (D->hasAttr<MinSizeAttr>()) |
| 3631 | return nullptr; |
| 3632 | |
| 3633 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 3634 | } |
| 3635 | |
| 3636 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 3637 | unsigned AttrSpellingListIndex) { |
| 3638 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 3639 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 3640 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3641 | D->dropAttr<AlwaysInlineAttr>(); |
| 3642 | } |
| 3643 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 3644 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 3645 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3646 | D->dropAttr<MinSizeAttr>(); |
| 3647 | } |
| 3648 | |
| 3649 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 3650 | return nullptr; |
| 3651 | |
| 3652 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 3653 | AttrSpellingListIndex); |
| 3654 | } |
| 3655 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3656 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 3657 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3658 | if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(), |
| 3659 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 3660 | return; |
| 3661 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3662 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
| 3663 | D, Attr.getRange(), Attr.getName(), |
| 3664 | Attr.getAttributeSpellingListIndex())) |
| 3665 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3668 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3669 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
| 3670 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3671 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3674 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, |
| 3675 | const AttributeList &Attr) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3676 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
| 3677 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3678 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3681 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 3682 | if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, Attr.getRange(), |
| 3683 | Attr.getName()) || |
| 3684 | checkAttrMutualExclusion<CUDAHostAttr>(S, D, Attr.getRange(), |
| 3685 | Attr.getName())) { |
| 3686 | return; |
| 3687 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3688 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3689 | if (!FD->getReturnType()->isVoidType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3690 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 3691 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3692 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3693 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 3694 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3695 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3696 | } |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 3697 | if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) { |
| 3698 | if (Method->isInstance()) { |
| 3699 | S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method) |
| 3700 | << Method; |
| 3701 | return; |
| 3702 | } |
| 3703 | S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method; |
| 3704 | } |
| 3705 | // Only warn for "inline" when compiling for host, to cut down on noise. |
| 3706 | if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice) |
| 3707 | S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3708 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3709 | D->addAttr(::new (S.Context) |
| 3710 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Eli Bendersky | 8386004 | 2014-09-02 22:00:06 +0000 | [diff] [blame] | 3711 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3714 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3715 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3716 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3717 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3718 | return; |
| 3719 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3720 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3721 | D->addAttr(::new (S.Context) |
| 3722 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3723 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3726 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3727 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3728 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3729 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3730 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3731 | CallingConv CC; |
Richard Smith | eec7cb1 | 2015-05-28 23:38:53 +0000 | [diff] [blame] | 3732 | if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3733 | return; |
| 3734 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3735 | if (!isa<ObjCMethodDecl>(D)) { |
| 3736 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3737 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3738 | return; |
| 3739 | } |
| 3740 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3741 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3742 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3743 | D->addAttr(::new (S.Context) |
| 3744 | FastCallAttr(Attr.getRange(), S.Context, |
| 3745 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3746 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3747 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3748 | D->addAttr(::new (S.Context) |
| 3749 | StdCallAttr(Attr.getRange(), S.Context, |
| 3750 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3751 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3752 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3753 | D->addAttr(::new (S.Context) |
| 3754 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3755 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3756 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3757 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3758 | D->addAttr(::new (S.Context) |
| 3759 | CDeclAttr(Attr.getRange(), S.Context, |
| 3760 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3761 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3762 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3763 | D->addAttr(::new (S.Context) |
| 3764 | PascalAttr(Attr.getRange(), S.Context, |
| 3765 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3766 | return; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 3767 | case AttributeList::AT_SwiftCall: |
| 3768 | D->addAttr(::new (S.Context) |
| 3769 | SwiftCallAttr(Attr.getRange(), S.Context, |
| 3770 | Attr.getAttributeSpellingListIndex())); |
| 3771 | return; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3772 | case AttributeList::AT_VectorCall: |
| 3773 | D->addAttr(::new (S.Context) |
| 3774 | VectorCallAttr(Attr.getRange(), S.Context, |
| 3775 | Attr.getAttributeSpellingListIndex())); |
| 3776 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3777 | case AttributeList::AT_MSABI: |
| 3778 | D->addAttr(::new (S.Context) |
| 3779 | MSABIAttr(Attr.getRange(), S.Context, |
| 3780 | Attr.getAttributeSpellingListIndex())); |
| 3781 | return; |
| 3782 | case AttributeList::AT_SysVABI: |
| 3783 | D->addAttr(::new (S.Context) |
| 3784 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3785 | Attr.getAttributeSpellingListIndex())); |
| 3786 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3787 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3788 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3789 | switch (CC) { |
| 3790 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3791 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3792 | break; |
| 3793 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3794 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3795 | break; |
| 3796 | default: |
| 3797 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3798 | } |
| 3799 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3800 | D->addAttr(::new (S.Context) |
| 3801 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3802 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3803 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3804 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3805 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3806 | D->addAttr(::new (S.Context) |
| 3807 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3808 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3809 | return; |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 3810 | case AttributeList::AT_PreserveMost: |
| 3811 | D->addAttr(::new (S.Context) PreserveMostAttr( |
| 3812 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 3813 | return; |
| 3814 | case AttributeList::AT_PreserveAll: |
| 3815 | D->addAttr(::new (S.Context) PreserveAllAttr( |
| 3816 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 3817 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3818 | default: |
| 3819 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3820 | } |
| 3821 | } |
| 3822 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3823 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3824 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3825 | if (attr.isInvalid()) |
| 3826 | return true; |
| 3827 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 3828 | if (attr.hasProcessingCache()) { |
| 3829 | CC = (CallingConv) attr.getProcessingCache(); |
| 3830 | return false; |
| 3831 | } |
| 3832 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3833 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3834 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3835 | attr.setInvalid(); |
| 3836 | return true; |
| 3837 | } |
| 3838 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3839 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3840 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3841 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3842 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3843 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3844 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3845 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 3846 | case AttributeList::AT_SwiftCall: CC = CC_Swift; break; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3847 | case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3848 | case AttributeList::AT_MSABI: |
| 3849 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3850 | CC_X86_64Win64; |
| 3851 | break; |
| 3852 | case AttributeList::AT_SysVABI: |
| 3853 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3854 | CC_C; |
| 3855 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3856 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3857 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3858 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3859 | attr.setInvalid(); |
| 3860 | return true; |
| 3861 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3862 | if (StrRef == "aapcs") { |
| 3863 | CC = CC_AAPCS; |
| 3864 | break; |
| 3865 | } else if (StrRef == "aapcs-vfp") { |
| 3866 | CC = CC_AAPCS_VFP; |
| 3867 | break; |
| 3868 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3869 | |
| 3870 | attr.setInvalid(); |
| 3871 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3872 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3873 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3874 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 3875 | case AttributeList::AT_PreserveMost: CC = CC_PreserveMost; break; |
| 3876 | case AttributeList::AT_PreserveAll: CC = CC_PreserveAll; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3877 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3878 | } |
| 3879 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3880 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3881 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 3882 | if (A != TargetInfo::CCCR_OK) { |
| 3883 | if (A == TargetInfo::CCCR_Warning) |
| 3884 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3885 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 3886 | // This convention is not valid for the target. Use the default function or |
| 3887 | // method calling convention. |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3888 | TargetInfo::CallingConvMethodType MT = TargetInfo::CCMT_Unknown; |
| 3889 | if (FD) |
| 3890 | MT = FD->isCXXInstanceMember() ? TargetInfo::CCMT_Member : |
| 3891 | TargetInfo::CCMT_NonMember; |
| 3892 | CC = TI.getDefaultCallingConv(MT); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3893 | } |
| 3894 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 3895 | attr.setProcessingCache((unsigned) CC); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3896 | return false; |
| 3897 | } |
| 3898 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 3899 | /// Pointer-like types in the default address space. |
| 3900 | static bool isValidSwiftContextType(QualType type) { |
| 3901 | if (!type->hasPointerRepresentation()) |
| 3902 | return type->isDependentType(); |
| 3903 | return type->getPointeeType().getAddressSpace() == 0; |
| 3904 | } |
| 3905 | |
| 3906 | /// Pointers and references in the default address space. |
| 3907 | static bool isValidSwiftIndirectResultType(QualType type) { |
| 3908 | if (auto ptrType = type->getAs<PointerType>()) { |
| 3909 | type = ptrType->getPointeeType(); |
| 3910 | } else if (auto refType = type->getAs<ReferenceType>()) { |
| 3911 | type = refType->getPointeeType(); |
| 3912 | } else { |
| 3913 | return type->isDependentType(); |
| 3914 | } |
| 3915 | return type.getAddressSpace() == 0; |
| 3916 | } |
| 3917 | |
| 3918 | /// Pointers and references to pointers in the default address space. |
| 3919 | static bool isValidSwiftErrorResultType(QualType type) { |
| 3920 | if (auto ptrType = type->getAs<PointerType>()) { |
| 3921 | type = ptrType->getPointeeType(); |
| 3922 | } else if (auto refType = type->getAs<ReferenceType>()) { |
| 3923 | type = refType->getPointeeType(); |
| 3924 | } else { |
| 3925 | return type->isDependentType(); |
| 3926 | } |
| 3927 | if (!type.getQualifiers().empty()) |
| 3928 | return false; |
| 3929 | return isValidSwiftContextType(type); |
| 3930 | } |
| 3931 | |
| 3932 | static void handleParameterABIAttr(Sema &S, Decl *D, const AttributeList &attr, |
| 3933 | ParameterABI abi) { |
| 3934 | S.AddParameterABIAttr(attr.getRange(), D, abi, |
| 3935 | attr.getAttributeSpellingListIndex()); |
| 3936 | } |
| 3937 | |
| 3938 | void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi, |
| 3939 | unsigned spellingIndex) { |
| 3940 | |
| 3941 | QualType type = cast<ParmVarDecl>(D)->getType(); |
| 3942 | |
| 3943 | if (auto existingAttr = D->getAttr<ParameterABIAttr>()) { |
| 3944 | if (existingAttr->getABI() != abi) { |
| 3945 | Diag(range.getBegin(), diag::err_attributes_are_not_compatible) |
| 3946 | << getParameterABISpelling(abi) << existingAttr; |
| 3947 | Diag(existingAttr->getLocation(), diag::note_conflicting_attribute); |
| 3948 | return; |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | switch (abi) { |
| 3953 | case ParameterABI::Ordinary: |
| 3954 | llvm_unreachable("explicit attribute for ordinary parameter ABI?"); |
| 3955 | |
| 3956 | case ParameterABI::SwiftContext: |
| 3957 | if (!isValidSwiftContextType(type)) { |
| 3958 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 3959 | << getParameterABISpelling(abi) |
| 3960 | << /*pointer to pointer */ 0 << type; |
| 3961 | } |
| 3962 | D->addAttr(::new (Context) |
| 3963 | SwiftContextAttr(range, Context, spellingIndex)); |
| 3964 | return; |
| 3965 | |
| 3966 | case ParameterABI::SwiftErrorResult: |
| 3967 | if (!isValidSwiftErrorResultType(type)) { |
| 3968 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 3969 | << getParameterABISpelling(abi) |
| 3970 | << /*pointer to pointer */ 1 << type; |
| 3971 | } |
| 3972 | D->addAttr(::new (Context) |
| 3973 | SwiftErrorResultAttr(range, Context, spellingIndex)); |
| 3974 | return; |
| 3975 | |
| 3976 | case ParameterABI::SwiftIndirectResult: |
| 3977 | if (!isValidSwiftIndirectResultType(type)) { |
| 3978 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 3979 | << getParameterABISpelling(abi) |
| 3980 | << /*pointer*/ 0 << type; |
| 3981 | } |
| 3982 | D->addAttr(::new (Context) |
| 3983 | SwiftIndirectResultAttr(range, Context, spellingIndex)); |
| 3984 | return; |
| 3985 | } |
| 3986 | llvm_unreachable("bad parameter ABI attribute"); |
| 3987 | } |
| 3988 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3989 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 3990 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3991 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 3992 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3993 | return true; |
| 3994 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 3995 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3996 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3997 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 3998 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 3999 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4000 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4001 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4002 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4003 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4004 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4005 | } |
| 4006 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4007 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4008 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4009 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4010 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4011 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4012 | } |
| 4013 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4014 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4015 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4016 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4017 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4018 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4019 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4020 | } |
| 4021 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4022 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4025 | // Checks whether an argument of launch_bounds attribute is acceptable |
| 4026 | // May output an error. |
| 4027 | static bool checkLaunchBoundsArgument(Sema &S, Expr *E, |
| 4028 | const CUDALaunchBoundsAttr &Attr, |
| 4029 | const unsigned Idx) { |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4030 | if (S.DiagnoseUnexpandedParameterPack(E)) |
| 4031 | return false; |
| 4032 | |
| 4033 | // Accept template arguments for now as they depend on something else. |
| 4034 | // We'll get to check them when they eventually get instantiated. |
| 4035 | if (E->isValueDependent()) |
| 4036 | return true; |
| 4037 | |
| 4038 | llvm::APSInt I(64); |
| 4039 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 4040 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
| 4041 | << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
| 4042 | return false; |
| 4043 | } |
| 4044 | // Make sure we can fit it in 32 bits. |
| 4045 | if (!I.isIntN(32)) { |
| 4046 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 4047 | << 32 << /* Unsigned */ 1; |
| 4048 | return false; |
| 4049 | } |
| 4050 | if (I < 0) |
| 4051 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
| 4052 | << &Attr << Idx << E->getSourceRange(); |
| 4053 | |
| 4054 | return true; |
| 4055 | } |
| 4056 | |
| 4057 | void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads, |
| 4058 | Expr *MinBlocks, unsigned SpellingListIndex) { |
| 4059 | CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks, |
| 4060 | SpellingListIndex); |
| 4061 | |
| 4062 | if (!checkLaunchBoundsArgument(*this, MaxThreads, TmpAttr, 0)) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4063 | return; |
| 4064 | |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4065 | if (MinBlocks && !checkLaunchBoundsArgument(*this, MinBlocks, TmpAttr, 1)) |
| 4066 | return; |
| 4067 | |
| 4068 | D->addAttr(::new (Context) CUDALaunchBoundsAttr( |
| 4069 | AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex)); |
| 4070 | } |
| 4071 | |
| 4072 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 4073 | const AttributeList &Attr) { |
| 4074 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1) || |
| 4075 | !checkAttributeAtMostNumArgs(S, Attr, 2)) |
| 4076 | return; |
| 4077 | |
| 4078 | S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 4079 | Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr, |
| 4080 | Attr.getAttributeSpellingListIndex()); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4081 | } |
| 4082 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4083 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4084 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4085 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4086 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4087 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4088 | return; |
| 4089 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4090 | |
| 4091 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4092 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4093 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4094 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4095 | |
| 4096 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4097 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4098 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4099 | return; |
| 4100 | } |
| 4101 | |
| 4102 | uint64_t ArgumentIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4103 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 4104 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4105 | return; |
| 4106 | |
| 4107 | uint64_t TypeTagIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4108 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 4109 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4110 | return; |
| 4111 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 4112 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4113 | if (IsPointer) { |
| 4114 | // Ensure that buffer has a pointer type. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4115 | QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4116 | if (!BufferTy->isPointerType()) { |
| 4117 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 4118 | << Attr.getName() << 0; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4119 | } |
| 4120 | } |
| 4121 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4122 | D->addAttr(::new (S.Context) |
| 4123 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4124 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4125 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4126 | } |
| 4127 | |
| 4128 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4129 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4130 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4131 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4132 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4133 | return; |
| 4134 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4135 | |
| 4136 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4137 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4138 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4139 | if (!isa<VarDecl>(D)) { |
| 4140 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4141 | << Attr.getName() << ExpectedVariable; |
| 4142 | return; |
| 4143 | } |
| 4144 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4145 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4146 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4147 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 4148 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4149 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4150 | D->addAttr(::new (S.Context) |
| 4151 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4152 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4153 | Attr.getLayoutCompatible(), |
| 4154 | Attr.getMustBeNull(), |
| 4155 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4156 | } |
| 4157 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4158 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4159 | // Checker-specific attribute handlers. |
| 4160 | //===----------------------------------------------------------------------===// |
| 4161 | |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4162 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) { |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4163 | return type->isDependentType() || |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4164 | type->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4165 | } |
| 4166 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4167 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4168 | return type->isDependentType() || |
| 4169 | type->isObjCObjectPointerType() || |
| 4170 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4171 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4172 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4173 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4174 | return type->isDependentType() || |
| 4175 | type->isPointerType() || |
| 4176 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4179 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4180 | S.AddNSConsumedAttr(Attr.getRange(), D, Attr.getAttributeSpellingListIndex(), |
| 4181 | Attr.getKind() == AttributeList::AT_NSConsumed, |
| 4182 | /*template instantiation*/ false); |
| 4183 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4184 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4185 | void Sema::AddNSConsumedAttr(SourceRange attrRange, Decl *D, |
| 4186 | unsigned spellingIndex, bool isNSConsumed, |
| 4187 | bool isTemplateInstantiation) { |
| 4188 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
| 4189 | bool typeOK; |
| 4190 | |
| 4191 | if (isNSConsumed) { |
| 4192 | typeOK = isValidSubjectOfNSAttribute(*this, param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4193 | } else { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4194 | typeOK = isValidSubjectOfCFAttribute(*this, param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | if (!typeOK) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4198 | // These attributes are normally just advisory, but in ARC, ns_consumed |
| 4199 | // is significant. Allow non-dependent code to contain inappropriate |
| 4200 | // attributes even in ARC, but require template instantiations to be |
| 4201 | // set up correctly. |
| 4202 | Diag(D->getLocStart(), |
| 4203 | (isTemplateInstantiation && isNSConsumed && |
| 4204 | getLangOpts().ObjCAutoRefCount |
| 4205 | ? diag::err_ns_attribute_wrong_parameter_type |
| 4206 | : diag::warn_ns_attribute_wrong_parameter_type)) |
| 4207 | << attrRange |
| 4208 | << (isNSConsumed ? "ns_consumed" : "cf_consumed") |
| 4209 | << (isNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4210 | return; |
| 4211 | } |
| 4212 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4213 | if (isNSConsumed) |
| 4214 | param->addAttr(::new (Context) |
| 4215 | NSConsumedAttr(attrRange, Context, spellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4216 | else |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4217 | param->addAttr(::new (Context) |
| 4218 | CFConsumedAttr(attrRange, Context, spellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4221 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4222 | const AttributeList &Attr) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4223 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4224 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4225 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4226 | returnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4227 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4228 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4229 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4230 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4231 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4232 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4233 | returnType = FD->getReturnType(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4234 | else if (auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 4235 | returnType = Param->getType()->getPointeeType(); |
| 4236 | if (returnType.isNull()) { |
| 4237 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4238 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4239 | << Attr.getRange(); |
| 4240 | return; |
| 4241 | } |
| 4242 | } else { |
| 4243 | AttributeDeclKind ExpectedDeclKind; |
| 4244 | switch (Attr.getKind()) { |
| 4245 | default: llvm_unreachable("invalid ownership attribute"); |
| 4246 | case AttributeList::AT_NSReturnsRetained: |
| 4247 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4248 | case AttributeList::AT_NSReturnsNotRetained: |
| 4249 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 4250 | break; |
| 4251 | |
| 4252 | case AttributeList::AT_CFReturnsRetained: |
| 4253 | case AttributeList::AT_CFReturnsNotRetained: |
| 4254 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 4255 | break; |
| 4256 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4257 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4258 | << Attr.getRange() << Attr.getName() << ExpectedDeclKind; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4259 | return; |
| 4260 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4261 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4262 | bool typeOK; |
| 4263 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4264 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4265 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4266 | case AttributeList::AT_NSReturnsRetained: |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4267 | typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4268 | cf = false; |
| 4269 | break; |
| 4270 | |
| 4271 | case AttributeList::AT_NSReturnsAutoreleased: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4272 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4273 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4274 | cf = false; |
| 4275 | break; |
| 4276 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4277 | case AttributeList::AT_CFReturnsRetained: |
| 4278 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4279 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4280 | cf = true; |
| 4281 | break; |
| 4282 | } |
| 4283 | |
| 4284 | if (!typeOK) { |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4285 | if (isa<ParmVarDecl>(D)) { |
| 4286 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4287 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4288 | << Attr.getRange(); |
| 4289 | } else { |
| 4290 | // Needs to be kept in sync with warn_ns_attribute_wrong_return_type. |
| 4291 | enum : unsigned { |
| 4292 | Function, |
| 4293 | Method, |
| 4294 | Property |
| 4295 | } SubjectKind = Function; |
| 4296 | if (isa<ObjCMethodDecl>(D)) |
| 4297 | SubjectKind = Method; |
| 4298 | else if (isa<ObjCPropertyDecl>(D)) |
| 4299 | SubjectKind = Property; |
| 4300 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4301 | << Attr.getName() << SubjectKind << cf |
| 4302 | << Attr.getRange(); |
| 4303 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4304 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4305 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4306 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4307 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4308 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4309 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4310 | case AttributeList::AT_NSReturnsAutoreleased: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4311 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr( |
| 4312 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4313 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4314 | case AttributeList::AT_CFReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4315 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr( |
| 4316 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4317 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4318 | case AttributeList::AT_NSReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4319 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr( |
| 4320 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4321 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4322 | case AttributeList::AT_CFReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4323 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr( |
| 4324 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4325 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4326 | case AttributeList::AT_NSReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4327 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr( |
| 4328 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4329 | return; |
| 4330 | }; |
| 4331 | } |
| 4332 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4333 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4334 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4335 | const int EP_ObjCMethod = 1; |
| 4336 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4337 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4338 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4339 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4340 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4341 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4342 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4343 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4344 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4345 | if (!resultType->isReferenceType() && |
| 4346 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4347 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4348 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4349 | << attr.getName() |
| 4350 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4351 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4352 | |
| 4353 | // Drop the attribute. |
| 4354 | return; |
| 4355 | } |
| 4356 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4357 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
| 4358 | attr.getRange(), S.Context, attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4359 | } |
| 4360 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4361 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4362 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4363 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4364 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4365 | DeclContext *DC = method->getDeclContext(); |
| 4366 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4367 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4368 | << attr.getName() << 0; |
| 4369 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4370 | return; |
| 4371 | } |
| 4372 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4373 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4374 | << attr.getName() << 1; |
| 4375 | return; |
| 4376 | } |
| 4377 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4378 | method->addAttr(::new (S.Context) |
| 4379 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4380 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4381 | } |
| 4382 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4383 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 4384 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4385 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(), |
| 4386 | Attr.getName())) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4387 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4388 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4389 | D->addAttr(::new (S.Context) |
| 4390 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 4391 | Attr.getAttributeSpellingListIndex())); |
| 4392 | } |
| 4393 | |
| 4394 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 4395 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4396 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(), |
| 4397 | Attr.getName())) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4398 | return; |
| 4399 | |
| 4400 | D->addAttr(::new (S.Context) |
| 4401 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 4402 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4403 | } |
| 4404 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4405 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 4406 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4407 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4408 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4409 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4410 | 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] | 4411 | return; |
| 4412 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4413 | |
| 4414 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
| 4415 | if (auto TD = dyn_cast<TypedefNameDecl>(D)) { |
| 4416 | if (!Parm->Ident->isStr("id")) { |
| 4417 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id) |
| 4418 | << Attr.getName(); |
| 4419 | return; |
| 4420 | } |
| 4421 | |
| 4422 | // Only allow 'cv void *'. |
| 4423 | QualType T = TD->getUnderlyingType(); |
| 4424 | if (!T->isVoidPointerType()) { |
| 4425 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
| 4426 | return; |
| 4427 | } |
| 4428 | } |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4429 | |
| 4430 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4431 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4432 | Attr.getAttributeSpellingListIndex())); |
| 4433 | } |
| 4434 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4435 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 4436 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4437 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
| 4438 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4439 | if (!Parm) { |
| 4440 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4441 | return; |
| 4442 | } |
| 4443 | |
| 4444 | D->addAttr(::new (S.Context) |
| 4445 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 4446 | Attr.getAttributeSpellingListIndex())); |
| 4447 | } |
| 4448 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4449 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4450 | const AttributeList &Attr) { |
| 4451 | IdentifierInfo *RelatedClass = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4452 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4453 | if (!RelatedClass) { |
| 4454 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4455 | return; |
| 4456 | } |
| 4457 | IdentifierInfo *ClassMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4458 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4459 | IdentifierInfo *InstanceMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4460 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4461 | D->addAttr(::new (S.Context) |
| 4462 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 4463 | ClassMethod, InstanceMethod, |
| 4464 | Attr.getAttributeSpellingListIndex())); |
| 4465 | } |
| 4466 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4467 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 4468 | const AttributeList &Attr) { |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4469 | ObjCInterfaceDecl *IFace; |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4470 | if (ObjCCategoryDecl *CatDecl = |
| 4471 | dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4472 | IFace = CatDecl->getClassInterface(); |
| 4473 | else |
| 4474 | IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 4475 | |
| 4476 | if (!IFace) |
| 4477 | return; |
| 4478 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 4479 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 4480 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4481 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 4482 | Attr.getAttributeSpellingListIndex())); |
| 4483 | } |
| 4484 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4485 | static void handleObjCRuntimeName(Sema &S, Decl *D, |
| 4486 | const AttributeList &Attr) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 4487 | StringRef MetaDataName; |
| 4488 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName)) |
| 4489 | return; |
| 4490 | D->addAttr(::new (S.Context) |
| 4491 | ObjCRuntimeNameAttr(Attr.getRange(), S.Context, |
| 4492 | MetaDataName, |
| 4493 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4494 | } |
| 4495 | |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4496 | // when a user wants to use objc_boxable with a union or struct |
| 4497 | // but she doesn't have access to the declaration (legacy/third-party code) |
| 4498 | // then she can 'enable' this feature via trick with a typedef |
| 4499 | // e.g.: |
| 4500 | // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct; |
| 4501 | static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4502 | bool notify = false; |
| 4503 | |
| 4504 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4505 | if (RD && RD->getDefinition()) { |
| 4506 | RD = RD->getDefinition(); |
| 4507 | notify = true; |
| 4508 | } |
| 4509 | |
| 4510 | if (RD) { |
| 4511 | ObjCBoxableAttr *BoxableAttr = ::new (S.Context) |
| 4512 | ObjCBoxableAttr(Attr.getRange(), S.Context, |
| 4513 | Attr.getAttributeSpellingListIndex()); |
| 4514 | RD->addAttr(BoxableAttr); |
| 4515 | if (notify) { |
| 4516 | // we need to notify ASTReader/ASTWriter about |
| 4517 | // modification of existing declaration |
| 4518 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 4519 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 4520 | } |
| 4521 | } |
| 4522 | } |
| 4523 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4524 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4525 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4526 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4527 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4528 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4529 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4532 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4533 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4534 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4535 | QualType type = vd->getType(); |
| 4536 | |
| 4537 | if (!type->isDependentType() && |
| 4538 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4539 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4540 | << type; |
| 4541 | return; |
| 4542 | } |
| 4543 | |
| 4544 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4545 | |
| 4546 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4547 | // going to infer. |
| 4548 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4549 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4550 | |
| 4551 | switch (lifetime) { |
| 4552 | case Qualifiers::OCL_None: |
| 4553 | assert(type->isDependentType() && |
| 4554 | "didn't infer lifetime for non-dependent type?"); |
| 4555 | break; |
| 4556 | |
| 4557 | case Qualifiers::OCL_Weak: // meaningful |
| 4558 | case Qualifiers::OCL_Strong: // meaningful |
| 4559 | break; |
| 4560 | |
| 4561 | case Qualifiers::OCL_ExplicitNone: |
| 4562 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4563 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4564 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4565 | break; |
| 4566 | } |
| 4567 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4568 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4569 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4570 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4573 | //===----------------------------------------------------------------------===// |
| 4574 | // Microsoft specific attribute handlers. |
| 4575 | //===----------------------------------------------------------------------===// |
| 4576 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4577 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 4578 | if (!S.LangOpts.CPlusPlus) { |
| 4579 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4580 | << Attr.getName() << AttributeLangSupport::C; |
| 4581 | return; |
| 4582 | } |
| 4583 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 4584 | if (!isa<CXXRecordDecl>(D)) { |
| 4585 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4586 | << Attr.getName() << ExpectedClass; |
| 4587 | return; |
| 4588 | } |
| 4589 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4590 | StringRef StrRef; |
| 4591 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4592 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4593 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4594 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4595 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4596 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4597 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4598 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4599 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4600 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4601 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4602 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4603 | return; |
| 4604 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4605 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4606 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4607 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4608 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4609 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4610 | return; |
| 4611 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4612 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4613 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4614 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4615 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4616 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4617 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4618 | D->addAttr(::new (S.Context) UuidAttr(Attr.getRange(), S.Context, StrRef, |
| 4619 | Attr.getAttributeSpellingListIndex())); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4622 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4623 | if (!S.LangOpts.CPlusPlus) { |
| 4624 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4625 | << Attr.getName() << AttributeLangSupport::C; |
| 4626 | return; |
| 4627 | } |
| 4628 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4629 | D, Attr.getRange(), /*BestCase=*/true, |
| 4630 | Attr.getAttributeSpellingListIndex(), |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4631 | (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling()); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 4632 | if (IA) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4633 | D->addAttr(IA); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 4634 | S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D)); |
| 4635 | } |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4636 | } |
| 4637 | |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 4638 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, |
| 4639 | const AttributeList &Attr) { |
| 4640 | VarDecl *VD = cast<VarDecl>(D); |
| 4641 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
| 4642 | S.Diag(Attr.getLoc(), diag::err_thread_unsupported); |
| 4643 | return; |
| 4644 | } |
| 4645 | if (VD->getTSCSpec() != TSCS_unspecified) { |
| 4646 | S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable); |
| 4647 | return; |
| 4648 | } |
| 4649 | if (VD->hasLocalStorage()) { |
| 4650 | S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
| 4651 | return; |
| 4652 | } |
| 4653 | VD->addAttr(::new (S.Context) ThreadAttr( |
| 4654 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4655 | } |
| 4656 | |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 4657 | static void handleAbiTagAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4658 | SmallVector<StringRef, 4> Tags; |
| 4659 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 4660 | StringRef Tag; |
| 4661 | if (!S.checkStringLiteralArgumentAttr(Attr, I, Tag)) |
| 4662 | return; |
| 4663 | Tags.push_back(Tag); |
| 4664 | } |
| 4665 | |
| 4666 | if (const auto *NS = dyn_cast<NamespaceDecl>(D)) { |
| 4667 | if (!NS->isInline()) { |
| 4668 | S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 0; |
| 4669 | return; |
| 4670 | } |
| 4671 | if (NS->isAnonymousNamespace()) { |
| 4672 | S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 1; |
| 4673 | return; |
| 4674 | } |
| 4675 | if (Attr.getNumArgs() == 0) |
| 4676 | Tags.push_back(NS->getName()); |
| 4677 | } else if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 4678 | return; |
| 4679 | |
| 4680 | // Store tags sorted and without duplicates. |
| 4681 | std::sort(Tags.begin(), Tags.end()); |
| 4682 | Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end()); |
| 4683 | |
| 4684 | D->addAttr(::new (S.Context) |
| 4685 | AbiTagAttr(Attr.getRange(), S.Context, Tags.data(), Tags.size(), |
| 4686 | Attr.getAttributeSpellingListIndex())); |
| 4687 | |
| 4688 | // FIXME: remove this warning as soon as mangled part is ready. |
| 4689 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 4690 | << Attr.getName(); |
| 4691 | } |
| 4692 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4693 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 4694 | const AttributeList &Attr) { |
| 4695 | // Check the attribute arguments. |
| 4696 | if (Attr.getNumArgs() > 1) { |
| 4697 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 4698 | << Attr.getName() << 1; |
| 4699 | return; |
| 4700 | } |
| 4701 | |
| 4702 | StringRef Str; |
| 4703 | SourceLocation ArgLoc; |
| 4704 | |
| 4705 | if (Attr.getNumArgs() == 0) |
| 4706 | Str = ""; |
| 4707 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 4708 | return; |
| 4709 | |
| 4710 | ARMInterruptAttr::InterruptType Kind; |
| 4711 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 4712 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 4713 | << Attr.getName() << Str << ArgLoc; |
| 4714 | return; |
| 4715 | } |
| 4716 | |
| 4717 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 4718 | D->addAttr(::new (S.Context) |
| 4719 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 4720 | } |
| 4721 | |
| 4722 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 4723 | const AttributeList &Attr) { |
| 4724 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4725 | return; |
| 4726 | |
| 4727 | if (!Attr.isArgExpr(0)) { |
| 4728 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 4729 | << AANT_ArgumentIntegerConstant; |
| 4730 | return; |
| 4731 | } |
| 4732 | |
| 4733 | // FIXME: Check for decl - it should be void ()(void). |
| 4734 | |
| 4735 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4736 | llvm::APSInt NumParams(32); |
| 4737 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 4738 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4739 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 4740 | << NumParamsExpr->getSourceRange(); |
| 4741 | return; |
| 4742 | } |
| 4743 | |
| 4744 | unsigned Num = NumParams.getLimitedValue(255); |
| 4745 | if ((Num & 1) || Num > 30) { |
| 4746 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 4747 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 4748 | << NumParamsExpr->getSourceRange(); |
| 4749 | return; |
| 4750 | } |
| 4751 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4752 | D->addAttr(::new (S.Context) |
| 4753 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 4754 | Attr.getAttributeSpellingListIndex())); |
| 4755 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 4758 | static void handleMipsInterruptAttr(Sema &S, Decl *D, |
| 4759 | const AttributeList &Attr) { |
| 4760 | // Only one optional argument permitted. |
| 4761 | if (Attr.getNumArgs() > 1) { |
| 4762 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 4763 | << Attr.getName() << 1; |
| 4764 | return; |
| 4765 | } |
| 4766 | |
| 4767 | StringRef Str; |
| 4768 | SourceLocation ArgLoc; |
| 4769 | |
| 4770 | if (Attr.getNumArgs() == 0) |
| 4771 | Str = ""; |
| 4772 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 4773 | return; |
| 4774 | |
| 4775 | // Semantic checks for a function with the 'interrupt' attribute for MIPS: |
| 4776 | // a) Must be a function. |
| 4777 | // b) Must have no parameters. |
| 4778 | // c) Must have the 'void' return type. |
| 4779 | // d) Cannot have the 'mips16' attribute, as that instruction set |
| 4780 | // lacks the 'eret' instruction. |
| 4781 | // e) The attribute itself must either have no argument or one of the |
| 4782 | // valid interrupt types, see [MipsInterruptDocs]. |
| 4783 | |
| 4784 | if (!isFunctionOrMethod(D)) { |
| 4785 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 4786 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 4787 | return; |
| 4788 | } |
| 4789 | |
| 4790 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 4791 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 4792 | << 0; |
| 4793 | return; |
| 4794 | } |
| 4795 | |
| 4796 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 4797 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 4798 | << 1; |
| 4799 | return; |
| 4800 | } |
| 4801 | |
| 4802 | if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(), |
| 4803 | Attr.getName())) |
| 4804 | return; |
| 4805 | |
| 4806 | MipsInterruptAttr::InterruptType Kind; |
| 4807 | if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 4808 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 4809 | << Attr.getName() << "'" + std::string(Str) + "'"; |
| 4810 | return; |
| 4811 | } |
| 4812 | |
| 4813 | D->addAttr(::new (S.Context) MipsInterruptAttr( |
| 4814 | Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex())); |
| 4815 | } |
| 4816 | |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 4817 | static void handleAnyX86InterruptAttr(Sema &S, Decl *D, |
| 4818 | const AttributeList &Attr) { |
| 4819 | // Semantic checks for a function with the 'interrupt' attribute. |
| 4820 | // a) Must be a function. |
| 4821 | // b) Must have the 'void' return type. |
| 4822 | // c) Must take 1 or 2 arguments. |
| 4823 | // d) The 1st argument must be a pointer. |
| 4824 | // e) The 2nd argument (if any) must be an unsigned integer. |
| 4825 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) || |
| 4826 | CXXMethodDecl::isStaticOverloadedOperator( |
| 4827 | cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) { |
| 4828 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4829 | << Attr.getName() << ExpectedFunctionWithProtoType; |
| 4830 | return; |
| 4831 | } |
| 4832 | // Interrupt handler must have void return type. |
| 4833 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 4834 | S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(), |
| 4835 | diag::err_anyx86_interrupt_attribute) |
| 4836 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 4837 | ? 0 |
| 4838 | : 1) |
| 4839 | << 0; |
| 4840 | return; |
| 4841 | } |
| 4842 | // Interrupt handler must have 1 or 2 parameters. |
| 4843 | unsigned NumParams = getFunctionOrMethodNumParams(D); |
| 4844 | if (NumParams < 1 || NumParams > 2) { |
| 4845 | S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute) |
| 4846 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 4847 | ? 0 |
| 4848 | : 1) |
| 4849 | << 1; |
| 4850 | return; |
| 4851 | } |
| 4852 | // The first argument must be a pointer. |
| 4853 | if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) { |
| 4854 | S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(), |
| 4855 | diag::err_anyx86_interrupt_attribute) |
| 4856 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 4857 | ? 0 |
| 4858 | : 1) |
| 4859 | << 2; |
| 4860 | return; |
| 4861 | } |
| 4862 | // The second argument, if present, must be an unsigned integer. |
| 4863 | unsigned TypeSize = |
| 4864 | S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64 |
| 4865 | ? 64 |
| 4866 | : 32; |
| 4867 | if (NumParams == 2 && |
| 4868 | (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() || |
| 4869 | S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) { |
| 4870 | S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(), |
| 4871 | diag::err_anyx86_interrupt_attribute) |
| 4872 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 4873 | ? 0 |
| 4874 | : 1) |
| 4875 | << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false); |
| 4876 | return; |
| 4877 | } |
| 4878 | D->addAttr(::new (S.Context) AnyX86InterruptAttr( |
| 4879 | Attr.getLoc(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4880 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
| 4881 | } |
| 4882 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4883 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4884 | // Dispatch the interrupt attribute based on the current target. |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 4885 | switch (S.Context.getTargetInfo().getTriple().getArch()) { |
| 4886 | case llvm::Triple::msp430: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4887 | handleMSP430InterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 4888 | break; |
| 4889 | case llvm::Triple::mipsel: |
| 4890 | case llvm::Triple::mips: |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 4891 | handleMipsInterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 4892 | break; |
| 4893 | case llvm::Triple::x86: |
| 4894 | case llvm::Triple::x86_64: |
| 4895 | handleAnyX86InterruptAttr(S, D, Attr); |
| 4896 | break; |
| 4897 | default: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4898 | handleARMInterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 4899 | break; |
| 4900 | } |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4901 | } |
| 4902 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 4903 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, |
| 4904 | const AttributeList &Attr) { |
| 4905 | uint32_t NumRegs; |
| 4906 | Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4907 | if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs)) |
| 4908 | return; |
| 4909 | |
| 4910 | D->addAttr(::new (S.Context) |
| 4911 | AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context, |
| 4912 | NumRegs, |
| 4913 | Attr.getAttributeSpellingListIndex())); |
| 4914 | } |
| 4915 | |
| 4916 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, |
| 4917 | const AttributeList &Attr) { |
| 4918 | uint32_t NumRegs; |
| 4919 | Expr *NumRegsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4920 | if (!checkUInt32Argument(S, Attr, NumRegsExpr, NumRegs)) |
| 4921 | return; |
| 4922 | |
| 4923 | D->addAttr(::new (S.Context) |
| 4924 | AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context, |
| 4925 | NumRegs, |
| 4926 | Attr.getAttributeSpellingListIndex())); |
| 4927 | } |
| 4928 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4929 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 4930 | const AttributeList& Attr) { |
| 4931 | // If we try to apply it to a function pointer, don't warn, but don't |
| 4932 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 4933 | // special about calling a force_align_arg_pointer function. |
| 4934 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 4935 | if (VD && VD->getType()->isFunctionPointerType()) |
| 4936 | return; |
| 4937 | // Also don't warn on function pointer typedefs. |
| 4938 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 4939 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 4940 | TD->getUnderlyingType()->isFunctionType())) |
| 4941 | return; |
| 4942 | // Attribute can only be applied to function types. |
| 4943 | if (!isa<FunctionDecl>(D)) { |
| 4944 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4945 | << Attr.getName() << /* function */0; |
| 4946 | return; |
| 4947 | } |
| 4948 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4949 | D->addAttr(::new (S.Context) |
| 4950 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 4951 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 4955 | unsigned AttrSpellingListIndex) { |
| 4956 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 4957 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4958 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4962 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4963 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 4964 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4965 | } |
| 4966 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4967 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 4968 | unsigned AttrSpellingListIndex) { |
| 4969 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 4970 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4971 | D->dropAttr<DLLImportAttr>(); |
| 4972 | } |
| 4973 | |
| 4974 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4975 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4976 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 4977 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4978 | } |
| 4979 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 4980 | static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 4981 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 4982 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 4983 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) |
| 4984 | << A.getName(); |
| 4985 | return; |
| 4986 | } |
| 4987 | |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 4988 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 4989 | if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport && |
| 4990 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 4991 | // MinGW doesn't allow dllimport on inline functions. |
| 4992 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
| 4993 | << A.getName(); |
| 4994 | return; |
| 4995 | } |
| 4996 | } |
| 4997 | |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 4998 | if (auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 4999 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 5000 | MD->getParent()->isLambda()) { |
| 5001 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName(); |
| 5002 | return; |
| 5003 | } |
| 5004 | } |
| 5005 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5006 | unsigned Index = A.getAttributeSpellingListIndex(); |
| 5007 | Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport |
| 5008 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 5009 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5010 | if (NewAttr) |
| 5011 | D->addAttr(NewAttr); |
| 5012 | } |
| 5013 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5014 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5015 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5016 | unsigned AttrSpellingListIndex, |
| 5017 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 5018 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 5019 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5020 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5021 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 5022 | << 1 /*previous declaration*/; |
| 5023 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 5024 | D->dropAttr<MSInheritanceAttr>(); |
| 5025 | } |
| 5026 | |
| 5027 | CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 5028 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5029 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 5030 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5031 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5032 | } |
| 5033 | } else { |
| 5034 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 5035 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5036 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5037 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5038 | } |
| 5039 | if (RD->getDescribedClassTemplate()) { |
| 5040 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5041 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5042 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5043 | } |
| 5044 | } |
| 5045 | |
| 5046 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5047 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5050 | static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5051 | // The capability attributes take a single string parameter for the name of |
| 5052 | // the capability they represent. The lockable attribute does not take any |
| 5053 | // parameters. However, semantically, both attributes represent the same |
| 5054 | // concept, and so they use the same semantic attribute. Eventually, the |
| 5055 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5056 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 5057 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5058 | // literal will be considered a "mutex." |
| 5059 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5060 | SourceLocation LiteralLoc; |
| 5061 | if (Attr.getKind() == AttributeList::AT_Capability && |
| 5062 | !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc)) |
| 5063 | return; |
| 5064 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5065 | // Currently, there are only two names allowed for a capability: role and |
| 5066 | // mutex (case insensitive). Diagnose other capability names. |
| 5067 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 5068 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 5069 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5070 | D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N, |
| 5071 | Attr.getAttributeSpellingListIndex())); |
| 5072 | } |
| 5073 | |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5074 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, |
| 5075 | const AttributeList &Attr) { |
| 5076 | D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context, |
| 5077 | Attr.getArgAsExpr(0), |
| 5078 | Attr.getAttributeSpellingListIndex())); |
| 5079 | } |
| 5080 | |
| 5081 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
| 5082 | const AttributeList &Attr) { |
| 5083 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5084 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5085 | return; |
| 5086 | |
| 5087 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(), |
| 5088 | S.Context, |
| 5089 | Args.data(), Args.size(), |
| 5090 | Attr.getAttributeSpellingListIndex())); |
| 5091 | } |
| 5092 | |
| 5093 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
| 5094 | const AttributeList &Attr) { |
| 5095 | SmallVector<Expr*, 2> Args; |
| 5096 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 5097 | return; |
| 5098 | |
| 5099 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(), |
| 5100 | S.Context, |
| 5101 | Attr.getArgAsExpr(0), |
| 5102 | Args.data(), |
| 5103 | Args.size(), |
| 5104 | Attr.getAttributeSpellingListIndex())); |
| 5105 | } |
| 5106 | |
| 5107 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
| 5108 | const AttributeList &Attr) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5109 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5110 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 5111 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5112 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5113 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
| 5114 | Attr.getRange(), S.Context, Args.data(), Args.size(), |
| 5115 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5118 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
| 5119 | const AttributeList &Attr) { |
| 5120 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5121 | return; |
| 5122 | |
| 5123 | // check that all arguments are lockable objects |
| 5124 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 5125 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5126 | if (Args.empty()) |
| 5127 | return; |
| 5128 | |
| 5129 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
| 5130 | RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(), |
| 5131 | Args.size(), Attr.getAttributeSpellingListIndex()); |
| 5132 | |
| 5133 | D->addAttr(RCA); |
| 5134 | } |
| 5135 | |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5136 | static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5137 | if (auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
| 5138 | if (NSD->isAnonymousNamespace()) { |
| 5139 | S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace); |
| 5140 | // Do not want to attach the attribute to the namespace because that will |
| 5141 | // cause confusing diagnostic reports for uses of declarations within the |
| 5142 | // namespace. |
| 5143 | return; |
| 5144 | } |
| 5145 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5146 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 5147 | // Handle the cases where the attribute has a text message. |
| 5148 | StringRef Str, Replacement; |
| 5149 | if (Attr.isArgExpr(0) && Attr.getArgAsExpr(0) && |
| 5150 | !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
| 5151 | return; |
| 5152 | |
| 5153 | // Only support a single optional message for Declspec and CXX11. |
| 5154 | if (Attr.isDeclspecAttribute() || Attr.isCXX11Attribute()) |
| 5155 | checkAttributeAtMostNumArgs(S, Attr, 1); |
| 5156 | else if (Attr.isArgExpr(1) && Attr.getArgAsExpr(1) && |
| 5157 | !S.checkStringLiteralArgumentAttr(Attr, 1, Replacement)) |
| 5158 | return; |
| 5159 | |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5160 | if (!S.getLangOpts().CPlusPlus14) |
| 5161 | if (Attr.isCXX11Attribute() && |
| 5162 | !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu"))) |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5163 | S.Diag(Attr.getLoc(), diag::ext_cxx14_attr) << Attr.getName(); |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5164 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 5165 | D->addAttr(::new (S.Context) DeprecatedAttr(Attr.getRange(), S.Context, Str, |
| 5166 | Replacement, |
| 5167 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5168 | } |
| 5169 | |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5170 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5171 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5172 | return; |
| 5173 | |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 5174 | std::vector<StringRef> Sanitizers; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5175 | |
| 5176 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 5177 | StringRef SanitizerName; |
| 5178 | SourceLocation LiteralLoc; |
| 5179 | |
| 5180 | if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc)) |
| 5181 | return; |
| 5182 | |
| 5183 | if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0) |
| 5184 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
| 5185 | |
| 5186 | Sanitizers.push_back(SanitizerName); |
| 5187 | } |
| 5188 | |
| 5189 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
| 5190 | Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(), |
| 5191 | Attr.getAttributeSpellingListIndex())); |
| 5192 | } |
| 5193 | |
| 5194 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
| 5195 | const AttributeList &Attr) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 5196 | StringRef AttrName = Attr.getName()->getName(); |
| 5197 | normalizeName(AttrName); |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 5198 | StringRef SanitizerName = |
| 5199 | llvm::StringSwitch<StringRef>(AttrName) |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5200 | .Case("no_address_safety_analysis", "address") |
| 5201 | .Case("no_sanitize_address", "address") |
| 5202 | .Case("no_sanitize_thread", "thread") |
Peter Collingbourne | 9441094 | 2015-05-15 20:11:18 +0000 | [diff] [blame] | 5203 | .Case("no_sanitize_memory", "memory"); |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5204 | D->addAttr(::new (S.Context) |
| 5205 | NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1, |
| 5206 | Attr.getAttributeSpellingListIndex())); |
| 5207 | } |
| 5208 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 5209 | static void handleInternalLinkageAttr(Sema &S, Decl *D, |
| 5210 | const AttributeList &Attr) { |
| 5211 | if (InternalLinkageAttr *Internal = |
| 5212 | S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(), |
| 5213 | Attr.getAttributeSpellingListIndex())) |
| 5214 | D->addAttr(Internal); |
| 5215 | } |
| 5216 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5217 | /// Handles semantic checking for features that are common to all attributes, |
| 5218 | /// such as checking whether a parameter was properly specified, or the correct |
| 5219 | /// number of arguments were passed, etc. |
| 5220 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 5221 | const AttributeList &Attr) { |
| 5222 | // Several attributes carry different semantics than the parsing requires, so |
| 5223 | // those are opted out of the common handling. |
| 5224 | // |
| 5225 | // We also bail on unknown and ignored attributes because those are handled |
| 5226 | // as part of the target-specific handling logic. |
| 5227 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5228 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5229 | return false; |
| 5230 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5231 | // Check whether the attribute requires specific language extensions to be |
| 5232 | // enabled. |
| 5233 | if (!Attr.diagnoseLangOpts(S)) |
| 5234 | return true; |
| 5235 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5236 | if (Attr.getMinArgs() == Attr.getMaxArgs()) { |
| 5237 | // If there are no optional arguments, then checking for the argument count |
| 5238 | // is trivial. |
| 5239 | if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 5240 | return true; |
| 5241 | } else { |
| 5242 | // There are optional arguments, so checking is slightly more involved. |
| 5243 | if (Attr.getMinArgs() && |
| 5244 | !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs())) |
| 5245 | return true; |
| 5246 | else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() && |
| 5247 | !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs())) |
| 5248 | return true; |
| 5249 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5250 | |
| 5251 | // Check whether the attribute appertains to the given subject. |
| 5252 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 5253 | return true; |
| 5254 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5255 | return false; |
| 5256 | } |
| 5257 | |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5258 | static void handleOpenCLAccessAttr(Sema &S, Decl *D, |
| 5259 | const AttributeList &Attr) { |
| 5260 | if (D->isInvalidDecl()) |
| 5261 | return; |
| 5262 | |
| 5263 | // Check if there is only one access qualifier. |
| 5264 | if (D->hasAttr<OpenCLAccessAttr>()) { |
| 5265 | S.Diag(Attr.getLoc(), diag::err_opencl_multiple_access_qualifiers) |
| 5266 | << D->getSourceRange(); |
| 5267 | D->setInvalidDecl(true); |
| 5268 | return; |
| 5269 | } |
| 5270 | |
| 5271 | // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an |
| 5272 | // image object can be read and written. |
| 5273 | // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe |
| 5274 | // object. Using the read_write (or __read_write) qualifier with the pipe |
| 5275 | // qualifier is a compilation error. |
| 5276 | if (const ParmVarDecl *PDecl = dyn_cast<ParmVarDecl>(D)) { |
| 5277 | const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr(); |
| 5278 | if (Attr.getName()->getName().find("read_write") != StringRef::npos) { |
| 5279 | if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) { |
| 5280 | S.Diag(Attr.getLoc(), diag::err_opencl_invalid_read_write) |
| 5281 | << Attr.getName() << PDecl->getType() << DeclTy->isImageType(); |
| 5282 | D->setInvalidDecl(true); |
| 5283 | return; |
| 5284 | } |
| 5285 | } |
| 5286 | } |
| 5287 | |
| 5288 | D->addAttr(::new (S.Context) OpenCLAccessAttr( |
| 5289 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 5290 | } |
| 5291 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5292 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5293 | // Top Level Sema Entry Points |
| 5294 | //===----------------------------------------------------------------------===// |
| 5295 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5296 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 5297 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 5298 | /// silently ignore it if a GNU attribute. |
| 5299 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 5300 | const AttributeList &Attr, |
| 5301 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5302 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5303 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 5304 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5305 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 5306 | // instead. |
| 5307 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 5308 | return; |
| 5309 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5310 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 5311 | // which do not apply to the current target architecture are treated as |
| 5312 | // though they were unknown attributes. |
| 5313 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 5314 | !Attr.existsInTarget(S.Context.getTargetInfo())) { |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5315 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() |
| 5316 | ? diag::warn_unhandled_ms_attribute_ignored |
| 5317 | : diag::warn_unknown_attribute_ignored) |
| 5318 | << Attr.getName(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5319 | return; |
| 5320 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5321 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5322 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 5323 | return; |
| 5324 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5325 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5326 | default: |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5327 | if (!Attr.isStmtAttr()) { |
| 5328 | // Type attributes are handled elsewhere; silently move on. |
| 5329 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
| 5330 | break; |
| 5331 | } |
| 5332 | S.Diag(Attr.getLoc(), diag::err_stmt_attribute_invalid_on_decl) |
| 5333 | << Attr.getName() << D->getLocation(); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5334 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5335 | case AttributeList::AT_Interrupt: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5336 | handleInterruptAttr(S, D, Attr); |
| 5337 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5338 | case AttributeList::AT_X86ForceAlignArgPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5339 | handleX86ForceAlignArgPointerAttr(S, D, Attr); |
| 5340 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5341 | case AttributeList::AT_DLLExport: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5342 | case AttributeList::AT_DLLImport: |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5343 | handleDLLAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5344 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5345 | case AttributeList::AT_Mips16: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5346 | handleSimpleAttributeWithExclusions<Mips16Attr, MipsInterruptAttr>(S, D, |
| 5347 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5348 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5349 | case AttributeList::AT_NoMips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5350 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); |
| 5351 | break; |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5352 | case AttributeList::AT_AMDGPUNumVGPR: |
| 5353 | handleAMDGPUNumVGPRAttr(S, D, Attr); |
| 5354 | break; |
| 5355 | case AttributeList::AT_AMDGPUNumSGPR: |
| 5356 | handleAMDGPUNumSGPRAttr(S, D, Attr); |
| 5357 | break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 5358 | case AttributeList::AT_IBAction: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5359 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); |
| 5360 | break; |
| 5361 | case AttributeList::AT_IBOutlet: |
| 5362 | handleIBOutlet(S, D, Attr); |
| 5363 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5364 | case AttributeList::AT_IBOutletCollection: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5365 | handleIBOutletCollection(S, D, Attr); |
| 5366 | break; |
| 5367 | case AttributeList::AT_Alias: |
| 5368 | handleAliasAttr(S, D, Attr); |
| 5369 | break; |
| 5370 | case AttributeList::AT_Aligned: |
| 5371 | handleAlignedAttr(S, D, Attr); |
| 5372 | break; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 5373 | case AttributeList::AT_AlignValue: |
| 5374 | handleAlignValueAttr(S, D, Attr); |
| 5375 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5376 | case AttributeList::AT_AlwaysInline: |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 5377 | handleAlwaysInlineAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5378 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5379 | case AttributeList::AT_AnalyzerNoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5380 | handleAnalyzerNoReturnAttr(S, D, Attr); |
| 5381 | break; |
| 5382 | case AttributeList::AT_TLSModel: |
| 5383 | handleTLSModelAttr(S, D, Attr); |
| 5384 | break; |
| 5385 | case AttributeList::AT_Annotate: |
| 5386 | handleAnnotateAttr(S, D, Attr); |
| 5387 | break; |
| 5388 | case AttributeList::AT_Availability: |
| 5389 | handleAvailabilityAttr(S, D, Attr); |
| 5390 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5391 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5392 | handleDependencyAttr(S, scope, D, Attr); |
| 5393 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5394 | case AttributeList::AT_Common: |
| 5395 | handleCommonAttr(S, D, Attr); |
| 5396 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5397 | case AttributeList::AT_CUDAConstant: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5398 | handleSimpleAttributeWithExclusions<CUDAConstantAttr, CUDASharedAttr>(S, D, |
| 5399 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5400 | break; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5401 | case AttributeList::AT_PassObjectSize: |
| 5402 | handlePassObjectSizeAttr(S, D, Attr); |
| 5403 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5404 | case AttributeList::AT_Constructor: |
| 5405 | handleConstructorAttr(S, D, Attr); |
| 5406 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5407 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5408 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); |
| 5409 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5410 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5411 | handleDeprecatedAttr(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 5412 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5413 | case AttributeList::AT_Destructor: |
| 5414 | handleDestructorAttr(S, D, Attr); |
| 5415 | break; |
| 5416 | case AttributeList::AT_EnableIf: |
| 5417 | handleEnableIfAttr(S, D, Attr); |
| 5418 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5419 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5420 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5421 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 5422 | case AttributeList::AT_MinSize: |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 5423 | handleMinSizeAttr(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 5424 | break; |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 5425 | case AttributeList::AT_OptimizeNone: |
| 5426 | handleOptimizeNoneAttr(S, D, Attr); |
| 5427 | break; |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 5428 | case AttributeList::AT_FlagEnum: |
| 5429 | handleSimpleAttribute<FlagEnumAttr>(S, D, Attr); |
| 5430 | break; |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 5431 | case AttributeList::AT_Flatten: |
| 5432 | handleSimpleAttribute<FlattenAttr>(S, D, Attr); |
| 5433 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5434 | case AttributeList::AT_Format: |
| 5435 | handleFormatAttr(S, D, Attr); |
| 5436 | break; |
| 5437 | case AttributeList::AT_FormatArg: |
| 5438 | handleFormatArgAttr(S, D, Attr); |
| 5439 | break; |
| 5440 | case AttributeList::AT_CUDAGlobal: |
| 5441 | handleGlobalAttr(S, D, Attr); |
| 5442 | break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 5443 | case AttributeList::AT_CUDADevice: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5444 | handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D, |
| 5445 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5446 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5447 | case AttributeList::AT_CUDAHost: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5448 | handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, |
| 5449 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5450 | break; |
| 5451 | case AttributeList::AT_GNUInline: |
| 5452 | handleGNUInlineAttr(S, D, Attr); |
| 5453 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5454 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5455 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 5456 | break; |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 5457 | case AttributeList::AT_Restrict: |
| 5458 | handleRestrictAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5459 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5460 | case AttributeList::AT_MayAlias: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5461 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); |
| 5462 | break; |
| 5463 | case AttributeList::AT_Mode: |
| 5464 | handleModeAttr(S, D, Attr); |
| 5465 | break; |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 5466 | case AttributeList::AT_NoAlias: |
| 5467 | handleSimpleAttribute<NoAliasAttr>(S, D, Attr); |
| 5468 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5469 | case AttributeList::AT_NoCommon: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5470 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); |
| 5471 | break; |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 5472 | case AttributeList::AT_NoSplitStack: |
| 5473 | handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr); |
| 5474 | break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 5475 | case AttributeList::AT_NonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5476 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 5477 | handleNonNullAttrParameter(S, PVD, Attr); |
| 5478 | else |
| 5479 | handleNonNullAttr(S, D, Attr); |
| 5480 | break; |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 5481 | case AttributeList::AT_ReturnsNonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5482 | handleReturnsNonNullAttr(S, D, Attr); |
| 5483 | break; |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 5484 | case AttributeList::AT_AssumeAligned: |
| 5485 | handleAssumeAlignedAttr(S, D, Attr); |
| 5486 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5487 | case AttributeList::AT_Overloadable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5488 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); |
| 5489 | break; |
| 5490 | case AttributeList::AT_Ownership: |
| 5491 | handleOwnershipAttr(S, D, Attr); |
| 5492 | break; |
| 5493 | case AttributeList::AT_Cold: |
| 5494 | handleColdAttr(S, D, Attr); |
| 5495 | break; |
| 5496 | case AttributeList::AT_Hot: |
| 5497 | handleHotAttr(S, D, Attr); |
| 5498 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5499 | case AttributeList::AT_Naked: |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 5500 | handleNakedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5501 | break; |
| 5502 | case AttributeList::AT_NoReturn: |
| 5503 | handleNoReturnAttr(S, D, Attr); |
| 5504 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 5505 | case AttributeList::AT_NoThrow: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5506 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); |
| 5507 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5508 | case AttributeList::AT_CUDAShared: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5509 | handleSimpleAttributeWithExclusions<CUDASharedAttr, CUDAConstantAttr>(S, D, |
| 5510 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5511 | break; |
| 5512 | case AttributeList::AT_VecReturn: |
| 5513 | handleVecReturnAttr(S, D, Attr); |
| 5514 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5515 | case AttributeList::AT_ObjCOwnership: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5516 | handleObjCOwnershipAttr(S, D, Attr); |
| 5517 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5518 | case AttributeList::AT_ObjCPreciseLifetime: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5519 | handleObjCPreciseLifetimeAttr(S, D, Attr); |
| 5520 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5521 | case AttributeList::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5522 | handleObjCReturnsInnerPointerAttr(S, D, Attr); |
| 5523 | break; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5524 | case AttributeList::AT_ObjCRequiresSuper: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5525 | handleObjCRequiresSuperAttr(S, D, Attr); |
| 5526 | break; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 5527 | case AttributeList::AT_ObjCBridge: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5528 | handleObjCBridgeAttr(S, scope, D, Attr); |
| 5529 | break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5530 | case AttributeList::AT_ObjCBridgeMutable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5531 | handleObjCBridgeMutableAttr(S, scope, D, Attr); |
| 5532 | break; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5533 | case AttributeList::AT_ObjCBridgeRelated: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5534 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); |
| 5535 | break; |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 5536 | case AttributeList::AT_ObjCDesignatedInitializer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5537 | handleObjCDesignatedInitializer(S, D, Attr); |
| 5538 | break; |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 5539 | case AttributeList::AT_ObjCRuntimeName: |
| 5540 | handleObjCRuntimeName(S, D, Attr); |
| 5541 | break; |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5542 | case AttributeList::AT_ObjCBoxable: |
| 5543 | handleObjCBoxable(S, D, Attr); |
| 5544 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5545 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5546 | handleCFAuditedTransferAttr(S, D, Attr); |
| 5547 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5548 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5549 | handleCFUnknownTransferAttr(S, D, Attr); |
| 5550 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5551 | case AttributeList::AT_CFConsumed: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5552 | case AttributeList::AT_NSConsumed: |
| 5553 | handleNSConsumedAttr(S, D, Attr); |
| 5554 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5555 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5556 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); |
| 5557 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5558 | case AttributeList::AT_NSReturnsAutoreleased: |
| 5559 | case AttributeList::AT_NSReturnsNotRetained: |
| 5560 | case AttributeList::AT_CFReturnsNotRetained: |
| 5561 | case AttributeList::AT_NSReturnsRetained: |
| 5562 | case AttributeList::AT_CFReturnsRetained: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5563 | handleNSReturnsRetainedAttr(S, D, Attr); |
| 5564 | break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 5565 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5566 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); |
| 5567 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5568 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5569 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); |
| 5570 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 5571 | case AttributeList::AT_VecTypeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5572 | handleVecTypeHint(S, D, Attr); |
| 5573 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5574 | case AttributeList::AT_InitPriority: |
| 5575 | handleInitPriorityAttr(S, D, Attr); |
| 5576 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5577 | case AttributeList::AT_Packed: |
| 5578 | handlePackedAttr(S, D, Attr); |
| 5579 | break; |
| 5580 | case AttributeList::AT_Section: |
| 5581 | handleSectionAttr(S, D, Attr); |
| 5582 | break; |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 5583 | case AttributeList::AT_Target: |
| 5584 | handleTargetAttr(S, D, Attr); |
| 5585 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5586 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 5587 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 5588 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5589 | case AttributeList::AT_ArcWeakrefUnavailable: |
| 5590 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); |
| 5591 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5592 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5593 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); |
| 5594 | break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 5595 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 5596 | handleObjCSuppresProtocolAttr(S, D, Attr); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 5597 | break; |
| 5598 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5599 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); |
| 5600 | break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 5601 | case AttributeList::AT_Unused: |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 5602 | handleUnusedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5603 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5604 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5605 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); |
| 5606 | break; |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 5607 | case AttributeList::AT_NotTailCalled: |
| 5608 | handleNotTailCalledAttr(S, D, Attr); |
| 5609 | break; |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 5610 | case AttributeList::AT_DisableTailCalls: |
| 5611 | handleDisableTailCallsAttr(S, D, Attr); |
| 5612 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5613 | case AttributeList::AT_Used: |
| 5614 | handleUsedAttr(S, D, Attr); |
| 5615 | break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 5616 | case AttributeList::AT_Visibility: |
| 5617 | handleVisibilityAttr(S, D, Attr, false); |
| 5618 | break; |
| 5619 | case AttributeList::AT_TypeVisibility: |
| 5620 | handleVisibilityAttr(S, D, Attr, true); |
| 5621 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 5622 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5623 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); |
| 5624 | break; |
| 5625 | case AttributeList::AT_WarnUnusedResult: |
| 5626 | handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 5627 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 5628 | case AttributeList::AT_Weak: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5629 | handleSimpleAttribute<WeakAttr>(S, D, Attr); |
| 5630 | break; |
| 5631 | case AttributeList::AT_WeakRef: |
| 5632 | handleWeakRefAttr(S, D, Attr); |
| 5633 | break; |
| 5634 | case AttributeList::AT_WeakImport: |
| 5635 | handleWeakImportAttr(S, D, Attr); |
| 5636 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5637 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5638 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5639 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5640 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5641 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); |
| 5642 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5643 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5644 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 5645 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5646 | case AttributeList::AT_ObjCNSObject: |
| 5647 | handleObjCNSObject(S, D, Attr); |
| 5648 | break; |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 5649 | case AttributeList::AT_ObjCIndependentClass: |
| 5650 | handleObjCIndependentClass(S, D, Attr); |
| 5651 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5652 | case AttributeList::AT_Blocks: |
| 5653 | handleBlocksAttr(S, D, Attr); |
| 5654 | break; |
| 5655 | case AttributeList::AT_Sentinel: |
| 5656 | handleSentinelAttr(S, D, Attr); |
| 5657 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 5658 | case AttributeList::AT_Const: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5659 | handleSimpleAttribute<ConstAttr>(S, D, Attr); |
| 5660 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5661 | case AttributeList::AT_Pure: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5662 | handleSimpleAttribute<PureAttr>(S, D, Attr); |
| 5663 | break; |
| 5664 | case AttributeList::AT_Cleanup: |
| 5665 | handleCleanupAttr(S, D, Attr); |
| 5666 | break; |
| 5667 | case AttributeList::AT_NoDebug: |
| 5668 | handleNoDebugAttr(S, D, Attr); |
| 5669 | break; |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 5670 | case AttributeList::AT_NoDuplicate: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5671 | handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr); |
| 5672 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5673 | case AttributeList::AT_NoInline: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5674 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); |
| 5675 | break; |
| 5676 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
| 5677 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); |
| 5678 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5679 | case AttributeList::AT_StdCall: |
| 5680 | case AttributeList::AT_CDecl: |
| 5681 | case AttributeList::AT_FastCall: |
| 5682 | case AttributeList::AT_ThisCall: |
| 5683 | case AttributeList::AT_Pascal: |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 5684 | case AttributeList::AT_SwiftCall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 5685 | case AttributeList::AT_VectorCall: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 5686 | case AttributeList::AT_MSABI: |
| 5687 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5688 | case AttributeList::AT_Pcs: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 5689 | case AttributeList::AT_IntelOclBicc: |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 5690 | case AttributeList::AT_PreserveMost: |
| 5691 | case AttributeList::AT_PreserveAll: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5692 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 5693 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5694 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5695 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); |
| 5696 | break; |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5697 | case AttributeList::AT_OpenCLAccess: |
| 5698 | handleOpenCLAccessAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5699 | break; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 5700 | case AttributeList::AT_SwiftContext: |
| 5701 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftContext); |
| 5702 | break; |
| 5703 | case AttributeList::AT_SwiftErrorResult: |
| 5704 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftErrorResult); |
| 5705 | break; |
| 5706 | case AttributeList::AT_SwiftIndirectResult: |
| 5707 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftIndirectResult); |
| 5708 | break; |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 5709 | case AttributeList::AT_InternalLinkage: |
| 5710 | handleInternalLinkageAttr(S, D, Attr); |
| 5711 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 5712 | |
| 5713 | // Microsoft attributes: |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 5714 | case AttributeList::AT_MSNoVTable: |
| 5715 | handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 5716 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 5717 | case AttributeList::AT_MSStruct: |
| 5718 | handleSimpleAttribute<MSStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 5719 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5720 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5721 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5722 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 5723 | case AttributeList::AT_MSInheritance: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5724 | handleMSInheritanceAttr(S, D, Attr); |
| 5725 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 5726 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5727 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); |
| 5728 | break; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5729 | case AttributeList::AT_Thread: |
| 5730 | handleDeclspecThreadAttr(S, D, Attr); |
| 5731 | break; |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5732 | case AttributeList::AT_AbiTag: |
| 5733 | handleAbiTagAttr(S, D, Attr); |
| 5734 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5735 | |
| 5736 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 5737 | case AttributeList::AT_AssertExclusiveLock: |
| 5738 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 5739 | break; |
| 5740 | case AttributeList::AT_AssertSharedLock: |
| 5741 | handleAssertSharedLockAttr(S, D, Attr); |
| 5742 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5743 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5744 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); |
| 5745 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5746 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5747 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5748 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5749 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5750 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); |
| 5751 | break; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5752 | case AttributeList::AT_NoSanitize: |
| 5753 | handleNoSanitizeAttr(S, D, Attr); |
| 5754 | break; |
| 5755 | case AttributeList::AT_NoSanitizeSpecific: |
| 5756 | handleNoSanitizeSpecificAttr(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 5757 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5758 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5759 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 5760 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5761 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5762 | handleGuardedByAttr(S, D, Attr); |
| 5763 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5764 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5765 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5766 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5767 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5768 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5769 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5770 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5771 | handleLockReturnedAttr(S, D, Attr); |
| 5772 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5773 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5774 | handleLocksExcludedAttr(S, D, Attr); |
| 5775 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5776 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5777 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5778 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5779 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5780 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5781 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5782 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5783 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5784 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5785 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5786 | // Capability analysis attributes. |
| 5787 | case AttributeList::AT_Capability: |
| 5788 | case AttributeList::AT_Lockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5789 | handleCapabilityAttr(S, D, Attr); |
| 5790 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5791 | case AttributeList::AT_RequiresCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5792 | handleRequiresCapabilityAttr(S, D, Attr); |
| 5793 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5794 | |
| 5795 | case AttributeList::AT_AssertCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5796 | handleAssertCapabilityAttr(S, D, Attr); |
| 5797 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5798 | case AttributeList::AT_AcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5799 | handleAcquireCapabilityAttr(S, D, Attr); |
| 5800 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5801 | case AttributeList::AT_ReleaseCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5802 | handleReleaseCapabilityAttr(S, D, Attr); |
| 5803 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5804 | case AttributeList::AT_TryAcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5805 | handleTryAcquireCapabilityAttr(S, D, Attr); |
| 5806 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5807 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 5808 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 5809 | case AttributeList::AT_Consumable: |
| 5810 | handleConsumableAttr(S, D, Attr); |
| 5811 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 5812 | case AttributeList::AT_ConsumableAutoCast: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5813 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); |
| 5814 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 5815 | case AttributeList::AT_ConsumableSetOnRead: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5816 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); |
| 5817 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 5818 | case AttributeList::AT_CallableWhen: |
| 5819 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 5820 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 5821 | case AttributeList::AT_ParamTypestate: |
| 5822 | handleParamTypestateAttr(S, D, Attr); |
| 5823 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 5824 | case AttributeList::AT_ReturnTypestate: |
| 5825 | handleReturnTypestateAttr(S, D, Attr); |
| 5826 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 5827 | case AttributeList::AT_SetTypestate: |
| 5828 | handleSetTypestateAttr(S, D, Attr); |
| 5829 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 5830 | case AttributeList::AT_TestTypestate: |
| 5831 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 5832 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 5833 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 5834 | // Type safety attributes. |
| 5835 | case AttributeList::AT_ArgumentWithTypeTag: |
| 5836 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 5837 | break; |
| 5838 | case AttributeList::AT_TypeTagForDatatype: |
| 5839 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 5840 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5841 | } |
| 5842 | } |
| 5843 | |
| 5844 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 5845 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 5846 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 5847 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5848 | bool IncludeCXX11Attributes) { |
| 5849 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5850 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5851 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5852 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5853 | // GCC accepts |
| 5854 | // static int a9 __attribute__((weakref)); |
| 5855 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5856 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 5857 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 5858 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 5859 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 5860 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5861 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5862 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 5863 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 5864 | // good to have a way to specify "these attributes must appear as a group", |
| 5865 | // for these. Additionally, it would be good to have a way to specify "these |
| 5866 | // attribute must never appear as a group" for attributes like cold and hot. |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5867 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 5868 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5869 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 5870 | // FIXME: This emits a different error message than |
| 5871 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5872 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5873 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 5874 | } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5875 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5876 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 5877 | } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 5878 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5879 | D->setInvalidDecl(); |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 5880 | } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 5881 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5882 | << A << ExpectedKernelFunction; |
| 5883 | D->setInvalidDecl(); |
| 5884 | } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 5885 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5886 | << A << ExpectedKernelFunction; |
| 5887 | D->setInvalidDecl(); |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 5888 | } |
| 5889 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5890 | } |
| 5891 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5892 | // Annotation attributes are the only attributes allowed after an access |
| 5893 | // specifier. |
| 5894 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 5895 | const AttributeList *AttrList) { |
| 5896 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5897 | if (l->getKind() == AttributeList::AT_Annotate) { |
David Majnemer | 706f315 | 2014-12-14 01:05:01 +0000 | [diff] [blame] | 5898 | ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 5899 | } else { |
| 5900 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 5901 | return true; |
| 5902 | } |
| 5903 | } |
| 5904 | |
| 5905 | return false; |
| 5906 | } |
| 5907 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5908 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 5909 | /// contains any decl attributes that we should warn about. |
| 5910 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 5911 | for ( ; A; A = A->getNext()) { |
| 5912 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 5913 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 5914 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 5915 | |
| 5916 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 5917 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 5918 | << A->getName() << A->getRange(); |
| 5919 | } else { |
| 5920 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 5921 | << A->getName() << A->getRange(); |
| 5922 | } |
| 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 5927 | /// used to build a declaration, complain about any decl attributes |
| 5928 | /// which might be lying around on it. |
| 5929 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 5930 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 5931 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 5932 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 5933 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 5934 | } |
| 5935 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5936 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5937 | /// \#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] | 5938 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 5939 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5940 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5941 | NamedDecl *NewD = nullptr; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5942 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 5943 | FunctionDecl *NewFD; |
| 5944 | // FIXME: Missing call to CheckFunctionDeclaration(). |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5945 | // FIXME: Mangling? |
| 5946 | // FIXME: Is the qualifier info correct? |
| 5947 | // FIXME: Is the DeclContext correct? |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 5948 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 5949 | Loc, Loc, DeclarationName(II), |
| 5950 | FD->getType(), FD->getTypeSourceInfo(), |
| 5951 | SC_None, false/*isInlineSpecified*/, |
| 5952 | FD->hasPrototype(), |
| 5953 | false/*isConstexprSpecified*/); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5954 | NewD = NewFD; |
| 5955 | |
| 5956 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5957 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5958 | |
| 5959 | // Fake up parameter variables; they are declared as if this were |
| 5960 | // a typedef. |
| 5961 | QualType FDTy = FD->getType(); |
| 5962 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 5963 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 5964 | for (const auto &AI : FT->param_types()) { |
| 5965 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5966 | Param->setScopeInfo(0, Params.size()); |
| 5967 | Params.push_back(Param); |
| 5968 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 5969 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5970 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5971 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 5972 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 5973 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 5974 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 5975 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5976 | if (VD->getQualifier()) { |
| 5977 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 5978 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 5979 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5980 | } |
| 5981 | return NewD; |
| 5982 | } |
| 5983 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 5984 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 5985 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 5986 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5987 | if (W.getUsed()) return; // only do this once |
| 5988 | W.setUsed(true); |
| 5989 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 5990 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 5991 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5992 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 5993 | W.getLocation())); |
| 5994 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 5995 | WeakTopLevelDecl.push_back(NewD); |
| 5996 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 5997 | // to insert Decl at TU scope, sorry. |
| 5998 | DeclContext *SavedContext = CurContext; |
| 5999 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 6000 | NewD->setDeclContext(CurContext); |
| 6001 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6002 | PushOnScopeChains(NewD, S); |
| 6003 | CurContext = SavedContext; |
| 6004 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6005 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6006 | } |
| 6007 | } |
| 6008 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6009 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 6010 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 6011 | // have to do this. |
| 6012 | LoadExternalWeakUndeclaredIdentifiers(); |
| 6013 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6014 | NamedDecl *ND = nullptr; |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6015 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 6016 | if (VD->isExternC()) |
| 6017 | ND = VD; |
| 6018 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 6019 | if (FD->isExternC()) |
| 6020 | ND = FD; |
| 6021 | if (ND) { |
| 6022 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 6023 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6024 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 6025 | WeakInfo W = I->second; |
| 6026 | DeclApplyPragmaWeak(S, ND, W); |
| 6027 | WeakUndeclaredIdentifiers[Id] = W; |
| 6028 | } |
| 6029 | } |
| 6030 | } |
| 6031 | } |
| 6032 | } |
| 6033 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6034 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 6035 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 6036 | /// 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] | 6037 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6038 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6039 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6040 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6041 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6042 | // Walk the declarator structure, applying decl attributes that were in a type |
| 6043 | // position to the decl itself. This handles cases like: |
| 6044 | // int *__attr__(x)** D; |
| 6045 | // when X is a decl attribute. |
| 6046 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 6047 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6048 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6049 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6050 | // Finally, apply any attributes on the decl itself. |
| 6051 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6052 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6053 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6054 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6055 | /// Is the given declaration allowed to use a forbidden type? |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6056 | /// If so, it'll still be annotated with an attribute that makes it |
| 6057 | /// illegal to actually use. |
| 6058 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl, |
| 6059 | const DelayedDiagnostic &diag, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6060 | UnavailableAttr::ImplicitReason &reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6061 | // Private ivars are always okay. Unfortunately, people don't |
| 6062 | // always properly make their ivars private, even in system headers. |
| 6063 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 6064 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 6065 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6066 | return false; |
| 6067 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6068 | // Silently accept unsupported uses of __weak in both user and system |
| 6069 | // declarations when it's been disabled, for ease of integration with |
| 6070 | // -fno-objc-arc files. We do have to take some care against attempts |
| 6071 | // to define such things; for now, we've only done that for ivars |
| 6072 | // and properties. |
| 6073 | if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) { |
| 6074 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 6075 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 6076 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 6077 | return true; |
| 6078 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6079 | } |
| 6080 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6081 | // Allow all sorts of things in system headers. |
| 6082 | if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) { |
| 6083 | // Currently, all the failures dealt with this way are due to ARC |
| 6084 | // restrictions. |
| 6085 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 6086 | return true; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
| 6089 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6090 | } |
| 6091 | |
| 6092 | /// Handle a delayed forbidden-type diagnostic. |
| 6093 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 6094 | Decl *decl) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6095 | auto reason = UnavailableAttr::IR_None; |
| 6096 | if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) { |
| 6097 | assert(reason && "didn't set reason?"); |
| 6098 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason, |
| 6099 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6100 | return; |
| 6101 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6102 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6103 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 6104 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6105 | // kind of forbidden type messages on unavailable functions. |
| 6106 | if (FD->hasAttr<UnavailableAttr>() && |
| 6107 | diag.getForbiddenTypeDiagnostic() == |
| 6108 | diag::err_arc_array_param_no_ownership) { |
| 6109 | diag.Triggered = true; |
| 6110 | return; |
| 6111 | } |
| 6112 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6113 | |
| 6114 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 6115 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 6116 | diag.Triggered = true; |
| 6117 | } |
| 6118 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6119 | static bool isDeclDeprecated(Decl *D) { |
| 6120 | do { |
| 6121 | if (D->isDeprecated()) |
| 6122 | return true; |
| 6123 | // A category implicitly has the availability of the interface. |
| 6124 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 6125 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 6126 | return Interface->isDeprecated(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6127 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 6128 | return false; |
| 6129 | } |
| 6130 | |
| 6131 | static bool isDeclUnavailable(Decl *D) { |
| 6132 | do { |
| 6133 | if (D->isUnavailable()) |
| 6134 | return true; |
| 6135 | // A category implicitly has the availability of the interface. |
| 6136 | if (const ObjCCategoryDecl *CatD = dyn_cast<ObjCCategoryDecl>(D)) |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 6137 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 6138 | return Interface->isUnavailable(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6139 | } while ((D = cast_or_null<Decl>(D->getDeclContext()))); |
| 6140 | return false; |
| 6141 | } |
| 6142 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6143 | static void DoEmitAvailabilityWarning(Sema &S, Sema::AvailabilityDiagnostic K, |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6144 | Decl *Ctx, const NamedDecl *D, |
| 6145 | StringRef Message, SourceLocation Loc, |
| 6146 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 6147 | const ObjCPropertyDecl *ObjCProperty, |
| 6148 | bool ObjCPropertyAccess) { |
| 6149 | // Diagnostics for deprecated or unavailable. |
| 6150 | unsigned diag, diag_message, diag_fwdclass_message; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6151 | unsigned diag_available_here = diag::note_availability_specified_here; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6152 | |
| 6153 | // Matches 'diag::note_property_attribute' options. |
| 6154 | unsigned property_note_select; |
| 6155 | |
| 6156 | // Matches diag::note_availability_specified_here. |
| 6157 | unsigned available_here_select_kind; |
| 6158 | |
| 6159 | // Don't warn if our current context is deprecated or unavailable. |
| 6160 | switch (K) { |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6161 | case Sema::AD_Deprecation: |
Jordan Rose | d17c03e | 2015-04-30 17:20:35 +0000 | [diff] [blame] | 6162 | if (isDeclDeprecated(Ctx) || isDeclUnavailable(Ctx)) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6163 | return; |
| 6164 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 6165 | : diag::warn_property_method_deprecated; |
| 6166 | diag_message = diag::warn_deprecated_message; |
| 6167 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 6168 | property_note_select = /* deprecated */ 0; |
| 6169 | available_here_select_kind = /* deprecated */ 2; |
| 6170 | break; |
| 6171 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6172 | case Sema::AD_Unavailable: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6173 | if (isDeclUnavailable(Ctx)) |
| 6174 | return; |
| 6175 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 6176 | : diag::err_property_method_unavailable; |
| 6177 | diag_message = diag::err_unavailable_message; |
| 6178 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 6179 | property_note_select = /* unavailable */ 1; |
| 6180 | available_here_select_kind = /* unavailable */ 0; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6181 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6182 | if (auto attr = D->getAttr<UnavailableAttr>()) { |
| 6183 | if (attr->isImplicit() && attr->getImplicitReason()) { |
| 6184 | // Most of these failures are due to extra restrictions in ARC; |
| 6185 | // reflect that in the primary diagnostic when applicable. |
| 6186 | auto flagARCError = [&] { |
| 6187 | if (S.getLangOpts().ObjCAutoRefCount && |
| 6188 | S.getSourceManager().isInSystemHeader(D->getLocation())) |
| 6189 | diag = diag::err_unavailable_in_arc; |
| 6190 | }; |
| 6191 | |
| 6192 | switch (attr->getImplicitReason()) { |
| 6193 | case UnavailableAttr::IR_None: break; |
| 6194 | |
| 6195 | case UnavailableAttr::IR_ARCForbiddenType: |
| 6196 | flagARCError(); |
| 6197 | diag_available_here = diag::note_arc_forbidden_type; |
| 6198 | break; |
| 6199 | |
| 6200 | case UnavailableAttr::IR_ForbiddenWeak: |
| 6201 | if (S.getLangOpts().ObjCWeakRuntime) |
| 6202 | diag_available_here = diag::note_arc_weak_disabled; |
| 6203 | else |
| 6204 | diag_available_here = diag::note_arc_weak_no_runtime; |
| 6205 | break; |
| 6206 | |
| 6207 | case UnavailableAttr::IR_ARCForbiddenConversion: |
| 6208 | flagARCError(); |
| 6209 | diag_available_here = diag::note_performs_forbidden_arc_conversion; |
| 6210 | break; |
| 6211 | |
| 6212 | case UnavailableAttr::IR_ARCInitReturnsUnrelated: |
| 6213 | flagARCError(); |
| 6214 | diag_available_here = diag::note_arc_init_returns_unrelated; |
| 6215 | break; |
| 6216 | |
| 6217 | case UnavailableAttr::IR_ARCFieldWithOwnership: |
| 6218 | flagARCError(); |
| 6219 | diag_available_here = diag::note_arc_field_with_ownership; |
| 6220 | break; |
| 6221 | } |
| 6222 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6223 | } |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6224 | break; |
| 6225 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6226 | case Sema::AD_Partial: |
| 6227 | diag = diag::warn_partial_availability; |
| 6228 | diag_message = diag::warn_partial_message; |
| 6229 | diag_fwdclass_message = diag::warn_partial_fwdclass_message; |
| 6230 | property_note_select = /* partial */ 2; |
| 6231 | available_here_select_kind = /* partial */ 3; |
| 6232 | break; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6233 | } |
| 6234 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6235 | CharSourceRange UseRange; |
| 6236 | StringRef Replacement; |
| 6237 | if (K == Sema::AD_Deprecation) { |
| 6238 | if (auto attr = D->getAttr<DeprecatedAttr>()) |
| 6239 | Replacement = attr->getReplacement(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame^] | 6240 | if (auto attr = D->getAttr<AvailabilityAttr>()) |
| 6241 | Replacement = attr->getReplacement(); |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6242 | |
| 6243 | if (!Replacement.empty()) |
| 6244 | UseRange = |
| 6245 | CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); |
| 6246 | } |
| 6247 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6248 | if (!Message.empty()) { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6249 | S.Diag(Loc, diag_message) << D << Message |
| 6250 | << (UseRange.isValid() ? |
| 6251 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6252 | if (ObjCProperty) |
| 6253 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 6254 | << ObjCProperty->getDeclName() << property_note_select; |
| 6255 | } else if (!UnknownObjCClass) { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6256 | S.Diag(Loc, diag) << D |
| 6257 | << (UseRange.isValid() ? |
| 6258 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6259 | if (ObjCProperty) |
| 6260 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 6261 | << ObjCProperty->getDeclName() << property_note_select; |
| 6262 | } else { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6263 | S.Diag(Loc, diag_fwdclass_message) << D |
| 6264 | << (UseRange.isValid() ? |
| 6265 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6266 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 6267 | } |
| 6268 | |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6269 | S.Diag(D->getLocation(), diag_available_here) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6270 | << D << available_here_select_kind; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6271 | if (K == Sema::AD_Partial) |
| 6272 | S.Diag(Loc, diag::note_partial_availability_silence) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6273 | } |
| 6274 | |
| 6275 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 6276 | Decl *Ctx) { |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6277 | assert(DD.Kind == DelayedDiagnostic::Deprecation || |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 6278 | DD.Kind == DelayedDiagnostic::Unavailable); |
| 6279 | Sema::AvailabilityDiagnostic AD = DD.Kind == DelayedDiagnostic::Deprecation |
| 6280 | ? Sema::AD_Deprecation |
| 6281 | : Sema::AD_Unavailable; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6282 | DD.Triggered = true; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6283 | DoEmitAvailabilityWarning( |
| 6284 | S, AD, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc, |
| 6285 | DD.getUnknownObjCClass(), DD.getObjCProperty(), false); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6286 | } |
| 6287 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6288 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 6289 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6290 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6291 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6292 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6293 | // When delaying diagnostics to run in the context of a parsed |
| 6294 | // declaration, we only want to actually emit anything if parsing |
| 6295 | // succeeds. |
| 6296 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6297 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6298 | // We emit all the active diagnostics in this pool or any of its |
| 6299 | // parents. In general, we'll get one pool for the decl spec |
| 6300 | // and a child pool for each declarator; in a decl group like: |
| 6301 | // deprecated_typedef foo, *bar, baz(); |
| 6302 | // only the declarator pops will be passed decls. This is correct; |
| 6303 | // we really do need to consider delayed diagnostics from the decl spec |
| 6304 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6305 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6306 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6307 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6308 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 6309 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 6310 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6311 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6312 | continue; |
| 6313 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6314 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6315 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 6316 | case DelayedDiagnostic::Unavailable: |
| 6317 | // Don't bother giving deprecation/unavailable diagnostics if |
| 6318 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 6319 | if (!decl->isInvalidDecl()) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6320 | handleDelayedAvailabilityCheck(*this, diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6321 | break; |
| 6322 | |
| 6323 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6324 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6325 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6326 | |
| 6327 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6328 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6329 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6330 | } |
| 6331 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6332 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6333 | } |
| 6334 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6335 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 6336 | /// been delayed in the current context instead of in the given pool. |
| 6337 | /// Essentially, this just moves them to the current pool. |
| 6338 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 6339 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 6340 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 6341 | curPool->steal(pool); |
| 6342 | } |
| 6343 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 6344 | void Sema::EmitAvailabilityWarning(AvailabilityDiagnostic AD, |
| 6345 | NamedDecl *D, StringRef Message, |
| 6346 | SourceLocation Loc, |
| 6347 | const ObjCInterfaceDecl *UnknownObjCClass, |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame] | 6348 | const ObjCPropertyDecl *ObjCProperty, |
| 6349 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6350 | // Delay if we're currently parsing a declaration. |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6351 | if (DelayedDiagnostics.shouldDelayDiagnostics() && AD != AD_Partial) { |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 6352 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability( |
| 6353 | AD, Loc, D, UnknownObjCClass, ObjCProperty, Message, |
| 6354 | ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6355 | return; |
| 6356 | } |
| 6357 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 6358 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6359 | DoEmitAvailabilityWarning(*this, AD, Ctx, D, Message, Loc, UnknownObjCClass, |
| 6360 | ObjCProperty, ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6361 | } |