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 | |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTConsumer.h" |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTMutationListener.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" |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 24 | #include "clang/AST/RecursiveASTVisitor.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" |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 31 | #include "clang/Sema/Initialization.h" |
John McCall | f1e8b34 | 2011-09-29 07:17:38 +0000 | [diff] [blame] | 32 | #include "clang/Sema/Lookup.h" |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 33 | #include "clang/Sema/Scope.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 34 | #include "clang/Sema/SemaInternal.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/StringExtras.h" |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 36 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 38 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 39 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 40 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 41 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 42 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 43 | C, |
| 44 | Cpp, |
| 45 | ObjC |
| 46 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 47 | } // end namespace AttributeLangSupport |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 49 | //===----------------------------------------------------------------------===// |
| 50 | // Helper functions |
| 51 | //===----------------------------------------------------------------------===// |
| 52 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 53 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 54 | /// type (function or function-typed variable) or an Objective-C |
| 55 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 56 | static bool isFunctionOrMethod(const Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 57 | return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 58 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 59 | |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 60 | /// \brief Return true if the given decl has function type (function or |
| 61 | /// function-typed variable) or an Objective-C method or a block. |
| 62 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 63 | return isFunctionOrMethod(D) || isa<BlockDecl>(D); |
| 64 | } |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 65 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 66 | /// Return true if the given decl has a declarator that should have |
| 67 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 68 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 69 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 70 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 71 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 74 | /// hasFunctionProto - Return true if the given decl has a argument |
| 75 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 76 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 77 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 78 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 79 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 80 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 83 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 84 | /// 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] | 85 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 86 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 87 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 88 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 89 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 90 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 91 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 94 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 95 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 96 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 97 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 98 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 99 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 100 | return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 103 | static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) { |
| 104 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 105 | return FD->getParamDecl(Idx)->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 106 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 107 | return MD->parameters()[Idx]->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 108 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 109 | return BD->getParamDecl(Idx)->getSourceRange(); |
| 110 | return SourceRange(); |
| 111 | } |
| 112 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 113 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 114 | if (const FunctionType *FnTy = D->getFunctionType()) |
Aaron Ballman | 6288d06 | 2014-07-11 16:31:29 +0000 | [diff] [blame] | 115 | return cast<FunctionType>(FnTy)->getReturnType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 116 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 119 | static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) { |
| 120 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 121 | return FD->getReturnTypeSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 122 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 123 | return MD->getReturnTypeSourceRange(); |
| 124 | return SourceRange(); |
| 125 | } |
| 126 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 127 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 128 | if (const FunctionType *FnTy = D->getFunctionType()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 129 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 130 | return proto->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 131 | } |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 132 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| 133 | return BD->isVariadic(); |
| 134 | |
| 135 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 138 | static bool isInstanceMethod(const Decl *D) { |
| 139 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 140 | return MethodDecl->isInstance(); |
| 141 | return false; |
| 142 | } |
| 143 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 144 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 145 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 146 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 147 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 148 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 149 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 150 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 151 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 152 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 153 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 154 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 155 | // FIXME: Should we walk the chain of classes? |
| 156 | return ClsName == &Ctx.Idents.get("NSString") || |
| 157 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 158 | } |
| 159 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 160 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 161 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 162 | if (!PT) |
| 163 | return false; |
| 164 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 165 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 166 | if (!RT) |
| 167 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 168 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 169 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 170 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 171 | return false; |
| 172 | |
| 173 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 174 | } |
| 175 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 176 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 177 | // FIXME: Include the type in the argument list. |
| 178 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 179 | } |
| 180 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 181 | template <typename Compare> |
| 182 | static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr, |
| 183 | unsigned Num, unsigned Diag, |
| 184 | Compare Comp) { |
| 185 | if (Comp(getNumAttributeArgs(Attr), Num)) { |
| 186 | S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
| 190 | return true; |
| 191 | } |
| 192 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 193 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 194 | /// output an error. |
| 195 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 196 | unsigned Num) { |
| 197 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 198 | diag::err_attribute_wrong_number_arguments, |
| 199 | std::not_equal_to<unsigned>()); |
| 200 | } |
| 201 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 202 | /// \brief Check if the attribute has at least as many args as Num. May |
| 203 | /// output an error. |
| 204 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 205 | unsigned Num) { |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 206 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 207 | diag::err_attribute_too_few_arguments, |
| 208 | std::less<unsigned>()); |
| 209 | } |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 210 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 211 | /// \brief Check if the attribute has at most as many args as Num. May |
| 212 | /// output an error. |
| 213 | static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr, |
| 214 | unsigned Num) { |
| 215 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 216 | diag::err_attribute_too_many_arguments, |
| 217 | std::greater<unsigned>()); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 220 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 221 | /// expression and return success or failure. May output an error. |
| 222 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 223 | const Expr *Expr, uint32_t &Val, |
| 224 | unsigned Idx = UINT_MAX) { |
| 225 | llvm::APSInt I(32); |
| 226 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 227 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 228 | if (Idx != UINT_MAX) |
| 229 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 230 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 231 | << Expr->getSourceRange(); |
| 232 | else |
| 233 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 234 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 235 | << Expr->getSourceRange(); |
| 236 | return false; |
| 237 | } |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 238 | |
| 239 | if (!I.isIntN(32)) { |
Aaron Ballman | 31f4231 | 2014-07-24 14:51:23 +0000 | [diff] [blame] | 240 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 241 | << I.toString(10, false) << 32 << /* Unsigned */ 1; |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 242 | return false; |
| 243 | } |
| 244 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 245 | Val = (uint32_t)I.getZExtValue(); |
| 246 | return true; |
| 247 | } |
| 248 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 249 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 250 | /// declaration. Returns true if diagnosed. |
| 251 | template <typename AttrTy> |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 252 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range, |
| 253 | IdentifierInfo *Ident) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 254 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 255 | S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident |
| 256 | << A; |
| 257 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | return false; |
| 261 | } |
| 262 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 263 | /// \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] | 264 | /// instance method D. May output an error. |
| 265 | /// |
| 266 | /// \returns true if IdxExpr is a valid index. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 267 | static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D, |
| 268 | const AttributeList &Attr, |
| 269 | unsigned AttrArgNum, |
| 270 | const Expr *IdxExpr, |
| 271 | uint64_t &Idx) { |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 272 | assert(isFunctionOrMethodOrBlock(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 273 | |
| 274 | // In C++ the implicit 'this' function parameter also counts. |
| 275 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 276 | bool HP = hasFunctionProto(D); |
| 277 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 278 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 279 | unsigned NumParams = |
| 280 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 281 | |
| 282 | llvm::APSInt IdxInt; |
| 283 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 284 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 285 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 286 | << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant |
| 287 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | |
| 291 | Idx = IdxInt.getLimitedValue(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 292 | if (Idx < 1 || (!IV && Idx > NumParams)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 293 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 294 | << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 295 | return false; |
| 296 | } |
| 297 | Idx--; // Convert to zero-based. |
| 298 | if (HasImplicitThisParam) { |
| 299 | if (Idx == 0) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 300 | S.Diag(Attr.getLoc(), |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 301 | diag::err_attribute_invalid_implicit_this_argument) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 302 | << Attr.getName() << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 303 | return false; |
| 304 | } |
| 305 | --Idx; |
| 306 | } |
| 307 | |
| 308 | return true; |
| 309 | } |
| 310 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 311 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 312 | /// If not emit an error and return false. If the argument is an identifier it |
| 313 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 314 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 315 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 316 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 317 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 318 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 319 | if (Attr.isArgIdent(ArgNum)) { |
| 320 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 321 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 322 | << Attr.getName() << AANT_ArgumentString |
| 323 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 324 | << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 325 | Str = Loc->Ident->getName(); |
| 326 | if (ArgLocation) |
| 327 | *ArgLocation = Loc->Loc; |
| 328 | return true; |
| 329 | } |
| 330 | |
| 331 | // Now check for an actual string literal. |
| 332 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 333 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 334 | if (ArgLocation) |
| 335 | *ArgLocation = ArgExpr->getLocStart(); |
| 336 | |
| 337 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 338 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 339 | << Attr.getName() << AANT_ArgumentString; |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | Str = Literal->getString(); |
| 344 | return true; |
| 345 | } |
| 346 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 347 | /// \brief Applies the given attribute to the Decl without performing any |
| 348 | /// additional semantic checking. |
| 349 | template <typename AttrType> |
| 350 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 351 | const AttributeList &Attr) { |
| 352 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 353 | Attr.getAttributeSpellingListIndex())); |
| 354 | } |
| 355 | |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 356 | template <typename AttrType> |
| 357 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
| 358 | const AttributeList &Attr) { |
| 359 | handleSimpleAttribute<AttrType>(S, D, Attr); |
| 360 | } |
| 361 | |
| 362 | /// \brief Applies the given attribute to the Decl so long as the Decl doesn't |
| 363 | /// already have one of the given incompatible attributes. |
| 364 | template <typename AttrType, typename IncompatibleAttrType, |
| 365 | typename... IncompatibleAttrTypes> |
| 366 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
| 367 | const AttributeList &Attr) { |
| 368 | if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, Attr.getRange(), |
| 369 | Attr.getName())) |
| 370 | return; |
| 371 | handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D, |
| 372 | Attr); |
| 373 | } |
| 374 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 375 | /// \brief Check if the passed-in expression is of type int or bool. |
| 376 | static bool isIntOrBool(Expr *Exp) { |
| 377 | QualType QT = Exp->getType(); |
| 378 | return QT->isBooleanType() || QT->isIntegerType(); |
| 379 | } |
| 380 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 381 | |
| 382 | // Check to see if the type is a smart pointer of some kind. We assume |
| 383 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 384 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 385 | DeclContextLookupResult Res1 = RT->getDecl()->lookup( |
| 386 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 387 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 388 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 389 | |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 390 | DeclContextLookupResult Res2 = RT->getDecl()->lookup( |
| 391 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 392 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 393 | return false; |
| 394 | |
| 395 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 398 | /// \brief Check if passed in Decl is a pointer type. |
| 399 | /// Note that this function may produce an error message. |
| 400 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 401 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 402 | const AttributeList &Attr) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 403 | const ValueDecl *vd = cast<ValueDecl>(D); |
| 404 | QualType QT = vd->getType(); |
| 405 | if (QT->isAnyPointerType()) |
| 406 | return true; |
| 407 | |
| 408 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 409 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 410 | // (We don't want to force template instantiation if we can avoid it, |
| 411 | // since that would alter the order in which templates are instantiated.) |
| 412 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 413 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 414 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 415 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 416 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 417 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 418 | |
| 419 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Aaron Ballman | 6828945 | 2013-12-26 15:06:01 +0000 | [diff] [blame] | 420 | << Attr.getName() << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 421 | return false; |
| 422 | } |
| 423 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 424 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 425 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 426 | static const RecordType *getRecordType(QualType QT) { |
| 427 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 428 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 429 | |
| 430 | // Now check if we point to record type. |
| 431 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 432 | return PT->getPointeeType()->getAs<RecordType>(); |
| 433 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 434 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 437 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 438 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 439 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 440 | if (!RT) |
| 441 | return false; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 442 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 443 | // 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] | 444 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 445 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 446 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 447 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 448 | // FIXME -- Check the type that the smart pointer points to. |
| 449 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 450 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 451 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 452 | // Check if the record itself has a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 453 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 454 | if (RD->hasAttr<CapabilityAttr>()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 455 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 456 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 457 | // Else check if any base classes have a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 458 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 459 | CXXBasePaths BPaths(false, false); |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 460 | if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) { |
| 461 | const auto *Type = BS->getType()->getAs<RecordType>(); |
| 462 | return Type->getDecl()->hasAttr<CapabilityAttr>(); |
| 463 | }, BPaths)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 464 | return true; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 465 | } |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 466 | return false; |
| 467 | } |
| 468 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 469 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 470 | const auto *TD = Ty->getAs<TypedefType>(); |
| 471 | if (!TD) |
| 472 | return false; |
| 473 | |
| 474 | TypedefNameDecl *TN = TD->getDecl(); |
| 475 | if (!TN) |
| 476 | return false; |
| 477 | |
| 478 | return TN->hasAttr<CapabilityAttr>(); |
| 479 | } |
| 480 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 481 | static bool typeHasCapability(Sema &S, QualType Ty) { |
| 482 | if (checkTypedefTypeForCapability(Ty)) |
| 483 | return true; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 484 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 485 | if (checkRecordTypeForCapability(S, Ty)) |
| 486 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 487 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 488 | return false; |
| 489 | } |
| 490 | |
| 491 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 492 | // Capability expressions are simple expressions involving the boolean logic |
| 493 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 494 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 495 | // is a capability or not. |
| 496 | |
| 497 | if (const auto *E = dyn_cast<DeclRefExpr>(Ex)) |
| 498 | return typeHasCapability(S, E->getType()); |
| 499 | else if (const auto *E = dyn_cast<CastExpr>(Ex)) |
| 500 | return isCapabilityExpr(S, E->getSubExpr()); |
| 501 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 502 | return isCapabilityExpr(S, E->getSubExpr()); |
| 503 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
| 504 | if (E->getOpcode() == UO_LNot) |
| 505 | return isCapabilityExpr(S, E->getSubExpr()); |
| 506 | return false; |
| 507 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 508 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 509 | return isCapabilityExpr(S, E->getLHS()) && |
| 510 | isCapabilityExpr(S, E->getRHS()); |
| 511 | return false; |
| 512 | } |
| 513 | |
| 514 | return false; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 517 | /// \brief Checks that all attribute arguments, starting from Sidx, resolve to |
| 518 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 519 | /// \param Sidx The attribute argument index to start checking with. |
| 520 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 521 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 522 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
| 523 | const AttributeList &Attr, |
| 524 | SmallVectorImpl<Expr *> &Args, |
| 525 | int Sidx = 0, |
| 526 | bool ParamIdxOk = false) { |
| 527 | for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 528 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 529 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 530 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 531 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 532 | Args.push_back(ArgExp); |
| 533 | continue; |
| 534 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 535 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 536 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 537 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 538 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 539 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 540 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 541 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 542 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 543 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 544 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 545 | // We allow constant strings to be used as a placeholder for expressions |
| 546 | // that are not valid C++ syntax, but warn that they are ignored. |
| 547 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 548 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 549 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 550 | continue; |
| 551 | } |
| 552 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 553 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 554 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 555 | // A pointer to member expression of the form &MyClass::mu is treated |
| 556 | // specially -- we need to look at the type of the member. |
| 557 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 558 | if (UOp->getOpcode() == UO_AddrOf) |
| 559 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 560 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 561 | ArgTy = DRE->getDecl()->getType(); |
| 562 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 563 | // 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] | 564 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 565 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 566 | // Now check if we index into a record type function param. |
| 567 | if(!RT && ParamIdxOk) { |
| 568 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 569 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 570 | if(FD && IL) { |
| 571 | unsigned int NumParams = FD->getNumParams(); |
| 572 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 573 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 574 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 575 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 576 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 577 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 578 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 579 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 580 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 584 | // If the type does not have a capability, see if the components of the |
| 585 | // expression have capabilities. This allows for writing C code where the |
| 586 | // capability may be on the type, and the expression is a capability |
| 587 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 588 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
| 589 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 590 | << Attr.getName() << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 591 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 592 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 593 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 596 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 597 | // Attribute Implementations |
| 598 | //===----------------------------------------------------------------------===// |
| 599 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 600 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 601 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 602 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 603 | return; |
| 604 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 605 | D->addAttr(::new (S.Context) |
| 606 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 607 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 610 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 611 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 612 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 613 | SmallVector<Expr*, 1> Args; |
| 614 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 615 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 616 | unsigned Size = Args.size(); |
| 617 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 618 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 619 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 620 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 621 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 622 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 625 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 626 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 627 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 628 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 629 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 630 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg, |
| 631 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 634 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 635 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 636 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 637 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 638 | return; |
| 639 | |
| 640 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 641 | return; |
| 642 | |
| 643 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 644 | S.Context, Arg, |
| 645 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 648 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 649 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 650 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 651 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 652 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 653 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 654 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 655 | QualType QT = cast<ValueDecl>(D)->getType(); |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 656 | if (!QT->isDependentType() && !typeHasCapability(S, QT)) { |
| 657 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
| 658 | << Attr.getName(); |
| 659 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 660 | } |
| 661 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 662 | // Check that all arguments are lockable objects. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 663 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 664 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 665 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 666 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 667 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 670 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 671 | const AttributeList &Attr) { |
| 672 | SmallVector<Expr*, 1> Args; |
| 673 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 674 | return; |
| 675 | |
| 676 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 677 | D->addAttr(::new (S.Context) |
| 678 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 679 | StartArg, Args.size(), |
| 680 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 683 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 684 | const AttributeList &Attr) { |
| 685 | SmallVector<Expr*, 1> Args; |
| 686 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 687 | return; |
| 688 | |
| 689 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 690 | D->addAttr(::new (S.Context) |
| 691 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 692 | StartArg, Args.size(), |
| 693 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 696 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 697 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 698 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 699 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 700 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 701 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 702 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 703 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 704 | } |
| 705 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 706 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 707 | const AttributeList &Attr) { |
| 708 | SmallVector<Expr*, 1> Args; |
| 709 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 710 | return; |
| 711 | |
| 712 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 713 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 714 | D->addAttr(::new (S.Context) |
| 715 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 716 | Attr.getAttributeSpellingListIndex())); |
| 717 | } |
| 718 | |
| 719 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 720 | const AttributeList &Attr) { |
| 721 | SmallVector<Expr*, 1> Args; |
| 722 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 723 | return; |
| 724 | |
| 725 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 726 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 727 | D->addAttr(::new (S.Context) |
| 728 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 729 | StartArg, Size, |
| 730 | Attr.getAttributeSpellingListIndex())); |
| 731 | } |
| 732 | |
| 733 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 734 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 735 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 736 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 737 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 738 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 739 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 740 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 741 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 742 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 743 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 747 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 748 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 749 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 752 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 753 | const AttributeList &Attr) { |
| 754 | SmallVector<Expr*, 2> Args; |
| 755 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 756 | return; |
| 757 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 758 | D->addAttr(::new (S.Context) |
| 759 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 760 | Attr.getArgAsExpr(0), |
| 761 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 762 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 765 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 766 | const AttributeList &Attr) { |
| 767 | SmallVector<Expr*, 2> Args; |
| 768 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 769 | return; |
| 770 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 771 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
| 772 | Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(), |
| 773 | Args.size(), Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 776 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 777 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 778 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 779 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 780 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 781 | unsigned Size = Args.size(); |
| 782 | if (Size == 0) |
| 783 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 784 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 785 | D->addAttr(::new (S.Context) |
| 786 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 787 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 791 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 792 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 793 | return; |
| 794 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 795 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 796 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 797 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 798 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 799 | if (Size == 0) |
| 800 | return; |
| 801 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 802 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 803 | D->addAttr(::new (S.Context) |
| 804 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 805 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 808 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Saleem Abdulrasool | 046ba5b | 2016-02-18 06:49:31 +0000 | [diff] [blame] | 809 | S.Diag(Attr.getLoc(), diag::ext_clang_enable_if); |
| 810 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 811 | Expr *Cond = Attr.getArgAsExpr(0); |
| 812 | if (!Cond->isTypeDependent()) { |
| 813 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 814 | if (Converted.isInvalid()) |
| 815 | return; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 816 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | StringRef Msg; |
| 820 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
| 821 | return; |
| 822 | |
| 823 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 824 | if (!Cond->isValueDependent() && |
| 825 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 826 | Diags)) { |
| 827 | S.Diag(Attr.getLoc(), diag::err_enable_if_never_constant_expr); |
| 828 | for (int I = 0, N = Diags.size(); I != N; ++I) |
| 829 | S.Diag(Diags[I].first, Diags[I].second); |
| 830 | return; |
| 831 | } |
| 832 | |
| 833 | D->addAttr(::new (S.Context) |
| 834 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 835 | Attr.getAttributeSpellingListIndex())); |
| 836 | } |
| 837 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 838 | static void handlePassObjectSizeAttr(Sema &S, Decl *D, |
| 839 | const AttributeList &Attr) { |
| 840 | if (D->hasAttr<PassObjectSizeAttr>()) { |
| 841 | S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter) |
| 842 | << Attr.getName(); |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | Expr *E = Attr.getArgAsExpr(0); |
| 847 | uint32_t Type; |
| 848 | if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1)) |
| 849 | return; |
| 850 | |
| 851 | // pass_object_size's argument is passed in as the second argument of |
| 852 | // __builtin_object_size. So, it has the same constraints as that second |
| 853 | // argument; namely, it must be in the range [0, 3]. |
| 854 | if (Type > 3) { |
| 855 | S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range) |
| 856 | << Attr.getName() << 0 << 3 << E->getSourceRange(); |
| 857 | return; |
| 858 | } |
| 859 | |
| 860 | // pass_object_size is only supported on constant pointer parameters; as a |
| 861 | // kindness to users, we allow the parameter to be non-const for declarations. |
| 862 | // At this point, we have no clue if `D` belongs to a function declaration or |
| 863 | // definition, so we defer the constness check until later. |
| 864 | if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) { |
| 865 | S.Diag(D->getLocStart(), diag::err_attribute_pointers_only) |
| 866 | << Attr.getName() << 1; |
| 867 | return; |
| 868 | } |
| 869 | |
| 870 | D->addAttr(::new (S.Context) |
| 871 | PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type, |
| 872 | Attr.getAttributeSpellingListIndex())); |
| 873 | } |
| 874 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 875 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 876 | ConsumableAttr::ConsumedState DefaultState; |
| 877 | |
| 878 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 879 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 880 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 881 | DefaultState)) { |
| 882 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 883 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 884 | return; |
| 885 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 886 | } else { |
| 887 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 888 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 889 | return; |
| 890 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 891 | |
| 892 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 893 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 894 | Attr.getAttributeSpellingListIndex())); |
| 895 | } |
| 896 | |
| 897 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 898 | const AttributeList &Attr) { |
| 899 | ASTContext &CurrContext = S.getASTContext(); |
| 900 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 901 | |
| 902 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 903 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 904 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 905 | RD->getNameAsString(); |
| 906 | |
| 907 | return false; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | return true; |
| 912 | } |
| 913 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 914 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 915 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 916 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 917 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 918 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 919 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 920 | return; |
| 921 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 922 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 923 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 924 | CallableWhenAttr::ConsumedState CallableState; |
| 925 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 926 | StringRef StateString; |
| 927 | SourceLocation Loc; |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 928 | if (Attr.isArgIdent(ArgIndex)) { |
| 929 | IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex); |
| 930 | StateString = Ident->Ident->getName(); |
| 931 | Loc = Ident->Loc; |
| 932 | } else { |
| 933 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 934 | return; |
| 935 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 936 | |
| 937 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 938 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 939 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 940 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 941 | return; |
| 942 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 943 | |
| 944 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 945 | } |
| 946 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 947 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 948 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 949 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 950 | } |
| 951 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 952 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 953 | const AttributeList &Attr) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 954 | ParamTypestateAttr::ConsumedState ParamState; |
| 955 | |
| 956 | if (Attr.isArgIdent(0)) { |
| 957 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 958 | StringRef StateString = Ident->Ident->getName(); |
| 959 | |
| 960 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 961 | ParamState)) { |
| 962 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 963 | << Attr.getName() << StateString; |
| 964 | return; |
| 965 | } |
| 966 | } else { |
| 967 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 968 | Attr.getName() << AANT_ArgumentIdentifier; |
| 969 | return; |
| 970 | } |
| 971 | |
| 972 | // FIXME: This check is currently being done in the analysis. It can be |
| 973 | // enabled here only after the parser propagates attributes at |
| 974 | // template specialization definition, not declaration. |
| 975 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 976 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 977 | // |
| 978 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 979 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 980 | // ReturnType.getAsString(); |
| 981 | // return; |
| 982 | //} |
| 983 | |
| 984 | D->addAttr(::new (S.Context) |
| 985 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 986 | Attr.getAttributeSpellingListIndex())); |
| 987 | } |
| 988 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 989 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 990 | const AttributeList &Attr) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 991 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 992 | |
| 993 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 994 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 995 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 996 | ReturnState)) { |
| 997 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 998 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 999 | return; |
| 1000 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1001 | } else { |
| 1002 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1003 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1004 | return; |
| 1005 | } |
| 1006 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1007 | // FIXME: This check is currently being done in the analysis. It can be |
| 1008 | // enabled here only after the parser propagates attributes at |
| 1009 | // template specialization definition, not declaration. |
| 1010 | //QualType ReturnType; |
| 1011 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1012 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1013 | // ReturnType = Param->getType(); |
| 1014 | // |
| 1015 | //} else if (const CXXConstructorDecl *Constructor = |
| 1016 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1017 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1018 | // |
| 1019 | //} else { |
| 1020 | // |
| 1021 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1022 | //} |
| 1023 | // |
| 1024 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1025 | // |
| 1026 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1027 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1028 | // ReturnType.getAsString(); |
| 1029 | // return; |
| 1030 | //} |
| 1031 | |
| 1032 | D->addAttr(::new (S.Context) |
| 1033 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1034 | Attr.getAttributeSpellingListIndex())); |
| 1035 | } |
| 1036 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1037 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1038 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1039 | return; |
| 1040 | |
| 1041 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1042 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1043 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1044 | StringRef Param = Ident->Ident->getName(); |
| 1045 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1046 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1047 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1048 | return; |
| 1049 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1050 | } else { |
| 1051 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1052 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | D->addAttr(::new (S.Context) |
| 1057 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1058 | Attr.getAttributeSpellingListIndex())); |
| 1059 | } |
| 1060 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1061 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1062 | const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1063 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1064 | return; |
| 1065 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1066 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1067 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1068 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1069 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1070 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1071 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1072 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1073 | return; |
| 1074 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1075 | } else { |
| 1076 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1077 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1078 | return; |
| 1079 | } |
| 1080 | |
| 1081 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1082 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1083 | Attr.getAttributeSpellingListIndex())); |
| 1084 | } |
| 1085 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1086 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1087 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1088 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1089 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1092 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1093 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1094 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1095 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1096 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1097 | // Report warning about changed offset in the newer compiler versions. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1098 | if (!FD->getType()->isDependentType() && |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1099 | !FD->getType()->isIncompleteType() && FD->isBitField() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1100 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1101 | S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield); |
| 1102 | |
| 1103 | FD->addAttr(::new (S.Context) PackedAttr( |
| 1104 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1105 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1106 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1109 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1110 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1111 | // variables or properties of Objective-C classes. The outlet must also |
| 1112 | // have an object reference type. |
| 1113 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1114 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1115 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1116 | << Attr.getName() << VD->getType() << 0; |
| 1117 | return false; |
| 1118 | } |
| 1119 | } |
| 1120 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1121 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1122 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1123 | << Attr.getName() << PD->getType() << 1; |
| 1124 | return false; |
| 1125 | } |
| 1126 | } |
| 1127 | else { |
| 1128 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1129 | return false; |
| 1130 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1131 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1132 | return true; |
| 1133 | } |
| 1134 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1135 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1136 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1137 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1138 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1139 | D->addAttr(::new (S.Context) |
| 1140 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1141 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1144 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1145 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1146 | |
| 1147 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1148 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1149 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1150 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1151 | return; |
| 1152 | } |
| 1153 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1154 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1155 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1156 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1157 | ParsedType PT; |
| 1158 | |
| 1159 | if (Attr.hasParsedType()) |
| 1160 | PT = Attr.getTypeArg(); |
| 1161 | else { |
| 1162 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1163 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1164 | if (!PT) { |
| 1165 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1166 | return; |
| 1167 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1168 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1169 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1170 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1171 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1172 | if (!QTLoc) |
| 1173 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1174 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1175 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1176 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1177 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1178 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1179 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1180 | S.Diag(Attr.getLoc(), |
| 1181 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1182 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1183 | return; |
| 1184 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1185 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1186 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1187 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1188 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1191 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1192 | if (RefOkay) { |
| 1193 | if (T->isReferenceType()) |
| 1194 | return true; |
| 1195 | } else { |
| 1196 | T = T.getNonReferenceType(); |
| 1197 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1198 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1199 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1200 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1201 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1202 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1203 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1204 | for (const auto *I : UD->fields()) { |
| 1205 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1206 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1207 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1208 | } |
| 1209 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1215 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1216 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1217 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1218 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1219 | if (!S.isValidPointerAttrType(T)) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1220 | if (isReturnValue) |
| 1221 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1222 | << Attr.getName() << AttrParmRange << TypeRange; |
| 1223 | else |
| 1224 | S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only) |
| 1225 | << Attr.getName() << AttrParmRange << TypeRange << 0; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1226 | return false; |
| 1227 | } |
| 1228 | return true; |
| 1229 | } |
| 1230 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1231 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1232 | SmallVector<unsigned, 8> NonNullArgs; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1233 | for (unsigned I = 0; I < Attr.getNumArgs(); ++I) { |
| 1234 | Expr *Ex = Attr.getArgAsExpr(I); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1235 | uint64_t Idx; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1236 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1237 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1238 | |
| 1239 | // Is the function argument a pointer type? |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1240 | if (Idx < getFunctionOrMethodNumParams(D) && |
| 1241 | !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1242 | Ex->getSourceRange(), |
| 1243 | getFunctionOrMethodParamRange(D, Idx))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1244 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1245 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1246 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1247 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1248 | |
| 1249 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1250 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1251 | // check if the attribute came from a macro expansion or a template |
| 1252 | // instantiation. |
| 1253 | if (NonNullArgs.empty() && Attr.getLoc().isFileID() && |
| 1254 | S.ActiveTemplateInstantiations.empty()) { |
| 1255 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1256 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1257 | I != E && !AnyPointers; ++I) { |
| 1258 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1259 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1260 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1261 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1262 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1263 | if (!AnyPointers) |
| 1264 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1265 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1266 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1267 | unsigned *Start = NonNullArgs.data(); |
| 1268 | unsigned Size = NonNullArgs.size(); |
| 1269 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1270 | D->addAttr(::new (S.Context) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1271 | NonNullAttr(Attr.getRange(), S.Context, Start, Size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1272 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1275 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1276 | const AttributeList &Attr) { |
| 1277 | if (Attr.getNumArgs() > 0) { |
| 1278 | if (D->getFunctionType()) { |
| 1279 | handleNonNullAttr(S, D, Attr); |
| 1280 | } else { |
| 1281 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1282 | << D->getSourceRange(); |
| 1283 | } |
| 1284 | return; |
| 1285 | } |
| 1286 | |
| 1287 | // Is the argument a pointer type? |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1288 | if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(), |
| 1289 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1290 | return; |
| 1291 | |
| 1292 | D->addAttr(::new (S.Context) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1293 | NonNullAttr(Attr.getRange(), S.Context, nullptr, 0, |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1294 | Attr.getAttributeSpellingListIndex())); |
| 1295 | } |
| 1296 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1297 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1298 | const AttributeList &Attr) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1299 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1300 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1301 | if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1302 | /* isReturnValue */ true)) |
| 1303 | return; |
| 1304 | |
| 1305 | D->addAttr(::new (S.Context) |
| 1306 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1307 | Attr.getAttributeSpellingListIndex())); |
| 1308 | } |
| 1309 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1310 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, |
| 1311 | const AttributeList &Attr) { |
| 1312 | Expr *E = Attr.getArgAsExpr(0), |
| 1313 | *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr; |
| 1314 | S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE, |
| 1315 | Attr.getAttributeSpellingListIndex()); |
| 1316 | } |
| 1317 | |
| 1318 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1319 | Expr *OE, unsigned SpellingListIndex) { |
| 1320 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1321 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1322 | |
| 1323 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1324 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1325 | |
| 1326 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1327 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1328 | << &TmpAttr << AttrRange << SR; |
| 1329 | return; |
| 1330 | } |
| 1331 | |
| 1332 | if (!E->isValueDependent()) { |
| 1333 | llvm::APSInt I(64); |
| 1334 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1335 | if (OE) |
| 1336 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1337 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1338 | << E->getSourceRange(); |
| 1339 | else |
| 1340 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1341 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1342 | << E->getSourceRange(); |
| 1343 | return; |
| 1344 | } |
| 1345 | |
| 1346 | if (!I.isPowerOf2()) { |
| 1347 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1348 | << E->getSourceRange(); |
| 1349 | return; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | if (OE) { |
| 1354 | if (!OE->isValueDependent()) { |
| 1355 | llvm::APSInt I(64); |
| 1356 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1357 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1358 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1359 | << OE->getSourceRange(); |
| 1360 | return; |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | D->addAttr(::new (Context) |
| 1366 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1367 | } |
| 1368 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1369 | /// Normalize the attribute, __foo__ becomes foo. |
| 1370 | /// Returns true if normalization was applied. |
| 1371 | static bool normalizeName(StringRef &AttrName) { |
Aaron Ballman | 6269236 | 2015-10-09 13:53:24 +0000 | [diff] [blame] | 1372 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1373 | AttrName.endswith("__")) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1374 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1375 | return true; |
| 1376 | } |
| 1377 | return false; |
| 1378 | } |
| 1379 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1380 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1381 | // This attribute must be applied to a function declaration. The first |
| 1382 | // argument to the attribute must be an identifier, the name of the resource, |
| 1383 | // for example: malloc. The following arguments must be argument indexes, the |
| 1384 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1385 | // 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] | 1386 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1387 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1388 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1389 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1390 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1391 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1392 | return; |
| 1393 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1394 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1395 | // Figure out our Kind. |
| 1396 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1397 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1398 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1399 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1400 | // Check arguments. |
| 1401 | switch (K) { |
| 1402 | case OwnershipAttr::Takes: |
| 1403 | case OwnershipAttr::Holds: |
| 1404 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1405 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1406 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1407 | return; |
| 1408 | } |
| 1409 | break; |
| 1410 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1411 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1412 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1413 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1414 | return; |
| 1415 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1416 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1419 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1420 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1421 | StringRef ModuleName = Module->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1422 | if (normalizeName(ModuleName)) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1423 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1424 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1425 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1426 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1427 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1428 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1429 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1430 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1431 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1432 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1433 | // Is the function argument a pointer type? |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1434 | QualType T = getFunctionOrMethodParamType(D, Idx); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1435 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1436 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1437 | case OwnershipAttr::Takes: |
| 1438 | case OwnershipAttr::Holds: |
| 1439 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1440 | Err = 0; |
| 1441 | break; |
| 1442 | case OwnershipAttr::Returns: |
| 1443 | if (!T->isIntegerType()) |
| 1444 | Err = 1; |
| 1445 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1446 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1447 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1448 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1449 | << Ex->getSourceRange(); |
| 1450 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1451 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1452 | |
| 1453 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1454 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1455 | // Cannot have two ownership attributes of different kinds for the same |
| 1456 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1457 | if (I->getOwnKind() != K && I->args_end() != |
| 1458 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1459 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1460 | << AL.getName() << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1461 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1462 | } else if (K == OwnershipAttr::Returns && |
| 1463 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1464 | // A returns attribute conflicts with any other returns attribute using |
| 1465 | // a different index. Note, diagnostic reporting is 1-based, but stored |
| 1466 | // argument indexes are 0-based. |
| 1467 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1468 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
| 1469 | << *(I->args_begin()) + 1; |
| 1470 | if (I->args_size()) |
| 1471 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
| 1472 | << (unsigned)Idx + 1 << Ex->getSourceRange(); |
| 1473 | return; |
| 1474 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1477 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | unsigned* start = OwnershipArgs.data(); |
| 1481 | unsigned size = OwnershipArgs.size(); |
| 1482 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1483 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1484 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1485 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1486 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1489 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1490 | // Check the attribute arguments. |
| 1491 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1492 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1493 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1494 | return; |
| 1495 | } |
| 1496 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1497 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1498 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1499 | // gcc rejects |
| 1500 | // class c { |
| 1501 | // static int a __attribute__((weakref ("v2"))); |
| 1502 | // static int b() __attribute__((weakref ("f3"))); |
| 1503 | // }; |
| 1504 | // and ignores the attributes of |
| 1505 | // void f(void) { |
| 1506 | // static int a __attribute__((weakref ("v2"))); |
| 1507 | // } |
| 1508 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1509 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1510 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1511 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1512 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1513 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | // The GCC manual says |
| 1517 | // |
| 1518 | // At present, a declaration to which `weakref' is attached can only |
| 1519 | // be `static'. |
| 1520 | // |
| 1521 | // It also says |
| 1522 | // |
| 1523 | // Without a TARGET, |
| 1524 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1525 | // equivalent to `weak'. |
| 1526 | // |
| 1527 | // gcc 4.4.1 will accept |
| 1528 | // int a7 __attribute__((weakref)); |
| 1529 | // as |
| 1530 | // int a7 __attribute__((weak)); |
| 1531 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1532 | // it if this behaviour is actually used. |
| 1533 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1534 | // GCC rejects |
| 1535 | // static ((alias ("y"), weakref)). |
| 1536 | // Should we? How to check that weakref is before or after alias? |
| 1537 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1538 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1539 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1540 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1541 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1542 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1543 | // GCC will accept anything as the argument of weakref. Should we |
| 1544 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1545 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1546 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1547 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1548 | D->addAttr(::new (S.Context) |
| 1549 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1550 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1551 | } |
| 1552 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1553 | static void handleIFuncAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1554 | StringRef Str; |
| 1555 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
| 1556 | return; |
| 1557 | |
| 1558 | // Aliases should be on declarations, not definitions. |
| 1559 | const auto *FD = cast<FunctionDecl>(D); |
| 1560 | if (FD->isThisDeclarationADefinition()) { |
| 1561 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD << 1; |
| 1562 | return; |
| 1563 | } |
| 1564 | // FIXME: it should be handled as a target specific attribute. |
| 1565 | if (S.Context.getTargetInfo().getTriple().getObjectFormat() != |
| 1566 | llvm::Triple::ELF) { |
| 1567 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1568 | return; |
| 1569 | } |
| 1570 | |
| 1571 | D->addAttr(::new (S.Context) IFuncAttr(Attr.getRange(), S.Context, Str, |
| 1572 | Attr.getAttributeSpellingListIndex())); |
| 1573 | } |
| 1574 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1575 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1576 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1577 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1578 | return; |
| 1579 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1580 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1581 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1582 | return; |
| 1583 | } |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1584 | if (S.Context.getTargetInfo().getTriple().isNVPTX()) { |
| 1585 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_nvptx); |
| 1586 | } |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1587 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1588 | // Aliases should be on declarations, not definitions. |
| 1589 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1590 | if (FD->isThisDeclarationADefinition()) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1591 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1592 | return; |
| 1593 | } |
| 1594 | } else { |
| 1595 | const auto *VD = cast<VarDecl>(D); |
| 1596 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1597 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1598 | return; |
| 1599 | } |
| 1600 | } |
| 1601 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1602 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1603 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1604 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1605 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1608 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1609 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1610 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1611 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1612 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1613 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1617 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1618 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1619 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1620 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1621 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1622 | } |
| 1623 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1624 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1625 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1626 | StringRef Model; |
| 1627 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1628 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1629 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1630 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1631 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1632 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1633 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1634 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1635 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1636 | return; |
| 1637 | } |
| 1638 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1639 | D->addAttr(::new (S.Context) |
| 1640 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1641 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1644 | static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1645 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1646 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1647 | D->addAttr(::new (S.Context) RestrictAttr( |
| 1648 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1649 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1652 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1653 | << Attr.getName() << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1656 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1657 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1658 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1659 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1660 | return; |
| 1661 | } |
| 1662 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1663 | if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(), |
| 1664 | Attr.getAttributeSpellingListIndex())) |
| 1665 | D->addAttr(CA); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 1668 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1669 | if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(), |
| 1670 | Attr.getName())) |
| 1671 | return; |
| 1672 | |
| 1673 | D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context, |
| 1674 | Attr.getAttributeSpellingListIndex())); |
| 1675 | } |
| 1676 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1677 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1678 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1679 | |
| 1680 | if (S.CheckNoReturnAttr(attr)) return; |
| 1681 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1682 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1683 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1684 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1685 | return; |
| 1686 | } |
| 1687 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1688 | D->addAttr(::new (S.Context) |
| 1689 | NoReturnAttr(attr.getRange(), S.Context, |
| 1690 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1694 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1695 | attr.setInvalid(); |
| 1696 | return true; |
| 1697 | } |
| 1698 | |
| 1699 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1700 | } |
| 1701 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1702 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1703 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1704 | |
| 1705 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1706 | // because 'analyzer_noreturn' does not impact the type. |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 1707 | if (!isFunctionOrMethodOrBlock(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1708 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1709 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 1710 | !VD->getType()->isFunctionPointerType())) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1711 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1712 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 1713 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1714 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1715 | return; |
| 1716 | } |
| 1717 | } |
| 1718 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1719 | D->addAttr(::new (S.Context) |
| 1720 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1721 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1724 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1725 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1726 | /* |
| 1727 | Returning a Vector Class in Registers |
| 1728 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1729 | According to the PPU ABI specifications, a class with a single member of |
| 1730 | vector type is returned in memory when used as the return value of a function. |
| 1731 | This results in inefficient code when implementing vector classes. To return |
| 1732 | the value in a single vector register, add the vecreturn attribute to the |
| 1733 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1734 | |
| 1735 | Example: |
| 1736 | |
| 1737 | struct Vector |
| 1738 | { |
| 1739 | __vector float xyzw; |
| 1740 | } __attribute__((vecreturn)); |
| 1741 | |
| 1742 | Vector Add(Vector lhs, Vector rhs) |
| 1743 | { |
| 1744 | Vector result; |
| 1745 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1746 | return result; // This will be returned in a register |
| 1747 | } |
| 1748 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1749 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1750 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1751 | return; |
| 1752 | } |
| 1753 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1754 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1755 | int count = 0; |
| 1756 | |
| 1757 | if (!isa<CXXRecordDecl>(record)) { |
| 1758 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1759 | return; |
| 1760 | } |
| 1761 | |
| 1762 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1763 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1764 | return; |
| 1765 | } |
| 1766 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1767 | for (const auto *I : record->fields()) { |
| 1768 | if ((count == 1) || !I->getType()->isVectorType()) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1769 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1770 | return; |
| 1771 | } |
| 1772 | count++; |
| 1773 | } |
| 1774 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1775 | D->addAttr(::new (S.Context) |
| 1776 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1777 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1780 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1781 | const AttributeList &Attr) { |
| 1782 | if (isa<ParmVarDecl>(D)) { |
| 1783 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1784 | // parameter of a function declaration or lambda. |
| 1785 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1786 | S.Diag(Attr.getLoc(), |
| 1787 | diag::err_carries_dependency_param_not_function_decl); |
| 1788 | return; |
| 1789 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1790 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1791 | |
| 1792 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1793 | Attr.getRange(), S.Context, |
| 1794 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 1797 | static void handleNotTailCalledAttr(Sema &S, Decl *D, |
| 1798 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1799 | if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(), |
| 1800 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 1801 | return; |
| 1802 | |
| 1803 | D->addAttr(::new (S.Context) NotTailCalledAttr( |
| 1804 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1805 | } |
| 1806 | |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 1807 | static void handleDisableTailCallsAttr(Sema &S, Decl *D, |
| 1808 | const AttributeList &Attr) { |
| 1809 | if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(), |
| 1810 | Attr.getName())) |
| 1811 | return; |
| 1812 | |
| 1813 | D->addAttr(::new (S.Context) DisableTailCallsAttr( |
| 1814 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1815 | } |
| 1816 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1817 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1818 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1819 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1820 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1821 | return; |
| 1822 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1823 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1824 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1825 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1826 | return; |
| 1827 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1828 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1829 | D->addAttr(::new (S.Context) |
| 1830 | UsedAttr(Attr.getRange(), S.Context, |
| 1831 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 1834 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1835 | bool IsCXX1zAttr = Attr.isCXX11Attribute() && !Attr.getScopeName(); |
| 1836 | |
| 1837 | if (IsCXX1zAttr && isa<VarDecl>(D)) { |
| 1838 | // The C++1z spelling of this attribute cannot be applied to a static data |
| 1839 | // member per [dcl.attr.unused]p2. |
| 1840 | if (cast<VarDecl>(D)->isStaticDataMember()) { |
| 1841 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 1842 | << Attr.getName() << ExpectedForMaybeUnused; |
| 1843 | return; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | // If this is spelled as the standard C++1z attribute, but not in C++1z, warn |
| 1848 | // about using it as an extension. |
| 1849 | if (!S.getLangOpts().CPlusPlus1z && IsCXX1zAttr) |
| 1850 | S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName(); |
| 1851 | |
| 1852 | D->addAttr(::new (S.Context) UnusedAttr( |
| 1853 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1854 | } |
| 1855 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1856 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1857 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1858 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1859 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 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) |
| 1863 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 1864 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1867 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 1868 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1869 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 1870 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 1871 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1872 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1873 | D->addAttr(::new (S.Context) |
| 1874 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 1875 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1878 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 1879 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 1880 | const AttributeList &Attr) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 1881 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1882 | StringRef Str; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 1883 | if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1884 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1885 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1886 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 1887 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 1888 | } |
| 1889 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1890 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1891 | const AttributeList &Attr) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 1892 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 1893 | S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
| 1894 | << Attr.getName() << Attr.getRange(); |
| 1895 | return; |
| 1896 | } |
| 1897 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1898 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 1899 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 1900 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 1903 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 1904 | IdentifierInfo *Platform, |
| 1905 | VersionTuple Introduced, |
| 1906 | VersionTuple Deprecated, |
| 1907 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1908 | StringRef PlatformName |
| 1909 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 1910 | if (PlatformName.empty()) |
| 1911 | PlatformName = Platform->getName(); |
| 1912 | |
| 1913 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 1914 | // of these steps are needed). |
| 1915 | if (!Introduced.empty() && !Deprecated.empty() && |
| 1916 | !(Introduced <= Deprecated)) { |
| 1917 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1918 | << 1 << PlatformName << Deprecated.getAsString() |
| 1919 | << 0 << Introduced.getAsString(); |
| 1920 | return true; |
| 1921 | } |
| 1922 | |
| 1923 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 1924 | !(Introduced <= Obsoleted)) { |
| 1925 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1926 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1927 | << 0 << Introduced.getAsString(); |
| 1928 | return true; |
| 1929 | } |
| 1930 | |
| 1931 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 1932 | !(Deprecated <= Obsoleted)) { |
| 1933 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 1934 | << 2 << PlatformName << Obsoleted.getAsString() |
| 1935 | << 1 << Deprecated.getAsString(); |
| 1936 | return true; |
| 1937 | } |
| 1938 | |
| 1939 | return false; |
| 1940 | } |
| 1941 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1942 | /// \brief Check whether the two versions match. |
| 1943 | /// |
| 1944 | /// If either version tuple is empty, then they are assumed to match. If |
| 1945 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 1946 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 1947 | bool BeforeIsOkay) { |
| 1948 | if (X.empty() || Y.empty()) |
| 1949 | return true; |
| 1950 | |
| 1951 | if (X == Y) |
| 1952 | return true; |
| 1953 | |
| 1954 | if (BeforeIsOkay && X < Y) |
| 1955 | return true; |
| 1956 | |
| 1957 | return false; |
| 1958 | } |
| 1959 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 1960 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1961 | IdentifierInfo *Platform, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 1962 | bool Implicit, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 1963 | VersionTuple Introduced, |
| 1964 | VersionTuple Deprecated, |
| 1965 | VersionTuple Obsoleted, |
| 1966 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 1967 | StringRef Message, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 1968 | bool IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1969 | StringRef Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1970 | AvailabilityMergeKind AMK, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1971 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1972 | VersionTuple MergedIntroduced = Introduced; |
| 1973 | VersionTuple MergedDeprecated = Deprecated; |
| 1974 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1975 | bool FoundAny = false; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 1976 | bool OverrideOrImpl = false; |
| 1977 | switch (AMK) { |
| 1978 | case AMK_None: |
| 1979 | case AMK_Redeclaration: |
| 1980 | OverrideOrImpl = false; |
| 1981 | break; |
| 1982 | |
| 1983 | case AMK_Override: |
| 1984 | case AMK_ProtocolImplementation: |
| 1985 | OverrideOrImpl = true; |
| 1986 | break; |
| 1987 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1988 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1989 | if (D->hasAttrs()) { |
| 1990 | AttrVec &Attrs = D->getAttrs(); |
| 1991 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 1992 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 1993 | if (!OldAA) { |
| 1994 | ++i; |
| 1995 | continue; |
| 1996 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 1997 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 1998 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 1999 | if (OldPlatform != Platform) { |
| 2000 | ++i; |
| 2001 | continue; |
| 2002 | } |
| 2003 | |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2004 | // If there is an existing availability attribute for this platform that |
| 2005 | // is explicit and the new one is implicit use the explicit one and |
| 2006 | // discard the new implicit attribute. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2007 | if (!OldAA->isImplicit() && Implicit) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2008 | return nullptr; |
| 2009 | } |
| 2010 | |
| 2011 | // If there is an existing attribute for this platform that is implicit |
| 2012 | // and the new attribute is explicit then erase the old one and |
| 2013 | // continue processing the attributes. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2014 | if (!Implicit && OldAA->isImplicit()) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2015 | Attrs.erase(Attrs.begin() + i); |
| 2016 | --e; |
| 2017 | continue; |
| 2018 | } |
| 2019 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2020 | FoundAny = true; |
| 2021 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2022 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2023 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2024 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2025 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2026 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 2027 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 2028 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2029 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2030 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 2031 | if (OverrideOrImpl) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2032 | int Which = -1; |
| 2033 | VersionTuple FirstVersion; |
| 2034 | VersionTuple SecondVersion; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2035 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2036 | Which = 0; |
| 2037 | FirstVersion = OldIntroduced; |
| 2038 | SecondVersion = Introduced; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2039 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2040 | Which = 1; |
| 2041 | FirstVersion = Deprecated; |
| 2042 | SecondVersion = OldDeprecated; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2043 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2044 | Which = 2; |
| 2045 | FirstVersion = Obsoleted; |
| 2046 | SecondVersion = OldObsoleted; |
| 2047 | } |
| 2048 | |
| 2049 | if (Which == -1) { |
| 2050 | Diag(OldAA->getLocation(), |
| 2051 | diag::warn_mismatched_availability_override_unavail) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2052 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2053 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2054 | } else { |
| 2055 | Diag(OldAA->getLocation(), |
| 2056 | diag::warn_mismatched_availability_override) |
| 2057 | << Which |
| 2058 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2059 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 2060 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2061 | } |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2062 | if (AMK == AMK_Override) |
| 2063 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2064 | else |
| 2065 | Diag(Range.getBegin(), diag::note_protocol_method); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2066 | } else { |
| 2067 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2068 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2069 | } |
| 2070 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2071 | Attrs.erase(Attrs.begin() + i); |
| 2072 | --e; |
| 2073 | continue; |
| 2074 | } |
| 2075 | |
| 2076 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2077 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2078 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2079 | |
| 2080 | if (MergedIntroduced2.empty()) |
| 2081 | MergedIntroduced2 = OldIntroduced; |
| 2082 | if (MergedDeprecated2.empty()) |
| 2083 | MergedDeprecated2 = OldDeprecated; |
| 2084 | if (MergedObsoleted2.empty()) |
| 2085 | MergedObsoleted2 = OldObsoleted; |
| 2086 | |
| 2087 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2088 | MergedIntroduced2, MergedDeprecated2, |
| 2089 | MergedObsoleted2)) { |
| 2090 | Attrs.erase(Attrs.begin() + i); |
| 2091 | --e; |
| 2092 | continue; |
| 2093 | } |
| 2094 | |
| 2095 | MergedIntroduced = MergedIntroduced2; |
| 2096 | MergedDeprecated = MergedDeprecated2; |
| 2097 | MergedObsoleted = MergedObsoleted2; |
| 2098 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2099 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | if (FoundAny && |
| 2103 | MergedIntroduced == Introduced && |
| 2104 | MergedDeprecated == Deprecated && |
| 2105 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2106 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2107 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2108 | // Only create a new attribute if !OverrideOrImpl, but we want to do |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2109 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2110 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2111 | MergedDeprecated, MergedObsoleted) && |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2112 | !OverrideOrImpl) { |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2113 | auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2114 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2115 | Obsoleted, IsUnavailable, Message, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2116 | IsStrict, Replacement, |
| 2117 | AttrSpellingListIndex); |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2118 | Avail->setImplicit(Implicit); |
| 2119 | return Avail; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2120 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2121 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2124 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2125 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2126 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2127 | return; |
| 2128 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2129 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2130 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2131 | IdentifierInfo *II = Platform->Ident; |
| 2132 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2133 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2134 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2135 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2136 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2137 | if (!ND) { |
| 2138 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2139 | return; |
| 2140 | } |
| 2141 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2142 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2143 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2144 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2145 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2146 | bool IsStrict = Attr.getStrictLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2147 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 2148 | if (const StringLiteral *SE = |
| 2149 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2150 | Str = SE->getString(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2151 | StringRef Replacement; |
| 2152 | if (const StringLiteral *SE = |
| 2153 | dyn_cast_or_null<StringLiteral>(Attr.getReplacementExpr())) |
| 2154 | Replacement = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2155 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2156 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2157 | false/*Implicit*/, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2158 | Introduced.Version, |
| 2159 | Deprecated.Version, |
| 2160 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2161 | IsUnavailable, Str, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2162 | IsStrict, Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2163 | Sema::AMK_None, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2164 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2165 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2166 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2167 | |
| 2168 | // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning |
| 2169 | // matches before the start of the watchOS platform. |
| 2170 | if (S.Context.getTargetInfo().getTriple().isWatchOS()) { |
| 2171 | IdentifierInfo *NewII = nullptr; |
| 2172 | if (II->getName() == "ios") |
| 2173 | NewII = &S.Context.Idents.get("watchos"); |
| 2174 | else if (II->getName() == "ios_app_extension") |
| 2175 | NewII = &S.Context.Idents.get("watchos_app_extension"); |
| 2176 | |
| 2177 | if (NewII) { |
| 2178 | auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple { |
| 2179 | if (Version.empty()) |
| 2180 | return Version; |
| 2181 | auto Major = Version.getMajor(); |
| 2182 | auto NewMajor = Major >= 9 ? Major - 7 : 0; |
| 2183 | if (NewMajor >= 2) { |
| 2184 | if (Version.getMinor().hasValue()) { |
| 2185 | if (Version.getSubminor().hasValue()) |
| 2186 | return VersionTuple(NewMajor, Version.getMinor().getValue(), |
| 2187 | Version.getSubminor().getValue()); |
| 2188 | else |
| 2189 | return VersionTuple(NewMajor, Version.getMinor().getValue()); |
| 2190 | } |
| 2191 | } |
| 2192 | |
| 2193 | return VersionTuple(2, 0); |
| 2194 | }; |
| 2195 | |
| 2196 | auto NewIntroduced = adjustWatchOSVersion(Introduced.Version); |
| 2197 | auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version); |
| 2198 | auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version); |
| 2199 | |
| 2200 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2201 | Attr.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2202 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2203 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2204 | NewIntroduced, |
| 2205 | NewDeprecated, |
| 2206 | NewObsoleted, |
| 2207 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2208 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2209 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2210 | Sema::AMK_None, |
| 2211 | Index); |
| 2212 | if (NewAttr) |
| 2213 | D->addAttr(NewAttr); |
| 2214 | } |
| 2215 | } else if (S.Context.getTargetInfo().getTriple().isTvOS()) { |
| 2216 | // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning |
| 2217 | // matches before the start of the tvOS platform. |
| 2218 | IdentifierInfo *NewII = nullptr; |
| 2219 | if (II->getName() == "ios") |
| 2220 | NewII = &S.Context.Idents.get("tvos"); |
| 2221 | else if (II->getName() == "ios_app_extension") |
| 2222 | NewII = &S.Context.Idents.get("tvos_app_extension"); |
| 2223 | |
| 2224 | if (NewII) { |
| 2225 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2226 | Attr.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2227 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2228 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2229 | Introduced.Version, |
| 2230 | Deprecated.Version, |
| 2231 | Obsoleted.Version, |
| 2232 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2233 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2234 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2235 | Sema::AMK_None, |
| 2236 | Index); |
| 2237 | if (NewAttr) |
| 2238 | D->addAttr(NewAttr); |
| 2239 | } |
| 2240 | } |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2241 | } |
| 2242 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2243 | template <class T> |
| 2244 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2245 | typename T::VisibilityType value, |
| 2246 | unsigned attrSpellingListIndex) { |
| 2247 | T *existingAttr = D->getAttr<T>(); |
| 2248 | if (existingAttr) { |
| 2249 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2250 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2251 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2252 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2253 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2254 | D->dropAttr<T>(); |
| 2255 | } |
| 2256 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2257 | } |
| 2258 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2259 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2260 | VisibilityAttr::VisibilityType Vis, |
| 2261 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2262 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2263 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2266 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2267 | TypeVisibilityAttr::VisibilityType Vis, |
| 2268 | unsigned AttrSpellingListIndex) { |
| 2269 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2270 | AttrSpellingListIndex); |
| 2271 | } |
| 2272 | |
| 2273 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2274 | bool isTypeVisibility) { |
| 2275 | // Visibility attributes don't mean anything on a typedef. |
| 2276 | if (isa<TypedefNameDecl>(D)) { |
| 2277 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2278 | << Attr.getName(); |
| 2279 | return; |
| 2280 | } |
| 2281 | |
| 2282 | // 'type_visibility' can only go on a type or namespace. |
| 2283 | if (isTypeVisibility && |
| 2284 | !(isa<TagDecl>(D) || |
| 2285 | isa<ObjCInterfaceDecl>(D) || |
| 2286 | isa<NamespaceDecl>(D))) { |
| 2287 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2288 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2289 | return; |
| 2290 | } |
| 2291 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2292 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2293 | StringRef TypeStr; |
| 2294 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2295 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2296 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2297 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2298 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2299 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2300 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2301 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2302 | return; |
| 2303 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2304 | |
| 2305 | // Complain about attempts to use protected visibility on targets |
| 2306 | // (like Darwin) that don't support it. |
| 2307 | if (type == VisibilityAttr::Protected && |
| 2308 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2309 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2310 | type = VisibilityAttr::Default; |
| 2311 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2312 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2313 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2314 | clang::Attr *newAttr; |
| 2315 | if (isTypeVisibility) { |
| 2316 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2317 | (TypeVisibilityAttr::VisibilityType) type, |
| 2318 | Index); |
| 2319 | } else { |
| 2320 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2321 | } |
| 2322 | if (newAttr) |
| 2323 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2326 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2327 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2328 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2329 | if (!Attr.isArgIdent(0)) { |
| 2330 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2331 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2332 | return; |
| 2333 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2334 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2335 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2336 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2337 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2338 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2339 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2340 | return; |
| 2341 | } |
| 2342 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2343 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 2344 | !method->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2345 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2346 | << method->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2347 | // Ignore the attribute. |
| 2348 | return; |
| 2349 | } |
| 2350 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2351 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2352 | S.Context, F, |
| 2353 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2356 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2357 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2358 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2359 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2360 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2361 | return; |
| 2362 | } |
| 2363 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2364 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2365 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2366 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2367 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2368 | return; |
| 2369 | } |
| 2370 | } |
| 2371 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2372 | // It is okay to include this attribute on properties, e.g.: |
| 2373 | // |
| 2374 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2375 | // |
| 2376 | // In this case it follows tradition and suppresses an error in the above |
| 2377 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2378 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2379 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2380 | D->addAttr(::new (S.Context) |
| 2381 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2382 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2385 | static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2386 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 2387 | QualType T = TD->getUnderlyingType(); |
| 2388 | if (!T->isObjCObjectPointerType()) { |
| 2389 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2390 | return; |
| 2391 | } |
| 2392 | } else { |
| 2393 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2394 | return; |
| 2395 | } |
| 2396 | D->addAttr(::new (S.Context) |
| 2397 | ObjCIndependentClassAttr(Attr.getRange(), S.Context, |
| 2398 | Attr.getAttributeSpellingListIndex())); |
| 2399 | } |
| 2400 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2401 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2402 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2403 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2404 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2405 | return; |
| 2406 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2407 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2408 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2409 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2410 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2411 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2412 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2413 | return; |
| 2414 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2415 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2416 | D->addAttr(::new (S.Context) |
| 2417 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2418 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2419 | } |
| 2420 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2421 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2422 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2423 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2424 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2425 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2426 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2427 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2428 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2429 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2430 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2431 | return; |
| 2432 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2433 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2434 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2435 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2436 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2437 | return; |
| 2438 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2439 | |
| 2440 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2443 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2444 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2445 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2446 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2447 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2448 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2449 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2450 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2451 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2452 | return; |
| 2453 | } |
| 2454 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2455 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2456 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2457 | // FIXME: This error message could be improved, it would be nice |
| 2458 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2459 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2460 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2461 | return; |
| 2462 | } |
| 2463 | } |
| 2464 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2465 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2466 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2467 | if (isa<FunctionNoProtoType>(FT)) { |
| 2468 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2469 | return; |
| 2470 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2471 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2472 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2473 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2474 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2475 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2476 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2477 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2478 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2479 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2480 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2481 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2482 | if (!BD->isVariadic()) { |
| 2483 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2484 | return; |
| 2485 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2486 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2487 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2488 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2489 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2490 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2491 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2492 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2493 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2494 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2495 | return; |
| 2496 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2497 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2498 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2499 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2500 | return; |
| 2501 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2502 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2503 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2504 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2505 | return; |
| 2506 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2507 | D->addAttr(::new (S.Context) |
| 2508 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2509 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2512 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2513 | if (D->getFunctionType() && |
| 2514 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2515 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2516 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2517 | return; |
| 2518 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2519 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2520 | if (MD->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2521 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2522 | << Attr.getName() << 1; |
| 2523 | return; |
| 2524 | } |
| 2525 | |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2526 | // If this is spelled as the standard C++1z attribute, but not in C++1z, warn |
| 2527 | // about using it as an extension. |
| 2528 | if (!S.getLangOpts().CPlusPlus1z && Attr.isCXX11Attribute() && |
| 2529 | !Attr.getScopeName()) |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 2530 | S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName(); |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2531 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2532 | D->addAttr(::new (S.Context) |
| 2533 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2534 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2537 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2538 | // weak_import only applies to variable & function declarations. |
| 2539 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2540 | if (!D->canBeWeakImported(isDef)) { |
| 2541 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2542 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2543 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2544 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2545 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2546 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2547 | // Nothing to warn about here. |
| 2548 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2549 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2550 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2551 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2552 | return; |
| 2553 | } |
| 2554 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2555 | D->addAttr(::new (S.Context) |
| 2556 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2557 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2558 | } |
| 2559 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2560 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2561 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2562 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2563 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2564 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2565 | for (unsigned i = 0; i < 3; ++i) { |
| 2566 | const Expr *E = Attr.getArgAsExpr(i); |
| 2567 | if (!checkUInt32Argument(S, Attr, E, WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2568 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2569 | if (WGSize[i] == 0) { |
| 2570 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero) |
| 2571 | << Attr.getName() << E->getSourceRange(); |
| 2572 | return; |
| 2573 | } |
| 2574 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2575 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2576 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2577 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2578 | Existing->getYDim() == WGSize[1] && |
| 2579 | Existing->getZDim() == WGSize[2])) |
| 2580 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2581 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2582 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2583 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2584 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2587 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2588 | if (!Attr.hasParsedType()) { |
| 2589 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2590 | << Attr.getName() << 1; |
| 2591 | return; |
| 2592 | } |
| 2593 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2594 | TypeSourceInfo *ParmTSI = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2595 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2596 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2597 | |
| 2598 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2599 | (ParmType->isBooleanType() || |
| 2600 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2601 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2602 | << ParmType; |
| 2603 | return; |
| 2604 | } |
| 2605 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2606 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2607 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2608 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2609 | return; |
| 2610 | } |
| 2611 | } |
| 2612 | |
| 2613 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2614 | ParmTSI, |
| 2615 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2618 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2619 | StringRef Name, |
| 2620 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2621 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2622 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2623 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2624 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2625 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2626 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2627 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2628 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2629 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2630 | } |
| 2631 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2632 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2633 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2634 | if (!Error.empty()) { |
| 2635 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error; |
| 2636 | return false; |
| 2637 | } |
| 2638 | return true; |
| 2639 | } |
| 2640 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2641 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2642 | // Make sure that there is a string literal as the sections's single |
| 2643 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2644 | StringRef Str; |
| 2645 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2646 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2647 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2648 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2649 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2650 | return; |
| 2651 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2652 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2653 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2654 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2655 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2656 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2657 | return; |
| 2658 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2659 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2660 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2661 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2662 | if (NewAttr) |
| 2663 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2666 | // Check for things we'd like to warn about, no errors or validation for now. |
| 2667 | // TODO: Validation should use a backend target library that specifies |
| 2668 | // the allowable subtarget features and cpus. We could use something like a |
| 2669 | // TargetCodeGenInfo hook here to do validation. |
| 2670 | void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 2671 | for (auto Str : {"tune=", "fpmath="}) |
| 2672 | if (AttrStr.find(Str) != StringRef::npos) |
| 2673 | Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str; |
| 2674 | } |
| 2675 | |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2676 | static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2677 | StringRef Str; |
| 2678 | SourceLocation LiteralLoc; |
| 2679 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
| 2680 | return; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2681 | S.checkTargetAttr(LiteralLoc, Str); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2682 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2683 | TargetAttr *NewAttr = |
| 2684 | ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index); |
| 2685 | D->addAttr(NewAttr); |
| 2686 | } |
| 2687 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2688 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2689 | VarDecl *VD = cast<VarDecl>(D); |
| 2690 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2691 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2692 | return; |
| 2693 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2694 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2695 | Expr *E = Attr.getArgAsExpr(0); |
| 2696 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2697 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2698 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2699 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2700 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2701 | // will warn the user. |
| 2702 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2703 | if (DRE->hasQualifier()) |
| 2704 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2705 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2706 | NI = DRE->getNameInfo(); |
| 2707 | if (!FD) { |
| 2708 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2709 | << NI.getName(); |
| 2710 | return; |
| 2711 | } |
| 2712 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2713 | if (ULE->hasExplicitTemplateArgs()) |
| 2714 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2715 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2716 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2717 | if (!FD) { |
| 2718 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2719 | << NI.getName(); |
| 2720 | if (ULE->getType() == S.Context.OverloadTy) |
| 2721 | S.NoteAllOverloadCandidates(ULE); |
| 2722 | return; |
| 2723 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2724 | } else { |
| 2725 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2726 | return; |
| 2727 | } |
| 2728 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2729 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2730 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2731 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2732 | return; |
| 2733 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2734 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2735 | // We're currently more strict than GCC about what function types we accept. |
| 2736 | // If this ever proves to be a problem it should be easy to fix. |
| 2737 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2738 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2739 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2740 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2741 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2742 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2743 | return; |
| 2744 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2745 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2746 | D->addAttr(::new (S.Context) |
| 2747 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2748 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2749 | } |
| 2750 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2751 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2752 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2753 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2754 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2755 | uint64_t Idx; |
| 2756 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2757 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2758 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2759 | // Make sure the format string is really a string. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2760 | QualType Ty = getFunctionOrMethodParamType(D, Idx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2761 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2762 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 2763 | if (NotNSStringTy && |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2764 | !isCFStringType(Ty, S.Context) && |
| 2765 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2766 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2767 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2768 | << "a string type" << IdxExpr->getSourceRange() |
| 2769 | << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2770 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2771 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2772 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2773 | if (!isNSStringType(Ty, S.Context) && |
| 2774 | !isCFStringType(Ty, S.Context) && |
| 2775 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2776 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2777 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2778 | << (NotNSStringTy ? "string type" : "NSString") |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 2779 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2780 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2783 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2784 | // because that has corrected for the implicit this parameter, and is zero- |
| 2785 | // based. The attribute expects what the user wrote explicitly. |
| 2786 | llvm::APSInt Val; |
| 2787 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2788 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2789 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2790 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2791 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2792 | } |
| 2793 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2794 | enum FormatAttrKind { |
| 2795 | CFStringFormat, |
| 2796 | NSStringFormat, |
| 2797 | StrftimeFormat, |
| 2798 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2799 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2800 | InvalidFormat |
| 2801 | }; |
| 2802 | |
| 2803 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2804 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2805 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2806 | return llvm::StringSwitch<FormatAttrKind>(Format) |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 2807 | // Check for formats that get handled specially. |
| 2808 | .Case("NSString", NSStringFormat) |
| 2809 | .Case("CFString", CFStringFormat) |
| 2810 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2811 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 2812 | // Otherwise, check for supported formats. |
| 2813 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2814 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2815 | .Case("kprintf", SupportedFormat) // OpenBSD. |
| 2816 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
| 2817 | .Case("os_trace", SupportedFormat) |
| 2818 | .Case("os_log", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2819 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 2820 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2821 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2824 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2825 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2826 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2827 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2828 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2829 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2830 | return; |
| 2831 | } |
| 2832 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2833 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2834 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2835 | Attr.setInvalid(); |
| 2836 | return; |
| 2837 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 2838 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 2839 | if (S.Context.getAsArrayType(T)) |
| 2840 | T = S.Context.getBaseElementType(T); |
| 2841 | if (!T->getAs<RecordType>()) { |
| 2842 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 2843 | Attr.setInvalid(); |
| 2844 | return; |
| 2845 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2846 | |
| 2847 | Expr *E = Attr.getArgAsExpr(0); |
| 2848 | uint32_t prioritynum; |
| 2849 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2850 | Attr.setInvalid(); |
| 2851 | return; |
| 2852 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2853 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2854 | if (prioritynum < 101 || prioritynum > 65535) { |
| 2855 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 2856 | << E->getSourceRange() << Attr.getName() << 101 << 65535; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2857 | Attr.setInvalid(); |
| 2858 | return; |
| 2859 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2860 | D->addAttr(::new (S.Context) |
| 2861 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 2862 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2865 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 2866 | IdentifierInfo *Format, int FormatIdx, |
| 2867 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2868 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2869 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2870 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 2871 | if (F->getType() == Format && |
| 2872 | F->getFormatIdx() == FormatIdx && |
| 2873 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2874 | // If we don't have a valid location for this attribute, adopt the |
| 2875 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 2876 | if (F->getLocation().isInvalid()) |
| 2877 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2878 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2879 | } |
| 2880 | } |
| 2881 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2882 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 2883 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 2884 | } |
| 2885 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2886 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2887 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2888 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2889 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2890 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2891 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2892 | return; |
| 2893 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2894 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2895 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 2896 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2897 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2898 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2899 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2900 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 2901 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2902 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 2903 | if (normalizeName(Format)) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 2904 | // If we've modified the string name, we need a new identifier for it. |
| 2905 | II = &S.Context.Idents.get(Format); |
| 2906 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2907 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2908 | // Check for supported formats. |
| 2909 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2910 | |
| 2911 | if (Kind == IgnoredFormat) |
| 2912 | return; |
| 2913 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2914 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2915 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 2916 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2917 | return; |
| 2918 | } |
| 2919 | |
| 2920 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2921 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2922 | uint32_t Idx; |
| 2923 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2924 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2925 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2926 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2927 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2928 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2929 | return; |
| 2930 | } |
| 2931 | |
| 2932 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2933 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2934 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2935 | if (HasImplicitThisParam) { |
| 2936 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2937 | S.Diag(Attr.getLoc(), |
| 2938 | diag::err_format_attribute_implicit_this_format_string) |
| 2939 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 2940 | return; |
| 2941 | } |
| 2942 | ArgIdx--; |
| 2943 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2944 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2945 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2946 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2947 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2948 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2949 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2950 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2951 | << "a CFString" << IdxExpr->getSourceRange() |
| 2952 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 2953 | return; |
| 2954 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2955 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 2956 | // FIXME: do we need to check if the type is NSString*? What are the |
| 2957 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 2958 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2959 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2960 | << "an NSString" << IdxExpr->getSourceRange() |
| 2961 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2962 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2963 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2964 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2965 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2966 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 2967 | << "a string type" << IdxExpr->getSourceRange() |
| 2968 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2969 | return; |
| 2970 | } |
| 2971 | |
| 2972 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2973 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2974 | uint32_t FirstArg; |
| 2975 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2976 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2977 | |
| 2978 | // check if the function is variadic if the 3rd argument non-zero |
| 2979 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2980 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2981 | ++NumArgs; // +1 for ... |
| 2982 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2983 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2984 | return; |
| 2985 | } |
| 2986 | } |
| 2987 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 2988 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 2989 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2990 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2991 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2992 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 2993 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2994 | return; |
| 2995 | } |
| 2996 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 2997 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2998 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 2999 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3000 | return; |
| 3001 | } |
| 3002 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3003 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3004 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3005 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3006 | if (NewAttr) |
| 3007 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3010 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3011 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3012 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3013 | RecordDecl *RD = nullptr; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3014 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3015 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3016 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3017 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3018 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3019 | |
| 3020 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3021 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3022 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3023 | return; |
| 3024 | } |
| 3025 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3026 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3027 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3028 | diag::warn_transparent_union_attribute_not_definition); |
| 3029 | return; |
| 3030 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3031 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3032 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3033 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3034 | if (Field == FieldEnd) { |
| 3035 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3036 | return; |
| 3037 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3038 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3039 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3040 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3041 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3042 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3043 | diag::warn_transparent_union_attribute_floating) |
| 3044 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3045 | return; |
| 3046 | } |
| 3047 | |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3048 | if (FirstType->isIncompleteType()) |
| 3049 | return; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3050 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3051 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3052 | for (; Field != FieldEnd; ++Field) { |
| 3053 | QualType FieldType = Field->getType(); |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3054 | if (FieldType->isIncompleteType()) |
| 3055 | return; |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3056 | // FIXME: this isn't fully correct; we also need to test whether the |
| 3057 | // members of the union would all have the same calling convention as the |
| 3058 | // first member of the union. Checking just the size and alignment isn't |
| 3059 | // sufficient (consider structs passed on the stack instead of in registers |
| 3060 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3061 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3062 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3063 | // Warn if we drop the attribute. |
| 3064 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3065 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3066 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3067 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3068 | diag::warn_transparent_union_attribute_field_size_align) |
| 3069 | << isSize << Field->getDeclName() << FieldBits; |
| 3070 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3071 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3072 | diag::note_transparent_union_first_field_size_align) |
| 3073 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3074 | return; |
| 3075 | } |
| 3076 | } |
| 3077 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3078 | RD->addAttr(::new (S.Context) |
| 3079 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3080 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3081 | } |
| 3082 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3083 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3084 | // Make sure that there is a string literal as the annotation's single |
| 3085 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3086 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3087 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3088 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3089 | |
| 3090 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3091 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 3092 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3093 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3094 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3095 | |
| 3096 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3097 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3098 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3101 | static void handleAlignValueAttr(Sema &S, Decl *D, |
| 3102 | const AttributeList &Attr) { |
| 3103 | S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 3104 | Attr.getAttributeSpellingListIndex()); |
| 3105 | } |
| 3106 | |
| 3107 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3108 | unsigned SpellingListIndex) { |
| 3109 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 3110 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3111 | |
| 3112 | QualType T; |
| 3113 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3114 | T = TD->getUnderlyingType(); |
| 3115 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3116 | T = VD->getType(); |
| 3117 | else |
| 3118 | llvm_unreachable("Unknown decl type for align_value"); |
| 3119 | |
| 3120 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 3121 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 3122 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 3123 | << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange(); |
| 3124 | return; |
| 3125 | } |
| 3126 | |
| 3127 | if (!E->isValueDependent()) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3128 | llvm::APSInt Alignment; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3129 | ExprResult ICE |
| 3130 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3131 | diag::err_align_value_attribute_argument_not_int, |
| 3132 | /*AllowFold*/ false); |
| 3133 | if (ICE.isInvalid()) |
| 3134 | return; |
| 3135 | |
| 3136 | if (!Alignment.isPowerOf2()) { |
| 3137 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3138 | << E->getSourceRange(); |
| 3139 | return; |
| 3140 | } |
| 3141 | |
| 3142 | D->addAttr(::new (Context) |
| 3143 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 3144 | SpellingListIndex)); |
| 3145 | return; |
| 3146 | } |
| 3147 | |
| 3148 | // Save dependent expressions in the AST to be instantiated. |
| 3149 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3152 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3153 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3154 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3155 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3156 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3157 | return; |
| 3158 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3159 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3160 | if (Attr.getNumArgs() == 0) { |
| 3161 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3162 | true, nullptr, Attr.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3163 | return; |
| 3164 | } |
| 3165 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3166 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3167 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3168 | S.Diag(Attr.getEllipsisLoc(), |
| 3169 | diag::err_pack_expansion_without_parameter_packs); |
| 3170 | return; |
| 3171 | } |
| 3172 | |
| 3173 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3174 | return; |
| 3175 | |
David Majnemer | 26a1e0e | 2015-04-07 02:37:09 +0000 | [diff] [blame] | 3176 | if (E->isValueDependent()) { |
| 3177 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 3178 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 3179 | S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name) |
| 3180 | << E->getSourceRange(); |
| 3181 | return; |
| 3182 | } |
| 3183 | } |
| 3184 | } |
| 3185 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3186 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3187 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3191 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3192 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3193 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3194 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3195 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3196 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3197 | // C++11 [dcl.align]p1: |
| 3198 | // An alignment-specifier may be applied to a variable or to a class |
| 3199 | // data member, but it shall not be applied to a bit-field, a function |
| 3200 | // parameter, the formal parameter of a catch clause, or a variable |
| 3201 | // declared with the register storage class specifier. An |
| 3202 | // alignment-specifier may also be applied to the declaration of a class |
| 3203 | // or enumeration type. |
| 3204 | // C11 6.7.5/2: |
| 3205 | // An alignment attribute shall not be specified in a declaration of |
| 3206 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3207 | // object declared with the register storage-class specifier. |
| 3208 | int DiagKind = -1; |
| 3209 | if (isa<ParmVarDecl>(D)) { |
| 3210 | DiagKind = 0; |
| 3211 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3212 | if (VD->getStorageClass() == SC_Register) |
| 3213 | DiagKind = 1; |
| 3214 | if (VD->isExceptionVariable()) |
| 3215 | DiagKind = 2; |
| 3216 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3217 | if (FD->isBitField()) |
| 3218 | DiagKind = 3; |
| 3219 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3220 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3221 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3222 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3223 | return; |
| 3224 | } |
| 3225 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3226 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3227 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3228 | return; |
| 3229 | } |
| 3230 | } |
| 3231 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3232 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3233 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3234 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3235 | AA->setPackExpansion(IsPackExpansion); |
| 3236 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3237 | return; |
| 3238 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3239 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3240 | // FIXME: Cache the number on the Attr object? |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3241 | llvm::APSInt Alignment; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3242 | ExprResult ICE |
| 3243 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3244 | diag::err_aligned_attribute_argument_not_int, |
| 3245 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3246 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3247 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3248 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3249 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3250 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3251 | // C++11 [dcl.align]p2: |
| 3252 | // -- if the constant expression evaluates to zero, the alignment |
| 3253 | // specifier shall have no effect |
| 3254 | // C11 6.7.5p6: |
| 3255 | // An alignment specification of zero has no effect. |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3256 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3257 | if (!llvm::isPowerOf2_64(AlignVal)) { |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3258 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3259 | << E->getSourceRange(); |
| 3260 | return; |
| 3261 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3262 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3263 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3264 | // Alignment calculations can wrap around if it's greater than 2**28. |
David Majnemer | 29c69db | 2015-07-26 01:48:59 +0000 | [diff] [blame] | 3265 | unsigned MaxValidAlignment = |
| 3266 | Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192 |
| 3267 | : 268435456; |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3268 | if (AlignVal > MaxValidAlignment) { |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3269 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 3270 | << E->getSourceRange(); |
| 3271 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3272 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3273 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3274 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3275 | unsigned MaxTLSAlign = |
| 3276 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3277 | .getQuantity(); |
| 3278 | auto *VD = dyn_cast<VarDecl>(D); |
| 3279 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3280 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3281 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3282 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3283 | return; |
| 3284 | } |
| 3285 | } |
| 3286 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3287 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3288 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3289 | AA->setPackExpansion(IsPackExpansion); |
| 3290 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3293 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3294 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3295 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3296 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3297 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3298 | SpellingListIndex); |
| 3299 | AA->setPackExpansion(IsPackExpansion); |
| 3300 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3301 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3302 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3303 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3304 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3305 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3306 | QualType UnderlyingTy, DiagTy; |
| 3307 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 3308 | UnderlyingTy = DiagTy = VD->getType(); |
| 3309 | } else { |
| 3310 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
| 3311 | if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3312 | UnderlyingTy = ED->getIntegerType(); |
| 3313 | } |
| 3314 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3315 | return; |
| 3316 | |
| 3317 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3318 | // The combined effect of all alignment attributes in a declaration shall |
| 3319 | // not specify an alignment that is less strict than the alignment that |
| 3320 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3321 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3322 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3323 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3324 | if (I->isAlignmentDependent()) |
| 3325 | return; |
| 3326 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3327 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3328 | Align = std::max(Align, I->getAlignment(Context)); |
| 3329 | } |
| 3330 | |
| 3331 | if (AlignasAttr && Align) { |
| 3332 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3333 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3334 | if (NaturalAlign > RequestedAlign) |
| 3335 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3336 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3337 | } |
| 3338 | } |
| 3339 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3340 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3341 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3342 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3343 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3344 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3345 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3346 | // have to wait until the record is defined to catch any mismatches. |
| 3347 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3348 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3349 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3350 | // The unspecified model never matches what a definition could need. |
| 3351 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3352 | return false; |
| 3353 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3354 | if (BestCase) { |
| 3355 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3356 | return false; |
| 3357 | } else { |
| 3358 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3359 | return false; |
| 3360 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3361 | |
| 3362 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3363 | << 0 /*definition*/; |
| 3364 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3365 | << RD->getNameAsString(); |
| 3366 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3367 | } |
| 3368 | |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3369 | /// parseModeAttrArg - Parses attribute mode string and returns parsed type |
| 3370 | /// attribute. |
| 3371 | static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, |
| 3372 | bool &IntegerMode, bool &ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3373 | IntegerMode = true; |
| 3374 | ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3375 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3376 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3377 | switch (Str[0]) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3378 | case 'Q': |
| 3379 | DestWidth = 8; |
| 3380 | break; |
| 3381 | case 'H': |
| 3382 | DestWidth = 16; |
| 3383 | break; |
| 3384 | case 'S': |
| 3385 | DestWidth = 32; |
| 3386 | break; |
| 3387 | case 'D': |
| 3388 | DestWidth = 64; |
| 3389 | break; |
| 3390 | case 'X': |
| 3391 | DestWidth = 96; |
| 3392 | break; |
| 3393 | case 'T': |
| 3394 | DestWidth = 128; |
| 3395 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3396 | } |
| 3397 | if (Str[1] == 'F') { |
| 3398 | IntegerMode = false; |
| 3399 | } else if (Str[1] == 'C') { |
| 3400 | IntegerMode = false; |
| 3401 | ComplexMode = true; |
| 3402 | } else if (Str[1] != 'I') { |
| 3403 | DestWidth = 0; |
| 3404 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3405 | break; |
| 3406 | case 4: |
| 3407 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3408 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3409 | if (Str == "word") |
Reid Kleckner | f27e752 | 2016-02-01 18:58:24 +0000 | [diff] [blame] | 3410 | DestWidth = S.Context.getTargetInfo().getRegisterWidth(); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3411 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3412 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3413 | break; |
| 3414 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3415 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3416 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3417 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3418 | case 11: |
| 3419 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3420 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3421 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3422 | } |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3423 | } |
| 3424 | |
| 3425 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
| 3426 | /// type. |
| 3427 | /// |
| 3428 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3429 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3430 | /// HImode, not an intermediate pointer. |
| 3431 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3432 | // This attribute isn't documented, but glibc uses it. It changes |
| 3433 | // the width of an int or unsigned int to the specified size. |
| 3434 | if (!Attr.isArgIdent(0)) { |
| 3435 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3436 | << AANT_ArgumentIdentifier; |
| 3437 | return; |
| 3438 | } |
| 3439 | |
| 3440 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3441 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3442 | S.AddModeAttr(Attr.getRange(), D, Name, Attr.getAttributeSpellingListIndex()); |
| 3443 | } |
| 3444 | |
| 3445 | void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name, |
| 3446 | unsigned SpellingListIndex, bool InInstantiation) { |
| 3447 | StringRef Str = Name->getName(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3448 | normalizeName(Str); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3449 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3450 | |
| 3451 | unsigned DestWidth = 0; |
| 3452 | bool IntegerMode = true; |
| 3453 | bool ComplexMode = false; |
| 3454 | llvm::APInt VectorSize(64, 0); |
| 3455 | if (Str.size() >= 4 && Str[0] == 'V') { |
| 3456 | // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2). |
| 3457 | size_t StrSize = Str.size(); |
| 3458 | size_t VectorStringLength = 0; |
| 3459 | while ((VectorStringLength + 1) < StrSize && |
| 3460 | isdigit(Str[VectorStringLength + 1])) |
| 3461 | ++VectorStringLength; |
| 3462 | if (VectorStringLength && |
| 3463 | !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) && |
| 3464 | VectorSize.isPowerOf2()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3465 | parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth, |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3466 | IntegerMode, ComplexMode); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3467 | // Avoid duplicate warning from template instantiation. |
| 3468 | if (!InInstantiation) |
| 3469 | Diag(AttrLoc, diag::warn_vector_mode_deprecated); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3470 | } else { |
| 3471 | VectorSize = 0; |
| 3472 | } |
| 3473 | } |
| 3474 | |
| 3475 | if (!VectorSize) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3476 | parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode); |
| 3477 | |
| 3478 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3479 | // and friends, at least with glibc. |
| 3480 | // FIXME: Make sure floating-point mappings are accurate |
| 3481 | // FIXME: Support XF and TF types |
| 3482 | if (!DestWidth) { |
| 3483 | Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
| 3484 | return; |
| 3485 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3486 | |
| 3487 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3488 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3489 | OldTy = TD->getUnderlyingType(); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3490 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 3491 | // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'. |
| 3492 | // Try to get type from enum declaration, default to int. |
| 3493 | OldTy = ED->getIntegerType(); |
| 3494 | if (OldTy.isNull()) |
| 3495 | OldTy = Context.IntTy; |
| 3496 | } else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3497 | OldTy = cast<ValueDecl>(D)->getType(); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3498 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3499 | if (OldTy->isDependentType()) { |
| 3500 | D->addAttr(::new (Context) |
| 3501 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
| 3502 | return; |
| 3503 | } |
| 3504 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3505 | // Base type can also be a vector type (see PR17453). |
| 3506 | // Distinguish between base type and base element type. |
| 3507 | QualType OldElemTy = OldTy; |
| 3508 | if (const VectorType *VT = OldTy->getAs<VectorType>()) |
| 3509 | OldElemTy = VT->getElementType(); |
| 3510 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3511 | // GCC allows 'mode' attribute on enumeration types (even incomplete), except |
| 3512 | // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete |
| 3513 | // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected. |
| 3514 | if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) && |
| 3515 | VectorSize.getBoolValue()) { |
| 3516 | Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange; |
| 3517 | return; |
| 3518 | } |
| 3519 | bool IntegralOrAnyEnumType = |
| 3520 | OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>(); |
| 3521 | |
| 3522 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() && |
| 3523 | !IntegralOrAnyEnumType) |
| 3524 | Diag(AttrLoc, diag::err_mode_not_primitive); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3525 | else if (IntegerMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3526 | if (!IntegralOrAnyEnumType) |
| 3527 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3528 | } else if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3529 | if (!OldElemTy->isComplexType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3530 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3531 | } else { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3532 | if (!OldElemTy->isFloatingType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3533 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3534 | } |
| 3535 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3536 | QualType NewElemTy; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3537 | |
| 3538 | if (IntegerMode) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3539 | NewElemTy = Context.getIntTypeForBitwidth(DestWidth, |
| 3540 | OldElemTy->isSignedIntegerType()); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3541 | else |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3542 | NewElemTy = Context.getRealTypeForBitwidth(DestWidth); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3543 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3544 | if (NewElemTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3545 | Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3546 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3549 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3550 | NewElemTy = Context.getComplexType(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | QualType NewTy = NewElemTy; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3554 | if (VectorSize.getBoolValue()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3555 | NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(), |
| 3556 | VectorType::GenericVector); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3557 | } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3558 | // Complex machine mode does not support base vector types. |
| 3559 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3560 | Diag(AttrLoc, diag::err_complex_mode_vector_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3561 | return; |
| 3562 | } |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3563 | unsigned NumElements = Context.getTypeSize(OldElemTy) * |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3564 | OldVT->getNumElements() / |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3565 | Context.getTypeSize(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3566 | NewTy = |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3567 | Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
| 3570 | if (NewTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3571 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3572 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3576 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3577 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3578 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3579 | ED->setIntegerType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3580 | else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3581 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3582 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3583 | D->addAttr(::new (Context) |
| 3584 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3585 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3586 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3587 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3588 | D->addAttr(::new (S.Context) |
| 3589 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3590 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3593 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3594 | IdentifierInfo *Ident, |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3595 | unsigned AttrSpellingListIndex) { |
| 3596 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3597 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3598 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3599 | return nullptr; |
| 3600 | } |
| 3601 | |
| 3602 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 3603 | return nullptr; |
| 3604 | |
| 3605 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 3606 | AttrSpellingListIndex); |
| 3607 | } |
| 3608 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3609 | CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range, |
| 3610 | IdentifierInfo *Ident, |
| 3611 | unsigned AttrSpellingListIndex) { |
| 3612 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident)) |
| 3613 | return nullptr; |
| 3614 | |
| 3615 | return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex); |
| 3616 | } |
| 3617 | |
| 3618 | InternalLinkageAttr * |
| 3619 | Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range, |
| 3620 | IdentifierInfo *Ident, |
| 3621 | unsigned AttrSpellingListIndex) { |
| 3622 | if (auto VD = dyn_cast<VarDecl>(D)) { |
| 3623 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 3624 | // ImplicitParm or VarTemplateSpecialization). |
| 3625 | if (VD->getKind() != Decl::Var) { |
| 3626 | Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type) |
| 3627 | << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 3628 | : ExpectedVariableOrFunction); |
| 3629 | return nullptr; |
| 3630 | } |
| 3631 | // Attribute does not apply to non-static local variables. |
| 3632 | if (VD->hasLocalStorage()) { |
| 3633 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 3634 | return nullptr; |
| 3635 | } |
| 3636 | } |
| 3637 | |
| 3638 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident)) |
| 3639 | return nullptr; |
| 3640 | |
| 3641 | return ::new (Context) |
| 3642 | InternalLinkageAttr(Range, Context, AttrSpellingListIndex); |
| 3643 | } |
| 3644 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3645 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 3646 | unsigned AttrSpellingListIndex) { |
| 3647 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 3648 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 3649 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3650 | return nullptr; |
| 3651 | } |
| 3652 | |
| 3653 | if (D->hasAttr<MinSizeAttr>()) |
| 3654 | return nullptr; |
| 3655 | |
| 3656 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 3657 | } |
| 3658 | |
| 3659 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 3660 | unsigned AttrSpellingListIndex) { |
| 3661 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 3662 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 3663 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3664 | D->dropAttr<AlwaysInlineAttr>(); |
| 3665 | } |
| 3666 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 3667 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 3668 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3669 | D->dropAttr<MinSizeAttr>(); |
| 3670 | } |
| 3671 | |
| 3672 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 3673 | return nullptr; |
| 3674 | |
| 3675 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 3676 | AttrSpellingListIndex); |
| 3677 | } |
| 3678 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3679 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 3680 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3681 | if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(), |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 3682 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 3683 | return; |
| 3684 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3685 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
| 3686 | D, Attr.getRange(), Attr.getName(), |
| 3687 | Attr.getAttributeSpellingListIndex())) |
| 3688 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3691 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3692 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
| 3693 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3694 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 3695 | } |
| 3696 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3697 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, |
| 3698 | const AttributeList &Attr) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3699 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
| 3700 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3701 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 3704 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3705 | if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, Attr.getRange(), |
| 3706 | Attr.getName())) |
| 3707 | return; |
| 3708 | auto *VD = cast<VarDecl>(D); |
| 3709 | if (!VD->hasGlobalStorage()) { |
| 3710 | S.Diag(Attr.getLoc(), diag::err_cuda_nonglobal_constant); |
| 3711 | return; |
| 3712 | } |
| 3713 | D->addAttr(::new (S.Context) CUDAConstantAttr( |
| 3714 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 3715 | } |
| 3716 | |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 3717 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3718 | if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, Attr.getRange(), |
| 3719 | Attr.getName())) |
| 3720 | return; |
| 3721 | auto *VD = cast<VarDecl>(D); |
Justin Lebar | 281ce2a | 2016-10-02 15:24:50 +0000 | [diff] [blame] | 3722 | // extern __shared__ is only allowed on arrays with no length (e.g. |
| 3723 | // "int x[]"). |
| 3724 | if (VD->hasExternalStorage() && !isa<IncompleteArrayType>(VD->getType())) { |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 3725 | S.Diag(Attr.getLoc(), diag::err_cuda_extern_shared) << VD; |
| 3726 | return; |
| 3727 | } |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 3728 | if (S.getLangOpts().CUDA && VD->hasLocalStorage() && |
| 3729 | S.CUDADiagIfHostCode(Attr.getLoc(), diag::err_cuda_host_shared) |
| 3730 | << S.CurrentCUDATarget()) |
| 3731 | return; |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 3732 | D->addAttr(::new (S.Context) CUDASharedAttr( |
| 3733 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 3734 | } |
| 3735 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3736 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 3737 | if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, Attr.getRange(), |
| 3738 | Attr.getName()) || |
| 3739 | checkAttrMutualExclusion<CUDAHostAttr>(S, D, Attr.getRange(), |
| 3740 | Attr.getName())) { |
| 3741 | return; |
| 3742 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3743 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3744 | if (!FD->getReturnType()->isVoidType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3745 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 3746 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3747 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3748 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 3749 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3750 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3751 | } |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 3752 | if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) { |
| 3753 | if (Method->isInstance()) { |
| 3754 | S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method) |
| 3755 | << Method; |
| 3756 | return; |
| 3757 | } |
| 3758 | S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method; |
| 3759 | } |
| 3760 | // Only warn for "inline" when compiling for host, to cut down on noise. |
| 3761 | if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice) |
| 3762 | S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3763 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3764 | D->addAttr(::new (S.Context) |
| 3765 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Eli Bendersky | 8386004 | 2014-09-02 22:00:06 +0000 | [diff] [blame] | 3766 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3769 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3770 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3771 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3772 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3773 | return; |
| 3774 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3775 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3776 | D->addAttr(::new (S.Context) |
| 3777 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3778 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3779 | } |
| 3780 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3781 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3782 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3783 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3784 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3785 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3786 | CallingConv CC; |
Richard Smith | eec7cb1 | 2015-05-28 23:38:53 +0000 | [diff] [blame] | 3787 | if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3788 | return; |
| 3789 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3790 | if (!isa<ObjCMethodDecl>(D)) { |
| 3791 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3792 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3793 | return; |
| 3794 | } |
| 3795 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3796 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3797 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3798 | D->addAttr(::new (S.Context) |
| 3799 | FastCallAttr(Attr.getRange(), S.Context, |
| 3800 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3801 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3802 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3803 | D->addAttr(::new (S.Context) |
| 3804 | StdCallAttr(Attr.getRange(), S.Context, |
| 3805 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3806 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3807 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3808 | D->addAttr(::new (S.Context) |
| 3809 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3810 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3811 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3812 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3813 | D->addAttr(::new (S.Context) |
| 3814 | CDeclAttr(Attr.getRange(), S.Context, |
| 3815 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3816 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3817 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3818 | D->addAttr(::new (S.Context) |
| 3819 | PascalAttr(Attr.getRange(), S.Context, |
| 3820 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3821 | return; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 3822 | case AttributeList::AT_SwiftCall: |
| 3823 | D->addAttr(::new (S.Context) |
| 3824 | SwiftCallAttr(Attr.getRange(), S.Context, |
| 3825 | Attr.getAttributeSpellingListIndex())); |
| 3826 | return; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3827 | case AttributeList::AT_VectorCall: |
| 3828 | D->addAttr(::new (S.Context) |
| 3829 | VectorCallAttr(Attr.getRange(), S.Context, |
| 3830 | Attr.getAttributeSpellingListIndex())); |
| 3831 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3832 | case AttributeList::AT_MSABI: |
| 3833 | D->addAttr(::new (S.Context) |
| 3834 | MSABIAttr(Attr.getRange(), S.Context, |
| 3835 | Attr.getAttributeSpellingListIndex())); |
| 3836 | return; |
| 3837 | case AttributeList::AT_SysVABI: |
| 3838 | D->addAttr(::new (S.Context) |
| 3839 | SysVABIAttr(Attr.getRange(), S.Context, |
| 3840 | Attr.getAttributeSpellingListIndex())); |
| 3841 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3842 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3843 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3844 | switch (CC) { |
| 3845 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3846 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3847 | break; |
| 3848 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3849 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 3850 | break; |
| 3851 | default: |
| 3852 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3855 | D->addAttr(::new (S.Context) |
| 3856 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 3857 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 3858 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3859 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3860 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3861 | D->addAttr(::new (S.Context) |
| 3862 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 3863 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3864 | return; |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 3865 | case AttributeList::AT_PreserveMost: |
| 3866 | D->addAttr(::new (S.Context) PreserveMostAttr( |
| 3867 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 3868 | return; |
| 3869 | case AttributeList::AT_PreserveAll: |
| 3870 | D->addAttr(::new (S.Context) PreserveAllAttr( |
| 3871 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 3872 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3873 | default: |
| 3874 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3875 | } |
| 3876 | } |
| 3877 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3878 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 3879 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3880 | if (attr.isInvalid()) |
| 3881 | return true; |
| 3882 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 3883 | if (attr.hasProcessingCache()) { |
| 3884 | CC = (CallingConv) attr.getProcessingCache(); |
| 3885 | return false; |
| 3886 | } |
| 3887 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3888 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3889 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3890 | attr.setInvalid(); |
| 3891 | return true; |
| 3892 | } |
| 3893 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 3894 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3895 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3896 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 3897 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 3898 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 3899 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 3900 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 3901 | case AttributeList::AT_SwiftCall: CC = CC_Swift; break; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3902 | case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 3903 | case AttributeList::AT_MSABI: |
| 3904 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 3905 | CC_X86_64Win64; |
| 3906 | break; |
| 3907 | case AttributeList::AT_SysVABI: |
| 3908 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 3909 | CC_C; |
| 3910 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3911 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 3912 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3913 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3914 | attr.setInvalid(); |
| 3915 | return true; |
| 3916 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3917 | if (StrRef == "aapcs") { |
| 3918 | CC = CC_AAPCS; |
| 3919 | break; |
| 3920 | } else if (StrRef == "aapcs-vfp") { |
| 3921 | CC = CC_AAPCS_VFP; |
| 3922 | break; |
| 3923 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 3924 | |
| 3925 | attr.setInvalid(); |
| 3926 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 3927 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 3928 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 3929 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 3930 | case AttributeList::AT_PreserveMost: CC = CC_PreserveMost; break; |
| 3931 | case AttributeList::AT_PreserveAll: CC = CC_PreserveAll; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 3932 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3933 | } |
| 3934 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3935 | const TargetInfo &TI = Context.getTargetInfo(); |
| 3936 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 3937 | if (A != TargetInfo::CCCR_OK) { |
| 3938 | if (A == TargetInfo::CCCR_Warning) |
| 3939 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 3940 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 3941 | // This convention is not valid for the target. Use the default function or |
| 3942 | // method calling convention. |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 3943 | bool IsCXXMethod = false, IsVariadic = false; |
| 3944 | if (FD) { |
| 3945 | IsCXXMethod = FD->isCXXInstanceMember(); |
| 3946 | IsVariadic = FD->isVariadic(); |
| 3947 | } |
| 3948 | CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 3949 | } |
| 3950 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 3951 | attr.setProcessingCache((unsigned) CC); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3952 | return false; |
| 3953 | } |
| 3954 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 3955 | /// Pointer-like types in the default address space. |
| 3956 | static bool isValidSwiftContextType(QualType type) { |
| 3957 | if (!type->hasPointerRepresentation()) |
| 3958 | return type->isDependentType(); |
| 3959 | return type->getPointeeType().getAddressSpace() == 0; |
| 3960 | } |
| 3961 | |
| 3962 | /// Pointers and references in the default address space. |
| 3963 | static bool isValidSwiftIndirectResultType(QualType type) { |
| 3964 | if (auto ptrType = type->getAs<PointerType>()) { |
| 3965 | type = ptrType->getPointeeType(); |
| 3966 | } else if (auto refType = type->getAs<ReferenceType>()) { |
| 3967 | type = refType->getPointeeType(); |
| 3968 | } else { |
| 3969 | return type->isDependentType(); |
| 3970 | } |
| 3971 | return type.getAddressSpace() == 0; |
| 3972 | } |
| 3973 | |
| 3974 | /// Pointers and references to pointers in the default address space. |
| 3975 | static bool isValidSwiftErrorResultType(QualType type) { |
| 3976 | if (auto ptrType = type->getAs<PointerType>()) { |
| 3977 | type = ptrType->getPointeeType(); |
| 3978 | } else if (auto refType = type->getAs<ReferenceType>()) { |
| 3979 | type = refType->getPointeeType(); |
| 3980 | } else { |
| 3981 | return type->isDependentType(); |
| 3982 | } |
| 3983 | if (!type.getQualifiers().empty()) |
| 3984 | return false; |
| 3985 | return isValidSwiftContextType(type); |
| 3986 | } |
| 3987 | |
| 3988 | static void handleParameterABIAttr(Sema &S, Decl *D, const AttributeList &attr, |
| 3989 | ParameterABI abi) { |
| 3990 | S.AddParameterABIAttr(attr.getRange(), D, abi, |
| 3991 | attr.getAttributeSpellingListIndex()); |
| 3992 | } |
| 3993 | |
| 3994 | void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi, |
| 3995 | unsigned spellingIndex) { |
| 3996 | |
| 3997 | QualType type = cast<ParmVarDecl>(D)->getType(); |
| 3998 | |
| 3999 | if (auto existingAttr = D->getAttr<ParameterABIAttr>()) { |
| 4000 | if (existingAttr->getABI() != abi) { |
| 4001 | Diag(range.getBegin(), diag::err_attributes_are_not_compatible) |
| 4002 | << getParameterABISpelling(abi) << existingAttr; |
| 4003 | Diag(existingAttr->getLocation(), diag::note_conflicting_attribute); |
| 4004 | return; |
| 4005 | } |
| 4006 | } |
| 4007 | |
| 4008 | switch (abi) { |
| 4009 | case ParameterABI::Ordinary: |
| 4010 | llvm_unreachable("explicit attribute for ordinary parameter ABI?"); |
| 4011 | |
| 4012 | case ParameterABI::SwiftContext: |
| 4013 | if (!isValidSwiftContextType(type)) { |
| 4014 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4015 | << getParameterABISpelling(abi) |
| 4016 | << /*pointer to pointer */ 0 << type; |
| 4017 | } |
| 4018 | D->addAttr(::new (Context) |
| 4019 | SwiftContextAttr(range, Context, spellingIndex)); |
| 4020 | return; |
| 4021 | |
| 4022 | case ParameterABI::SwiftErrorResult: |
| 4023 | if (!isValidSwiftErrorResultType(type)) { |
| 4024 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4025 | << getParameterABISpelling(abi) |
| 4026 | << /*pointer to pointer */ 1 << type; |
| 4027 | } |
| 4028 | D->addAttr(::new (Context) |
| 4029 | SwiftErrorResultAttr(range, Context, spellingIndex)); |
| 4030 | return; |
| 4031 | |
| 4032 | case ParameterABI::SwiftIndirectResult: |
| 4033 | if (!isValidSwiftIndirectResultType(type)) { |
| 4034 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4035 | << getParameterABISpelling(abi) |
| 4036 | << /*pointer*/ 0 << type; |
| 4037 | } |
| 4038 | D->addAttr(::new (Context) |
| 4039 | SwiftIndirectResultAttr(range, Context, spellingIndex)); |
| 4040 | return; |
| 4041 | } |
| 4042 | llvm_unreachable("bad parameter ABI attribute"); |
| 4043 | } |
| 4044 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4045 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4046 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4047 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 4048 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4049 | return true; |
| 4050 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 4051 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4052 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4053 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4054 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4055 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4056 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4057 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4058 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4059 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4060 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4061 | } |
| 4062 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4063 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4064 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4065 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4066 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4067 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4068 | } |
| 4069 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4070 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4071 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4072 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4073 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4074 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4075 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4076 | } |
| 4077 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4078 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4079 | } |
| 4080 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4081 | // Checks whether an argument of launch_bounds attribute is |
| 4082 | // acceptable, performs implicit conversion to Rvalue, and returns |
| 4083 | // non-nullptr Expr result on success. Otherwise, it returns nullptr |
| 4084 | // and may output an error. |
| 4085 | static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E, |
| 4086 | const CUDALaunchBoundsAttr &Attr, |
| 4087 | const unsigned Idx) { |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4088 | if (S.DiagnoseUnexpandedParameterPack(E)) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4089 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4090 | |
| 4091 | // Accept template arguments for now as they depend on something else. |
| 4092 | // We'll get to check them when they eventually get instantiated. |
| 4093 | if (E->isValueDependent()) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4094 | return E; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4095 | |
| 4096 | llvm::APSInt I(64); |
| 4097 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 4098 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
| 4099 | << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4100 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4101 | } |
| 4102 | // Make sure we can fit it in 32 bits. |
| 4103 | if (!I.isIntN(32)) { |
| 4104 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 4105 | << 32 << /* Unsigned */ 1; |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4106 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4107 | } |
| 4108 | if (I < 0) |
| 4109 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
| 4110 | << &Attr << Idx << E->getSourceRange(); |
| 4111 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4112 | // We may need to perform implicit conversion of the argument. |
| 4113 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 4114 | S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false); |
| 4115 | ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E); |
| 4116 | assert(!ValArg.isInvalid() && |
| 4117 | "Unexpected PerformCopyInitialization() failure."); |
| 4118 | |
| 4119 | return ValArg.getAs<Expr>(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads, |
| 4123 | Expr *MinBlocks, unsigned SpellingListIndex) { |
| 4124 | CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks, |
| 4125 | SpellingListIndex); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4126 | MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0); |
| 4127 | if (MaxThreads == nullptr) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4128 | return; |
| 4129 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4130 | if (MinBlocks) { |
| 4131 | MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1); |
| 4132 | if (MinBlocks == nullptr) |
| 4133 | return; |
| 4134 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4135 | |
| 4136 | D->addAttr(::new (Context) CUDALaunchBoundsAttr( |
| 4137 | AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex)); |
| 4138 | } |
| 4139 | |
| 4140 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 4141 | const AttributeList &Attr) { |
| 4142 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1) || |
| 4143 | !checkAttributeAtMostNumArgs(S, Attr, 2)) |
| 4144 | return; |
| 4145 | |
| 4146 | S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 4147 | Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr, |
| 4148 | Attr.getAttributeSpellingListIndex()); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4151 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4152 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4153 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4154 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4155 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4156 | return; |
| 4157 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4158 | |
| 4159 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4160 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4161 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4162 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4163 | |
| 4164 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4165 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4166 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4167 | return; |
| 4168 | } |
| 4169 | |
| 4170 | uint64_t ArgumentIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4171 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 4172 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4173 | return; |
| 4174 | |
| 4175 | uint64_t TypeTagIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4176 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 4177 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4178 | return; |
| 4179 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 4180 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4181 | if (IsPointer) { |
| 4182 | // Ensure that buffer has a pointer type. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4183 | QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4184 | if (!BufferTy->isPointerType()) { |
| 4185 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 4186 | << Attr.getName() << 0; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4187 | } |
| 4188 | } |
| 4189 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4190 | D->addAttr(::new (S.Context) |
| 4191 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4192 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4193 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4197 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4198 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4199 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4200 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4201 | return; |
| 4202 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4203 | |
| 4204 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4205 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4206 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4207 | if (!isa<VarDecl>(D)) { |
| 4208 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4209 | << Attr.getName() << ExpectedVariable; |
| 4210 | return; |
| 4211 | } |
| 4212 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4213 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4214 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4215 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 4216 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4217 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4218 | D->addAttr(::new (S.Context) |
| 4219 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4220 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4221 | Attr.getLayoutCompatible(), |
| 4222 | Attr.getMustBeNull(), |
| 4223 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4224 | } |
| 4225 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4226 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4227 | // Checker-specific attribute handlers. |
| 4228 | //===----------------------------------------------------------------------===// |
| 4229 | |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4230 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) { |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4231 | return type->isDependentType() || |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4232 | type->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4233 | } |
| 4234 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4235 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4236 | return type->isDependentType() || |
| 4237 | type->isObjCObjectPointerType() || |
| 4238 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4239 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4240 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4241 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4242 | return type->isDependentType() || |
| 4243 | type->isPointerType() || |
| 4244 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4247 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4248 | S.AddNSConsumedAttr(Attr.getRange(), D, Attr.getAttributeSpellingListIndex(), |
| 4249 | Attr.getKind() == AttributeList::AT_NSConsumed, |
| 4250 | /*template instantiation*/ false); |
| 4251 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4252 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4253 | void Sema::AddNSConsumedAttr(SourceRange attrRange, Decl *D, |
| 4254 | unsigned spellingIndex, bool isNSConsumed, |
| 4255 | bool isTemplateInstantiation) { |
| 4256 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
| 4257 | bool typeOK; |
| 4258 | |
| 4259 | if (isNSConsumed) { |
| 4260 | typeOK = isValidSubjectOfNSAttribute(*this, param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4261 | } else { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4262 | typeOK = isValidSubjectOfCFAttribute(*this, param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | if (!typeOK) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4266 | // These attributes are normally just advisory, but in ARC, ns_consumed |
| 4267 | // is significant. Allow non-dependent code to contain inappropriate |
| 4268 | // attributes even in ARC, but require template instantiations to be |
| 4269 | // set up correctly. |
| 4270 | Diag(D->getLocStart(), |
| 4271 | (isTemplateInstantiation && isNSConsumed && |
| 4272 | getLangOpts().ObjCAutoRefCount |
| 4273 | ? diag::err_ns_attribute_wrong_parameter_type |
| 4274 | : diag::warn_ns_attribute_wrong_parameter_type)) |
| 4275 | << attrRange |
| 4276 | << (isNSConsumed ? "ns_consumed" : "cf_consumed") |
| 4277 | << (isNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4278 | return; |
| 4279 | } |
| 4280 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4281 | if (isNSConsumed) |
| 4282 | param->addAttr(::new (Context) |
| 4283 | NSConsumedAttr(attrRange, Context, spellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4284 | else |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4285 | param->addAttr(::new (Context) |
| 4286 | CFConsumedAttr(attrRange, Context, spellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4289 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4290 | const AttributeList &Attr) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4291 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4292 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4293 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4294 | returnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4295 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4296 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4297 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4298 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4299 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4300 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4301 | returnType = FD->getReturnType(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4302 | else if (auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 4303 | returnType = Param->getType()->getPointeeType(); |
| 4304 | if (returnType.isNull()) { |
| 4305 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4306 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4307 | << Attr.getRange(); |
| 4308 | return; |
| 4309 | } |
| 4310 | } else { |
| 4311 | AttributeDeclKind ExpectedDeclKind; |
| 4312 | switch (Attr.getKind()) { |
| 4313 | default: llvm_unreachable("invalid ownership attribute"); |
| 4314 | case AttributeList::AT_NSReturnsRetained: |
| 4315 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4316 | case AttributeList::AT_NSReturnsNotRetained: |
| 4317 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 4318 | break; |
| 4319 | |
| 4320 | case AttributeList::AT_CFReturnsRetained: |
| 4321 | case AttributeList::AT_CFReturnsNotRetained: |
| 4322 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 4323 | break; |
| 4324 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4325 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4326 | << Attr.getRange() << Attr.getName() << ExpectedDeclKind; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4327 | return; |
| 4328 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4329 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4330 | bool typeOK; |
| 4331 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4332 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4333 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4334 | case AttributeList::AT_NSReturnsRetained: |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4335 | typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4336 | cf = false; |
| 4337 | break; |
| 4338 | |
| 4339 | case AttributeList::AT_NSReturnsAutoreleased: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4340 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4341 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4342 | cf = false; |
| 4343 | break; |
| 4344 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4345 | case AttributeList::AT_CFReturnsRetained: |
| 4346 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4347 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4348 | cf = true; |
| 4349 | break; |
| 4350 | } |
| 4351 | |
| 4352 | if (!typeOK) { |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4353 | if (isa<ParmVarDecl>(D)) { |
| 4354 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4355 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4356 | << Attr.getRange(); |
| 4357 | } else { |
| 4358 | // Needs to be kept in sync with warn_ns_attribute_wrong_return_type. |
| 4359 | enum : unsigned { |
| 4360 | Function, |
| 4361 | Method, |
| 4362 | Property |
| 4363 | } SubjectKind = Function; |
| 4364 | if (isa<ObjCMethodDecl>(D)) |
| 4365 | SubjectKind = Method; |
| 4366 | else if (isa<ObjCPropertyDecl>(D)) |
| 4367 | SubjectKind = Property; |
| 4368 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4369 | << Attr.getName() << SubjectKind << cf |
| 4370 | << Attr.getRange(); |
| 4371 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4372 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4373 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4374 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4375 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4376 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4377 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4378 | case AttributeList::AT_NSReturnsAutoreleased: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4379 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr( |
| 4380 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4381 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4382 | case AttributeList::AT_CFReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4383 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr( |
| 4384 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4385 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4386 | case AttributeList::AT_NSReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4387 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr( |
| 4388 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4389 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4390 | case AttributeList::AT_CFReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4391 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr( |
| 4392 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4393 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4394 | case AttributeList::AT_NSReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4395 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr( |
| 4396 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4397 | return; |
| 4398 | }; |
| 4399 | } |
| 4400 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4401 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4402 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4403 | const int EP_ObjCMethod = 1; |
| 4404 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4405 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4406 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4407 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4408 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4409 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4410 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4411 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4412 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4413 | if (!resultType->isReferenceType() && |
| 4414 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4415 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4416 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4417 | << attr.getName() |
| 4418 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4419 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4420 | |
| 4421 | // Drop the attribute. |
| 4422 | return; |
| 4423 | } |
| 4424 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4425 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
| 4426 | attr.getRange(), S.Context, attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4429 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4430 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4431 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4432 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4433 | DeclContext *DC = method->getDeclContext(); |
| 4434 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4435 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4436 | << attr.getName() << 0; |
| 4437 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4438 | return; |
| 4439 | } |
| 4440 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4441 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4442 | << attr.getName() << 1; |
| 4443 | return; |
| 4444 | } |
| 4445 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4446 | method->addAttr(::new (S.Context) |
| 4447 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4448 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4449 | } |
| 4450 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4451 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 4452 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4453 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(), |
| 4454 | Attr.getName())) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4455 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4456 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4457 | D->addAttr(::new (S.Context) |
| 4458 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 4459 | Attr.getAttributeSpellingListIndex())); |
| 4460 | } |
| 4461 | |
| 4462 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 4463 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4464 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(), |
| 4465 | Attr.getName())) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4466 | return; |
| 4467 | |
| 4468 | D->addAttr(::new (S.Context) |
| 4469 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 4470 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4471 | } |
| 4472 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4473 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 4474 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4475 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4476 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4477 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4478 | 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] | 4479 | return; |
| 4480 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4481 | |
| 4482 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
| 4483 | if (auto TD = dyn_cast<TypedefNameDecl>(D)) { |
| 4484 | if (!Parm->Ident->isStr("id")) { |
| 4485 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id) |
| 4486 | << Attr.getName(); |
| 4487 | return; |
| 4488 | } |
| 4489 | |
| 4490 | // Only allow 'cv void *'. |
| 4491 | QualType T = TD->getUnderlyingType(); |
| 4492 | if (!T->isVoidPointerType()) { |
| 4493 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
| 4494 | return; |
| 4495 | } |
| 4496 | } |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4497 | |
| 4498 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4499 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4500 | Attr.getAttributeSpellingListIndex())); |
| 4501 | } |
| 4502 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4503 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 4504 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4505 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
| 4506 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4507 | if (!Parm) { |
| 4508 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4509 | return; |
| 4510 | } |
| 4511 | |
| 4512 | D->addAttr(::new (S.Context) |
| 4513 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 4514 | Attr.getAttributeSpellingListIndex())); |
| 4515 | } |
| 4516 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4517 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4518 | const AttributeList &Attr) { |
| 4519 | IdentifierInfo *RelatedClass = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4520 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4521 | if (!RelatedClass) { |
| 4522 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4523 | return; |
| 4524 | } |
| 4525 | IdentifierInfo *ClassMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4526 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4527 | IdentifierInfo *InstanceMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4528 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4529 | D->addAttr(::new (S.Context) |
| 4530 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 4531 | ClassMethod, InstanceMethod, |
| 4532 | Attr.getAttributeSpellingListIndex())); |
| 4533 | } |
| 4534 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4535 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 4536 | const AttributeList &Attr) { |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4537 | ObjCInterfaceDecl *IFace; |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4538 | if (ObjCCategoryDecl *CatDecl = |
| 4539 | dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4540 | IFace = CatDecl->getClassInterface(); |
| 4541 | else |
| 4542 | IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 4543 | |
| 4544 | if (!IFace) |
| 4545 | return; |
| 4546 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 4547 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 4548 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4549 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 4550 | Attr.getAttributeSpellingListIndex())); |
| 4551 | } |
| 4552 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4553 | static void handleObjCRuntimeName(Sema &S, Decl *D, |
| 4554 | const AttributeList &Attr) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 4555 | StringRef MetaDataName; |
| 4556 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName)) |
| 4557 | return; |
| 4558 | D->addAttr(::new (S.Context) |
| 4559 | ObjCRuntimeNameAttr(Attr.getRange(), S.Context, |
| 4560 | MetaDataName, |
| 4561 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 4564 | // When a user wants to use objc_boxable with a union or struct |
| 4565 | // but they don't have access to the declaration (legacy/third-party code) |
| 4566 | // then they can 'enable' this feature with a typedef: |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4567 | // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct; |
| 4568 | static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4569 | bool notify = false; |
| 4570 | |
| 4571 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4572 | if (RD && RD->getDefinition()) { |
| 4573 | RD = RD->getDefinition(); |
| 4574 | notify = true; |
| 4575 | } |
| 4576 | |
| 4577 | if (RD) { |
| 4578 | ObjCBoxableAttr *BoxableAttr = ::new (S.Context) |
| 4579 | ObjCBoxableAttr(Attr.getRange(), S.Context, |
| 4580 | Attr.getAttributeSpellingListIndex()); |
| 4581 | RD->addAttr(BoxableAttr); |
| 4582 | if (notify) { |
| 4583 | // we need to notify ASTReader/ASTWriter about |
| 4584 | // modification of existing declaration |
| 4585 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 4586 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 4587 | } |
| 4588 | } |
| 4589 | } |
| 4590 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4591 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4592 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4593 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4594 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4595 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4596 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4599 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4600 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4601 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4602 | QualType type = vd->getType(); |
| 4603 | |
| 4604 | if (!type->isDependentType() && |
| 4605 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4606 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4607 | << type; |
| 4608 | return; |
| 4609 | } |
| 4610 | |
| 4611 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4612 | |
| 4613 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4614 | // going to infer. |
| 4615 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4616 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4617 | |
| 4618 | switch (lifetime) { |
| 4619 | case Qualifiers::OCL_None: |
| 4620 | assert(type->isDependentType() && |
| 4621 | "didn't infer lifetime for non-dependent type?"); |
| 4622 | break; |
| 4623 | |
| 4624 | case Qualifiers::OCL_Weak: // meaningful |
| 4625 | case Qualifiers::OCL_Strong: // meaningful |
| 4626 | break; |
| 4627 | |
| 4628 | case Qualifiers::OCL_ExplicitNone: |
| 4629 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4630 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4631 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4632 | break; |
| 4633 | } |
| 4634 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4635 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4636 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4637 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4638 | } |
| 4639 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4640 | //===----------------------------------------------------------------------===// |
| 4641 | // Microsoft specific attribute handlers. |
| 4642 | //===----------------------------------------------------------------------===// |
| 4643 | |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 4644 | UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range, |
| 4645 | unsigned AttrSpellingListIndex, StringRef Uuid) { |
| 4646 | if (const auto *UA = D->getAttr<UuidAttr>()) { |
Nico Weber | d58c260 | 2016-09-14 01:16:54 +0000 | [diff] [blame] | 4647 | if (UA->getGuid().equals_lower(Uuid)) |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 4648 | return nullptr; |
| 4649 | Diag(UA->getLocation(), diag::err_mismatched_uuid); |
| 4650 | Diag(Range.getBegin(), diag::note_previous_uuid); |
| 4651 | D->dropAttr<UuidAttr>(); |
| 4652 | } |
| 4653 | |
| 4654 | return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex); |
| 4655 | } |
| 4656 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4657 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 4658 | if (!S.LangOpts.CPlusPlus) { |
| 4659 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4660 | << Attr.getName() << AttributeLangSupport::C; |
| 4661 | return; |
| 4662 | } |
| 4663 | |
Aaron Ballman | 60e705e | 2013-11-24 20:58:02 +0000 | [diff] [blame] | 4664 | if (!isa<CXXRecordDecl>(D)) { |
| 4665 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4666 | << Attr.getName() << ExpectedClass; |
| 4667 | return; |
| 4668 | } |
| 4669 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4670 | StringRef StrRef; |
| 4671 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4672 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4673 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4674 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4675 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4676 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4677 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4678 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4679 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4680 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4681 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4682 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4683 | return; |
| 4684 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4685 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4686 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4687 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4688 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4689 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4690 | return; |
| 4691 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4692 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4693 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4694 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4695 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4696 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4697 | |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 4698 | UuidAttr *UA = S.mergeUuidAttr(D, Attr.getRange(), |
| 4699 | Attr.getAttributeSpellingListIndex(), StrRef); |
| 4700 | if (UA) |
| 4701 | D->addAttr(UA); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4702 | } |
| 4703 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4704 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4705 | if (!S.LangOpts.CPlusPlus) { |
| 4706 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4707 | << Attr.getName() << AttributeLangSupport::C; |
| 4708 | return; |
| 4709 | } |
| 4710 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4711 | D, Attr.getRange(), /*BestCase=*/true, |
| 4712 | Attr.getAttributeSpellingListIndex(), |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4713 | (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling()); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 4714 | if (IA) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4715 | D->addAttr(IA); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 4716 | S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D)); |
| 4717 | } |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 4720 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, |
| 4721 | const AttributeList &Attr) { |
| 4722 | VarDecl *VD = cast<VarDecl>(D); |
| 4723 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
| 4724 | S.Diag(Attr.getLoc(), diag::err_thread_unsupported); |
| 4725 | return; |
| 4726 | } |
| 4727 | if (VD->getTSCSpec() != TSCS_unspecified) { |
| 4728 | S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable); |
| 4729 | return; |
| 4730 | } |
| 4731 | if (VD->hasLocalStorage()) { |
| 4732 | S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
| 4733 | return; |
| 4734 | } |
| 4735 | VD->addAttr(::new (S.Context) ThreadAttr( |
| 4736 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4737 | } |
| 4738 | |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 4739 | static void handleAbiTagAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4740 | SmallVector<StringRef, 4> Tags; |
| 4741 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 4742 | StringRef Tag; |
| 4743 | if (!S.checkStringLiteralArgumentAttr(Attr, I, Tag)) |
| 4744 | return; |
| 4745 | Tags.push_back(Tag); |
| 4746 | } |
| 4747 | |
| 4748 | if (const auto *NS = dyn_cast<NamespaceDecl>(D)) { |
| 4749 | if (!NS->isInline()) { |
| 4750 | S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 0; |
| 4751 | return; |
| 4752 | } |
| 4753 | if (NS->isAnonymousNamespace()) { |
| 4754 | S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 1; |
| 4755 | return; |
| 4756 | } |
| 4757 | if (Attr.getNumArgs() == 0) |
| 4758 | Tags.push_back(NS->getName()); |
| 4759 | } else if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 4760 | return; |
| 4761 | |
| 4762 | // Store tags sorted and without duplicates. |
| 4763 | std::sort(Tags.begin(), Tags.end()); |
| 4764 | Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end()); |
| 4765 | |
| 4766 | D->addAttr(::new (S.Context) |
| 4767 | AbiTagAttr(Attr.getRange(), S.Context, Tags.data(), Tags.size(), |
| 4768 | Attr.getAttributeSpellingListIndex())); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 4769 | } |
| 4770 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4771 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 4772 | const AttributeList &Attr) { |
| 4773 | // Check the attribute arguments. |
| 4774 | if (Attr.getNumArgs() > 1) { |
| 4775 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 4776 | << Attr.getName() << 1; |
| 4777 | return; |
| 4778 | } |
| 4779 | |
| 4780 | StringRef Str; |
| 4781 | SourceLocation ArgLoc; |
| 4782 | |
| 4783 | if (Attr.getNumArgs() == 0) |
| 4784 | Str = ""; |
| 4785 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 4786 | return; |
| 4787 | |
| 4788 | ARMInterruptAttr::InterruptType Kind; |
| 4789 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 4790 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 4791 | << Attr.getName() << Str << ArgLoc; |
| 4792 | return; |
| 4793 | } |
| 4794 | |
| 4795 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 4796 | D->addAttr(::new (S.Context) |
| 4797 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 4798 | } |
| 4799 | |
| 4800 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 4801 | const AttributeList &Attr) { |
| 4802 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4803 | return; |
| 4804 | |
| 4805 | if (!Attr.isArgExpr(0)) { |
| 4806 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 4807 | << AANT_ArgumentIntegerConstant; |
| 4808 | return; |
| 4809 | } |
| 4810 | |
| 4811 | // FIXME: Check for decl - it should be void ()(void). |
| 4812 | |
| 4813 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4814 | llvm::APSInt NumParams(32); |
| 4815 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 4816 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4817 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 4818 | << NumParamsExpr->getSourceRange(); |
| 4819 | return; |
| 4820 | } |
| 4821 | |
| 4822 | unsigned Num = NumParams.getLimitedValue(255); |
| 4823 | if ((Num & 1) || Num > 30) { |
| 4824 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 4825 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 4826 | << NumParamsExpr->getSourceRange(); |
| 4827 | return; |
| 4828 | } |
| 4829 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4830 | D->addAttr(::new (S.Context) |
| 4831 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 4832 | Attr.getAttributeSpellingListIndex())); |
| 4833 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4834 | } |
| 4835 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 4836 | static void handleMipsInterruptAttr(Sema &S, Decl *D, |
| 4837 | const AttributeList &Attr) { |
| 4838 | // Only one optional argument permitted. |
| 4839 | if (Attr.getNumArgs() > 1) { |
| 4840 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 4841 | << Attr.getName() << 1; |
| 4842 | return; |
| 4843 | } |
| 4844 | |
| 4845 | StringRef Str; |
| 4846 | SourceLocation ArgLoc; |
| 4847 | |
| 4848 | if (Attr.getNumArgs() == 0) |
| 4849 | Str = ""; |
| 4850 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 4851 | return; |
| 4852 | |
| 4853 | // Semantic checks for a function with the 'interrupt' attribute for MIPS: |
| 4854 | // a) Must be a function. |
| 4855 | // b) Must have no parameters. |
| 4856 | // c) Must have the 'void' return type. |
| 4857 | // d) Cannot have the 'mips16' attribute, as that instruction set |
| 4858 | // lacks the 'eret' instruction. |
| 4859 | // e) The attribute itself must either have no argument or one of the |
| 4860 | // valid interrupt types, see [MipsInterruptDocs]. |
| 4861 | |
| 4862 | if (!isFunctionOrMethod(D)) { |
| 4863 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 4864 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 4865 | return; |
| 4866 | } |
| 4867 | |
| 4868 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 4869 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 4870 | << 0; |
| 4871 | return; |
| 4872 | } |
| 4873 | |
| 4874 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 4875 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 4876 | << 1; |
| 4877 | return; |
| 4878 | } |
| 4879 | |
| 4880 | if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(), |
| 4881 | Attr.getName())) |
| 4882 | return; |
| 4883 | |
| 4884 | MipsInterruptAttr::InterruptType Kind; |
| 4885 | if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 4886 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 4887 | << Attr.getName() << "'" + std::string(Str) + "'"; |
| 4888 | return; |
| 4889 | } |
| 4890 | |
| 4891 | D->addAttr(::new (S.Context) MipsInterruptAttr( |
| 4892 | Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex())); |
| 4893 | } |
| 4894 | |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 4895 | static void handleAnyX86InterruptAttr(Sema &S, Decl *D, |
| 4896 | const AttributeList &Attr) { |
| 4897 | // Semantic checks for a function with the 'interrupt' attribute. |
| 4898 | // a) Must be a function. |
| 4899 | // b) Must have the 'void' return type. |
| 4900 | // c) Must take 1 or 2 arguments. |
| 4901 | // d) The 1st argument must be a pointer. |
| 4902 | // e) The 2nd argument (if any) must be an unsigned integer. |
| 4903 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) || |
| 4904 | CXXMethodDecl::isStaticOverloadedOperator( |
| 4905 | cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) { |
| 4906 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4907 | << Attr.getName() << ExpectedFunctionWithProtoType; |
| 4908 | return; |
| 4909 | } |
| 4910 | // Interrupt handler must have void return type. |
| 4911 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 4912 | S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(), |
| 4913 | diag::err_anyx86_interrupt_attribute) |
| 4914 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 4915 | ? 0 |
| 4916 | : 1) |
| 4917 | << 0; |
| 4918 | return; |
| 4919 | } |
| 4920 | // Interrupt handler must have 1 or 2 parameters. |
| 4921 | unsigned NumParams = getFunctionOrMethodNumParams(D); |
| 4922 | if (NumParams < 1 || NumParams > 2) { |
| 4923 | S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute) |
| 4924 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 4925 | ? 0 |
| 4926 | : 1) |
| 4927 | << 1; |
| 4928 | return; |
| 4929 | } |
| 4930 | // The first argument must be a pointer. |
| 4931 | if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) { |
| 4932 | S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(), |
| 4933 | diag::err_anyx86_interrupt_attribute) |
| 4934 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 4935 | ? 0 |
| 4936 | : 1) |
| 4937 | << 2; |
| 4938 | return; |
| 4939 | } |
| 4940 | // The second argument, if present, must be an unsigned integer. |
| 4941 | unsigned TypeSize = |
| 4942 | S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64 |
| 4943 | ? 64 |
| 4944 | : 32; |
| 4945 | if (NumParams == 2 && |
| 4946 | (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() || |
| 4947 | S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) { |
| 4948 | S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(), |
| 4949 | diag::err_anyx86_interrupt_attribute) |
| 4950 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 4951 | ? 0 |
| 4952 | : 1) |
| 4953 | << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false); |
| 4954 | return; |
| 4955 | } |
| 4956 | D->addAttr(::new (S.Context) AnyX86InterruptAttr( |
| 4957 | Attr.getLoc(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4958 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
| 4959 | } |
| 4960 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4961 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4962 | // Dispatch the interrupt attribute based on the current target. |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 4963 | switch (S.Context.getTargetInfo().getTriple().getArch()) { |
| 4964 | case llvm::Triple::msp430: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4965 | handleMSP430InterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 4966 | break; |
| 4967 | case llvm::Triple::mipsel: |
| 4968 | case llvm::Triple::mips: |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 4969 | handleMipsInterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 4970 | break; |
| 4971 | case llvm::Triple::x86: |
| 4972 | case llvm::Triple::x86_64: |
| 4973 | handleAnyX86InterruptAttr(S, D, Attr); |
| 4974 | break; |
| 4975 | default: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4976 | handleARMInterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 4977 | break; |
| 4978 | } |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4979 | } |
| 4980 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 4981 | static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D, |
| 4982 | const AttributeList &Attr) { |
| 4983 | uint32_t Min = 0; |
| 4984 | Expr *MinExpr = Attr.getArgAsExpr(0); |
| 4985 | if (!checkUInt32Argument(S, Attr, MinExpr, Min)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 4986 | return; |
| 4987 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 4988 | uint32_t Max = 0; |
| 4989 | Expr *MaxExpr = Attr.getArgAsExpr(1); |
| 4990 | if (!checkUInt32Argument(S, Attr, MaxExpr, Max)) |
| 4991 | return; |
| 4992 | |
| 4993 | if (Min == 0 && Max != 0) { |
| 4994 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 4995 | << Attr.getName() << 0; |
| 4996 | return; |
| 4997 | } |
| 4998 | if (Min > Max) { |
| 4999 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5000 | << Attr.getName() << 1; |
| 5001 | return; |
| 5002 | } |
| 5003 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5004 | D->addAttr(::new (S.Context) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5005 | AMDGPUFlatWorkGroupSizeAttr(Attr.getLoc(), S.Context, Min, Max, |
| 5006 | Attr.getAttributeSpellingListIndex())); |
| 5007 | } |
| 5008 | |
| 5009 | static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, |
| 5010 | const AttributeList &Attr) { |
| 5011 | uint32_t Min = 0; |
| 5012 | Expr *MinExpr = Attr.getArgAsExpr(0); |
| 5013 | if (!checkUInt32Argument(S, Attr, MinExpr, Min)) |
| 5014 | return; |
| 5015 | |
| 5016 | uint32_t Max = 0; |
| 5017 | if (Attr.getNumArgs() == 2) { |
| 5018 | Expr *MaxExpr = Attr.getArgAsExpr(1); |
| 5019 | if (!checkUInt32Argument(S, Attr, MaxExpr, Max)) |
| 5020 | return; |
| 5021 | } |
| 5022 | |
| 5023 | if (Min == 0 && Max != 0) { |
| 5024 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5025 | << Attr.getName() << 0; |
| 5026 | return; |
| 5027 | } |
| 5028 | if (Max != 0 && Min > Max) { |
| 5029 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5030 | << Attr.getName() << 1; |
| 5031 | return; |
| 5032 | } |
| 5033 | |
| 5034 | D->addAttr(::new (S.Context) |
| 5035 | AMDGPUWavesPerEUAttr(Attr.getLoc(), S.Context, Min, Max, |
| 5036 | Attr.getAttributeSpellingListIndex())); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5037 | } |
| 5038 | |
| 5039 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, |
| 5040 | const AttributeList &Attr) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5041 | uint32_t NumSGPR = 0; |
| 5042 | Expr *NumSGPRExpr = Attr.getArgAsExpr(0); |
| 5043 | if (!checkUInt32Argument(S, Attr, NumSGPRExpr, NumSGPR)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5044 | return; |
| 5045 | |
| 5046 | D->addAttr(::new (S.Context) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5047 | AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context, NumSGPR, |
| 5048 | Attr.getAttributeSpellingListIndex())); |
| 5049 | } |
| 5050 | |
| 5051 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, |
| 5052 | const AttributeList &Attr) { |
| 5053 | uint32_t NumVGPR = 0; |
| 5054 | Expr *NumVGPRExpr = Attr.getArgAsExpr(0); |
| 5055 | if (!checkUInt32Argument(S, Attr, NumVGPRExpr, NumVGPR)) |
| 5056 | return; |
| 5057 | |
| 5058 | D->addAttr(::new (S.Context) |
| 5059 | AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context, NumVGPR, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5060 | Attr.getAttributeSpellingListIndex())); |
| 5061 | } |
| 5062 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5063 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 5064 | const AttributeList& Attr) { |
| 5065 | // If we try to apply it to a function pointer, don't warn, but don't |
| 5066 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 5067 | // special about calling a force_align_arg_pointer function. |
| 5068 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 5069 | if (VD && VD->getType()->isFunctionPointerType()) |
| 5070 | return; |
| 5071 | // Also don't warn on function pointer typedefs. |
| 5072 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 5073 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 5074 | TD->getUnderlyingType()->isFunctionType())) |
| 5075 | return; |
| 5076 | // Attribute can only be applied to function types. |
| 5077 | if (!isa<FunctionDecl>(D)) { |
| 5078 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 5079 | << Attr.getName() << /* function */0; |
| 5080 | return; |
| 5081 | } |
| 5082 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5083 | D->addAttr(::new (S.Context) |
| 5084 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 5085 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5088 | static void handleLayoutVersion(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5089 | uint32_t Version; |
| 5090 | Expr *VersionExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 5091 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), Version)) |
| 5092 | return; |
| 5093 | |
| 5094 | // TODO: Investigate what happens with the next major version of MSVC. |
| 5095 | if (Version != LangOptions::MSVC2015) { |
| 5096 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 5097 | << Attr.getName() << Version << VersionExpr->getSourceRange(); |
| 5098 | return; |
| 5099 | } |
| 5100 | |
| 5101 | D->addAttr(::new (S.Context) |
| 5102 | LayoutVersionAttr(Attr.getRange(), S.Context, Version, |
| 5103 | Attr.getAttributeSpellingListIndex())); |
| 5104 | } |
| 5105 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5106 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 5107 | unsigned AttrSpellingListIndex) { |
| 5108 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5109 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5110 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
| 5113 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5114 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5115 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5116 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5117 | } |
| 5118 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5119 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 5120 | unsigned AttrSpellingListIndex) { |
| 5121 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5122 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5123 | D->dropAttr<DLLImportAttr>(); |
| 5124 | } |
| 5125 | |
| 5126 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5127 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5128 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5129 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5130 | } |
| 5131 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5132 | static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 5133 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 5134 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5135 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) |
| 5136 | << A.getName(); |
| 5137 | return; |
| 5138 | } |
| 5139 | |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 5140 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5141 | if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport && |
| 5142 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5143 | // MinGW doesn't allow dllimport on inline functions. |
| 5144 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
| 5145 | << A.getName(); |
| 5146 | return; |
| 5147 | } |
| 5148 | } |
| 5149 | |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 5150 | if (auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 5151 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 5152 | MD->getParent()->isLambda()) { |
| 5153 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName(); |
| 5154 | return; |
| 5155 | } |
| 5156 | } |
| 5157 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5158 | unsigned Index = A.getAttributeSpellingListIndex(); |
| 5159 | Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport |
| 5160 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 5161 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5162 | if (NewAttr) |
| 5163 | D->addAttr(NewAttr); |
| 5164 | } |
| 5165 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5166 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5167 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5168 | unsigned AttrSpellingListIndex, |
| 5169 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 5170 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 5171 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5172 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5173 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 5174 | << 1 /*previous declaration*/; |
| 5175 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 5176 | D->dropAttr<MSInheritanceAttr>(); |
| 5177 | } |
| 5178 | |
| 5179 | CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 5180 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5181 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 5182 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5183 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5184 | } |
| 5185 | } else { |
| 5186 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 5187 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5188 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5189 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5190 | } |
| 5191 | if (RD->getDescribedClassTemplate()) { |
| 5192 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5193 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5194 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5195 | } |
| 5196 | } |
| 5197 | |
| 5198 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5199 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5200 | } |
| 5201 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5202 | static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5203 | // The capability attributes take a single string parameter for the name of |
| 5204 | // the capability they represent. The lockable attribute does not take any |
| 5205 | // parameters. However, semantically, both attributes represent the same |
| 5206 | // concept, and so they use the same semantic attribute. Eventually, the |
| 5207 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5208 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 5209 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5210 | // literal will be considered a "mutex." |
| 5211 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5212 | SourceLocation LiteralLoc; |
| 5213 | if (Attr.getKind() == AttributeList::AT_Capability && |
| 5214 | !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc)) |
| 5215 | return; |
| 5216 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5217 | // Currently, there are only two names allowed for a capability: role and |
| 5218 | // mutex (case insensitive). Diagnose other capability names. |
| 5219 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 5220 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 5221 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5222 | D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N, |
| 5223 | Attr.getAttributeSpellingListIndex())); |
| 5224 | } |
| 5225 | |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5226 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, |
| 5227 | const AttributeList &Attr) { |
| 5228 | D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context, |
| 5229 | Attr.getArgAsExpr(0), |
| 5230 | Attr.getAttributeSpellingListIndex())); |
| 5231 | } |
| 5232 | |
| 5233 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
| 5234 | const AttributeList &Attr) { |
| 5235 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5236 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5237 | return; |
| 5238 | |
| 5239 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(), |
| 5240 | S.Context, |
| 5241 | Args.data(), Args.size(), |
| 5242 | Attr.getAttributeSpellingListIndex())); |
| 5243 | } |
| 5244 | |
| 5245 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
| 5246 | const AttributeList &Attr) { |
| 5247 | SmallVector<Expr*, 2> Args; |
| 5248 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 5249 | return; |
| 5250 | |
| 5251 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(), |
| 5252 | S.Context, |
| 5253 | Attr.getArgAsExpr(0), |
| 5254 | Args.data(), |
| 5255 | Args.size(), |
| 5256 | Attr.getAttributeSpellingListIndex())); |
| 5257 | } |
| 5258 | |
| 5259 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
| 5260 | const AttributeList &Attr) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5261 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5262 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 5263 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5264 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5265 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
| 5266 | Attr.getRange(), S.Context, Args.data(), Args.size(), |
| 5267 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5268 | } |
| 5269 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5270 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
| 5271 | const AttributeList &Attr) { |
| 5272 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5273 | return; |
| 5274 | |
| 5275 | // check that all arguments are lockable objects |
| 5276 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 5277 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5278 | if (Args.empty()) |
| 5279 | return; |
| 5280 | |
| 5281 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
| 5282 | RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(), |
| 5283 | Args.size(), Attr.getAttributeSpellingListIndex()); |
| 5284 | |
| 5285 | D->addAttr(RCA); |
| 5286 | } |
| 5287 | |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5288 | static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5289 | if (auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
| 5290 | if (NSD->isAnonymousNamespace()) { |
| 5291 | S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace); |
| 5292 | // Do not want to attach the attribute to the namespace because that will |
| 5293 | // cause confusing diagnostic reports for uses of declarations within the |
| 5294 | // namespace. |
| 5295 | return; |
| 5296 | } |
| 5297 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5298 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 5299 | // Handle the cases where the attribute has a text message. |
| 5300 | StringRef Str, Replacement; |
| 5301 | if (Attr.isArgExpr(0) && Attr.getArgAsExpr(0) && |
| 5302 | !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
| 5303 | return; |
| 5304 | |
| 5305 | // Only support a single optional message for Declspec and CXX11. |
| 5306 | if (Attr.isDeclspecAttribute() || Attr.isCXX11Attribute()) |
| 5307 | checkAttributeAtMostNumArgs(S, Attr, 1); |
| 5308 | else if (Attr.isArgExpr(1) && Attr.getArgAsExpr(1) && |
| 5309 | !S.checkStringLiteralArgumentAttr(Attr, 1, Replacement)) |
| 5310 | return; |
| 5311 | |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5312 | if (!S.getLangOpts().CPlusPlus14) |
| 5313 | if (Attr.isCXX11Attribute() && |
| 5314 | !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu"))) |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5315 | S.Diag(Attr.getLoc(), diag::ext_cxx14_attr) << Attr.getName(); |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5316 | |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5317 | D->addAttr(::new (S.Context) |
| 5318 | DeprecatedAttr(Attr.getRange(), S.Context, Str, Replacement, |
| 5319 | Attr.getAttributeSpellingListIndex())); |
| 5320 | } |
| 5321 | |
| 5322 | static bool isGlobalVar(const Decl *D) { |
| 5323 | if (const auto *S = dyn_cast<VarDecl>(D)) |
| 5324 | return S->hasGlobalStorage(); |
| 5325 | return false; |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5326 | } |
| 5327 | |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5328 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5329 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5330 | return; |
| 5331 | |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 5332 | std::vector<StringRef> Sanitizers; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5333 | |
| 5334 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 5335 | StringRef SanitizerName; |
| 5336 | SourceLocation LiteralLoc; |
| 5337 | |
| 5338 | if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc)) |
| 5339 | return; |
| 5340 | |
| 5341 | if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0) |
| 5342 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5343 | else if (isGlobalVar(D) && SanitizerName != "address") |
| 5344 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5345 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5346 | Sanitizers.push_back(SanitizerName); |
| 5347 | } |
| 5348 | |
| 5349 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
| 5350 | Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(), |
| 5351 | Attr.getAttributeSpellingListIndex())); |
| 5352 | } |
| 5353 | |
| 5354 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
| 5355 | const AttributeList &Attr) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 5356 | StringRef AttrName = Attr.getName()->getName(); |
| 5357 | normalizeName(AttrName); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5358 | StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName) |
| 5359 | .Case("no_address_safety_analysis", "address") |
| 5360 | .Case("no_sanitize_address", "address") |
| 5361 | .Case("no_sanitize_thread", "thread") |
| 5362 | .Case("no_sanitize_memory", "memory"); |
| 5363 | if (isGlobalVar(D) && SanitizerName != "address") |
| 5364 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5365 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5366 | D->addAttr(::new (S.Context) |
| 5367 | NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1, |
| 5368 | Attr.getAttributeSpellingListIndex())); |
| 5369 | } |
| 5370 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 5371 | static void handleInternalLinkageAttr(Sema &S, Decl *D, |
| 5372 | const AttributeList &Attr) { |
| 5373 | if (InternalLinkageAttr *Internal = |
| 5374 | S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(), |
| 5375 | Attr.getAttributeSpellingListIndex())) |
| 5376 | D->addAttr(Internal); |
| 5377 | } |
| 5378 | |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 5379 | static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5380 | if (S.LangOpts.OpenCLVersion != 200) |
| 5381 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_opencl_version) |
| 5382 | << Attr.getName() << "2.0" << 0; |
| 5383 | else |
| 5384 | S.Diag(Attr.getLoc(), diag::warn_opencl_attr_deprecated_ignored) |
| 5385 | << Attr.getName() << "2.0"; |
| 5386 | } |
| 5387 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5388 | /// Handles semantic checking for features that are common to all attributes, |
| 5389 | /// such as checking whether a parameter was properly specified, or the correct |
| 5390 | /// number of arguments were passed, etc. |
| 5391 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 5392 | const AttributeList &Attr) { |
| 5393 | // Several attributes carry different semantics than the parsing requires, so |
| 5394 | // those are opted out of the common handling. |
| 5395 | // |
| 5396 | // We also bail on unknown and ignored attributes because those are handled |
| 5397 | // as part of the target-specific handling logic. |
| 5398 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5399 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5400 | return false; |
| 5401 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5402 | // Check whether the attribute requires specific language extensions to be |
| 5403 | // enabled. |
| 5404 | if (!Attr.diagnoseLangOpts(S)) |
| 5405 | return true; |
| 5406 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5407 | if (Attr.getMinArgs() == Attr.getMaxArgs()) { |
| 5408 | // If there are no optional arguments, then checking for the argument count |
| 5409 | // is trivial. |
| 5410 | if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 5411 | return true; |
| 5412 | } else { |
| 5413 | // There are optional arguments, so checking is slightly more involved. |
| 5414 | if (Attr.getMinArgs() && |
| 5415 | !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs())) |
| 5416 | return true; |
| 5417 | else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() && |
| 5418 | !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs())) |
| 5419 | return true; |
| 5420 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5421 | |
| 5422 | // Check whether the attribute appertains to the given subject. |
| 5423 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 5424 | return true; |
| 5425 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5426 | return false; |
| 5427 | } |
| 5428 | |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5429 | static void handleOpenCLAccessAttr(Sema &S, Decl *D, |
| 5430 | const AttributeList &Attr) { |
| 5431 | if (D->isInvalidDecl()) |
| 5432 | return; |
| 5433 | |
| 5434 | // Check if there is only one access qualifier. |
| 5435 | if (D->hasAttr<OpenCLAccessAttr>()) { |
| 5436 | S.Diag(Attr.getLoc(), diag::err_opencl_multiple_access_qualifiers) |
| 5437 | << D->getSourceRange(); |
| 5438 | D->setInvalidDecl(true); |
| 5439 | return; |
| 5440 | } |
| 5441 | |
| 5442 | // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an |
| 5443 | // image object can be read and written. |
| 5444 | // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe |
| 5445 | // object. Using the read_write (or __read_write) qualifier with the pipe |
| 5446 | // qualifier is a compilation error. |
| 5447 | if (const ParmVarDecl *PDecl = dyn_cast<ParmVarDecl>(D)) { |
| 5448 | const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr(); |
| 5449 | if (Attr.getName()->getName().find("read_write") != StringRef::npos) { |
| 5450 | if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) { |
| 5451 | S.Diag(Attr.getLoc(), diag::err_opencl_invalid_read_write) |
| 5452 | << Attr.getName() << PDecl->getType() << DeclTy->isImageType(); |
| 5453 | D->setInvalidDecl(true); |
| 5454 | return; |
| 5455 | } |
| 5456 | } |
| 5457 | } |
| 5458 | |
| 5459 | D->addAttr(::new (S.Context) OpenCLAccessAttr( |
| 5460 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 5461 | } |
| 5462 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5463 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5464 | // Top Level Sema Entry Points |
| 5465 | //===----------------------------------------------------------------------===// |
| 5466 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5467 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 5468 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 5469 | /// silently ignore it if a GNU attribute. |
| 5470 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 5471 | const AttributeList &Attr, |
| 5472 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5473 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5474 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 5475 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5476 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 5477 | // instead. |
| 5478 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 5479 | return; |
| 5480 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5481 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 5482 | // which do not apply to the current target architecture are treated as |
| 5483 | // though they were unknown attributes. |
| 5484 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 5485 | !Attr.existsInTarget(S.Context.getTargetInfo())) { |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5486 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() |
| 5487 | ? diag::warn_unhandled_ms_attribute_ignored |
| 5488 | : diag::warn_unknown_attribute_ignored) |
| 5489 | << Attr.getName(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5490 | return; |
| 5491 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5492 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5493 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 5494 | return; |
| 5495 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5496 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5497 | default: |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5498 | if (!Attr.isStmtAttr()) { |
| 5499 | // Type attributes are handled elsewhere; silently move on. |
| 5500 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
| 5501 | break; |
| 5502 | } |
| 5503 | S.Diag(Attr.getLoc(), diag::err_stmt_attribute_invalid_on_decl) |
| 5504 | << Attr.getName() << D->getLocation(); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5505 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5506 | case AttributeList::AT_Interrupt: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5507 | handleInterruptAttr(S, D, Attr); |
| 5508 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5509 | case AttributeList::AT_X86ForceAlignArgPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5510 | handleX86ForceAlignArgPointerAttr(S, D, Attr); |
| 5511 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5512 | case AttributeList::AT_DLLExport: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5513 | case AttributeList::AT_DLLImport: |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5514 | handleDLLAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5515 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5516 | case AttributeList::AT_Mips16: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5517 | handleSimpleAttributeWithExclusions<Mips16Attr, MipsInterruptAttr>(S, D, |
| 5518 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5519 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5520 | case AttributeList::AT_NoMips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5521 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); |
| 5522 | break; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5523 | case AttributeList::AT_AMDGPUFlatWorkGroupSize: |
| 5524 | handleAMDGPUFlatWorkGroupSizeAttr(S, D, Attr); |
| 5525 | break; |
| 5526 | case AttributeList::AT_AMDGPUWavesPerEU: |
| 5527 | handleAMDGPUWavesPerEUAttr(S, D, Attr); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5528 | break; |
| 5529 | case AttributeList::AT_AMDGPUNumSGPR: |
| 5530 | handleAMDGPUNumSGPRAttr(S, D, Attr); |
| 5531 | break; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5532 | case AttributeList::AT_AMDGPUNumVGPR: |
| 5533 | handleAMDGPUNumVGPRAttr(S, D, Attr); |
| 5534 | break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 5535 | case AttributeList::AT_IBAction: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5536 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); |
| 5537 | break; |
| 5538 | case AttributeList::AT_IBOutlet: |
| 5539 | handleIBOutlet(S, D, Attr); |
| 5540 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5541 | case AttributeList::AT_IBOutletCollection: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5542 | handleIBOutletCollection(S, D, Attr); |
| 5543 | break; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 5544 | case AttributeList::AT_IFunc: |
| 5545 | handleIFuncAttr(S, D, Attr); |
| 5546 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5547 | case AttributeList::AT_Alias: |
| 5548 | handleAliasAttr(S, D, Attr); |
| 5549 | break; |
| 5550 | case AttributeList::AT_Aligned: |
| 5551 | handleAlignedAttr(S, D, Attr); |
| 5552 | break; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 5553 | case AttributeList::AT_AlignValue: |
| 5554 | handleAlignValueAttr(S, D, Attr); |
| 5555 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5556 | case AttributeList::AT_AlwaysInline: |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 5557 | handleAlwaysInlineAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5558 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5559 | case AttributeList::AT_AnalyzerNoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5560 | handleAnalyzerNoReturnAttr(S, D, Attr); |
| 5561 | break; |
| 5562 | case AttributeList::AT_TLSModel: |
| 5563 | handleTLSModelAttr(S, D, Attr); |
| 5564 | break; |
| 5565 | case AttributeList::AT_Annotate: |
| 5566 | handleAnnotateAttr(S, D, Attr); |
| 5567 | break; |
| 5568 | case AttributeList::AT_Availability: |
| 5569 | handleAvailabilityAttr(S, D, Attr); |
| 5570 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5571 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5572 | handleDependencyAttr(S, scope, D, Attr); |
| 5573 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5574 | case AttributeList::AT_Common: |
| 5575 | handleCommonAttr(S, D, Attr); |
| 5576 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5577 | case AttributeList::AT_CUDAConstant: |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 5578 | handleConstantAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5579 | break; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5580 | case AttributeList::AT_PassObjectSize: |
| 5581 | handlePassObjectSizeAttr(S, D, Attr); |
| 5582 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5583 | case AttributeList::AT_Constructor: |
| 5584 | handleConstructorAttr(S, D, Attr); |
| 5585 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5586 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5587 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); |
| 5588 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5589 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5590 | handleDeprecatedAttr(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 5591 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5592 | case AttributeList::AT_Destructor: |
| 5593 | handleDestructorAttr(S, D, Attr); |
| 5594 | break; |
| 5595 | case AttributeList::AT_EnableIf: |
| 5596 | handleEnableIfAttr(S, D, Attr); |
| 5597 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5598 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5599 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5600 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 5601 | case AttributeList::AT_MinSize: |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 5602 | handleMinSizeAttr(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 5603 | break; |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 5604 | case AttributeList::AT_OptimizeNone: |
| 5605 | handleOptimizeNoneAttr(S, D, Attr); |
| 5606 | break; |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 5607 | case AttributeList::AT_FlagEnum: |
| 5608 | handleSimpleAttribute<FlagEnumAttr>(S, D, Attr); |
| 5609 | break; |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 5610 | case AttributeList::AT_Flatten: |
| 5611 | handleSimpleAttribute<FlattenAttr>(S, D, Attr); |
| 5612 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5613 | case AttributeList::AT_Format: |
| 5614 | handleFormatAttr(S, D, Attr); |
| 5615 | break; |
| 5616 | case AttributeList::AT_FormatArg: |
| 5617 | handleFormatArgAttr(S, D, Attr); |
| 5618 | break; |
| 5619 | case AttributeList::AT_CUDAGlobal: |
| 5620 | handleGlobalAttr(S, D, Attr); |
| 5621 | break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 5622 | case AttributeList::AT_CUDADevice: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5623 | handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D, |
| 5624 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5625 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5626 | case AttributeList::AT_CUDAHost: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5627 | handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, |
| 5628 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5629 | break; |
| 5630 | case AttributeList::AT_GNUInline: |
| 5631 | handleGNUInlineAttr(S, D, Attr); |
| 5632 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5633 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5634 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 5635 | break; |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 5636 | case AttributeList::AT_Restrict: |
| 5637 | handleRestrictAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5638 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5639 | case AttributeList::AT_MayAlias: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5640 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); |
| 5641 | break; |
| 5642 | case AttributeList::AT_Mode: |
| 5643 | handleModeAttr(S, D, Attr); |
| 5644 | break; |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 5645 | case AttributeList::AT_NoAlias: |
| 5646 | handleSimpleAttribute<NoAliasAttr>(S, D, Attr); |
| 5647 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5648 | case AttributeList::AT_NoCommon: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5649 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); |
| 5650 | break; |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 5651 | case AttributeList::AT_NoSplitStack: |
| 5652 | handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr); |
| 5653 | break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 5654 | case AttributeList::AT_NonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5655 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 5656 | handleNonNullAttrParameter(S, PVD, Attr); |
| 5657 | else |
| 5658 | handleNonNullAttr(S, D, Attr); |
| 5659 | break; |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 5660 | case AttributeList::AT_ReturnsNonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5661 | handleReturnsNonNullAttr(S, D, Attr); |
| 5662 | break; |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 5663 | case AttributeList::AT_AssumeAligned: |
| 5664 | handleAssumeAlignedAttr(S, D, Attr); |
| 5665 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5666 | case AttributeList::AT_Overloadable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5667 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); |
| 5668 | break; |
| 5669 | case AttributeList::AT_Ownership: |
| 5670 | handleOwnershipAttr(S, D, Attr); |
| 5671 | break; |
| 5672 | case AttributeList::AT_Cold: |
| 5673 | handleColdAttr(S, D, Attr); |
| 5674 | break; |
| 5675 | case AttributeList::AT_Hot: |
| 5676 | handleHotAttr(S, D, Attr); |
| 5677 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5678 | case AttributeList::AT_Naked: |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 5679 | handleNakedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5680 | break; |
| 5681 | case AttributeList::AT_NoReturn: |
| 5682 | handleNoReturnAttr(S, D, Attr); |
| 5683 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 5684 | case AttributeList::AT_NoThrow: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5685 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); |
| 5686 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5687 | case AttributeList::AT_CUDAShared: |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 5688 | handleSharedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5689 | break; |
| 5690 | case AttributeList::AT_VecReturn: |
| 5691 | handleVecReturnAttr(S, D, Attr); |
| 5692 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5693 | case AttributeList::AT_ObjCOwnership: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5694 | handleObjCOwnershipAttr(S, D, Attr); |
| 5695 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5696 | case AttributeList::AT_ObjCPreciseLifetime: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5697 | handleObjCPreciseLifetimeAttr(S, D, Attr); |
| 5698 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5699 | case AttributeList::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5700 | handleObjCReturnsInnerPointerAttr(S, D, Attr); |
| 5701 | break; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5702 | case AttributeList::AT_ObjCRequiresSuper: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5703 | handleObjCRequiresSuperAttr(S, D, Attr); |
| 5704 | break; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 5705 | case AttributeList::AT_ObjCBridge: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5706 | handleObjCBridgeAttr(S, scope, D, Attr); |
| 5707 | break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5708 | case AttributeList::AT_ObjCBridgeMutable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5709 | handleObjCBridgeMutableAttr(S, scope, D, Attr); |
| 5710 | break; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5711 | case AttributeList::AT_ObjCBridgeRelated: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5712 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); |
| 5713 | break; |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 5714 | case AttributeList::AT_ObjCDesignatedInitializer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5715 | handleObjCDesignatedInitializer(S, D, Attr); |
| 5716 | break; |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 5717 | case AttributeList::AT_ObjCRuntimeName: |
| 5718 | handleObjCRuntimeName(S, D, Attr); |
| 5719 | break; |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 5720 | case AttributeList::AT_ObjCRuntimeVisible: |
| 5721 | handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, Attr); |
| 5722 | break; |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5723 | case AttributeList::AT_ObjCBoxable: |
| 5724 | handleObjCBoxable(S, D, Attr); |
| 5725 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5726 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5727 | handleCFAuditedTransferAttr(S, D, Attr); |
| 5728 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5729 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5730 | handleCFUnknownTransferAttr(S, D, Attr); |
| 5731 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5732 | case AttributeList::AT_CFConsumed: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5733 | case AttributeList::AT_NSConsumed: |
| 5734 | handleNSConsumedAttr(S, D, Attr); |
| 5735 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5736 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5737 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); |
| 5738 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5739 | case AttributeList::AT_NSReturnsAutoreleased: |
| 5740 | case AttributeList::AT_NSReturnsNotRetained: |
| 5741 | case AttributeList::AT_CFReturnsNotRetained: |
| 5742 | case AttributeList::AT_NSReturnsRetained: |
| 5743 | case AttributeList::AT_CFReturnsRetained: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5744 | handleNSReturnsRetainedAttr(S, D, Attr); |
| 5745 | break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 5746 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5747 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); |
| 5748 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5749 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5750 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); |
| 5751 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 5752 | case AttributeList::AT_VecTypeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5753 | handleVecTypeHint(S, D, Attr); |
| 5754 | break; |
Eric Fiselier | 341e825 | 2016-09-02 18:53:31 +0000 | [diff] [blame] | 5755 | case AttributeList::AT_RequireConstantInit: |
| 5756 | handleSimpleAttribute<RequireConstantInitAttr>(S, D, Attr); |
| 5757 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5758 | case AttributeList::AT_InitPriority: |
| 5759 | handleInitPriorityAttr(S, D, Attr); |
| 5760 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5761 | case AttributeList::AT_Packed: |
| 5762 | handlePackedAttr(S, D, Attr); |
| 5763 | break; |
| 5764 | case AttributeList::AT_Section: |
| 5765 | handleSectionAttr(S, D, Attr); |
| 5766 | break; |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 5767 | case AttributeList::AT_Target: |
| 5768 | handleTargetAttr(S, D, Attr); |
| 5769 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5770 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 5771 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 5772 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5773 | case AttributeList::AT_ArcWeakrefUnavailable: |
| 5774 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); |
| 5775 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5776 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5777 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); |
| 5778 | break; |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame^] | 5779 | case AttributeList::AT_ObjCSubclassingRestricted: |
| 5780 | handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, Attr); |
| 5781 | break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 5782 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 5783 | handleObjCSuppresProtocolAttr(S, D, Attr); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 5784 | break; |
| 5785 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5786 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); |
| 5787 | break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 5788 | case AttributeList::AT_Unused: |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 5789 | handleUnusedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5790 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5791 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5792 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); |
| 5793 | break; |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 5794 | case AttributeList::AT_NotTailCalled: |
| 5795 | handleNotTailCalledAttr(S, D, Attr); |
| 5796 | break; |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 5797 | case AttributeList::AT_DisableTailCalls: |
| 5798 | handleDisableTailCallsAttr(S, D, Attr); |
| 5799 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5800 | case AttributeList::AT_Used: |
| 5801 | handleUsedAttr(S, D, Attr); |
| 5802 | break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 5803 | case AttributeList::AT_Visibility: |
| 5804 | handleVisibilityAttr(S, D, Attr, false); |
| 5805 | break; |
| 5806 | case AttributeList::AT_TypeVisibility: |
| 5807 | handleVisibilityAttr(S, D, Attr, true); |
| 5808 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 5809 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5810 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); |
| 5811 | break; |
| 5812 | case AttributeList::AT_WarnUnusedResult: |
| 5813 | handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 5814 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 5815 | case AttributeList::AT_Weak: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5816 | handleSimpleAttribute<WeakAttr>(S, D, Attr); |
| 5817 | break; |
| 5818 | case AttributeList::AT_WeakRef: |
| 5819 | handleWeakRefAttr(S, D, Attr); |
| 5820 | break; |
| 5821 | case AttributeList::AT_WeakImport: |
| 5822 | handleWeakImportAttr(S, D, Attr); |
| 5823 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5824 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5825 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5826 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5827 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5828 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); |
| 5829 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5830 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5831 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 5832 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5833 | case AttributeList::AT_ObjCNSObject: |
| 5834 | handleObjCNSObject(S, D, Attr); |
| 5835 | break; |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 5836 | case AttributeList::AT_ObjCIndependentClass: |
| 5837 | handleObjCIndependentClass(S, D, Attr); |
| 5838 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5839 | case AttributeList::AT_Blocks: |
| 5840 | handleBlocksAttr(S, D, Attr); |
| 5841 | break; |
| 5842 | case AttributeList::AT_Sentinel: |
| 5843 | handleSentinelAttr(S, D, Attr); |
| 5844 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 5845 | case AttributeList::AT_Const: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5846 | handleSimpleAttribute<ConstAttr>(S, D, Attr); |
| 5847 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5848 | case AttributeList::AT_Pure: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5849 | handleSimpleAttribute<PureAttr>(S, D, Attr); |
| 5850 | break; |
| 5851 | case AttributeList::AT_Cleanup: |
| 5852 | handleCleanupAttr(S, D, Attr); |
| 5853 | break; |
| 5854 | case AttributeList::AT_NoDebug: |
| 5855 | handleNoDebugAttr(S, D, Attr); |
| 5856 | break; |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 5857 | case AttributeList::AT_NoDuplicate: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5858 | handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr); |
| 5859 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5860 | case AttributeList::AT_NoInline: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5861 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); |
| 5862 | break; |
| 5863 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
| 5864 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); |
| 5865 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5866 | case AttributeList::AT_StdCall: |
| 5867 | case AttributeList::AT_CDecl: |
| 5868 | case AttributeList::AT_FastCall: |
| 5869 | case AttributeList::AT_ThisCall: |
| 5870 | case AttributeList::AT_Pascal: |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 5871 | case AttributeList::AT_SwiftCall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 5872 | case AttributeList::AT_VectorCall: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 5873 | case AttributeList::AT_MSABI: |
| 5874 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5875 | case AttributeList::AT_Pcs: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 5876 | case AttributeList::AT_IntelOclBicc: |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 5877 | case AttributeList::AT_PreserveMost: |
| 5878 | case AttributeList::AT_PreserveAll: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5879 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 5880 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5881 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5882 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); |
| 5883 | break; |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5884 | case AttributeList::AT_OpenCLAccess: |
| 5885 | handleOpenCLAccessAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5886 | break; |
Anastasia Stulova | fde7622 | 2016-04-01 16:05:09 +0000 | [diff] [blame] | 5887 | case AttributeList::AT_OpenCLNoSVM: |
| 5888 | handleOpenCLNoSVMAttr(S, D, Attr); |
| 5889 | break; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 5890 | case AttributeList::AT_SwiftContext: |
| 5891 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftContext); |
| 5892 | break; |
| 5893 | case AttributeList::AT_SwiftErrorResult: |
| 5894 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftErrorResult); |
| 5895 | break; |
| 5896 | case AttributeList::AT_SwiftIndirectResult: |
| 5897 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftIndirectResult); |
| 5898 | break; |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 5899 | case AttributeList::AT_InternalLinkage: |
| 5900 | handleInternalLinkageAttr(S, D, Attr); |
| 5901 | break; |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 5902 | case AttributeList::AT_LTOVisibilityPublic: |
| 5903 | handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, Attr); |
| 5904 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 5905 | |
| 5906 | // Microsoft attributes: |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5907 | case AttributeList::AT_EmptyBases: |
| 5908 | handleSimpleAttribute<EmptyBasesAttr>(S, D, Attr); |
| 5909 | break; |
| 5910 | case AttributeList::AT_LayoutVersion: |
| 5911 | handleLayoutVersion(S, D, Attr); |
| 5912 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 5913 | case AttributeList::AT_MSNoVTable: |
| 5914 | handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 5915 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 5916 | case AttributeList::AT_MSStruct: |
| 5917 | handleSimpleAttribute<MSStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 5918 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5919 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5920 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5921 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 5922 | case AttributeList::AT_MSInheritance: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5923 | handleMSInheritanceAttr(S, D, Attr); |
| 5924 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 5925 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5926 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); |
| 5927 | break; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5928 | case AttributeList::AT_Thread: |
| 5929 | handleDeclspecThreadAttr(S, D, Attr); |
| 5930 | break; |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5931 | |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5932 | case AttributeList::AT_AbiTag: |
| 5933 | handleAbiTagAttr(S, D, Attr); |
| 5934 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5935 | |
| 5936 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 5937 | case AttributeList::AT_AssertExclusiveLock: |
| 5938 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 5939 | break; |
| 5940 | case AttributeList::AT_AssertSharedLock: |
| 5941 | handleAssertSharedLockAttr(S, D, Attr); |
| 5942 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5943 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5944 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); |
| 5945 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5946 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5947 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5948 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5949 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5950 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); |
| 5951 | break; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5952 | case AttributeList::AT_NoSanitize: |
| 5953 | handleNoSanitizeAttr(S, D, Attr); |
| 5954 | break; |
| 5955 | case AttributeList::AT_NoSanitizeSpecific: |
| 5956 | handleNoSanitizeSpecificAttr(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 5957 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5958 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5959 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 5960 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5961 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5962 | handleGuardedByAttr(S, D, Attr); |
| 5963 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5964 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5965 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5966 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5967 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5968 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5969 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5970 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5971 | handleLockReturnedAttr(S, D, Attr); |
| 5972 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5973 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5974 | handleLocksExcludedAttr(S, D, Attr); |
| 5975 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5976 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5977 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5978 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5979 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5980 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5981 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5982 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 5983 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 5984 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 5985 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5986 | // Capability analysis attributes. |
| 5987 | case AttributeList::AT_Capability: |
| 5988 | case AttributeList::AT_Lockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5989 | handleCapabilityAttr(S, D, Attr); |
| 5990 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5991 | case AttributeList::AT_RequiresCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5992 | handleRequiresCapabilityAttr(S, D, Attr); |
| 5993 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5994 | |
| 5995 | case AttributeList::AT_AssertCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5996 | handleAssertCapabilityAttr(S, D, Attr); |
| 5997 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5998 | case AttributeList::AT_AcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5999 | handleAcquireCapabilityAttr(S, D, Attr); |
| 6000 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6001 | case AttributeList::AT_ReleaseCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6002 | handleReleaseCapabilityAttr(S, D, Attr); |
| 6003 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6004 | case AttributeList::AT_TryAcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6005 | handleTryAcquireCapabilityAttr(S, D, Attr); |
| 6006 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6007 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 6008 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 6009 | case AttributeList::AT_Consumable: |
| 6010 | handleConsumableAttr(S, D, Attr); |
| 6011 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 6012 | case AttributeList::AT_ConsumableAutoCast: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6013 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); |
| 6014 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 6015 | case AttributeList::AT_ConsumableSetOnRead: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6016 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); |
| 6017 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 6018 | case AttributeList::AT_CallableWhen: |
| 6019 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6020 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 6021 | case AttributeList::AT_ParamTypestate: |
| 6022 | handleParamTypestateAttr(S, D, Attr); |
| 6023 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 6024 | case AttributeList::AT_ReturnTypestate: |
| 6025 | handleReturnTypestateAttr(S, D, Attr); |
| 6026 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6027 | case AttributeList::AT_SetTypestate: |
| 6028 | handleSetTypestateAttr(S, D, Attr); |
| 6029 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 6030 | case AttributeList::AT_TestTypestate: |
| 6031 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6032 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6033 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6034 | // Type safety attributes. |
| 6035 | case AttributeList::AT_ArgumentWithTypeTag: |
| 6036 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 6037 | break; |
| 6038 | case AttributeList::AT_TypeTagForDatatype: |
| 6039 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 6040 | break; |
Pirama Arumuga Nainar | e5d2d71 | 2016-06-10 21:51:18 +0000 | [diff] [blame] | 6041 | case AttributeList::AT_RenderScriptKernel: |
| 6042 | handleSimpleAttribute<RenderScriptKernelAttr>(S, D, Attr); |
Pirama Arumuga Nainar | 8b788d0 | 2016-06-09 23:34:20 +0000 | [diff] [blame] | 6043 | break; |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 6044 | // XRay attributes. |
| 6045 | case AttributeList::AT_XRayInstrument: |
| 6046 | handleSimpleAttribute<XRayInstrumentAttr>(S, D, Attr); |
| 6047 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6048 | } |
| 6049 | } |
| 6050 | |
| 6051 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 6052 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 6053 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 6054 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 6055 | bool IncludeCXX11Attributes) { |
| 6056 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6057 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6058 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6059 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6060 | // GCC accepts |
| 6061 | // static int a9 __attribute__((weakref)); |
| 6062 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6063 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 6064 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 6065 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 6066 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6067 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6068 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6069 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 6070 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 6071 | // good to have a way to specify "these attributes must appear as a group", |
| 6072 | // for these. Additionally, it would be good to have a way to specify "these |
| 6073 | // 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] | 6074 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 6075 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6076 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 6077 | // FIXME: This emits a different error message than |
| 6078 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6079 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6080 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 6081 | } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6082 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6083 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 6084 | } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6085 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6086 | D->setInvalidDecl(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6087 | } else if (Attr *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) { |
| 6088 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6089 | << A << ExpectedKernelFunction; |
| 6090 | D->setInvalidDecl(); |
| 6091 | } else if (Attr *A = D->getAttr<AMDGPUWavesPerEUAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 6092 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6093 | << A << ExpectedKernelFunction; |
| 6094 | D->setInvalidDecl(); |
| 6095 | } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6096 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6097 | << A << ExpectedKernelFunction; |
| 6098 | D->setInvalidDecl(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6099 | } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6100 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6101 | << A << ExpectedKernelFunction; |
| 6102 | D->setInvalidDecl(); |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6103 | } |
| 6104 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6105 | } |
| 6106 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6107 | // Annotation attributes are the only attributes allowed after an access |
| 6108 | // specifier. |
| 6109 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 6110 | const AttributeList *AttrList) { |
| 6111 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6112 | if (l->getKind() == AttributeList::AT_Annotate) { |
David Majnemer | 706f315 | 2014-12-14 01:05:01 +0000 | [diff] [blame] | 6113 | ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6114 | } else { |
| 6115 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 6116 | return true; |
| 6117 | } |
| 6118 | } |
| 6119 | |
| 6120 | return false; |
| 6121 | } |
| 6122 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6123 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 6124 | /// contains any decl attributes that we should warn about. |
| 6125 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 6126 | for ( ; A; A = A->getNext()) { |
| 6127 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 6128 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6129 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 6130 | |
| 6131 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 6132 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 6133 | << A->getName() << A->getRange(); |
| 6134 | } else { |
| 6135 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 6136 | << A->getName() << A->getRange(); |
| 6137 | } |
| 6138 | } |
| 6139 | } |
| 6140 | |
| 6141 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 6142 | /// used to build a declaration, complain about any decl attributes |
| 6143 | /// which might be lying around on it. |
| 6144 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 6145 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 6146 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 6147 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 6148 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 6149 | } |
| 6150 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6151 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6152 | /// \#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] | 6153 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 6154 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6155 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6156 | NamedDecl *NewD = nullptr; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6157 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6158 | FunctionDecl *NewFD; |
| 6159 | // FIXME: Missing call to CheckFunctionDeclaration(). |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6160 | // FIXME: Mangling? |
| 6161 | // FIXME: Is the qualifier info correct? |
| 6162 | // FIXME: Is the DeclContext correct? |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6163 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 6164 | Loc, Loc, DeclarationName(II), |
| 6165 | FD->getType(), FD->getTypeSourceInfo(), |
| 6166 | SC_None, false/*isInlineSpecified*/, |
| 6167 | FD->hasPrototype(), |
| 6168 | false/*isConstexprSpecified*/); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6169 | NewD = NewFD; |
| 6170 | |
| 6171 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 6172 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6173 | |
| 6174 | // Fake up parameter variables; they are declared as if this were |
| 6175 | // a typedef. |
| 6176 | QualType FDTy = FD->getType(); |
| 6177 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 6178 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 6179 | for (const auto &AI : FT->param_types()) { |
| 6180 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6181 | Param->setScopeInfo(0, Params.size()); |
| 6182 | Params.push_back(Param); |
| 6183 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 6184 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6185 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6186 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 6187 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6188 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 6189 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6190 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6191 | if (VD->getQualifier()) { |
| 6192 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 6193 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6194 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6195 | } |
| 6196 | return NewD; |
| 6197 | } |
| 6198 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6199 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6200 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6201 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6202 | if (W.getUsed()) return; // only do this once |
| 6203 | W.setUsed(true); |
| 6204 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 6205 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6206 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6207 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 6208 | W.getLocation())); |
| 6209 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6210 | WeakTopLevelDecl.push_back(NewD); |
| 6211 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 6212 | // to insert Decl at TU scope, sorry. |
| 6213 | DeclContext *SavedContext = CurContext; |
| 6214 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 6215 | NewD->setDeclContext(CurContext); |
| 6216 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6217 | PushOnScopeChains(NewD, S); |
| 6218 | CurContext = SavedContext; |
| 6219 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6220 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6221 | } |
| 6222 | } |
| 6223 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6224 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 6225 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 6226 | // have to do this. |
| 6227 | LoadExternalWeakUndeclaredIdentifiers(); |
| 6228 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6229 | NamedDecl *ND = nullptr; |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6230 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 6231 | if (VD->isExternC()) |
| 6232 | ND = VD; |
| 6233 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 6234 | if (FD->isExternC()) |
| 6235 | ND = FD; |
| 6236 | if (ND) { |
| 6237 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 6238 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6239 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 6240 | WeakInfo W = I->second; |
| 6241 | DeclApplyPragmaWeak(S, ND, W); |
| 6242 | WeakUndeclaredIdentifiers[Id] = W; |
| 6243 | } |
| 6244 | } |
| 6245 | } |
| 6246 | } |
| 6247 | } |
| 6248 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6249 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 6250 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 6251 | /// 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] | 6252 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6253 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6254 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6255 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6256 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6257 | // Walk the declarator structure, applying decl attributes that were in a type |
| 6258 | // position to the decl itself. This handles cases like: |
| 6259 | // int *__attr__(x)** D; |
| 6260 | // when X is a decl attribute. |
| 6261 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 6262 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6263 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6264 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6265 | // Finally, apply any attributes on the decl itself. |
| 6266 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6267 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6268 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6269 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6270 | /// Is the given declaration allowed to use a forbidden type? |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6271 | /// If so, it'll still be annotated with an attribute that makes it |
| 6272 | /// illegal to actually use. |
| 6273 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl, |
| 6274 | const DelayedDiagnostic &diag, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6275 | UnavailableAttr::ImplicitReason &reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6276 | // Private ivars are always okay. Unfortunately, people don't |
| 6277 | // always properly make their ivars private, even in system headers. |
| 6278 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 6279 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 6280 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6281 | return false; |
| 6282 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6283 | // Silently accept unsupported uses of __weak in both user and system |
| 6284 | // declarations when it's been disabled, for ease of integration with |
| 6285 | // -fno-objc-arc files. We do have to take some care against attempts |
| 6286 | // to define such things; for now, we've only done that for ivars |
| 6287 | // and properties. |
| 6288 | if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) { |
| 6289 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 6290 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 6291 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 6292 | return true; |
| 6293 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6294 | } |
| 6295 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6296 | // Allow all sorts of things in system headers. |
| 6297 | if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) { |
| 6298 | // Currently, all the failures dealt with this way are due to ARC |
| 6299 | // restrictions. |
| 6300 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 6301 | return true; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6302 | } |
| 6303 | |
| 6304 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6305 | } |
| 6306 | |
| 6307 | /// Handle a delayed forbidden-type diagnostic. |
| 6308 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 6309 | Decl *decl) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6310 | auto reason = UnavailableAttr::IR_None; |
| 6311 | if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) { |
| 6312 | assert(reason && "didn't set reason?"); |
| 6313 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason, |
| 6314 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6315 | return; |
| 6316 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6317 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6318 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 6319 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6320 | // kind of forbidden type messages on unavailable functions. |
| 6321 | if (FD->hasAttr<UnavailableAttr>() && |
| 6322 | diag.getForbiddenTypeDiagnostic() == |
| 6323 | diag::err_arc_array_param_no_ownership) { |
| 6324 | diag.Triggered = true; |
| 6325 | return; |
| 6326 | } |
| 6327 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6328 | |
| 6329 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 6330 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 6331 | diag.Triggered = true; |
| 6332 | } |
| 6333 | |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 6334 | static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context, |
| 6335 | const Decl *D) { |
| 6336 | // Check each AvailabilityAttr to find the one for this platform. |
| 6337 | for (const auto *A : D->attrs()) { |
| 6338 | if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) { |
| 6339 | // FIXME: this is copied from CheckAvailability. We should try to |
| 6340 | // de-duplicate. |
| 6341 | |
| 6342 | // Check if this is an App Extension "platform", and if so chop off |
| 6343 | // the suffix for matching with the actual platform. |
| 6344 | StringRef ActualPlatform = Avail->getPlatform()->getName(); |
| 6345 | StringRef RealizedPlatform = ActualPlatform; |
| 6346 | if (Context.getLangOpts().AppExt) { |
| 6347 | size_t suffix = RealizedPlatform.rfind("_app_extension"); |
| 6348 | if (suffix != StringRef::npos) |
| 6349 | RealizedPlatform = RealizedPlatform.slice(0, suffix); |
| 6350 | } |
| 6351 | |
| 6352 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 6353 | |
| 6354 | // Match the platform name. |
| 6355 | if (RealizedPlatform == TargetPlatform) |
| 6356 | return Avail; |
| 6357 | } |
| 6358 | } |
| 6359 | return nullptr; |
| 6360 | } |
| 6361 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6362 | /// \brief whether we should emit a diagnostic for \c K and \c DeclVersion in |
| 6363 | /// the context of \c Ctx. For example, we should emit an unavailable diagnostic |
| 6364 | /// in a deprecated context, but not the other way around. |
| 6365 | static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K, |
| 6366 | VersionTuple DeclVersion, |
| 6367 | Decl *Ctx) { |
| 6368 | assert(K != AR_Available && "Expected an unavailable declaration here!"); |
| 6369 | |
| 6370 | // Checks if we should emit the availability diagnostic in the context of C. |
| 6371 | auto CheckContext = [&](const Decl *C) { |
| 6372 | if (K == AR_NotYetIntroduced) { |
| 6373 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C)) |
| 6374 | if (AA->getIntroduced() >= DeclVersion) |
| 6375 | return true; |
| 6376 | } else if (K == AR_Deprecated) |
| 6377 | if (C->isDeprecated()) |
| 6378 | return true; |
| 6379 | |
| 6380 | if (C->isUnavailable()) |
| 6381 | return true; |
| 6382 | return false; |
| 6383 | }; |
| 6384 | |
| 6385 | // FIXME: This is a temporary workaround! Some existing Apple headers depends |
| 6386 | // on nested declarations in an @interface having the availability of the |
| 6387 | // interface when they really shouldn't: they are members of the enclosing |
| 6388 | // context, and can referenced from there. |
| 6389 | if (S.OriginalLexicalContext && cast<Decl>(S.OriginalLexicalContext) != Ctx) { |
| 6390 | auto *OrigCtx = cast<Decl>(S.OriginalLexicalContext); |
| 6391 | if (CheckContext(OrigCtx)) |
| 6392 | return false; |
| 6393 | |
| 6394 | // An implementation implicitly has the availability of the interface. |
| 6395 | if (auto *CatOrImpl = dyn_cast<ObjCImplDecl>(OrigCtx)) { |
| 6396 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 6397 | if (CheckContext(Interface)) |
| 6398 | return false; |
| 6399 | } |
| 6400 | // A category implicitly has the availability of the interface. |
| 6401 | else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(OrigCtx)) |
| 6402 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 6403 | if (CheckContext(Interface)) |
| 6404 | return false; |
| 6405 | } |
| 6406 | |
| 6407 | do { |
| 6408 | if (CheckContext(Ctx)) |
| 6409 | return false; |
| 6410 | |
| 6411 | // An implementation implicitly has the availability of the interface. |
| 6412 | if (auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) { |
| 6413 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 6414 | if (CheckContext(Interface)) |
| 6415 | return false; |
| 6416 | } |
| 6417 | // A category implicitly has the availability of the interface. |
| 6418 | else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx)) |
| 6419 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 6420 | if (CheckContext(Interface)) |
| 6421 | return false; |
| 6422 | } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext()))); |
| 6423 | |
| 6424 | return true; |
| 6425 | } |
| 6426 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6427 | static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K, |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6428 | Decl *Ctx, const NamedDecl *D, |
| 6429 | StringRef Message, SourceLocation Loc, |
| 6430 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 6431 | const ObjCPropertyDecl *ObjCProperty, |
| 6432 | bool ObjCPropertyAccess) { |
| 6433 | // Diagnostics for deprecated or unavailable. |
| 6434 | unsigned diag, diag_message, diag_fwdclass_message; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6435 | unsigned diag_available_here = diag::note_availability_specified_here; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6436 | |
| 6437 | // Matches 'diag::note_property_attribute' options. |
| 6438 | unsigned property_note_select; |
| 6439 | |
| 6440 | // Matches diag::note_availability_specified_here. |
| 6441 | unsigned available_here_select_kind; |
| 6442 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6443 | VersionTuple DeclVersion; |
| 6444 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, D)) |
| 6445 | DeclVersion = AA->getIntroduced(); |
| 6446 | |
| 6447 | if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx)) |
| 6448 | return; |
| 6449 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6450 | switch (K) { |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6451 | case AR_Deprecated: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6452 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 6453 | : diag::warn_property_method_deprecated; |
| 6454 | diag_message = diag::warn_deprecated_message; |
| 6455 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 6456 | property_note_select = /* deprecated */ 0; |
| 6457 | available_here_select_kind = /* deprecated */ 2; |
| 6458 | break; |
| 6459 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6460 | case AR_Unavailable: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6461 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 6462 | : diag::err_property_method_unavailable; |
| 6463 | diag_message = diag::err_unavailable_message; |
| 6464 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 6465 | property_note_select = /* unavailable */ 1; |
| 6466 | available_here_select_kind = /* unavailable */ 0; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6467 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6468 | if (auto attr = D->getAttr<UnavailableAttr>()) { |
| 6469 | if (attr->isImplicit() && attr->getImplicitReason()) { |
| 6470 | // Most of these failures are due to extra restrictions in ARC; |
| 6471 | // reflect that in the primary diagnostic when applicable. |
| 6472 | auto flagARCError = [&] { |
| 6473 | if (S.getLangOpts().ObjCAutoRefCount && |
| 6474 | S.getSourceManager().isInSystemHeader(D->getLocation())) |
| 6475 | diag = diag::err_unavailable_in_arc; |
| 6476 | }; |
| 6477 | |
| 6478 | switch (attr->getImplicitReason()) { |
| 6479 | case UnavailableAttr::IR_None: break; |
| 6480 | |
| 6481 | case UnavailableAttr::IR_ARCForbiddenType: |
| 6482 | flagARCError(); |
| 6483 | diag_available_here = diag::note_arc_forbidden_type; |
| 6484 | break; |
| 6485 | |
| 6486 | case UnavailableAttr::IR_ForbiddenWeak: |
| 6487 | if (S.getLangOpts().ObjCWeakRuntime) |
| 6488 | diag_available_here = diag::note_arc_weak_disabled; |
| 6489 | else |
| 6490 | diag_available_here = diag::note_arc_weak_no_runtime; |
| 6491 | break; |
| 6492 | |
| 6493 | case UnavailableAttr::IR_ARCForbiddenConversion: |
| 6494 | flagARCError(); |
| 6495 | diag_available_here = diag::note_performs_forbidden_arc_conversion; |
| 6496 | break; |
| 6497 | |
| 6498 | case UnavailableAttr::IR_ARCInitReturnsUnrelated: |
| 6499 | flagARCError(); |
| 6500 | diag_available_here = diag::note_arc_init_returns_unrelated; |
| 6501 | break; |
| 6502 | |
| 6503 | case UnavailableAttr::IR_ARCFieldWithOwnership: |
| 6504 | flagARCError(); |
| 6505 | diag_available_here = diag::note_arc_field_with_ownership; |
| 6506 | break; |
| 6507 | } |
| 6508 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6509 | } |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6510 | break; |
| 6511 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6512 | case AR_NotYetIntroduced: |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6513 | assert(!S.getCurFunctionOrMethodDecl() && |
| 6514 | "Function-level partial availablity should not be diagnosed here!"); |
| 6515 | |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6516 | diag = diag::warn_partial_availability; |
| 6517 | diag_message = diag::warn_partial_message; |
| 6518 | diag_fwdclass_message = diag::warn_partial_fwdclass_message; |
| 6519 | property_note_select = /* partial */ 2; |
| 6520 | available_here_select_kind = /* partial */ 3; |
| 6521 | break; |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6522 | |
| 6523 | case AR_Available: |
| 6524 | llvm_unreachable("Warning for availability of available declaration?"); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6525 | } |
| 6526 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6527 | CharSourceRange UseRange; |
| 6528 | StringRef Replacement; |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6529 | if (K == AR_Deprecated) { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6530 | if (auto attr = D->getAttr<DeprecatedAttr>()) |
| 6531 | Replacement = attr->getReplacement(); |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 6532 | if (auto attr = getAttrForPlatform(S.Context, D)) |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 6533 | Replacement = attr->getReplacement(); |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6534 | |
| 6535 | if (!Replacement.empty()) |
| 6536 | UseRange = |
| 6537 | CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); |
| 6538 | } |
| 6539 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6540 | if (!Message.empty()) { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6541 | S.Diag(Loc, diag_message) << D << Message |
| 6542 | << (UseRange.isValid() ? |
| 6543 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6544 | if (ObjCProperty) |
| 6545 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 6546 | << ObjCProperty->getDeclName() << property_note_select; |
| 6547 | } else if (!UnknownObjCClass) { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6548 | S.Diag(Loc, diag) << D |
| 6549 | << (UseRange.isValid() ? |
| 6550 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6551 | if (ObjCProperty) |
| 6552 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 6553 | << ObjCProperty->getDeclName() << property_note_select; |
| 6554 | } else { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6555 | S.Diag(Loc, diag_fwdclass_message) << D |
| 6556 | << (UseRange.isValid() ? |
| 6557 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6558 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 6559 | } |
| 6560 | |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 6561 | // The declaration can have multiple availability attributes, we are looking |
| 6562 | // at one of them. |
| 6563 | const AvailabilityAttr *A = getAttrForPlatform(S.Context, D); |
| 6564 | if (A && A->isInherited()) { |
| 6565 | for (const Decl *Redecl = D->getMostRecentDecl(); Redecl; |
| 6566 | Redecl = Redecl->getPreviousDecl()) { |
| 6567 | const AvailabilityAttr *AForRedecl = getAttrForPlatform(S.Context, |
| 6568 | Redecl); |
| 6569 | if (AForRedecl && !AForRedecl->isInherited()) { |
| 6570 | // If D is a declaration with inherited attributes, the note should |
| 6571 | // point to the declaration with actual attributes. |
| 6572 | S.Diag(Redecl->getLocation(), diag_available_here) << D |
| 6573 | << available_here_select_kind; |
| 6574 | break; |
| 6575 | } |
| 6576 | } |
| 6577 | } |
| 6578 | else |
| 6579 | S.Diag(D->getLocation(), diag_available_here) |
| 6580 | << D << available_here_select_kind; |
| 6581 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6582 | if (K == AR_NotYetIntroduced) |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6583 | S.Diag(Loc, diag::note_partial_availability_silence) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6584 | } |
| 6585 | |
| 6586 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 6587 | Decl *Ctx) { |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6588 | assert(DD.Kind == DelayedDiagnostic::Deprecation || |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 6589 | DD.Kind == DelayedDiagnostic::Unavailable); |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6590 | AvailabilityResult AR = DD.Kind == DelayedDiagnostic::Deprecation |
| 6591 | ? AR_Deprecated |
| 6592 | : AR_Unavailable; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6593 | DD.Triggered = true; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6594 | DoEmitAvailabilityWarning( |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6595 | S, AR, Ctx, DD.getDeprecationDecl(), DD.getDeprecationMessage(), DD.Loc, |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6596 | DD.getUnknownObjCClass(), DD.getObjCProperty(), false); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6597 | } |
| 6598 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6599 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 6600 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6601 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6602 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6603 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6604 | // When delaying diagnostics to run in the context of a parsed |
| 6605 | // declaration, we only want to actually emit anything if parsing |
| 6606 | // succeeds. |
| 6607 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6608 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6609 | // We emit all the active diagnostics in this pool or any of its |
| 6610 | // parents. In general, we'll get one pool for the decl spec |
| 6611 | // and a child pool for each declarator; in a decl group like: |
| 6612 | // deprecated_typedef foo, *bar, baz(); |
| 6613 | // only the declarator pops will be passed decls. This is correct; |
| 6614 | // we really do need to consider delayed diagnostics from the decl spec |
| 6615 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6616 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6617 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6618 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6619 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 6620 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 6621 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6622 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6623 | continue; |
| 6624 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6625 | switch (diag.Kind) { |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6626 | case DelayedDiagnostic::Deprecation: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 6627 | case DelayedDiagnostic::Unavailable: |
| 6628 | // Don't bother giving deprecation/unavailable diagnostics if |
| 6629 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 6630 | if (!decl->isInvalidDecl()) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6631 | handleDelayedAvailabilityCheck(*this, diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6632 | break; |
| 6633 | |
| 6634 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6635 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6636 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6637 | |
| 6638 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6639 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6640 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6641 | } |
| 6642 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6643 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6644 | } |
| 6645 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6646 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 6647 | /// been delayed in the current context instead of in the given pool. |
| 6648 | /// Essentially, this just moves them to the current pool. |
| 6649 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 6650 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 6651 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 6652 | curPool->steal(pool); |
| 6653 | } |
| 6654 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6655 | void Sema::EmitAvailabilityWarning(AvailabilityResult AR, |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 6656 | NamedDecl *D, StringRef Message, |
| 6657 | SourceLocation Loc, |
| 6658 | const ObjCInterfaceDecl *UnknownObjCClass, |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame] | 6659 | const ObjCPropertyDecl *ObjCProperty, |
| 6660 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6661 | // Delay if we're currently parsing a declaration. |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6662 | if (DelayedDiagnostics.shouldDelayDiagnostics() && |
| 6663 | AR != AR_NotYetIntroduced) { |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 6664 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability( |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6665 | AR, Loc, D, UnknownObjCClass, ObjCProperty, Message, |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 6666 | ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6667 | return; |
| 6668 | } |
| 6669 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 6670 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6671 | DoEmitAvailabilityWarning(*this, AR, Ctx, D, Message, Loc, UnknownObjCClass, |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6672 | ObjCProperty, ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6673 | } |
Erik Pilkington | 48c7cc9 | 2016-07-29 17:37:38 +0000 | [diff] [blame] | 6674 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6675 | namespace { |
| 6676 | |
| 6677 | /// \brief This class implements -Wunguarded-availability. |
| 6678 | /// |
| 6679 | /// This is done with a traversal of the AST of a function that makes reference |
| 6680 | /// to a partially available declaration. Whenever we encounter an \c if of the |
| 6681 | /// form: \c if(@available(...)), we use the version from the condition to visit |
| 6682 | /// the then statement. |
| 6683 | class DiagnoseUnguardedAvailability |
| 6684 | : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> { |
| 6685 | typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base; |
| 6686 | |
| 6687 | Sema &SemaRef; |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6688 | Decl *Ctx; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6689 | |
| 6690 | /// Stack of potentially nested 'if (@available(...))'s. |
| 6691 | SmallVector<VersionTuple, 8> AvailabilityStack; |
| 6692 | |
| 6693 | void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range); |
| 6694 | |
| 6695 | public: |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6696 | DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx) |
| 6697 | : SemaRef(SemaRef), Ctx(Ctx) { |
| 6698 | AvailabilityStack.push_back( |
| 6699 | SemaRef.Context.getTargetInfo().getPlatformMinVersion()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6700 | } |
| 6701 | |
| 6702 | void IssueDiagnostics(Stmt *S) { TraverseStmt(S); } |
| 6703 | |
| 6704 | bool TraverseIfStmt(IfStmt *If); |
| 6705 | |
| 6706 | bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) { |
| 6707 | if (ObjCMethodDecl *D = Msg->getMethodDecl()) |
| 6708 | DiagnoseDeclAvailability( |
| 6709 | D, SourceRange(Msg->getSelectorStartLoc(), Msg->getLocEnd())); |
| 6710 | return true; |
| 6711 | } |
| 6712 | |
| 6713 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 6714 | DiagnoseDeclAvailability(DRE->getDecl(), |
| 6715 | SourceRange(DRE->getLocStart(), DRE->getLocEnd())); |
| 6716 | return true; |
| 6717 | } |
| 6718 | |
| 6719 | bool VisitMemberExpr(MemberExpr *ME) { |
| 6720 | DiagnoseDeclAvailability(ME->getMemberDecl(), |
| 6721 | SourceRange(ME->getLocStart(), ME->getLocEnd())); |
| 6722 | return true; |
| 6723 | } |
| 6724 | |
| 6725 | bool VisitTypeLoc(TypeLoc Ty); |
| 6726 | }; |
| 6727 | |
| 6728 | void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability( |
| 6729 | NamedDecl *D, SourceRange Range) { |
| 6730 | |
| 6731 | VersionTuple ContextVersion = AvailabilityStack.back(); |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6732 | if (AvailabilityResult Result = |
| 6733 | SemaRef.ShouldDiagnoseAvailabilityOfDecl(D, nullptr)) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6734 | // All other diagnostic kinds have already been handled in |
| 6735 | // DiagnoseAvailabilityOfDecl. |
| 6736 | if (Result != AR_NotYetIntroduced) |
| 6737 | return; |
| 6738 | |
| 6739 | const AvailabilityAttr *AA = getAttrForPlatform(SemaRef.getASTContext(), D); |
| 6740 | VersionTuple Introduced = AA->getIntroduced(); |
| 6741 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6742 | if (ContextVersion >= Introduced) |
| 6743 | return; |
| 6744 | |
| 6745 | // If the context of this function is less available than D, we should not |
| 6746 | // emit a diagnostic. |
| 6747 | if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx)) |
| 6748 | return; |
| 6749 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6750 | SemaRef.Diag(Range.getBegin(), diag::warn_unguarded_availability) |
| 6751 | << Range << D |
| 6752 | << AvailabilityAttr::getPrettyPlatformName( |
| 6753 | SemaRef.getASTContext().getTargetInfo().getPlatformName()) |
| 6754 | << Introduced.getAsString(); |
| 6755 | |
| 6756 | SemaRef.Diag(D->getLocation(), diag::note_availability_specified_here) |
| 6757 | << D << /* partial */ 3; |
| 6758 | |
| 6759 | // FIXME: Replace this with a fixit diagnostic. |
| 6760 | SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence) |
| 6761 | << Range << D; |
| 6762 | } |
| 6763 | } |
| 6764 | |
| 6765 | bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) { |
| 6766 | const Type *TyPtr = Ty.getTypePtr(); |
| 6767 | SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()}; |
| 6768 | |
| 6769 | if (const TagType *TT = dyn_cast<TagType>(TyPtr)) { |
| 6770 | TagDecl *TD = TT->getDecl(); |
| 6771 | DiagnoseDeclAvailability(TD, Range); |
| 6772 | |
| 6773 | } else if (const TypedefType *TD = dyn_cast<TypedefType>(TyPtr)) { |
| 6774 | TypedefNameDecl *D = TD->getDecl(); |
| 6775 | DiagnoseDeclAvailability(D, Range); |
| 6776 | |
| 6777 | } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) { |
| 6778 | if (NamedDecl *D = ObjCO->getInterface()) |
| 6779 | DiagnoseDeclAvailability(D, Range); |
| 6780 | } |
| 6781 | |
| 6782 | return true; |
| 6783 | } |
| 6784 | |
| 6785 | bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) { |
| 6786 | VersionTuple CondVersion; |
| 6787 | if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) { |
| 6788 | CondVersion = E->getVersion(); |
| 6789 | |
| 6790 | // If we're using the '*' case here or if this check is redundant, then we |
| 6791 | // use the enclosing version to check both branches. |
| 6792 | if (CondVersion.empty() || CondVersion <= AvailabilityStack.back()) |
| 6793 | return Base::TraverseStmt(If->getThen()) && |
| 6794 | Base::TraverseStmt(If->getElse()); |
| 6795 | } else { |
| 6796 | // This isn't an availability checking 'if', we can just continue. |
| 6797 | return Base::TraverseIfStmt(If); |
| 6798 | } |
| 6799 | |
| 6800 | AvailabilityStack.push_back(CondVersion); |
| 6801 | bool ShouldContinue = TraverseStmt(If->getThen()); |
| 6802 | AvailabilityStack.pop_back(); |
| 6803 | |
| 6804 | return ShouldContinue && TraverseStmt(If->getElse()); |
| 6805 | } |
| 6806 | |
| 6807 | } // end anonymous namespace |
| 6808 | |
| 6809 | void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) { |
| 6810 | Stmt *Body = nullptr; |
| 6811 | |
| 6812 | if (auto *FD = D->getAsFunction()) { |
| 6813 | // FIXME: We only examine the pattern decl for availability violations now, |
| 6814 | // but we should also examine instantiated templates. |
| 6815 | if (FD->isTemplateInstantiation()) |
| 6816 | return; |
| 6817 | |
| 6818 | Body = FD->getBody(); |
| 6819 | } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 6820 | Body = MD->getBody(); |
| 6821 | |
| 6822 | assert(Body && "Need a body here!"); |
| 6823 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6824 | DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6825 | } |