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" |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/StringExtras.h" |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MathExtras.h" |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 39 | using namespace clang; |
John McCall | b45a1e7 | 2010-08-26 02:13:20 +0000 | [diff] [blame] | 40 | using namespace sema; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 41 | |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 42 | namespace AttributeLangSupport { |
NAKAMURA Takumi | 01d27f9 | 2013-11-25 00:52:29 +0000 | [diff] [blame] | 43 | enum LANG { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 44 | C, |
| 45 | Cpp, |
| 46 | ObjC |
| 47 | }; |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 48 | } // end namespace AttributeLangSupport |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | // Helper functions |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | |
Ted Kremenek | 527042b | 2009-08-14 20:49:40 +0000 | [diff] [blame] | 54 | /// isFunctionOrMethod - Return true if the given decl has function |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 55 | /// type (function or function-typed variable) or an Objective-C |
| 56 | /// method. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 57 | static bool isFunctionOrMethod(const Decl *D) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 58 | return (D->getFunctionType() != nullptr) || isa<ObjCMethodDecl>(D); |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 59 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 60 | |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 61 | /// \brief Return true if the given decl has function type (function or |
| 62 | /// function-typed variable) or an Objective-C method or a block. |
| 63 | static bool isFunctionOrMethodOrBlock(const Decl *D) { |
| 64 | return isFunctionOrMethod(D) || isa<BlockDecl>(D); |
| 65 | } |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 66 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 67 | /// Return true if the given decl has a declarator that should have |
| 68 | /// been processed by Sema::GetTypeForDeclarator. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 69 | static bool hasDeclarator(const Decl *D) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 70 | // In some sense, TypedefDecl really *ought* to be a DeclaratorDecl. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 71 | return isa<DeclaratorDecl>(D) || isa<BlockDecl>(D) || isa<TypedefNameDecl>(D) || |
| 72 | isa<ObjCPropertyDecl>(D); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 75 | /// hasFunctionProto - Return true if the given decl has a argument |
| 76 | /// information. This decl should have already passed |
Fariborz Jahanian | 4447e17 | 2009-05-15 23:15:03 +0000 | [diff] [blame] | 77 | /// isFunctionOrMethod or isFunctionOrMethodOrBlock. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 78 | static bool hasFunctionProto(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 79 | if (const FunctionType *FnTy = D->getFunctionType()) |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 80 | return isa<FunctionProtoType>(FnTy); |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 81 | return isa<ObjCMethodDecl>(D) || isa<BlockDecl>(D); |
Daniel Dunbar | 70e3eba | 2008-10-19 02:04:16 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 84 | /// getFunctionOrMethodNumParams - Return number of function or method |
| 85 | /// 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] | 86 | /// hasFunctionProto first). |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 87 | static unsigned getFunctionOrMethodNumParams(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 88 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 89 | return cast<FunctionProtoType>(FnTy)->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 90 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 91 | return BD->getNumParams(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 92 | return cast<ObjCMethodDecl>(D)->param_size(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 95 | static QualType getFunctionOrMethodParamType(const Decl *D, unsigned Idx) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 96 | if (const FunctionType *FnTy = D->getFunctionType()) |
Alp Toker | 9cacbab | 2014-01-20 20:26:09 +0000 | [diff] [blame] | 97 | return cast<FunctionProtoType>(FnTy)->getParamType(Idx); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 98 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Fariborz Jahanian | 960910a | 2009-05-19 17:08:59 +0000 | [diff] [blame] | 99 | return BD->getParamDecl(Idx)->getType(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 100 | |
Alp Toker | 03376dc | 2014-07-07 09:02:20 +0000 | [diff] [blame] | 101 | return cast<ObjCMethodDecl>(D)->parameters()[Idx]->getType(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 104 | static SourceRange getFunctionOrMethodParamRange(const Decl *D, unsigned Idx) { |
| 105 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 106 | return FD->getParamDecl(Idx)->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 107 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 108 | return MD->parameters()[Idx]->getSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 109 | if (const auto *BD = dyn_cast<BlockDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 110 | return BD->getParamDecl(Idx)->getSourceRange(); |
| 111 | return SourceRange(); |
| 112 | } |
| 113 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 114 | static QualType getFunctionOrMethodResultType(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 115 | if (const FunctionType *FnTy = D->getFunctionType()) |
Aaron Ballman | 6288d06 | 2014-07-11 16:31:29 +0000 | [diff] [blame] | 116 | return cast<FunctionType>(FnTy)->getReturnType(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 117 | return cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 120 | static SourceRange getFunctionOrMethodResultSourceRange(const Decl *D) { |
| 121 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
| 122 | return FD->getReturnTypeSourceRange(); |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 123 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 124 | return MD->getReturnTypeSourceRange(); |
| 125 | return SourceRange(); |
| 126 | } |
| 127 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 128 | static bool isFunctionOrMethodVariadic(const Decl *D) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 129 | if (const FunctionType *FnTy = D->getFunctionType()) { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 130 | const FunctionProtoType *proto = cast<FunctionProtoType>(FnTy); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 131 | return proto->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 132 | } |
Aaron Ballman | e13d009 | 2014-08-01 17:02:34 +0000 | [diff] [blame] | 133 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| 134 | return BD->isVariadic(); |
| 135 | |
| 136 | return cast<ObjCMethodDecl>(D)->isVariadic(); |
Daniel Dunbar | c136e0c | 2008-09-26 04:12:28 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 139 | static bool isInstanceMethod(const Decl *D) { |
| 140 | if (const CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 141 | return MethodDecl->isInstance(); |
| 142 | return false; |
| 143 | } |
| 144 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 145 | static inline bool isNSStringType(QualType T, ASTContext &Ctx) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 146 | const ObjCObjectPointerType *PT = T->getAs<ObjCObjectPointerType>(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 147 | if (!PT) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 148 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 149 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 150 | ObjCInterfaceDecl *Cls = PT->getObjectType()->getInterface(); |
| 151 | if (!Cls) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 152 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 153 | |
John McCall | 96fa484 | 2010-05-17 21:00:27 +0000 | [diff] [blame] | 154 | IdentifierInfo* ClsName = Cls->getIdentifier(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 156 | // FIXME: Should we walk the chain of classes? |
| 157 | return ClsName == &Ctx.Idents.get("NSString") || |
| 158 | ClsName == &Ctx.Idents.get("NSMutableString"); |
| 159 | } |
| 160 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 161 | static inline bool isCFStringType(QualType T, ASTContext &Ctx) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 162 | const PointerType *PT = T->getAs<PointerType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 163 | if (!PT) |
| 164 | return false; |
| 165 | |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 166 | const RecordType *RT = PT->getPointeeType()->getAs<RecordType>(); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 167 | if (!RT) |
| 168 | return false; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 169 | |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 170 | const RecordDecl *RD = RT->getDecl(); |
Abramo Bagnara | 6150c88 | 2010-05-11 21:36:43 +0000 | [diff] [blame] | 171 | if (RD->getTagKind() != TTK_Struct) |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 172 | return false; |
| 173 | |
| 174 | return RD->getIdentifier() == &Ctx.Idents.get("__CFString"); |
| 175 | } |
| 176 | |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 177 | static unsigned getNumAttributeArgs(const AttributeList &Attr) { |
| 178 | // FIXME: Include the type in the argument list. |
| 179 | return Attr.getNumArgs() + Attr.hasParsedType(); |
| 180 | } |
| 181 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 182 | template <typename Compare> |
| 183 | static bool checkAttributeNumArgsImpl(Sema &S, const AttributeList &Attr, |
| 184 | unsigned Num, unsigned Diag, |
| 185 | Compare Comp) { |
| 186 | if (Comp(getNumAttributeArgs(Attr), Num)) { |
| 187 | S.Diag(Attr.getLoc(), Diag) << Attr.getName() << Num; |
Chandler Carruth | fcc48d9 | 2011-07-11 23:30:35 +0000 | [diff] [blame] | 188 | return false; |
| 189 | } |
| 190 | |
| 191 | return true; |
| 192 | } |
| 193 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 194 | /// \brief Check if the attribute has exactly as many args as Num. May |
| 195 | /// output an error. |
| 196 | static bool checkAttributeNumArgs(Sema &S, const AttributeList &Attr, |
| 197 | unsigned Num) { |
| 198 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 199 | diag::err_attribute_wrong_number_arguments, |
| 200 | std::not_equal_to<unsigned>()); |
| 201 | } |
| 202 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 203 | /// \brief Check if the attribute has at least as many args as Num. May |
| 204 | /// output an error. |
| 205 | static bool checkAttributeAtLeastNumArgs(Sema &S, const AttributeList &Attr, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 206 | unsigned Num) { |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 207 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 208 | diag::err_attribute_too_few_arguments, |
| 209 | std::less<unsigned>()); |
| 210 | } |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 211 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 212 | /// \brief Check if the attribute has at most as many args as Num. May |
| 213 | /// output an error. |
| 214 | static bool checkAttributeAtMostNumArgs(Sema &S, const AttributeList &Attr, |
| 215 | unsigned Num) { |
| 216 | return checkAttributeNumArgsImpl(S, Attr, Num, |
| 217 | diag::err_attribute_too_many_arguments, |
| 218 | std::greater<unsigned>()); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 221 | /// \brief A helper function to provide Attribute Location for the Attr types |
| 222 | /// AND the AttributeList. |
| 223 | template <typename AttrInfo> |
| 224 | static typename std::enable_if<std::is_base_of<clang::Attr, AttrInfo>::value, |
| 225 | SourceLocation>::type |
| 226 | getAttrLoc(const AttrInfo &Attr) { |
| 227 | return Attr.getLocation(); |
| 228 | } |
| 229 | static SourceLocation getAttrLoc(const clang::AttributeList &Attr) { |
| 230 | return Attr.getLoc(); |
| 231 | } |
| 232 | |
| 233 | /// \brief A helper function to provide Attribute Name for the Attr types |
| 234 | /// AND the AttributeList. |
| 235 | template <typename AttrInfo> |
| 236 | static typename std::enable_if<std::is_base_of<clang::Attr, AttrInfo>::value, |
| 237 | const AttrInfo *>::type |
| 238 | getAttrName(const AttrInfo &Attr) { |
| 239 | return &Attr; |
| 240 | } |
Benjamin Kramer | 674d579 | 2017-05-26 20:08:24 +0000 | [diff] [blame] | 241 | static const IdentifierInfo *getAttrName(const clang::AttributeList &Attr) { |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 242 | return Attr.getName(); |
| 243 | } |
| 244 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 245 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 246 | /// expression and return success or failure. May output an error. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 247 | template<typename AttrInfo> |
| 248 | static bool checkUInt32Argument(Sema &S, const AttrInfo& Attr, const Expr *Expr, |
| 249 | uint32_t &Val, unsigned Idx = UINT_MAX) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 250 | llvm::APSInt I(32); |
| 251 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 252 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 253 | if (Idx != UINT_MAX) |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 254 | S.Diag(getAttrLoc(Attr), diag::err_attribute_argument_n_type) |
| 255 | << getAttrName(Attr) << Idx << AANT_ArgumentIntegerConstant |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 256 | << Expr->getSourceRange(); |
| 257 | else |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 258 | S.Diag(getAttrLoc(Attr), diag::err_attribute_argument_type) |
| 259 | << getAttrName(Attr) << AANT_ArgumentIntegerConstant |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 260 | << Expr->getSourceRange(); |
| 261 | return false; |
| 262 | } |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 263 | |
| 264 | if (!I.isIntN(32)) { |
Aaron Ballman | 31f4231 | 2014-07-24 14:51:23 +0000 | [diff] [blame] | 265 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 266 | << I.toString(10, false) << 32 << /* Unsigned */ 1; |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 267 | return false; |
| 268 | } |
| 269 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 270 | Val = (uint32_t)I.getZExtValue(); |
| 271 | return true; |
| 272 | } |
| 273 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 274 | /// \brief Wrapper around checkUInt32Argument, with an extra check to be sure |
| 275 | /// that the result will fit into a regular (signed) int. All args have the same |
| 276 | /// purpose as they do in checkUInt32Argument. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 277 | template<typename AttrInfo> |
| 278 | static bool checkPositiveIntArgument(Sema &S, const AttrInfo& Attr, const Expr *Expr, |
| 279 | int &Val, unsigned Idx = UINT_MAX) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 280 | uint32_t UVal; |
| 281 | if (!checkUInt32Argument(S, Attr, Expr, UVal, Idx)) |
| 282 | return false; |
| 283 | |
George Burgess IV | a804957 | 2016-12-22 19:00:31 +0000 | [diff] [blame] | 284 | if (UVal > (uint32_t)std::numeric_limits<int>::max()) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 285 | llvm::APSInt I(32); // for toString |
| 286 | I = UVal; |
| 287 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 288 | << I.toString(10, false) << 32 << /* Unsigned */ 0; |
| 289 | return false; |
| 290 | } |
| 291 | |
| 292 | Val = UVal; |
| 293 | return true; |
| 294 | } |
| 295 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 296 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 297 | /// declaration. Returns true if diagnosed. |
| 298 | template <typename AttrTy> |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 299 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range, |
| 300 | IdentifierInfo *Ident) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 301 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 302 | S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident |
| 303 | << A; |
| 304 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 305 | return true; |
| 306 | } |
| 307 | return false; |
| 308 | } |
| 309 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 310 | /// \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] | 311 | /// instance method D. May output an error. |
| 312 | /// |
| 313 | /// \returns true if IdxExpr is a valid index. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 314 | template <typename AttrInfo> |
| 315 | static bool checkFunctionOrMethodParameterIndex( |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 316 | Sema &S, const Decl *D, const AttrInfo &Attr, unsigned AttrArgNum, |
| 317 | const Expr *IdxExpr, uint64_t &Idx, bool AllowImplicitThis = false) { |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 318 | assert(isFunctionOrMethodOrBlock(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 319 | |
| 320 | // In C++ the implicit 'this' function parameter also counts. |
| 321 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 322 | bool HP = hasFunctionProto(D); |
| 323 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 324 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 325 | unsigned NumParams = |
| 326 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 327 | |
| 328 | llvm::APSInt IdxInt; |
| 329 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 330 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 331 | S.Diag(getAttrLoc(Attr), diag::err_attribute_argument_n_type) |
| 332 | << getAttrName(Attr) << AttrArgNum << AANT_ArgumentIntegerConstant |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 333 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 334 | return false; |
| 335 | } |
| 336 | |
| 337 | Idx = IdxInt.getLimitedValue(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 338 | if (Idx < 1 || (!IV && Idx > NumParams)) { |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 339 | S.Diag(getAttrLoc(Attr), diag::err_attribute_argument_out_of_bounds) |
| 340 | << getAttrName(Attr) << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | Idx--; // Convert to zero-based. |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 344 | if (HasImplicitThisParam && !AllowImplicitThis) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 345 | if (Idx == 0) { |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 346 | S.Diag(getAttrLoc(Attr), |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 347 | diag::err_attribute_invalid_implicit_this_argument) |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 348 | << getAttrName(Attr) << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 349 | return false; |
| 350 | } |
| 351 | --Idx; |
| 352 | } |
| 353 | |
| 354 | return true; |
| 355 | } |
| 356 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 357 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 358 | /// If not emit an error and return false. If the argument is an identifier it |
| 359 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 360 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 361 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 362 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 363 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 364 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 365 | if (Attr.isArgIdent(ArgNum)) { |
| 366 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 367 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 368 | << Attr.getName() << AANT_ArgumentString |
| 369 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 370 | << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 371 | Str = Loc->Ident->getName(); |
| 372 | if (ArgLocation) |
| 373 | *ArgLocation = Loc->Loc; |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | // Now check for an actual string literal. |
| 378 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 379 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 380 | if (ArgLocation) |
| 381 | *ArgLocation = ArgExpr->getLocStart(); |
| 382 | |
| 383 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 384 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 385 | << Attr.getName() << AANT_ArgumentString; |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | Str = Literal->getString(); |
| 390 | return true; |
| 391 | } |
| 392 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 393 | /// \brief Applies the given attribute to the Decl without performing any |
| 394 | /// additional semantic checking. |
| 395 | template <typename AttrType> |
| 396 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 397 | const AttributeList &Attr) { |
| 398 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 399 | Attr.getAttributeSpellingListIndex())); |
| 400 | } |
| 401 | |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 402 | template <typename AttrType> |
| 403 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
| 404 | const AttributeList &Attr) { |
| 405 | handleSimpleAttribute<AttrType>(S, D, Attr); |
| 406 | } |
| 407 | |
| 408 | /// \brief Applies the given attribute to the Decl so long as the Decl doesn't |
| 409 | /// already have one of the given incompatible attributes. |
| 410 | template <typename AttrType, typename IncompatibleAttrType, |
| 411 | typename... IncompatibleAttrTypes> |
| 412 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
| 413 | const AttributeList &Attr) { |
| 414 | if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, Attr.getRange(), |
| 415 | Attr.getName())) |
| 416 | return; |
| 417 | handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D, |
| 418 | Attr); |
| 419 | } |
| 420 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 421 | /// \brief Check if the passed-in expression is of type int or bool. |
| 422 | static bool isIntOrBool(Expr *Exp) { |
| 423 | QualType QT = Exp->getType(); |
| 424 | return QT->isBooleanType() || QT->isIntegerType(); |
| 425 | } |
| 426 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 427 | |
| 428 | // Check to see if the type is a smart pointer of some kind. We assume |
| 429 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 430 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 431 | DeclContextLookupResult Res1 = RT->getDecl()->lookup( |
| 432 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 433 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 434 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 435 | |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 436 | DeclContextLookupResult Res2 = RT->getDecl()->lookup( |
| 437 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 438 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 439 | return false; |
| 440 | |
| 441 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 444 | /// \brief Check if passed in Decl is a pointer type. |
| 445 | /// Note that this function may produce an error message. |
| 446 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 447 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 448 | const AttributeList &Attr) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 449 | const ValueDecl *vd = cast<ValueDecl>(D); |
| 450 | QualType QT = vd->getType(); |
| 451 | if (QT->isAnyPointerType()) |
| 452 | return true; |
| 453 | |
| 454 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 455 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 456 | // (We don't want to force template instantiation if we can avoid it, |
| 457 | // since that would alter the order in which templates are instantiated.) |
| 458 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 459 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 460 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 461 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 462 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 463 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 464 | |
| 465 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Aaron Ballman | 6828945 | 2013-12-26 15:06:01 +0000 | [diff] [blame] | 466 | << Attr.getName() << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 467 | return false; |
| 468 | } |
| 469 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 470 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 471 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 472 | static const RecordType *getRecordType(QualType QT) { |
| 473 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 474 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 475 | |
| 476 | // Now check if we point to record type. |
| 477 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 478 | return PT->getPointeeType()->getAs<RecordType>(); |
| 479 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 480 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 483 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 484 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 485 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 486 | if (!RT) |
| 487 | return false; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 488 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 489 | // 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] | 490 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 491 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 492 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 493 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 494 | // FIXME -- Check the type that the smart pointer points to. |
| 495 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 496 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 497 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 498 | // Check if the record itself has a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 499 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 500 | if (RD->hasAttr<CapabilityAttr>()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 501 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 502 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 503 | // Else check if any base classes have a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 504 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 505 | CXXBasePaths BPaths(false, false); |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 506 | if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) { |
| 507 | const auto *Type = BS->getType()->getAs<RecordType>(); |
| 508 | return Type->getDecl()->hasAttr<CapabilityAttr>(); |
| 509 | }, BPaths)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 510 | return true; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 511 | } |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 512 | return false; |
| 513 | } |
| 514 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 515 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 516 | const auto *TD = Ty->getAs<TypedefType>(); |
| 517 | if (!TD) |
| 518 | return false; |
| 519 | |
| 520 | TypedefNameDecl *TN = TD->getDecl(); |
| 521 | if (!TN) |
| 522 | return false; |
| 523 | |
| 524 | return TN->hasAttr<CapabilityAttr>(); |
| 525 | } |
| 526 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 527 | static bool typeHasCapability(Sema &S, QualType Ty) { |
| 528 | if (checkTypedefTypeForCapability(Ty)) |
| 529 | return true; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 530 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 531 | if (checkRecordTypeForCapability(S, Ty)) |
| 532 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 533 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 534 | return false; |
| 535 | } |
| 536 | |
| 537 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 538 | // Capability expressions are simple expressions involving the boolean logic |
| 539 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 540 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 541 | // is a capability or not. |
| 542 | |
| 543 | if (const auto *E = dyn_cast<DeclRefExpr>(Ex)) |
| 544 | return typeHasCapability(S, E->getType()); |
| 545 | else if (const auto *E = dyn_cast<CastExpr>(Ex)) |
| 546 | return isCapabilityExpr(S, E->getSubExpr()); |
| 547 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 548 | return isCapabilityExpr(S, E->getSubExpr()); |
| 549 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
| 550 | if (E->getOpcode() == UO_LNot) |
| 551 | return isCapabilityExpr(S, E->getSubExpr()); |
| 552 | return false; |
| 553 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 554 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 555 | return isCapabilityExpr(S, E->getLHS()) && |
| 556 | isCapabilityExpr(S, E->getRHS()); |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | return false; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 563 | /// \brief Checks that all attribute arguments, starting from Sidx, resolve to |
| 564 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 565 | /// \param Sidx The attribute argument index to start checking with. |
| 566 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 567 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 568 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
| 569 | const AttributeList &Attr, |
| 570 | SmallVectorImpl<Expr *> &Args, |
| 571 | int Sidx = 0, |
| 572 | bool ParamIdxOk = false) { |
| 573 | for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 574 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 575 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 576 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 577 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 578 | Args.push_back(ArgExp); |
| 579 | continue; |
| 580 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 581 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 582 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 583 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 584 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 585 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 586 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 587 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 588 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 589 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 590 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 591 | // We allow constant strings to be used as a placeholder for expressions |
| 592 | // that are not valid C++ syntax, but warn that they are ignored. |
| 593 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 594 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 595 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 596 | continue; |
| 597 | } |
| 598 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 599 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 600 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 601 | // A pointer to member expression of the form &MyClass::mu is treated |
| 602 | // specially -- we need to look at the type of the member. |
| 603 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 604 | if (UOp->getOpcode() == UO_AddrOf) |
| 605 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 606 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 607 | ArgTy = DRE->getDecl()->getType(); |
| 608 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 609 | // 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] | 610 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 611 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 612 | // Now check if we index into a record type function param. |
| 613 | if(!RT && ParamIdxOk) { |
| 614 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 615 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 616 | if(FD && IL) { |
| 617 | unsigned int NumParams = FD->getNumParams(); |
| 618 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 619 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 620 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 621 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 622 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 623 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 624 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 625 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 626 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 627 | } |
| 628 | } |
| 629 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 630 | // If the type does not have a capability, see if the components of the |
| 631 | // expression have capabilities. This allows for writing C code where the |
| 632 | // capability may be on the type, and the expression is a capability |
| 633 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 634 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
| 635 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 636 | << Attr.getName() << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 637 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 638 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 639 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 642 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 643 | // Attribute Implementations |
| 644 | //===----------------------------------------------------------------------===// |
| 645 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 646 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 647 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 648 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 649 | return; |
| 650 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 651 | D->addAttr(::new (S.Context) |
| 652 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 653 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 656 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 657 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 658 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 659 | SmallVector<Expr*, 1> Args; |
| 660 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 661 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 662 | unsigned Size = Args.size(); |
| 663 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 664 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 665 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 666 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 667 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 668 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 671 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 672 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 673 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 674 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 675 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 676 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg, |
| 677 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 680 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 681 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 682 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 683 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 684 | return; |
| 685 | |
| 686 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 687 | return; |
| 688 | |
| 689 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 690 | S.Context, Arg, |
| 691 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 692 | } |
| 693 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 694 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 695 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 696 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 697 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 698 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 699 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 700 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 701 | QualType QT = cast<ValueDecl>(D)->getType(); |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 702 | if (!QT->isDependentType() && !typeHasCapability(S, QT)) { |
| 703 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
| 704 | << Attr.getName(); |
| 705 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 706 | } |
| 707 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 708 | // Check that all arguments are lockable objects. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 709 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 710 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 711 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 712 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 713 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 714 | } |
| 715 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 716 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 717 | const AttributeList &Attr) { |
| 718 | SmallVector<Expr*, 1> Args; |
| 719 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 720 | return; |
| 721 | |
| 722 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 723 | D->addAttr(::new (S.Context) |
| 724 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 725 | StartArg, Args.size(), |
| 726 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 729 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 730 | const AttributeList &Attr) { |
| 731 | SmallVector<Expr*, 1> Args; |
| 732 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 733 | return; |
| 734 | |
| 735 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 736 | D->addAttr(::new (S.Context) |
| 737 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 738 | StartArg, Args.size(), |
| 739 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 742 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 743 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 744 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 745 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 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, 0, /*ParamIdxOk=*/true); |
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 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 752 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 753 | const AttributeList &Attr) { |
| 754 | SmallVector<Expr*, 1> Args; |
| 755 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 756 | return; |
| 757 | |
| 758 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 759 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 760 | D->addAttr(::new (S.Context) |
| 761 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 762 | Attr.getAttributeSpellingListIndex())); |
| 763 | } |
| 764 | |
| 765 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 766 | const AttributeList &Attr) { |
| 767 | SmallVector<Expr*, 1> Args; |
| 768 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 769 | return; |
| 770 | |
| 771 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 772 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 773 | D->addAttr(::new (S.Context) |
| 774 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 775 | StartArg, Size, |
| 776 | Attr.getAttributeSpellingListIndex())); |
| 777 | } |
| 778 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 779 | /// \brief Checks to be sure that the given parameter number is in bounds, and is |
| 780 | /// an integral type. Will emit appropriate diagnostics if this returns |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 781 | /// false. |
| 782 | /// |
| 783 | /// FuncParamNo is expected to be from the user, so is base-1. AttrArgNo is used |
| 784 | /// to actually retrieve the argument, so it's base-0. |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 785 | template <typename AttrInfo> |
| 786 | static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD, |
| 787 | const AttrInfo &Attr, Expr *AttrArg, |
| 788 | unsigned FuncParamNo, unsigned AttrArgNo, |
| 789 | bool AllowDependentType = false) { |
| 790 | uint64_t Idx; |
| 791 | if (!checkFunctionOrMethodParameterIndex(S, FD, Attr, FuncParamNo, AttrArg, |
| 792 | Idx)) |
| 793 | return false; |
| 794 | |
| 795 | const ParmVarDecl *Param = FD->getParamDecl(Idx); |
| 796 | if (AllowDependentType && Param->getType()->isDependentType()) |
| 797 | return true; |
| 798 | if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) { |
| 799 | SourceLocation SrcLoc = AttrArg->getLocStart(); |
| 800 | S.Diag(SrcLoc, diag::err_attribute_integers_only) |
| 801 | << getAttrName(Attr) << Param->getSourceRange(); |
| 802 | return false; |
| 803 | } |
| 804 | return true; |
| 805 | } |
| 806 | |
| 807 | /// \brief Checks to be sure that the given parameter number is in bounds, and is |
| 808 | /// an integral type. Will emit appropriate diagnostics if this returns false. |
| 809 | /// |
| 810 | /// FuncParamNo is expected to be from the user, so is base-1. AttrArgNo is used |
| 811 | /// to actually retrieve the argument, so it's base-0. |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 812 | static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD, |
| 813 | const AttributeList &Attr, |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 814 | unsigned FuncParamNo, unsigned AttrArgNo, |
| 815 | bool AllowDependentType = false) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 816 | assert(Attr.isArgExpr(AttrArgNo) && "Expected expression argument"); |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 817 | return checkParamIsIntegerType(S, FD, Attr, Attr.getArgAsExpr(AttrArgNo), |
| 818 | FuncParamNo, AttrArgNo, AllowDependentType); |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 822 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1) || |
| 823 | !checkAttributeAtMostNumArgs(S, Attr, 2)) |
| 824 | return; |
| 825 | |
| 826 | const auto *FD = cast<FunctionDecl>(D); |
| 827 | if (!FD->getReturnType()->isPointerType()) { |
| 828 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 829 | << Attr.getName(); |
| 830 | return; |
| 831 | } |
| 832 | |
| 833 | const Expr *SizeExpr = Attr.getArgAsExpr(0); |
| 834 | int SizeArgNo; |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame] | 835 | // Parameter indices are 1-indexed, hence Index=1 |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 836 | if (!checkPositiveIntArgument(S, Attr, SizeExpr, SizeArgNo, /*Index=*/1)) |
| 837 | return; |
| 838 | |
| 839 | if (!checkParamIsIntegerType(S, FD, Attr, SizeArgNo, /*AttrArgNo=*/0)) |
| 840 | return; |
| 841 | |
| 842 | // Args are 1-indexed, so 0 implies that the arg was not present |
| 843 | int NumberArgNo = 0; |
| 844 | if (Attr.getNumArgs() == 2) { |
| 845 | const Expr *NumberExpr = Attr.getArgAsExpr(1); |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame] | 846 | // Parameter indices are 1-based, hence Index=2 |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 847 | if (!checkPositiveIntArgument(S, Attr, NumberExpr, NumberArgNo, |
| 848 | /*Index=*/2)) |
| 849 | return; |
| 850 | |
| 851 | if (!checkParamIsIntegerType(S, FD, Attr, NumberArgNo, /*AttrArgNo=*/1)) |
| 852 | return; |
| 853 | } |
| 854 | |
| 855 | D->addAttr(::new (S.Context) AllocSizeAttr( |
| 856 | Attr.getRange(), S.Context, SizeArgNo, NumberArgNo, |
| 857 | Attr.getAttributeSpellingListIndex())); |
| 858 | } |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 859 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 860 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 861 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 862 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 863 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 864 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 865 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 866 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 867 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 868 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 869 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 873 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 874 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 875 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 878 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 879 | const AttributeList &Attr) { |
| 880 | SmallVector<Expr*, 2> Args; |
| 881 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 882 | return; |
| 883 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 884 | D->addAttr(::new (S.Context) |
| 885 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 886 | Attr.getArgAsExpr(0), |
| 887 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 888 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 891 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 892 | const AttributeList &Attr) { |
| 893 | SmallVector<Expr*, 2> Args; |
| 894 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 895 | return; |
| 896 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 897 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
| 898 | Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(), |
| 899 | Args.size(), Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 902 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 903 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 904 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 905 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 906 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 907 | unsigned Size = Args.size(); |
| 908 | if (Size == 0) |
| 909 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 910 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 911 | D->addAttr(::new (S.Context) |
| 912 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 913 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 917 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 918 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 919 | return; |
| 920 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 921 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 922 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 923 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 924 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 925 | if (Size == 0) |
| 926 | return; |
| 927 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 928 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 929 | D->addAttr(::new (S.Context) |
| 930 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 931 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 932 | } |
| 933 | |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 934 | static bool checkFunctionConditionAttr(Sema &S, Decl *D, |
| 935 | const AttributeList &Attr, |
| 936 | Expr *&Cond, StringRef &Msg) { |
| 937 | Cond = Attr.getArgAsExpr(0); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 938 | if (!Cond->isTypeDependent()) { |
| 939 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 940 | if (Converted.isInvalid()) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 941 | return false; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 942 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 945 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 946 | return false; |
| 947 | |
| 948 | if (Msg.empty()) |
| 949 | Msg = "<no message provided>"; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 950 | |
| 951 | SmallVector<PartialDiagnosticAt, 8> Diags; |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 952 | if (isa<FunctionDecl>(D) && !Cond->isValueDependent() && |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 953 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 954 | Diags)) { |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 955 | S.Diag(Attr.getLoc(), diag::err_attr_cond_never_constant_expr) |
| 956 | << Attr.getName(); |
George Burgess IV | 0d54653 | 2016-11-10 21:47:12 +0000 | [diff] [blame] | 957 | for (const PartialDiagnosticAt &PDiag : Diags) |
| 958 | S.Diag(PDiag.first, PDiag.second); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 959 | return false; |
| 960 | } |
| 961 | return true; |
| 962 | } |
| 963 | |
| 964 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 965 | S.Diag(Attr.getLoc(), diag::ext_clang_enable_if); |
| 966 | |
| 967 | Expr *Cond; |
| 968 | StringRef Msg; |
| 969 | if (checkFunctionConditionAttr(S, D, Attr, Cond, Msg)) |
| 970 | D->addAttr(::new (S.Context) |
| 971 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 972 | Attr.getAttributeSpellingListIndex())); |
| 973 | } |
| 974 | |
| 975 | namespace { |
| 976 | /// Determines if a given Expr references any of the given function's |
| 977 | /// ParmVarDecls, or the function's implicit `this` parameter (if applicable). |
| 978 | class ArgumentDependenceChecker |
| 979 | : public RecursiveASTVisitor<ArgumentDependenceChecker> { |
| 980 | #ifndef NDEBUG |
| 981 | const CXXRecordDecl *ClassType; |
| 982 | #endif |
| 983 | llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms; |
| 984 | bool Result; |
| 985 | |
| 986 | public: |
| 987 | ArgumentDependenceChecker(const FunctionDecl *FD) { |
| 988 | #ifndef NDEBUG |
| 989 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 990 | ClassType = MD->getParent(); |
| 991 | else |
| 992 | ClassType = nullptr; |
| 993 | #endif |
| 994 | Parms.insert(FD->param_begin(), FD->param_end()); |
| 995 | } |
| 996 | |
| 997 | bool referencesArgs(Expr *E) { |
| 998 | Result = false; |
| 999 | TraverseStmt(E); |
| 1000 | return Result; |
| 1001 | } |
| 1002 | |
| 1003 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 1004 | assert(E->getType()->getPointeeCXXRecordDecl() == ClassType && |
| 1005 | "`this` doesn't refer to the enclosing class?"); |
| 1006 | Result = true; |
| 1007 | return false; |
| 1008 | } |
| 1009 | |
| 1010 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 1011 | if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) |
| 1012 | if (Parms.count(PVD)) { |
| 1013 | Result = true; |
| 1014 | return false; |
| 1015 | } |
| 1016 | return true; |
| 1017 | } |
| 1018 | }; |
| 1019 | } |
| 1020 | |
| 1021 | static void handleDiagnoseIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1022 | S.Diag(Attr.getLoc(), diag::ext_clang_diagnose_if); |
| 1023 | |
| 1024 | Expr *Cond; |
| 1025 | StringRef Msg; |
| 1026 | if (!checkFunctionConditionAttr(S, D, Attr, Cond, Msg)) |
| 1027 | return; |
| 1028 | |
| 1029 | StringRef DiagTypeStr; |
| 1030 | if (!S.checkStringLiteralArgumentAttr(Attr, 2, DiagTypeStr)) |
| 1031 | return; |
| 1032 | |
| 1033 | DiagnoseIfAttr::DiagnosticType DiagType; |
| 1034 | if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) { |
| 1035 | S.Diag(Attr.getArgAsExpr(2)->getLocStart(), |
| 1036 | diag::err_diagnose_if_invalid_diagnostic_type); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1037 | return; |
| 1038 | } |
| 1039 | |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1040 | bool ArgDependent = false; |
Argyrios Kyrtzidis | 5f0c0aa | 2017-05-24 18:35:01 +0000 | [diff] [blame] | 1041 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1042 | ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1043 | D->addAttr(::new (S.Context) DiagnoseIfAttr( |
Argyrios Kyrtzidis | a7233bd | 2017-05-24 00:46:27 +0000 | [diff] [blame] | 1044 | Attr.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent, cast<NamedDecl>(D), |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1045 | Attr.getAttributeSpellingListIndex())); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1048 | static void handlePassObjectSizeAttr(Sema &S, Decl *D, |
| 1049 | const AttributeList &Attr) { |
| 1050 | if (D->hasAttr<PassObjectSizeAttr>()) { |
| 1051 | S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter) |
| 1052 | << Attr.getName(); |
| 1053 | return; |
| 1054 | } |
| 1055 | |
| 1056 | Expr *E = Attr.getArgAsExpr(0); |
| 1057 | uint32_t Type; |
| 1058 | if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1)) |
| 1059 | return; |
| 1060 | |
| 1061 | // pass_object_size's argument is passed in as the second argument of |
| 1062 | // __builtin_object_size. So, it has the same constraints as that second |
| 1063 | // argument; namely, it must be in the range [0, 3]. |
| 1064 | if (Type > 3) { |
| 1065 | S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range) |
| 1066 | << Attr.getName() << 0 << 3 << E->getSourceRange(); |
| 1067 | return; |
| 1068 | } |
| 1069 | |
| 1070 | // pass_object_size is only supported on constant pointer parameters; as a |
| 1071 | // kindness to users, we allow the parameter to be non-const for declarations. |
| 1072 | // At this point, we have no clue if `D` belongs to a function declaration or |
| 1073 | // definition, so we defer the constness check until later. |
| 1074 | if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) { |
| 1075 | S.Diag(D->getLocStart(), diag::err_attribute_pointers_only) |
| 1076 | << Attr.getName() << 1; |
| 1077 | return; |
| 1078 | } |
| 1079 | |
| 1080 | D->addAttr(::new (S.Context) |
| 1081 | PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type, |
| 1082 | Attr.getAttributeSpellingListIndex())); |
| 1083 | } |
| 1084 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1085 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1086 | ConsumableAttr::ConsumedState DefaultState; |
| 1087 | |
| 1088 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1089 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1090 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1091 | DefaultState)) { |
| 1092 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1093 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1094 | return; |
| 1095 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1096 | } else { |
| 1097 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1098 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 1099 | return; |
| 1100 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1101 | |
| 1102 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1103 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1104 | Attr.getAttributeSpellingListIndex())); |
| 1105 | } |
| 1106 | |
| 1107 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 1108 | const AttributeList &Attr) { |
| 1109 | ASTContext &CurrContext = S.getASTContext(); |
| 1110 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 1111 | |
| 1112 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1113 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 1114 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 1115 | RD->getNameAsString(); |
| 1116 | |
| 1117 | return false; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | return true; |
| 1122 | } |
| 1123 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1124 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 1125 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1126 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1127 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1128 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1129 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1130 | return; |
| 1131 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1132 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 1133 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 1134 | CallableWhenAttr::ConsumedState CallableState; |
| 1135 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1136 | StringRef StateString; |
| 1137 | SourceLocation Loc; |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 1138 | if (Attr.isArgIdent(ArgIndex)) { |
| 1139 | IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex); |
| 1140 | StateString = Ident->Ident->getName(); |
| 1141 | Loc = Ident->Loc; |
| 1142 | } else { |
| 1143 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 1144 | return; |
| 1145 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1146 | |
| 1147 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1148 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1149 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 1150 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1151 | return; |
| 1152 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1153 | |
| 1154 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1155 | } |
| 1156 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1157 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1158 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 1159 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1162 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 1163 | const AttributeList &Attr) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1164 | ParamTypestateAttr::ConsumedState ParamState; |
| 1165 | |
| 1166 | if (Attr.isArgIdent(0)) { |
| 1167 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1168 | StringRef StateString = Ident->Ident->getName(); |
| 1169 | |
| 1170 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 1171 | ParamState)) { |
| 1172 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1173 | << Attr.getName() << StateString; |
| 1174 | return; |
| 1175 | } |
| 1176 | } else { |
| 1177 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1178 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1179 | return; |
| 1180 | } |
| 1181 | |
| 1182 | // FIXME: This check is currently being done in the analysis. It can be |
| 1183 | // enabled here only after the parser propagates attributes at |
| 1184 | // template specialization definition, not declaration. |
| 1185 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 1186 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1187 | // |
| 1188 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1189 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1190 | // ReturnType.getAsString(); |
| 1191 | // return; |
| 1192 | //} |
| 1193 | |
| 1194 | D->addAttr(::new (S.Context) |
| 1195 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 1196 | Attr.getAttributeSpellingListIndex())); |
| 1197 | } |
| 1198 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1199 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 1200 | const AttributeList &Attr) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1201 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 1202 | |
| 1203 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1204 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1205 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1206 | ReturnState)) { |
| 1207 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1208 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1209 | return; |
| 1210 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1211 | } else { |
| 1212 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1213 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1214 | return; |
| 1215 | } |
| 1216 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1217 | // FIXME: This check is currently being done in the analysis. It can be |
| 1218 | // enabled here only after the parser propagates attributes at |
| 1219 | // template specialization definition, not declaration. |
| 1220 | //QualType ReturnType; |
| 1221 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1222 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1223 | // ReturnType = Param->getType(); |
| 1224 | // |
| 1225 | //} else if (const CXXConstructorDecl *Constructor = |
| 1226 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1227 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1228 | // |
| 1229 | //} else { |
| 1230 | // |
| 1231 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1232 | //} |
| 1233 | // |
| 1234 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1235 | // |
| 1236 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1237 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1238 | // ReturnType.getAsString(); |
| 1239 | // return; |
| 1240 | //} |
| 1241 | |
| 1242 | D->addAttr(::new (S.Context) |
| 1243 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1244 | Attr.getAttributeSpellingListIndex())); |
| 1245 | } |
| 1246 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1247 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1248 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1249 | return; |
| 1250 | |
| 1251 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1252 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1253 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1254 | StringRef Param = Ident->Ident->getName(); |
| 1255 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1256 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1257 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1258 | return; |
| 1259 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1260 | } else { |
| 1261 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1262 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1263 | return; |
| 1264 | } |
| 1265 | |
| 1266 | D->addAttr(::new (S.Context) |
| 1267 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1268 | Attr.getAttributeSpellingListIndex())); |
| 1269 | } |
| 1270 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1271 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1272 | const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1273 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1274 | return; |
| 1275 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1276 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1277 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1278 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1279 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1280 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1281 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1282 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1283 | return; |
| 1284 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1285 | } else { |
| 1286 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1287 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1288 | return; |
| 1289 | } |
| 1290 | |
| 1291 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1292 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1293 | Attr.getAttributeSpellingListIndex())); |
| 1294 | } |
| 1295 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1296 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1297 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1298 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1299 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1302 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1303 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1304 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1305 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1306 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1307 | bool BitfieldByteAligned = (!FD->getType()->isDependentType() && |
| 1308 | !FD->getType()->isIncompleteType() && |
| 1309 | FD->isBitField() && |
| 1310 | S.Context.getTypeAlign(FD->getType()) <= 8); |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1311 | |
Aaron Ballman | b6fd726 | 2017-08-08 18:07:17 +0000 | [diff] [blame] | 1312 | if (S.getASTContext().getTargetInfo().getTriple().isPS4()) { |
| 1313 | if (BitfieldByteAligned) |
| 1314 | // The PS4 target needs to maintain ABI backwards compatibility. |
| 1315 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored_for_field_of_type) |
| 1316 | << Attr.getName() << FD->getType(); |
| 1317 | else |
| 1318 | FD->addAttr(::new (S.Context) PackedAttr( |
| 1319 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1320 | } else { |
| 1321 | // Report warning about changed offset in the newer compiler versions. |
| 1322 | if (BitfieldByteAligned) |
| 1323 | S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield); |
| 1324 | |
| 1325 | FD->addAttr(::new (S.Context) PackedAttr( |
| 1326 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1327 | } |
| 1328 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1329 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1330 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1333 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1334 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1335 | // variables or properties of Objective-C classes. The outlet must also |
| 1336 | // have an object reference type. |
| 1337 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1338 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1339 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1340 | << Attr.getName() << VD->getType() << 0; |
| 1341 | return false; |
| 1342 | } |
| 1343 | } |
| 1344 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1345 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1346 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1347 | << Attr.getName() << PD->getType() << 1; |
| 1348 | return false; |
| 1349 | } |
| 1350 | } |
| 1351 | else { |
| 1352 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1353 | return false; |
| 1354 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1355 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1356 | return true; |
| 1357 | } |
| 1358 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1359 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1360 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1361 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1362 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1363 | D->addAttr(::new (S.Context) |
| 1364 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1365 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1368 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1369 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1370 | |
| 1371 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1372 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1373 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1374 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1375 | return; |
| 1376 | } |
| 1377 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1378 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1379 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1380 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1381 | ParsedType PT; |
| 1382 | |
| 1383 | if (Attr.hasParsedType()) |
| 1384 | PT = Attr.getTypeArg(); |
| 1385 | else { |
| 1386 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1387 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1388 | if (!PT) { |
| 1389 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1390 | return; |
| 1391 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1392 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1393 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1394 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1395 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1396 | if (!QTLoc) |
| 1397 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1398 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1399 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1400 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1401 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1402 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1403 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1404 | S.Diag(Attr.getLoc(), |
| 1405 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1406 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1407 | return; |
| 1408 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1409 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1410 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1411 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1412 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1415 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1416 | if (RefOkay) { |
| 1417 | if (T->isReferenceType()) |
| 1418 | return true; |
| 1419 | } else { |
| 1420 | T = T.getNonReferenceType(); |
| 1421 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1422 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1423 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1424 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1425 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1426 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1427 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1428 | for (const auto *I : UD->fields()) { |
| 1429 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1430 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1431 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1432 | } |
| 1433 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1439 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1440 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1441 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1442 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1443 | if (!S.isValidPointerAttrType(T)) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1444 | if (isReturnValue) |
| 1445 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1446 | << Attr.getName() << AttrParmRange << TypeRange; |
| 1447 | else |
| 1448 | S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only) |
| 1449 | << Attr.getName() << AttrParmRange << TypeRange << 0; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1450 | return false; |
| 1451 | } |
| 1452 | return true; |
| 1453 | } |
| 1454 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1455 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1456 | SmallVector<unsigned, 8> NonNullArgs; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1457 | for (unsigned I = 0; I < Attr.getNumArgs(); ++I) { |
| 1458 | Expr *Ex = Attr.getArgAsExpr(I); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1459 | uint64_t Idx; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1460 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1461 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1462 | |
| 1463 | // Is the function argument a pointer type? |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1464 | if (Idx < getFunctionOrMethodNumParams(D) && |
| 1465 | !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1466 | Ex->getSourceRange(), |
| 1467 | getFunctionOrMethodParamRange(D, Idx))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1468 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1469 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1470 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1471 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1472 | |
| 1473 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1474 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1475 | // check if the attribute came from a macro expansion or a template |
| 1476 | // instantiation. |
| 1477 | if (NonNullArgs.empty() && Attr.getLoc().isFileID() && |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1478 | !S.inTemplateInstantiation()) { |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1479 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1480 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1481 | I != E && !AnyPointers; ++I) { |
| 1482 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1483 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1484 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1485 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1486 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1487 | if (!AnyPointers) |
| 1488 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1489 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1490 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1491 | unsigned *Start = NonNullArgs.data(); |
| 1492 | unsigned Size = NonNullArgs.size(); |
| 1493 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1494 | D->addAttr(::new (S.Context) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1495 | NonNullAttr(Attr.getRange(), S.Context, Start, Size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1496 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1499 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1500 | const AttributeList &Attr) { |
| 1501 | if (Attr.getNumArgs() > 0) { |
| 1502 | if (D->getFunctionType()) { |
| 1503 | handleNonNullAttr(S, D, Attr); |
| 1504 | } else { |
| 1505 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1506 | << D->getSourceRange(); |
| 1507 | } |
| 1508 | return; |
| 1509 | } |
| 1510 | |
| 1511 | // Is the argument a pointer type? |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1512 | if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(), |
| 1513 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1514 | return; |
| 1515 | |
| 1516 | D->addAttr(::new (S.Context) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1517 | NonNullAttr(Attr.getRange(), S.Context, nullptr, 0, |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1518 | Attr.getAttributeSpellingListIndex())); |
| 1519 | } |
| 1520 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1521 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1522 | const AttributeList &Attr) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1523 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1524 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1525 | if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1526 | /* isReturnValue */ true)) |
| 1527 | return; |
| 1528 | |
| 1529 | D->addAttr(::new (S.Context) |
| 1530 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1531 | Attr.getAttributeSpellingListIndex())); |
| 1532 | } |
| 1533 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1534 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, |
| 1535 | const AttributeList &Attr) { |
| 1536 | Expr *E = Attr.getArgAsExpr(0), |
| 1537 | *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr; |
| 1538 | S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE, |
| 1539 | Attr.getAttributeSpellingListIndex()); |
| 1540 | } |
| 1541 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1542 | static void handleAllocAlignAttr(Sema &S, Decl *D, |
| 1543 | const AttributeList &Attr) { |
| 1544 | S.AddAllocAlignAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 1545 | Attr.getAttributeSpellingListIndex()); |
| 1546 | } |
| 1547 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1548 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1549 | Expr *OE, unsigned SpellingListIndex) { |
| 1550 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1551 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1552 | |
| 1553 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1554 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1555 | |
| 1556 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1557 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1558 | << &TmpAttr << AttrRange << SR; |
| 1559 | return; |
| 1560 | } |
| 1561 | |
| 1562 | if (!E->isValueDependent()) { |
| 1563 | llvm::APSInt I(64); |
| 1564 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1565 | if (OE) |
| 1566 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1567 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1568 | << E->getSourceRange(); |
| 1569 | else |
| 1570 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1571 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1572 | << E->getSourceRange(); |
| 1573 | return; |
| 1574 | } |
| 1575 | |
| 1576 | if (!I.isPowerOf2()) { |
| 1577 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1578 | << E->getSourceRange(); |
| 1579 | return; |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | if (OE) { |
| 1584 | if (!OE->isValueDependent()) { |
| 1585 | llvm::APSInt I(64); |
| 1586 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1587 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1588 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1589 | << OE->getSourceRange(); |
| 1590 | return; |
| 1591 | } |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | D->addAttr(::new (Context) |
| 1596 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1597 | } |
| 1598 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1599 | void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr, |
| 1600 | unsigned SpellingListIndex) { |
| 1601 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1602 | |
| 1603 | AllocAlignAttr TmpAttr(AttrRange, Context, 0, SpellingListIndex); |
| 1604 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1605 | |
| 1606 | if (!ResultType->isDependentType() && |
| 1607 | !isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1608 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1609 | << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D); |
| 1610 | return; |
| 1611 | } |
| 1612 | |
| 1613 | uint64_t IndexVal; |
| 1614 | const auto *FuncDecl = cast<FunctionDecl>(D); |
| 1615 | if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr, |
| 1616 | /*AttrArgNo=*/1, ParamExpr, |
| 1617 | IndexVal)) |
| 1618 | return; |
| 1619 | |
| 1620 | QualType Ty = getFunctionOrMethodParamType(D, IndexVal); |
| 1621 | if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) { |
| 1622 | Diag(ParamExpr->getLocStart(), diag::err_attribute_integers_only) |
| 1623 | << &TmpAttr << FuncDecl->getParamDecl(IndexVal)->getSourceRange(); |
| 1624 | return; |
| 1625 | } |
| 1626 | |
| 1627 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
| 1628 | // because that has corrected for the implicit this parameter, and is zero- |
| 1629 | // based. The attribute expects what the user wrote explicitly. |
| 1630 | llvm::APSInt Val; |
| 1631 | ParamExpr->EvaluateAsInt(Val, Context); |
| 1632 | |
| 1633 | D->addAttr(::new (Context) AllocAlignAttr( |
| 1634 | AttrRange, Context, Val.getZExtValue(), SpellingListIndex)); |
| 1635 | } |
| 1636 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1637 | /// Normalize the attribute, __foo__ becomes foo. |
| 1638 | /// Returns true if normalization was applied. |
| 1639 | static bool normalizeName(StringRef &AttrName) { |
Aaron Ballman | 6269236 | 2015-10-09 13:53:24 +0000 | [diff] [blame] | 1640 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1641 | AttrName.endswith("__")) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1642 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1643 | return true; |
| 1644 | } |
| 1645 | return false; |
| 1646 | } |
| 1647 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1648 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1649 | // This attribute must be applied to a function declaration. The first |
| 1650 | // argument to the attribute must be an identifier, the name of the resource, |
| 1651 | // for example: malloc. The following arguments must be argument indexes, the |
| 1652 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1653 | // 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] | 1654 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1655 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1656 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1657 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1658 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1659 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1660 | return; |
| 1661 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1662 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1663 | // Figure out our Kind. |
| 1664 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1665 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1666 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1667 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1668 | // Check arguments. |
| 1669 | switch (K) { |
| 1670 | case OwnershipAttr::Takes: |
| 1671 | case OwnershipAttr::Holds: |
| 1672 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1673 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1674 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1675 | return; |
| 1676 | } |
| 1677 | break; |
| 1678 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1679 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1680 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1681 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1682 | return; |
| 1683 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1684 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1687 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1688 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1689 | StringRef ModuleName = Module->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1690 | if (normalizeName(ModuleName)) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1691 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1692 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1693 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1694 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1695 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1696 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1697 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1698 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1699 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1700 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1701 | // Is the function argument a pointer type? |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1702 | QualType T = getFunctionOrMethodParamType(D, Idx); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1703 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1704 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1705 | case OwnershipAttr::Takes: |
| 1706 | case OwnershipAttr::Holds: |
| 1707 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1708 | Err = 0; |
| 1709 | break; |
| 1710 | case OwnershipAttr::Returns: |
| 1711 | if (!T->isIntegerType()) |
| 1712 | Err = 1; |
| 1713 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1714 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1715 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1716 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1717 | << Ex->getSourceRange(); |
| 1718 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1719 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1720 | |
| 1721 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1722 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1723 | // Cannot have two ownership attributes of different kinds for the same |
| 1724 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1725 | if (I->getOwnKind() != K && I->args_end() != |
| 1726 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1727 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1728 | << AL.getName() << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1729 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1730 | } else if (K == OwnershipAttr::Returns && |
| 1731 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1732 | // A returns attribute conflicts with any other returns attribute using |
| 1733 | // a different index. Note, diagnostic reporting is 1-based, but stored |
| 1734 | // argument indexes are 0-based. |
| 1735 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1736 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
| 1737 | << *(I->args_begin()) + 1; |
| 1738 | if (I->args_size()) |
| 1739 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
| 1740 | << (unsigned)Idx + 1 << Ex->getSourceRange(); |
| 1741 | return; |
| 1742 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1743 | } |
| 1744 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1745 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
| 1748 | unsigned* start = OwnershipArgs.data(); |
| 1749 | unsigned size = OwnershipArgs.size(); |
| 1750 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1751 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1752 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1753 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1754 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1757 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1758 | // Check the attribute arguments. |
| 1759 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1760 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1761 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1762 | return; |
| 1763 | } |
| 1764 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1765 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1766 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1767 | // gcc rejects |
| 1768 | // class c { |
| 1769 | // static int a __attribute__((weakref ("v2"))); |
| 1770 | // static int b() __attribute__((weakref ("f3"))); |
| 1771 | // }; |
| 1772 | // and ignores the attributes of |
| 1773 | // void f(void) { |
| 1774 | // static int a __attribute__((weakref ("v2"))); |
| 1775 | // } |
| 1776 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1777 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1778 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1779 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1780 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1781 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1782 | } |
| 1783 | |
| 1784 | // The GCC manual says |
| 1785 | // |
| 1786 | // At present, a declaration to which `weakref' is attached can only |
| 1787 | // be `static'. |
| 1788 | // |
| 1789 | // It also says |
| 1790 | // |
| 1791 | // Without a TARGET, |
| 1792 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1793 | // equivalent to `weak'. |
| 1794 | // |
| 1795 | // gcc 4.4.1 will accept |
| 1796 | // int a7 __attribute__((weakref)); |
| 1797 | // as |
| 1798 | // int a7 __attribute__((weak)); |
| 1799 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1800 | // it if this behaviour is actually used. |
| 1801 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1802 | // GCC rejects |
| 1803 | // static ((alias ("y"), weakref)). |
| 1804 | // Should we? How to check that weakref is before or after alias? |
| 1805 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1806 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1807 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1808 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1809 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1810 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1811 | // GCC will accept anything as the argument of weakref. Should we |
| 1812 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1813 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1814 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1815 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1816 | D->addAttr(::new (S.Context) |
| 1817 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1818 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1821 | static void handleIFuncAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1822 | StringRef Str; |
| 1823 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
| 1824 | return; |
| 1825 | |
| 1826 | // Aliases should be on declarations, not definitions. |
| 1827 | const auto *FD = cast<FunctionDecl>(D); |
| 1828 | if (FD->isThisDeclarationADefinition()) { |
| 1829 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD << 1; |
| 1830 | return; |
| 1831 | } |
| 1832 | // FIXME: it should be handled as a target specific attribute. |
| 1833 | if (S.Context.getTargetInfo().getTriple().getObjectFormat() != |
| 1834 | llvm::Triple::ELF) { |
| 1835 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1836 | return; |
| 1837 | } |
| 1838 | |
| 1839 | D->addAttr(::new (S.Context) IFuncAttr(Attr.getRange(), S.Context, Str, |
| 1840 | Attr.getAttributeSpellingListIndex())); |
| 1841 | } |
| 1842 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1843 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1844 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1845 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1846 | return; |
| 1847 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1848 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1849 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1850 | return; |
| 1851 | } |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1852 | if (S.Context.getTargetInfo().getTriple().isNVPTX()) { |
| 1853 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_nvptx); |
| 1854 | } |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1855 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1856 | // Aliases should be on declarations, not definitions. |
| 1857 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1858 | if (FD->isThisDeclarationADefinition()) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1859 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1860 | return; |
| 1861 | } |
| 1862 | } else { |
| 1863 | const auto *VD = cast<VarDecl>(D); |
| 1864 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1865 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1866 | return; |
| 1867 | } |
| 1868 | } |
| 1869 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1870 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1871 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1872 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1873 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1876 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1877 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1878 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1879 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1880 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1881 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
| 1884 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1885 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1886 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1887 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1888 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1889 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1892 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1893 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1894 | StringRef Model; |
| 1895 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1896 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1897 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1898 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1899 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1900 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1901 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1902 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1903 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1904 | return; |
| 1905 | } |
| 1906 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1907 | D->addAttr(::new (S.Context) |
| 1908 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1909 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1912 | static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1913 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1914 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1915 | D->addAttr(::new (S.Context) RestrictAttr( |
| 1916 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1917 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1920 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1921 | << Attr.getName() << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1924 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1925 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1926 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1927 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1928 | return; |
| 1929 | } |
| 1930 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1931 | if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(), |
| 1932 | Attr.getAttributeSpellingListIndex())) |
| 1933 | D->addAttr(CA); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1934 | } |
| 1935 | |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 1936 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1937 | if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(), |
| 1938 | Attr.getName())) |
| 1939 | return; |
| 1940 | |
Saleem Abdulrasool | b51bcaf | 2017-04-07 15:13:47 +0000 | [diff] [blame] | 1941 | if (Attr.isDeclspecAttribute()) { |
| 1942 | const auto &Triple = S.getASTContext().getTargetInfo().getTriple(); |
| 1943 | const auto &Arch = Triple.getArch(); |
| 1944 | if (Arch != llvm::Triple::x86 && |
| 1945 | (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) { |
| 1946 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_on_arch) |
| 1947 | << Attr.getName() << Triple.getArchName(); |
| 1948 | return; |
| 1949 | } |
| 1950 | } |
| 1951 | |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 1952 | D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context, |
| 1953 | Attr.getAttributeSpellingListIndex())); |
| 1954 | } |
| 1955 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1956 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1957 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1958 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1959 | if (S.CheckNoReturnAttr(attr)) |
| 1960 | return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1961 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1962 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1963 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1964 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1965 | return; |
| 1966 | } |
| 1967 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1968 | D->addAttr(::new (S.Context) NoReturnAttr( |
| 1969 | attr.getRange(), S.Context, attr.getAttributeSpellingListIndex())); |
| 1970 | } |
| 1971 | |
| 1972 | static void handleNoCallerSavedRegsAttr(Sema &S, Decl *D, |
| 1973 | const AttributeList &Attr) { |
| 1974 | if (S.CheckNoCallerSavedRegsAttr(Attr)) |
| 1975 | return; |
| 1976 | |
| 1977 | D->addAttr(::new (S.Context) AnyX86NoCallerSavedRegistersAttr( |
| 1978 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1982 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1983 | attr.setInvalid(); |
| 1984 | return true; |
| 1985 | } |
| 1986 | |
| 1987 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1990 | bool Sema::CheckNoCallerSavedRegsAttr(const AttributeList &Attr) { |
| 1991 | // Check whether the attribute is valid on the current target. |
| 1992 | if (!Attr.existsInTarget(Context.getTargetInfo())) { |
| 1993 | Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) << Attr.getName(); |
| 1994 | Attr.setInvalid(); |
| 1995 | return true; |
| 1996 | } |
| 1997 | |
| 1998 | if (!checkAttributeNumArgs(*this, Attr, 0)) { |
| 1999 | Attr.setInvalid(); |
| 2000 | return true; |
| 2001 | } |
| 2002 | |
| 2003 | return false; |
| 2004 | } |
| 2005 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2006 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 2007 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 2008 | |
| 2009 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 2010 | // because 'analyzer_noreturn' does not impact the type. |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 2011 | if (!isFunctionOrMethodOrBlock(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2012 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2013 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 2014 | !VD->getType()->isFunctionPointerType())) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 2015 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2016 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 2017 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2018 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 2019 | return; |
| 2020 | } |
| 2021 | } |
| 2022 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2023 | D->addAttr(::new (S.Context) |
| 2024 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 2025 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2028 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2029 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2030 | /* |
| 2031 | Returning a Vector Class in Registers |
| 2032 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2033 | According to the PPU ABI specifications, a class with a single member of |
| 2034 | vector type is returned in memory when used as the return value of a function. |
| 2035 | This results in inefficient code when implementing vector classes. To return |
| 2036 | the value in a single vector register, add the vecreturn attribute to the |
| 2037 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2038 | |
| 2039 | Example: |
| 2040 | |
| 2041 | struct Vector |
| 2042 | { |
| 2043 | __vector float xyzw; |
| 2044 | } __attribute__((vecreturn)); |
| 2045 | |
| 2046 | Vector Add(Vector lhs, Vector rhs) |
| 2047 | { |
| 2048 | Vector result; |
| 2049 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 2050 | return result; // This will be returned in a register |
| 2051 | } |
| 2052 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 2053 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 2054 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2055 | return; |
| 2056 | } |
| 2057 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2058 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2059 | int count = 0; |
| 2060 | |
| 2061 | if (!isa<CXXRecordDecl>(record)) { |
| 2062 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 2063 | return; |
| 2064 | } |
| 2065 | |
| 2066 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 2067 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 2068 | return; |
| 2069 | } |
| 2070 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 2071 | for (const auto *I : record->fields()) { |
| 2072 | if ((count == 1) || !I->getType()->isVectorType()) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2073 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 2074 | return; |
| 2075 | } |
| 2076 | count++; |
| 2077 | } |
| 2078 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2079 | D->addAttr(::new (S.Context) |
| 2080 | VecReturnAttr(Attr.getRange(), S.Context, |
| 2081 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2082 | } |
| 2083 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2084 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 2085 | const AttributeList &Attr) { |
| 2086 | if (isa<ParmVarDecl>(D)) { |
| 2087 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 2088 | // parameter of a function declaration or lambda. |
| 2089 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 2090 | S.Diag(Attr.getLoc(), |
| 2091 | diag::err_carries_dependency_param_not_function_decl); |
| 2092 | return; |
| 2093 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2094 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2095 | |
| 2096 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 2097 | Attr.getRange(), S.Context, |
| 2098 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2099 | } |
| 2100 | |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 2101 | static void handleNotTailCalledAttr(Sema &S, Decl *D, |
| 2102 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 2103 | if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(), |
| 2104 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 2105 | return; |
| 2106 | |
| 2107 | D->addAttr(::new (S.Context) NotTailCalledAttr( |
| 2108 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 2109 | } |
| 2110 | |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 2111 | static void handleDisableTailCallsAttr(Sema &S, Decl *D, |
| 2112 | const AttributeList &Attr) { |
| 2113 | if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(), |
| 2114 | Attr.getName())) |
| 2115 | return; |
| 2116 | |
| 2117 | D->addAttr(::new (S.Context) DisableTailCallsAttr( |
| 2118 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 2119 | } |
| 2120 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2121 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2122 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 2123 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 2124 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2125 | return; |
| 2126 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2127 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2128 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2129 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2130 | return; |
| 2131 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2132 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2133 | D->addAttr(::new (S.Context) |
| 2134 | UsedAttr(Attr.getRange(), S.Context, |
| 2135 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2138 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2139 | bool IsCXX1zAttr = Attr.isCXX11Attribute() && !Attr.getScopeName(); |
| 2140 | |
| 2141 | if (IsCXX1zAttr && isa<VarDecl>(D)) { |
| 2142 | // The C++1z spelling of this attribute cannot be applied to a static data |
| 2143 | // member per [dcl.attr.unused]p2. |
| 2144 | if (cast<VarDecl>(D)->isStaticDataMember()) { |
| 2145 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2146 | << Attr.getName() << ExpectedForMaybeUnused; |
| 2147 | return; |
| 2148 | } |
| 2149 | } |
| 2150 | |
| 2151 | // If this is spelled as the standard C++1z attribute, but not in C++1z, warn |
| 2152 | // about using it as an extension. |
| 2153 | if (!S.getLangOpts().CPlusPlus1z && IsCXX1zAttr) |
| 2154 | S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName(); |
| 2155 | |
| 2156 | D->addAttr(::new (S.Context) UnusedAttr( |
| 2157 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 2158 | } |
| 2159 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2160 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2161 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 2162 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2163 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 2164 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2165 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2166 | D->addAttr(::new (S.Context) |
| 2167 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 2168 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2171 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 2172 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 2173 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2174 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 2175 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2176 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2177 | D->addAttr(::new (S.Context) |
| 2178 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2179 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2182 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2183 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2184 | const AttributeList &Attr) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2185 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2186 | StringRef Str; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 2187 | if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2188 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2189 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2190 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2191 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2192 | } |
| 2193 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2194 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2195 | const AttributeList &Attr) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2196 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 2197 | S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
| 2198 | << Attr.getName() << Attr.getRange(); |
| 2199 | return; |
| 2200 | } |
| 2201 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2202 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 2203 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 2204 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2207 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2208 | IdentifierInfo *Platform, |
| 2209 | VersionTuple Introduced, |
| 2210 | VersionTuple Deprecated, |
| 2211 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2212 | StringRef PlatformName |
| 2213 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2214 | if (PlatformName.empty()) |
| 2215 | PlatformName = Platform->getName(); |
| 2216 | |
| 2217 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2218 | // of these steps are needed). |
| 2219 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2220 | !(Introduced <= Deprecated)) { |
| 2221 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2222 | << 1 << PlatformName << Deprecated.getAsString() |
| 2223 | << 0 << Introduced.getAsString(); |
| 2224 | return true; |
| 2225 | } |
| 2226 | |
| 2227 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2228 | !(Introduced <= Obsoleted)) { |
| 2229 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2230 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2231 | << 0 << Introduced.getAsString(); |
| 2232 | return true; |
| 2233 | } |
| 2234 | |
| 2235 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2236 | !(Deprecated <= Obsoleted)) { |
| 2237 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2238 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2239 | << 1 << Deprecated.getAsString(); |
| 2240 | return true; |
| 2241 | } |
| 2242 | |
| 2243 | return false; |
| 2244 | } |
| 2245 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2246 | /// \brief Check whether the two versions match. |
| 2247 | /// |
| 2248 | /// If either version tuple is empty, then they are assumed to match. If |
| 2249 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2250 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2251 | bool BeforeIsOkay) { |
| 2252 | if (X.empty() || Y.empty()) |
| 2253 | return true; |
| 2254 | |
| 2255 | if (X == Y) |
| 2256 | return true; |
| 2257 | |
| 2258 | if (BeforeIsOkay && X < Y) |
| 2259 | return true; |
| 2260 | |
| 2261 | return false; |
| 2262 | } |
| 2263 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2264 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2265 | IdentifierInfo *Platform, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2266 | bool Implicit, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2267 | VersionTuple Introduced, |
| 2268 | VersionTuple Deprecated, |
| 2269 | VersionTuple Obsoleted, |
| 2270 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2271 | StringRef Message, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2272 | bool IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2273 | StringRef Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2274 | AvailabilityMergeKind AMK, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2275 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2276 | VersionTuple MergedIntroduced = Introduced; |
| 2277 | VersionTuple MergedDeprecated = Deprecated; |
| 2278 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2279 | bool FoundAny = false; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2280 | bool OverrideOrImpl = false; |
| 2281 | switch (AMK) { |
| 2282 | case AMK_None: |
| 2283 | case AMK_Redeclaration: |
| 2284 | OverrideOrImpl = false; |
| 2285 | break; |
| 2286 | |
| 2287 | case AMK_Override: |
| 2288 | case AMK_ProtocolImplementation: |
| 2289 | OverrideOrImpl = true; |
| 2290 | break; |
| 2291 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2292 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2293 | if (D->hasAttrs()) { |
| 2294 | AttrVec &Attrs = D->getAttrs(); |
| 2295 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2296 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2297 | if (!OldAA) { |
| 2298 | ++i; |
| 2299 | continue; |
| 2300 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2301 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2302 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2303 | if (OldPlatform != Platform) { |
| 2304 | ++i; |
| 2305 | continue; |
| 2306 | } |
| 2307 | |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2308 | // If there is an existing availability attribute for this platform that |
| 2309 | // is explicit and the new one is implicit use the explicit one and |
| 2310 | // discard the new implicit attribute. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2311 | if (!OldAA->isImplicit() && Implicit) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2312 | return nullptr; |
| 2313 | } |
| 2314 | |
| 2315 | // If there is an existing attribute for this platform that is implicit |
| 2316 | // and the new attribute is explicit then erase the old one and |
| 2317 | // continue processing the attributes. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2318 | if (!Implicit && OldAA->isImplicit()) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2319 | Attrs.erase(Attrs.begin() + i); |
| 2320 | --e; |
| 2321 | continue; |
| 2322 | } |
| 2323 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2324 | FoundAny = true; |
| 2325 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2326 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2327 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2328 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2329 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2330 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 2331 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 2332 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2333 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2334 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 2335 | if (OverrideOrImpl) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2336 | int Which = -1; |
| 2337 | VersionTuple FirstVersion; |
| 2338 | VersionTuple SecondVersion; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2339 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2340 | Which = 0; |
| 2341 | FirstVersion = OldIntroduced; |
| 2342 | SecondVersion = Introduced; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2343 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2344 | Which = 1; |
| 2345 | FirstVersion = Deprecated; |
| 2346 | SecondVersion = OldDeprecated; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2347 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2348 | Which = 2; |
| 2349 | FirstVersion = Obsoleted; |
| 2350 | SecondVersion = OldObsoleted; |
| 2351 | } |
| 2352 | |
| 2353 | if (Which == -1) { |
| 2354 | Diag(OldAA->getLocation(), |
| 2355 | diag::warn_mismatched_availability_override_unavail) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2356 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2357 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2358 | } else { |
| 2359 | Diag(OldAA->getLocation(), |
| 2360 | diag::warn_mismatched_availability_override) |
| 2361 | << Which |
| 2362 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2363 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 2364 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2365 | } |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2366 | if (AMK == AMK_Override) |
| 2367 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2368 | else |
| 2369 | Diag(Range.getBegin(), diag::note_protocol_method); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2370 | } else { |
| 2371 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2372 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2373 | } |
| 2374 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2375 | Attrs.erase(Attrs.begin() + i); |
| 2376 | --e; |
| 2377 | continue; |
| 2378 | } |
| 2379 | |
| 2380 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2381 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2382 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2383 | |
| 2384 | if (MergedIntroduced2.empty()) |
| 2385 | MergedIntroduced2 = OldIntroduced; |
| 2386 | if (MergedDeprecated2.empty()) |
| 2387 | MergedDeprecated2 = OldDeprecated; |
| 2388 | if (MergedObsoleted2.empty()) |
| 2389 | MergedObsoleted2 = OldObsoleted; |
| 2390 | |
| 2391 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2392 | MergedIntroduced2, MergedDeprecated2, |
| 2393 | MergedObsoleted2)) { |
| 2394 | Attrs.erase(Attrs.begin() + i); |
| 2395 | --e; |
| 2396 | continue; |
| 2397 | } |
| 2398 | |
| 2399 | MergedIntroduced = MergedIntroduced2; |
| 2400 | MergedDeprecated = MergedDeprecated2; |
| 2401 | MergedObsoleted = MergedObsoleted2; |
| 2402 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2403 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | if (FoundAny && |
| 2407 | MergedIntroduced == Introduced && |
| 2408 | MergedDeprecated == Deprecated && |
| 2409 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2410 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2411 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2412 | // Only create a new attribute if !OverrideOrImpl, but we want to do |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2413 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2414 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2415 | MergedDeprecated, MergedObsoleted) && |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2416 | !OverrideOrImpl) { |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2417 | auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2418 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2419 | Obsoleted, IsUnavailable, Message, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2420 | IsStrict, Replacement, |
| 2421 | AttrSpellingListIndex); |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2422 | Avail->setImplicit(Implicit); |
| 2423 | return Avail; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2424 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2425 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2426 | } |
| 2427 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2428 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2429 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2430 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2431 | return; |
| 2432 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2433 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2434 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2435 | IdentifierInfo *II = Platform->Ident; |
| 2436 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2437 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2438 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2439 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2440 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Alex Lorenz | 472cc79 | 2017-04-20 09:35:02 +0000 | [diff] [blame] | 2441 | if (!ND) // We warned about this already, so just return. |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2442 | return; |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2443 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2444 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2445 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2446 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2447 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2448 | bool IsStrict = Attr.getStrictLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2449 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 2450 | if (const StringLiteral *SE = |
| 2451 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2452 | Str = SE->getString(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2453 | StringRef Replacement; |
| 2454 | if (const StringLiteral *SE = |
| 2455 | dyn_cast_or_null<StringLiteral>(Attr.getReplacementExpr())) |
| 2456 | Replacement = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2457 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2458 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2459 | false/*Implicit*/, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2460 | Introduced.Version, |
| 2461 | Deprecated.Version, |
| 2462 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2463 | IsUnavailable, Str, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2464 | IsStrict, Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2465 | Sema::AMK_None, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2466 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2467 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2468 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2469 | |
| 2470 | // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning |
| 2471 | // matches before the start of the watchOS platform. |
| 2472 | if (S.Context.getTargetInfo().getTriple().isWatchOS()) { |
| 2473 | IdentifierInfo *NewII = nullptr; |
| 2474 | if (II->getName() == "ios") |
| 2475 | NewII = &S.Context.Idents.get("watchos"); |
| 2476 | else if (II->getName() == "ios_app_extension") |
| 2477 | NewII = &S.Context.Idents.get("watchos_app_extension"); |
| 2478 | |
| 2479 | if (NewII) { |
| 2480 | auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple { |
| 2481 | if (Version.empty()) |
| 2482 | return Version; |
| 2483 | auto Major = Version.getMajor(); |
| 2484 | auto NewMajor = Major >= 9 ? Major - 7 : 0; |
| 2485 | if (NewMajor >= 2) { |
| 2486 | if (Version.getMinor().hasValue()) { |
| 2487 | if (Version.getSubminor().hasValue()) |
| 2488 | return VersionTuple(NewMajor, Version.getMinor().getValue(), |
| 2489 | Version.getSubminor().getValue()); |
| 2490 | else |
| 2491 | return VersionTuple(NewMajor, Version.getMinor().getValue()); |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | return VersionTuple(2, 0); |
| 2496 | }; |
| 2497 | |
| 2498 | auto NewIntroduced = adjustWatchOSVersion(Introduced.Version); |
| 2499 | auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version); |
| 2500 | auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version); |
| 2501 | |
| 2502 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2503 | Attr.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2504 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2505 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2506 | NewIntroduced, |
| 2507 | NewDeprecated, |
| 2508 | NewObsoleted, |
| 2509 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2510 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2511 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2512 | Sema::AMK_None, |
| 2513 | Index); |
| 2514 | if (NewAttr) |
| 2515 | D->addAttr(NewAttr); |
| 2516 | } |
| 2517 | } else if (S.Context.getTargetInfo().getTriple().isTvOS()) { |
| 2518 | // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning |
| 2519 | // matches before the start of the tvOS platform. |
| 2520 | IdentifierInfo *NewII = nullptr; |
| 2521 | if (II->getName() == "ios") |
| 2522 | NewII = &S.Context.Idents.get("tvos"); |
| 2523 | else if (II->getName() == "ios_app_extension") |
| 2524 | NewII = &S.Context.Idents.get("tvos_app_extension"); |
| 2525 | |
| 2526 | if (NewII) { |
| 2527 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2528 | Attr.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2529 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2530 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2531 | Introduced.Version, |
| 2532 | Deprecated.Version, |
| 2533 | Obsoleted.Version, |
| 2534 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2535 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2536 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2537 | Sema::AMK_None, |
| 2538 | Index); |
| 2539 | if (NewAttr) |
| 2540 | D->addAttr(NewAttr); |
| 2541 | } |
| 2542 | } |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2543 | } |
| 2544 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2545 | static void handleExternalSourceSymbolAttr(Sema &S, Decl *D, |
| 2546 | const AttributeList &Attr) { |
| 2547 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 2548 | return; |
| 2549 | assert(checkAttributeAtMostNumArgs(S, Attr, 3) && |
| 2550 | "Invalid number of arguments in an external_source_symbol attribute"); |
| 2551 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2552 | StringRef Language; |
| 2553 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(Attr.getArgAsExpr(0))) |
| 2554 | Language = SE->getString(); |
| 2555 | StringRef DefinedIn; |
| 2556 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(Attr.getArgAsExpr(1))) |
| 2557 | DefinedIn = SE->getString(); |
| 2558 | bool IsGeneratedDeclaration = Attr.getArgAsIdent(2) != nullptr; |
| 2559 | |
| 2560 | D->addAttr(::new (S.Context) ExternalSourceSymbolAttr( |
| 2561 | Attr.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration, |
| 2562 | Attr.getAttributeSpellingListIndex())); |
| 2563 | } |
| 2564 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2565 | template <class T> |
| 2566 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2567 | typename T::VisibilityType value, |
| 2568 | unsigned attrSpellingListIndex) { |
| 2569 | T *existingAttr = D->getAttr<T>(); |
| 2570 | if (existingAttr) { |
| 2571 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2572 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2573 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2574 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2575 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2576 | D->dropAttr<T>(); |
| 2577 | } |
| 2578 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2579 | } |
| 2580 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2581 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2582 | VisibilityAttr::VisibilityType Vis, |
| 2583 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2584 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2585 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2588 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2589 | TypeVisibilityAttr::VisibilityType Vis, |
| 2590 | unsigned AttrSpellingListIndex) { |
| 2591 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2592 | AttrSpellingListIndex); |
| 2593 | } |
| 2594 | |
| 2595 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2596 | bool isTypeVisibility) { |
| 2597 | // Visibility attributes don't mean anything on a typedef. |
| 2598 | if (isa<TypedefNameDecl>(D)) { |
| 2599 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2600 | << Attr.getName(); |
| 2601 | return; |
| 2602 | } |
| 2603 | |
| 2604 | // 'type_visibility' can only go on a type or namespace. |
| 2605 | if (isTypeVisibility && |
| 2606 | !(isa<TagDecl>(D) || |
| 2607 | isa<ObjCInterfaceDecl>(D) || |
| 2608 | isa<NamespaceDecl>(D))) { |
| 2609 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2610 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2611 | return; |
| 2612 | } |
| 2613 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2614 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2615 | StringRef TypeStr; |
| 2616 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2617 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2618 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2619 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2620 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2621 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2622 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2623 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2624 | return; |
| 2625 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2626 | |
| 2627 | // Complain about attempts to use protected visibility on targets |
| 2628 | // (like Darwin) that don't support it. |
| 2629 | if (type == VisibilityAttr::Protected && |
| 2630 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2631 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2632 | type = VisibilityAttr::Default; |
| 2633 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2634 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2635 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2636 | clang::Attr *newAttr; |
| 2637 | if (isTypeVisibility) { |
| 2638 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2639 | (TypeVisibilityAttr::VisibilityType) type, |
| 2640 | Index); |
| 2641 | } else { |
| 2642 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2643 | } |
| 2644 | if (newAttr) |
| 2645 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2646 | } |
| 2647 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2648 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2649 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2650 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2651 | if (!Attr.isArgIdent(0)) { |
| 2652 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2653 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2654 | return; |
| 2655 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2656 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2657 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2658 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2659 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2660 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2661 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2662 | return; |
| 2663 | } |
| 2664 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2665 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 2666 | !method->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2667 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2668 | << method->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2669 | // Ignore the attribute. |
| 2670 | return; |
| 2671 | } |
| 2672 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2673 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2674 | S.Context, F, |
| 2675 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2678 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2679 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2680 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2681 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2682 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2683 | return; |
| 2684 | } |
| 2685 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2686 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2687 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2688 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2689 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2690 | return; |
| 2691 | } |
| 2692 | } |
| 2693 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2694 | // It is okay to include this attribute on properties, e.g.: |
| 2695 | // |
| 2696 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2697 | // |
| 2698 | // In this case it follows tradition and suppresses an error in the above |
| 2699 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2700 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2701 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2702 | D->addAttr(::new (S.Context) |
| 2703 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2704 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2707 | static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2708 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 2709 | QualType T = TD->getUnderlyingType(); |
| 2710 | if (!T->isObjCObjectPointerType()) { |
| 2711 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2712 | return; |
| 2713 | } |
| 2714 | } else { |
| 2715 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2716 | return; |
| 2717 | } |
| 2718 | D->addAttr(::new (S.Context) |
| 2719 | ObjCIndependentClassAttr(Attr.getRange(), S.Context, |
| 2720 | Attr.getAttributeSpellingListIndex())); |
| 2721 | } |
| 2722 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2723 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2724 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2725 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2726 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2727 | return; |
| 2728 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2729 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2730 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2731 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2732 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2733 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2734 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2735 | return; |
| 2736 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2737 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2738 | D->addAttr(::new (S.Context) |
| 2739 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2740 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2743 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2744 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2745 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2746 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2747 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2748 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2749 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2750 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2751 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2752 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2753 | return; |
| 2754 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2755 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2756 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2757 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2758 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2759 | return; |
| 2760 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2761 | |
| 2762 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2765 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2766 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2767 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2768 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2769 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2770 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2771 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2772 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2773 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2774 | return; |
| 2775 | } |
| 2776 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2777 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2778 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2779 | // FIXME: This error message could be improved, it would be nice |
| 2780 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2781 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2782 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2783 | return; |
| 2784 | } |
| 2785 | } |
| 2786 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2787 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2788 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2789 | if (isa<FunctionNoProtoType>(FT)) { |
| 2790 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2791 | return; |
| 2792 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2793 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2794 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2795 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2796 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2797 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2798 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2799 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2800 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2801 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2802 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2803 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2804 | if (!BD->isVariadic()) { |
| 2805 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2806 | return; |
| 2807 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2808 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2809 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2810 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2811 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2812 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2813 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2814 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2815 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2816 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2817 | return; |
| 2818 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2819 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2820 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2821 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2822 | return; |
| 2823 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2824 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2825 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2826 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2827 | return; |
| 2828 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2829 | D->addAttr(::new (S.Context) |
| 2830 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2831 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2834 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2835 | if (D->getFunctionType() && |
| 2836 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2837 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2838 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2839 | return; |
| 2840 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2841 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2842 | if (MD->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2843 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2844 | << Attr.getName() << 1; |
| 2845 | return; |
| 2846 | } |
| 2847 | |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2848 | // If this is spelled as the standard C++1z attribute, but not in C++1z, warn |
| 2849 | // about using it as an extension. |
| 2850 | if (!S.getLangOpts().CPlusPlus1z && Attr.isCXX11Attribute() && |
| 2851 | !Attr.getScopeName()) |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 2852 | S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName(); |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2853 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2854 | D->addAttr(::new (S.Context) |
| 2855 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2856 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2859 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2860 | // weak_import only applies to variable & function declarations. |
| 2861 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2862 | if (!D->canBeWeakImported(isDef)) { |
| 2863 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2864 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2865 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2866 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2867 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2868 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2869 | // Nothing to warn about here. |
| 2870 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2871 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2872 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2873 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2874 | return; |
| 2875 | } |
| 2876 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2877 | D->addAttr(::new (S.Context) |
| 2878 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2879 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2880 | } |
| 2881 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2882 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2883 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2884 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2885 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2886 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2887 | for (unsigned i = 0; i < 3; ++i) { |
| 2888 | const Expr *E = Attr.getArgAsExpr(i); |
| 2889 | if (!checkUInt32Argument(S, Attr, E, WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2890 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2891 | if (WGSize[i] == 0) { |
| 2892 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero) |
| 2893 | << Attr.getName() << E->getSourceRange(); |
| 2894 | return; |
| 2895 | } |
| 2896 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2897 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2898 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2899 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2900 | Existing->getYDim() == WGSize[1] && |
| 2901 | Existing->getZDim() == WGSize[2])) |
| 2902 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2903 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2904 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2905 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2906 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 2909 | // Handles intel_reqd_sub_group_size. |
| 2910 | static void handleSubGroupSize(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2911 | uint32_t SGSize; |
| 2912 | const Expr *E = Attr.getArgAsExpr(0); |
| 2913 | if (!checkUInt32Argument(S, Attr, E, SGSize)) |
| 2914 | return; |
| 2915 | if (SGSize == 0) { |
| 2916 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero) |
| 2917 | << Attr.getName() << E->getSourceRange(); |
| 2918 | return; |
| 2919 | } |
| 2920 | |
| 2921 | OpenCLIntelReqdSubGroupSizeAttr *Existing = |
| 2922 | D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>(); |
| 2923 | if (Existing && Existing->getSubGroupSize() != SGSize) |
| 2924 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2925 | |
| 2926 | D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr( |
| 2927 | Attr.getRange(), S.Context, SGSize, |
| 2928 | Attr.getAttributeSpellingListIndex())); |
| 2929 | } |
| 2930 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2931 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2932 | if (!Attr.hasParsedType()) { |
| 2933 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2934 | << Attr.getName() << 1; |
| 2935 | return; |
| 2936 | } |
| 2937 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2938 | TypeSourceInfo *ParmTSI = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2939 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2940 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2941 | |
| 2942 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2943 | (ParmType->isBooleanType() || |
| 2944 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2945 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2946 | << ParmType; |
| 2947 | return; |
| 2948 | } |
| 2949 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2950 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2951 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2952 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2953 | return; |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2958 | ParmTSI, |
| 2959 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2962 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2963 | StringRef Name, |
| 2964 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2965 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2966 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2967 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2968 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2969 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2970 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2971 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2972 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2973 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2976 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2977 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2978 | if (!Error.empty()) { |
| 2979 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error; |
| 2980 | return false; |
| 2981 | } |
| 2982 | return true; |
| 2983 | } |
| 2984 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2985 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2986 | // Make sure that there is a string literal as the sections's single |
| 2987 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2988 | StringRef Str; |
| 2989 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2990 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2991 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2993 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2994 | return; |
| 2995 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2996 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2997 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2998 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2999 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 3000 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 3001 | return; |
| 3002 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3004 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3005 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3006 | if (NewAttr) |
| 3007 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 3010 | // Check for things we'd like to warn about. Multiversioning issues are |
| 3011 | // handled later in the process, once we know how many exist. |
| 3012 | bool Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 3013 | enum FirstParam { Unsupported, Duplicate }; |
| 3014 | enum SecondParam { None, Architecture }; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 3015 | for (auto Str : {"tune=", "fpmath="}) |
| 3016 | if (AttrStr.find(Str) != StringRef::npos) |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 3017 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3018 | << Unsupported << None << Str; |
| 3019 | |
| 3020 | TargetAttr::ParsedTargetAttr ParsedAttrs = TargetAttr::parse(AttrStr); |
| 3021 | |
| 3022 | if (!ParsedAttrs.Architecture.empty() && |
| 3023 | !Context.getTargetInfo().isValidCPUName(ParsedAttrs.Architecture)) |
| 3024 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3025 | << Unsupported << Architecture << ParsedAttrs.Architecture; |
| 3026 | |
| 3027 | if (ParsedAttrs.DuplicateArchitecture) |
| 3028 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3029 | << Duplicate << None << "arch="; |
| 3030 | |
| 3031 | for (const auto &Feature : ParsedAttrs.Features) { |
| 3032 | auto CurFeature = StringRef(Feature).drop_front(); // remove + or -. |
| 3033 | if (!Context.getTargetInfo().isValidFeatureName(CurFeature)) |
| 3034 | return Diag(LiteralLoc, diag::warn_unsupported_target_attribute) |
| 3035 | << Unsupported << None << CurFeature; |
| 3036 | } |
| 3037 | |
| 3038 | return true; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3041 | static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3042 | StringRef Str; |
| 3043 | SourceLocation LiteralLoc; |
Erich Keane | 57e15cd | 2017-07-19 22:06:33 +0000 | [diff] [blame] | 3044 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc) || |
| 3045 | !S.checkTargetAttr(LiteralLoc, Str)) |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3046 | return; |
| 3047 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3048 | TargetAttr *NewAttr = |
| 3049 | ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index); |
| 3050 | D->addAttr(NewAttr); |
| 3051 | } |
| 3052 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3053 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3054 | VarDecl *VD = cast<VarDecl>(D); |
| 3055 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3056 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3057 | return; |
| 3058 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3059 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3060 | Expr *E = Attr.getArgAsExpr(0); |
| 3061 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3062 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3063 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3064 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3065 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 3066 | // will warn the user. |
| 3067 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 3068 | if (DRE->hasQualifier()) |
| 3069 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 3070 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 3071 | NI = DRE->getNameInfo(); |
| 3072 | if (!FD) { |
| 3073 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 3074 | << NI.getName(); |
| 3075 | return; |
| 3076 | } |
| 3077 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 3078 | if (ULE->hasExplicitTemplateArgs()) |
| 3079 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3080 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 3081 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 3082 | if (!FD) { |
| 3083 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 3084 | << NI.getName(); |
| 3085 | if (ULE->getType() == S.Context.OverloadTy) |
| 3086 | S.NoteAllOverloadCandidates(ULE); |
| 3087 | return; |
| 3088 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3089 | } else { |
| 3090 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3091 | return; |
| 3092 | } |
| 3093 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3094 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3095 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 3096 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3097 | return; |
| 3098 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3099 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3100 | // We're currently more strict than GCC about what function types we accept. |
| 3101 | // If this ever proves to be a problem it should be easy to fix. |
| 3102 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 3103 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3104 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 3105 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3106 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 3107 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3108 | return; |
| 3109 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3110 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3111 | D->addAttr(::new (S.Context) |
| 3112 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 3113 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3116 | static void handleEnumExtensibilityAttr(Sema &S, Decl *D, |
| 3117 | const AttributeList &Attr) { |
| 3118 | if (!Attr.isArgIdent(0)) { |
| 3119 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 3120 | << Attr.getName() << 0 << AANT_ArgumentIdentifier; |
| 3121 | return; |
| 3122 | } |
| 3123 | |
| 3124 | EnumExtensibilityAttr::Kind ExtensibilityKind; |
| 3125 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 3126 | if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(), |
| 3127 | ExtensibilityKind)) { |
| 3128 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 3129 | << Attr.getName() << II; |
| 3130 | return; |
| 3131 | } |
| 3132 | |
| 3133 | D->addAttr(::new (S.Context) EnumExtensibilityAttr( |
| 3134 | Attr.getRange(), S.Context, ExtensibilityKind, |
| 3135 | Attr.getAttributeSpellingListIndex())); |
| 3136 | } |
| 3137 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3138 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3139 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3140 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3141 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3142 | uint64_t Idx; |
| 3143 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3144 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3145 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3146 | // Make sure the format string is really a string. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3147 | QualType Ty = getFunctionOrMethodParamType(D, Idx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3148 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3149 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 3150 | if (NotNSStringTy && |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3151 | !isCFStringType(Ty, S.Context) && |
| 3152 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3153 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3154 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3155 | << "a string type" << IdxExpr->getSourceRange() |
| 3156 | << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3157 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3158 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3159 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3160 | if (!isNSStringType(Ty, S.Context) && |
| 3161 | !isCFStringType(Ty, S.Context) && |
| 3162 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3163 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3164 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3165 | << (NotNSStringTy ? "string type" : "NSString") |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 3166 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3167 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3170 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 3171 | // because that has corrected for the implicit this parameter, and is zero- |
| 3172 | // based. The attribute expects what the user wrote explicitly. |
| 3173 | llvm::APSInt Val; |
| 3174 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 3175 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3176 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 3177 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3178 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3181 | enum FormatAttrKind { |
| 3182 | CFStringFormat, |
| 3183 | NSStringFormat, |
| 3184 | StrftimeFormat, |
| 3185 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3186 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3187 | InvalidFormat |
| 3188 | }; |
| 3189 | |
| 3190 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3191 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3192 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3193 | return llvm::StringSwitch<FormatAttrKind>(Format) |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3194 | // Check for formats that get handled specially. |
| 3195 | .Case("NSString", NSStringFormat) |
| 3196 | .Case("CFString", CFStringFormat) |
| 3197 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3198 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3199 | // Otherwise, check for supported formats. |
| 3200 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3201 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3202 | .Case("kprintf", SupportedFormat) // OpenBSD. |
| 3203 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
| 3204 | .Case("os_trace", SupportedFormat) |
| 3205 | .Case("os_log", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3206 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3207 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3208 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3211 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3212 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3213 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3214 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3215 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3216 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3217 | return; |
| 3218 | } |
| 3219 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3220 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3221 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3222 | Attr.setInvalid(); |
| 3223 | return; |
| 3224 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3225 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3226 | if (S.Context.getAsArrayType(T)) |
| 3227 | T = S.Context.getBaseElementType(T); |
| 3228 | if (!T->getAs<RecordType>()) { |
| 3229 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3230 | Attr.setInvalid(); |
| 3231 | return; |
| 3232 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3233 | |
| 3234 | Expr *E = Attr.getArgAsExpr(0); |
| 3235 | uint32_t prioritynum; |
| 3236 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3237 | Attr.setInvalid(); |
| 3238 | return; |
| 3239 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3240 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3241 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3242 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3243 | << E->getSourceRange() << Attr.getName() << 101 << 65535; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3244 | Attr.setInvalid(); |
| 3245 | return; |
| 3246 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3247 | D->addAttr(::new (S.Context) |
| 3248 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3249 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3252 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3253 | IdentifierInfo *Format, int FormatIdx, |
| 3254 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3255 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3256 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3257 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 3258 | if (F->getType() == Format && |
| 3259 | F->getFormatIdx() == FormatIdx && |
| 3260 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3261 | // If we don't have a valid location for this attribute, adopt the |
| 3262 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3263 | if (F->getLocation().isInvalid()) |
| 3264 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3265 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3266 | } |
| 3267 | } |
| 3268 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3269 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3270 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3273 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3274 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3275 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3276 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3277 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3278 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3279 | return; |
| 3280 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3281 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3282 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3283 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3284 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3285 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3286 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3287 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 3288 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3289 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 3290 | if (normalizeName(Format)) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3291 | // If we've modified the string name, we need a new identifier for it. |
| 3292 | II = &S.Context.Idents.get(Format); |
| 3293 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3294 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3295 | // Check for supported formats. |
| 3296 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3297 | |
| 3298 | if (Kind == IgnoredFormat) |
| 3299 | return; |
| 3300 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3301 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3302 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 3303 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3304 | return; |
| 3305 | } |
| 3306 | |
| 3307 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3308 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3309 | uint32_t Idx; |
| 3310 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3311 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3312 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3313 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3314 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3315 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3316 | return; |
| 3317 | } |
| 3318 | |
| 3319 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3320 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3321 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3322 | if (HasImplicitThisParam) { |
| 3323 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3324 | S.Diag(Attr.getLoc(), |
| 3325 | diag::err_format_attribute_implicit_this_format_string) |
| 3326 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3327 | return; |
| 3328 | } |
| 3329 | ArgIdx--; |
| 3330 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3331 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3332 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3333 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3334 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3335 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3336 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3337 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3338 | << "a CFString" << IdxExpr->getSourceRange() |
| 3339 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3340 | return; |
| 3341 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3342 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3343 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3344 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3345 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3346 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3347 | << "an NSString" << IdxExpr->getSourceRange() |
| 3348 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3349 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3350 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3351 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3352 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3353 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3354 | << "a string type" << IdxExpr->getSourceRange() |
| 3355 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3356 | return; |
| 3357 | } |
| 3358 | |
| 3359 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3360 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3361 | uint32_t FirstArg; |
| 3362 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3363 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3364 | |
| 3365 | // check if the function is variadic if the 3rd argument non-zero |
| 3366 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3367 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3368 | ++NumArgs; // +1 for ... |
| 3369 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3370 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3371 | return; |
| 3372 | } |
| 3373 | } |
| 3374 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3375 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3376 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3377 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3378 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3379 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3380 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3381 | return; |
| 3382 | } |
| 3383 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3384 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3385 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3386 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3387 | return; |
| 3388 | } |
| 3389 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3390 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3391 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3392 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3393 | if (NewAttr) |
| 3394 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3397 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3398 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3399 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3400 | RecordDecl *RD = nullptr; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3401 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3402 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3403 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3404 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3405 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3406 | |
| 3407 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3408 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3409 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3410 | return; |
| 3411 | } |
| 3412 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3413 | if (!RD->isCompleteDefinition()) { |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 3414 | if (!RD->isBeingDefined()) |
| 3415 | S.Diag(Attr.getLoc(), |
| 3416 | diag::warn_transparent_union_attribute_not_definition); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3417 | return; |
| 3418 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3419 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3420 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3421 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3422 | if (Field == FieldEnd) { |
| 3423 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3424 | return; |
| 3425 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3426 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3427 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3428 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3429 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3430 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3431 | diag::warn_transparent_union_attribute_floating) |
| 3432 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3433 | return; |
| 3434 | } |
| 3435 | |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3436 | if (FirstType->isIncompleteType()) |
| 3437 | return; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3438 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3439 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3440 | for (; Field != FieldEnd; ++Field) { |
| 3441 | QualType FieldType = Field->getType(); |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3442 | if (FieldType->isIncompleteType()) |
| 3443 | return; |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3444 | // FIXME: this isn't fully correct; we also need to test whether the |
| 3445 | // members of the union would all have the same calling convention as the |
| 3446 | // first member of the union. Checking just the size and alignment isn't |
| 3447 | // sufficient (consider structs passed on the stack instead of in registers |
| 3448 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3449 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3450 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3451 | // Warn if we drop the attribute. |
| 3452 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3453 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3454 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3455 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3456 | diag::warn_transparent_union_attribute_field_size_align) |
| 3457 | << isSize << Field->getDeclName() << FieldBits; |
| 3458 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3459 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3460 | diag::note_transparent_union_first_field_size_align) |
| 3461 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3462 | return; |
| 3463 | } |
| 3464 | } |
| 3465 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3466 | RD->addAttr(::new (S.Context) |
| 3467 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3468 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3471 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3472 | // Make sure that there is a string literal as the annotation's single |
| 3473 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3474 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3475 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3476 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3477 | |
| 3478 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3479 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 3480 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3481 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3482 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3483 | |
| 3484 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3485 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3486 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3487 | } |
| 3488 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3489 | static void handleAlignValueAttr(Sema &S, Decl *D, |
| 3490 | const AttributeList &Attr) { |
| 3491 | S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 3492 | Attr.getAttributeSpellingListIndex()); |
| 3493 | } |
| 3494 | |
| 3495 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3496 | unsigned SpellingListIndex) { |
| 3497 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 3498 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3499 | |
| 3500 | QualType T; |
| 3501 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3502 | T = TD->getUnderlyingType(); |
| 3503 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3504 | T = VD->getType(); |
| 3505 | else |
| 3506 | llvm_unreachable("Unknown decl type for align_value"); |
| 3507 | |
| 3508 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 3509 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 3510 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 3511 | << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange(); |
| 3512 | return; |
| 3513 | } |
| 3514 | |
| 3515 | if (!E->isValueDependent()) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3516 | llvm::APSInt Alignment; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3517 | ExprResult ICE |
| 3518 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3519 | diag::err_align_value_attribute_argument_not_int, |
| 3520 | /*AllowFold*/ false); |
| 3521 | if (ICE.isInvalid()) |
| 3522 | return; |
| 3523 | |
| 3524 | if (!Alignment.isPowerOf2()) { |
| 3525 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3526 | << E->getSourceRange(); |
| 3527 | return; |
| 3528 | } |
| 3529 | |
| 3530 | D->addAttr(::new (Context) |
| 3531 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 3532 | SpellingListIndex)); |
| 3533 | return; |
| 3534 | } |
| 3535 | |
| 3536 | // Save dependent expressions in the AST to be instantiated. |
| 3537 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3538 | } |
| 3539 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3540 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3541 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3542 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3543 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3544 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3545 | return; |
| 3546 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3547 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3548 | if (Attr.getNumArgs() == 0) { |
| 3549 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3550 | true, nullptr, Attr.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3551 | return; |
| 3552 | } |
| 3553 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3554 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3555 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3556 | S.Diag(Attr.getEllipsisLoc(), |
| 3557 | diag::err_pack_expansion_without_parameter_packs); |
| 3558 | return; |
| 3559 | } |
| 3560 | |
| 3561 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3562 | return; |
| 3563 | |
David Majnemer | 26a1e0e | 2015-04-07 02:37:09 +0000 | [diff] [blame] | 3564 | if (E->isValueDependent()) { |
| 3565 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 3566 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 3567 | S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name) |
| 3568 | << E->getSourceRange(); |
| 3569 | return; |
| 3570 | } |
| 3571 | } |
| 3572 | } |
| 3573 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3574 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3575 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3576 | } |
| 3577 | |
| 3578 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3579 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3580 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3581 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3582 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3583 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3584 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3585 | // C++11 [dcl.align]p1: |
| 3586 | // An alignment-specifier may be applied to a variable or to a class |
| 3587 | // data member, but it shall not be applied to a bit-field, a function |
| 3588 | // parameter, the formal parameter of a catch clause, or a variable |
| 3589 | // declared with the register storage class specifier. An |
| 3590 | // alignment-specifier may also be applied to the declaration of a class |
| 3591 | // or enumeration type. |
| 3592 | // C11 6.7.5/2: |
| 3593 | // An alignment attribute shall not be specified in a declaration of |
| 3594 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3595 | // object declared with the register storage-class specifier. |
| 3596 | int DiagKind = -1; |
| 3597 | if (isa<ParmVarDecl>(D)) { |
| 3598 | DiagKind = 0; |
| 3599 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3600 | if (VD->getStorageClass() == SC_Register) |
| 3601 | DiagKind = 1; |
| 3602 | if (VD->isExceptionVariable()) |
| 3603 | DiagKind = 2; |
| 3604 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3605 | if (FD->isBitField()) |
| 3606 | DiagKind = 3; |
| 3607 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3608 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3609 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3610 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3611 | return; |
| 3612 | } |
| 3613 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3614 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3615 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3616 | return; |
| 3617 | } |
| 3618 | } |
| 3619 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3620 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3621 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3622 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3623 | AA->setPackExpansion(IsPackExpansion); |
| 3624 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3625 | return; |
| 3626 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3627 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3628 | // FIXME: Cache the number on the Attr object? |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3629 | llvm::APSInt Alignment; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3630 | ExprResult ICE |
| 3631 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3632 | diag::err_aligned_attribute_argument_not_int, |
| 3633 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3634 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3635 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3636 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3637 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3638 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3639 | // C++11 [dcl.align]p2: |
| 3640 | // -- if the constant expression evaluates to zero, the alignment |
| 3641 | // specifier shall have no effect |
| 3642 | // C11 6.7.5p6: |
| 3643 | // An alignment specification of zero has no effect. |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3644 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3645 | if (!llvm::isPowerOf2_64(AlignVal)) { |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3646 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3647 | << E->getSourceRange(); |
| 3648 | return; |
| 3649 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3650 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3651 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3652 | // Alignment calculations can wrap around if it's greater than 2**28. |
David Majnemer | 29c69db | 2015-07-26 01:48:59 +0000 | [diff] [blame] | 3653 | unsigned MaxValidAlignment = |
| 3654 | Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192 |
| 3655 | : 268435456; |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3656 | if (AlignVal > MaxValidAlignment) { |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3657 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 3658 | << E->getSourceRange(); |
| 3659 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3660 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3661 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3662 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3663 | unsigned MaxTLSAlign = |
| 3664 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3665 | .getQuantity(); |
| 3666 | auto *VD = dyn_cast<VarDecl>(D); |
| 3667 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3668 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3669 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3670 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3671 | return; |
| 3672 | } |
| 3673 | } |
| 3674 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3675 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3676 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3677 | AA->setPackExpansion(IsPackExpansion); |
| 3678 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3679 | } |
| 3680 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3681 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3682 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3683 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3684 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3685 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3686 | SpellingListIndex); |
| 3687 | AA->setPackExpansion(IsPackExpansion); |
| 3688 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3689 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3690 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3691 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3692 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3693 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3694 | QualType UnderlyingTy, DiagTy; |
| 3695 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 3696 | UnderlyingTy = DiagTy = VD->getType(); |
| 3697 | } else { |
| 3698 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
| 3699 | if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3700 | UnderlyingTy = ED->getIntegerType(); |
| 3701 | } |
| 3702 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3703 | return; |
| 3704 | |
| 3705 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3706 | // The combined effect of all alignment attributes in a declaration shall |
| 3707 | // not specify an alignment that is less strict than the alignment that |
| 3708 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3709 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3710 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3711 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3712 | if (I->isAlignmentDependent()) |
| 3713 | return; |
| 3714 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3715 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3716 | Align = std::max(Align, I->getAlignment(Context)); |
| 3717 | } |
| 3718 | |
| 3719 | if (AlignasAttr && Align) { |
| 3720 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3721 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3722 | if (NaturalAlign > RequestedAlign) |
| 3723 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3724 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3725 | } |
| 3726 | } |
| 3727 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3728 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3729 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3730 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3731 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3732 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3733 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3734 | // have to wait until the record is defined to catch any mismatches. |
| 3735 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3736 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3737 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3738 | // The unspecified model never matches what a definition could need. |
| 3739 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3740 | return false; |
| 3741 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3742 | if (BestCase) { |
| 3743 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3744 | return false; |
| 3745 | } else { |
| 3746 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3747 | return false; |
| 3748 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3749 | |
| 3750 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3751 | << 0 /*definition*/; |
| 3752 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3753 | << RD->getNameAsString(); |
| 3754 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3755 | } |
| 3756 | |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3757 | /// parseModeAttrArg - Parses attribute mode string and returns parsed type |
| 3758 | /// attribute. |
| 3759 | static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, |
| 3760 | bool &IntegerMode, bool &ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3761 | IntegerMode = true; |
| 3762 | ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3763 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3764 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3765 | switch (Str[0]) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3766 | case 'Q': |
| 3767 | DestWidth = 8; |
| 3768 | break; |
| 3769 | case 'H': |
| 3770 | DestWidth = 16; |
| 3771 | break; |
| 3772 | case 'S': |
| 3773 | DestWidth = 32; |
| 3774 | break; |
| 3775 | case 'D': |
| 3776 | DestWidth = 64; |
| 3777 | break; |
| 3778 | case 'X': |
| 3779 | DestWidth = 96; |
| 3780 | break; |
| 3781 | case 'T': |
| 3782 | DestWidth = 128; |
| 3783 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3784 | } |
| 3785 | if (Str[1] == 'F') { |
| 3786 | IntegerMode = false; |
| 3787 | } else if (Str[1] == 'C') { |
| 3788 | IntegerMode = false; |
| 3789 | ComplexMode = true; |
| 3790 | } else if (Str[1] != 'I') { |
| 3791 | DestWidth = 0; |
| 3792 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3793 | break; |
| 3794 | case 4: |
| 3795 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3796 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3797 | if (Str == "word") |
Reid Kleckner | f27e752 | 2016-02-01 18:58:24 +0000 | [diff] [blame] | 3798 | DestWidth = S.Context.getTargetInfo().getRegisterWidth(); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3799 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3800 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3801 | break; |
| 3802 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3803 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3804 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3805 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3806 | case 11: |
| 3807 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3808 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3809 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3810 | } |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
| 3814 | /// type. |
| 3815 | /// |
| 3816 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3817 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3818 | /// HImode, not an intermediate pointer. |
| 3819 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3820 | // This attribute isn't documented, but glibc uses it. It changes |
| 3821 | // the width of an int or unsigned int to the specified size. |
| 3822 | if (!Attr.isArgIdent(0)) { |
| 3823 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3824 | << AANT_ArgumentIdentifier; |
| 3825 | return; |
| 3826 | } |
| 3827 | |
| 3828 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3829 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3830 | S.AddModeAttr(Attr.getRange(), D, Name, Attr.getAttributeSpellingListIndex()); |
| 3831 | } |
| 3832 | |
| 3833 | void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name, |
| 3834 | unsigned SpellingListIndex, bool InInstantiation) { |
| 3835 | StringRef Str = Name->getName(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3836 | normalizeName(Str); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3837 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3838 | |
| 3839 | unsigned DestWidth = 0; |
| 3840 | bool IntegerMode = true; |
| 3841 | bool ComplexMode = false; |
| 3842 | llvm::APInt VectorSize(64, 0); |
| 3843 | if (Str.size() >= 4 && Str[0] == 'V') { |
| 3844 | // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2). |
| 3845 | size_t StrSize = Str.size(); |
| 3846 | size_t VectorStringLength = 0; |
| 3847 | while ((VectorStringLength + 1) < StrSize && |
| 3848 | isdigit(Str[VectorStringLength + 1])) |
| 3849 | ++VectorStringLength; |
| 3850 | if (VectorStringLength && |
| 3851 | !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) && |
| 3852 | VectorSize.isPowerOf2()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3853 | parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth, |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3854 | IntegerMode, ComplexMode); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3855 | // Avoid duplicate warning from template instantiation. |
| 3856 | if (!InInstantiation) |
| 3857 | Diag(AttrLoc, diag::warn_vector_mode_deprecated); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3858 | } else { |
| 3859 | VectorSize = 0; |
| 3860 | } |
| 3861 | } |
| 3862 | |
| 3863 | if (!VectorSize) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3864 | parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode); |
| 3865 | |
| 3866 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3867 | // and friends, at least with glibc. |
| 3868 | // FIXME: Make sure floating-point mappings are accurate |
| 3869 | // FIXME: Support XF and TF types |
| 3870 | if (!DestWidth) { |
| 3871 | Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
| 3872 | return; |
| 3873 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3874 | |
| 3875 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3876 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3877 | OldTy = TD->getUnderlyingType(); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3878 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 3879 | // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'. |
| 3880 | // Try to get type from enum declaration, default to int. |
| 3881 | OldTy = ED->getIntegerType(); |
| 3882 | if (OldTy.isNull()) |
| 3883 | OldTy = Context.IntTy; |
| 3884 | } else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3885 | OldTy = cast<ValueDecl>(D)->getType(); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3886 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3887 | if (OldTy->isDependentType()) { |
| 3888 | D->addAttr(::new (Context) |
| 3889 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
| 3890 | return; |
| 3891 | } |
| 3892 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3893 | // Base type can also be a vector type (see PR17453). |
| 3894 | // Distinguish between base type and base element type. |
| 3895 | QualType OldElemTy = OldTy; |
| 3896 | if (const VectorType *VT = OldTy->getAs<VectorType>()) |
| 3897 | OldElemTy = VT->getElementType(); |
| 3898 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3899 | // GCC allows 'mode' attribute on enumeration types (even incomplete), except |
| 3900 | // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete |
| 3901 | // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected. |
| 3902 | if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) && |
| 3903 | VectorSize.getBoolValue()) { |
| 3904 | Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange; |
| 3905 | return; |
| 3906 | } |
| 3907 | bool IntegralOrAnyEnumType = |
| 3908 | OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>(); |
| 3909 | |
| 3910 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() && |
| 3911 | !IntegralOrAnyEnumType) |
| 3912 | Diag(AttrLoc, diag::err_mode_not_primitive); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3913 | else if (IntegerMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3914 | if (!IntegralOrAnyEnumType) |
| 3915 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3916 | } else if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3917 | if (!OldElemTy->isComplexType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3918 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3919 | } else { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3920 | if (!OldElemTy->isFloatingType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3921 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3922 | } |
| 3923 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3924 | QualType NewElemTy; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3925 | |
| 3926 | if (IntegerMode) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3927 | NewElemTy = Context.getIntTypeForBitwidth(DestWidth, |
| 3928 | OldElemTy->isSignedIntegerType()); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3929 | else |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3930 | NewElemTy = Context.getRealTypeForBitwidth(DestWidth); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3931 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3932 | if (NewElemTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3933 | Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3934 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3937 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3938 | NewElemTy = Context.getComplexType(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | QualType NewTy = NewElemTy; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3942 | if (VectorSize.getBoolValue()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3943 | NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(), |
| 3944 | VectorType::GenericVector); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3945 | } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3946 | // Complex machine mode does not support base vector types. |
| 3947 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3948 | Diag(AttrLoc, diag::err_complex_mode_vector_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3949 | return; |
| 3950 | } |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3951 | unsigned NumElements = Context.getTypeSize(OldElemTy) * |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3952 | OldVT->getNumElements() / |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3953 | Context.getTypeSize(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3954 | NewTy = |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3955 | Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
| 3958 | if (NewTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3959 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3960 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3964 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3965 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3966 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3967 | ED->setIntegerType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3968 | else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3969 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3970 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3971 | D->addAttr(::new (Context) |
| 3972 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3973 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3974 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3975 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3976 | D->addAttr(::new (S.Context) |
| 3977 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3978 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3979 | } |
| 3980 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3981 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3982 | IdentifierInfo *Ident, |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3983 | unsigned AttrSpellingListIndex) { |
| 3984 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3985 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3986 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3987 | return nullptr; |
| 3988 | } |
| 3989 | |
| 3990 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 3991 | return nullptr; |
| 3992 | |
| 3993 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 3994 | AttrSpellingListIndex); |
| 3995 | } |
| 3996 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3997 | CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range, |
| 3998 | IdentifierInfo *Ident, |
| 3999 | unsigned AttrSpellingListIndex) { |
| 4000 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident)) |
| 4001 | return nullptr; |
| 4002 | |
| 4003 | return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex); |
| 4004 | } |
| 4005 | |
| 4006 | InternalLinkageAttr * |
| 4007 | Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range, |
| 4008 | IdentifierInfo *Ident, |
| 4009 | unsigned AttrSpellingListIndex) { |
| 4010 | if (auto VD = dyn_cast<VarDecl>(D)) { |
| 4011 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 4012 | // ImplicitParm or VarTemplateSpecialization). |
| 4013 | if (VD->getKind() != Decl::Var) { |
| 4014 | Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type) |
| 4015 | << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 4016 | : ExpectedVariableOrFunction); |
| 4017 | return nullptr; |
| 4018 | } |
| 4019 | // Attribute does not apply to non-static local variables. |
| 4020 | if (VD->hasLocalStorage()) { |
| 4021 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 4022 | return nullptr; |
| 4023 | } |
| 4024 | } |
| 4025 | |
| 4026 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident)) |
| 4027 | return nullptr; |
| 4028 | |
| 4029 | return ::new (Context) |
| 4030 | InternalLinkageAttr(Range, Context, AttrSpellingListIndex); |
| 4031 | } |
| 4032 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 4033 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 4034 | unsigned AttrSpellingListIndex) { |
| 4035 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 4036 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 4037 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 4038 | return nullptr; |
| 4039 | } |
| 4040 | |
| 4041 | if (D->hasAttr<MinSizeAttr>()) |
| 4042 | return nullptr; |
| 4043 | |
| 4044 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 4045 | } |
| 4046 | |
| 4047 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 4048 | unsigned AttrSpellingListIndex) { |
| 4049 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 4050 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 4051 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4052 | D->dropAttr<AlwaysInlineAttr>(); |
| 4053 | } |
| 4054 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 4055 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 4056 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4057 | D->dropAttr<MinSizeAttr>(); |
| 4058 | } |
| 4059 | |
| 4060 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 4061 | return nullptr; |
| 4062 | |
| 4063 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 4064 | AttrSpellingListIndex); |
| 4065 | } |
| 4066 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4067 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 4068 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4069 | if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(), |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 4070 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 4071 | return; |
| 4072 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4073 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
| 4074 | D, Attr.getRange(), Attr.getName(), |
| 4075 | Attr.getAttributeSpellingListIndex())) |
| 4076 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4077 | } |
| 4078 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4079 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4080 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
| 4081 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 4082 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4085 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, |
| 4086 | const AttributeList &Attr) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4087 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
| 4088 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 4089 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4090 | } |
| 4091 | |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4092 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4093 | if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, Attr.getRange(), |
| 4094 | Attr.getName())) |
| 4095 | return; |
| 4096 | auto *VD = cast<VarDecl>(D); |
| 4097 | if (!VD->hasGlobalStorage()) { |
| 4098 | S.Diag(Attr.getLoc(), diag::err_cuda_nonglobal_constant); |
| 4099 | return; |
| 4100 | } |
| 4101 | D->addAttr(::new (S.Context) CUDAConstantAttr( |
| 4102 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4103 | } |
| 4104 | |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4105 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4106 | if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, Attr.getRange(), |
| 4107 | Attr.getName())) |
| 4108 | return; |
| 4109 | auto *VD = cast<VarDecl>(D); |
Justin Lebar | 281ce2a | 2016-10-02 15:24:50 +0000 | [diff] [blame] | 4110 | // extern __shared__ is only allowed on arrays with no length (e.g. |
| 4111 | // "int x[]"). |
| 4112 | if (VD->hasExternalStorage() && !isa<IncompleteArrayType>(VD->getType())) { |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4113 | S.Diag(Attr.getLoc(), diag::err_cuda_extern_shared) << VD; |
| 4114 | return; |
| 4115 | } |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 4116 | if (S.getLangOpts().CUDA && VD->hasLocalStorage() && |
| 4117 | S.CUDADiagIfHostCode(Attr.getLoc(), diag::err_cuda_host_shared) |
| 4118 | << S.CurrentCUDATarget()) |
| 4119 | return; |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4120 | D->addAttr(::new (S.Context) CUDASharedAttr( |
| 4121 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4122 | } |
| 4123 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4124 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 4125 | if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, Attr.getRange(), |
| 4126 | Attr.getName()) || |
| 4127 | checkAttrMutualExclusion<CUDAHostAttr>(S, D, Attr.getRange(), |
| 4128 | Attr.getName())) { |
| 4129 | return; |
| 4130 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4131 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4132 | if (!FD->getReturnType()->isVoidType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4133 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 4134 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4135 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4136 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 4137 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4138 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4139 | } |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4140 | if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) { |
| 4141 | if (Method->isInstance()) { |
| 4142 | S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method) |
| 4143 | << Method; |
| 4144 | return; |
| 4145 | } |
| 4146 | S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method; |
| 4147 | } |
| 4148 | // Only warn for "inline" when compiling for host, to cut down on noise. |
| 4149 | if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice) |
| 4150 | S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4151 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4152 | D->addAttr(::new (S.Context) |
| 4153 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Eli Bendersky | 8386004 | 2014-09-02 22:00:06 +0000 | [diff] [blame] | 4154 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4155 | } |
| 4156 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4157 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4158 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 4159 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 4160 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 4161 | return; |
| 4162 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4163 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4164 | D->addAttr(::new (S.Context) |
| 4165 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 4166 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 4167 | } |
| 4168 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4169 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4170 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4171 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4172 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4173 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 4174 | CallingConv CC; |
Richard Smith | eec7cb1 | 2015-05-28 23:38:53 +0000 | [diff] [blame] | 4175 | if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4176 | return; |
| 4177 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4178 | if (!isa<ObjCMethodDecl>(D)) { |
| 4179 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4180 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4181 | return; |
| 4182 | } |
| 4183 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4184 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4185 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4186 | D->addAttr(::new (S.Context) |
| 4187 | FastCallAttr(Attr.getRange(), S.Context, |
| 4188 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4189 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4190 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4191 | D->addAttr(::new (S.Context) |
| 4192 | StdCallAttr(Attr.getRange(), S.Context, |
| 4193 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4194 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4195 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4196 | D->addAttr(::new (S.Context) |
| 4197 | ThisCallAttr(Attr.getRange(), S.Context, |
| 4198 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 4199 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4200 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4201 | D->addAttr(::new (S.Context) |
| 4202 | CDeclAttr(Attr.getRange(), S.Context, |
| 4203 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4204 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4205 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4206 | D->addAttr(::new (S.Context) |
| 4207 | PascalAttr(Attr.getRange(), S.Context, |
| 4208 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4209 | return; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4210 | case AttributeList::AT_SwiftCall: |
| 4211 | D->addAttr(::new (S.Context) |
| 4212 | SwiftCallAttr(Attr.getRange(), S.Context, |
| 4213 | Attr.getAttributeSpellingListIndex())); |
| 4214 | return; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4215 | case AttributeList::AT_VectorCall: |
| 4216 | D->addAttr(::new (S.Context) |
| 4217 | VectorCallAttr(Attr.getRange(), S.Context, |
| 4218 | Attr.getAttributeSpellingListIndex())); |
| 4219 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4220 | case AttributeList::AT_MSABI: |
| 4221 | D->addAttr(::new (S.Context) |
| 4222 | MSABIAttr(Attr.getRange(), S.Context, |
| 4223 | Attr.getAttributeSpellingListIndex())); |
| 4224 | return; |
| 4225 | case AttributeList::AT_SysVABI: |
| 4226 | D->addAttr(::new (S.Context) |
| 4227 | SysVABIAttr(Attr.getRange(), S.Context, |
| 4228 | Attr.getAttributeSpellingListIndex())); |
| 4229 | return; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4230 | case AttributeList::AT_RegCall: |
| 4231 | D->addAttr(::new (S.Context) RegCallAttr( |
| 4232 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4233 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4234 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4235 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4236 | switch (CC) { |
| 4237 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4238 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4239 | break; |
| 4240 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4241 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4242 | break; |
| 4243 | default: |
| 4244 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4247 | D->addAttr(::new (S.Context) |
| 4248 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 4249 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4250 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4251 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4252 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4253 | D->addAttr(::new (S.Context) |
| 4254 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 4255 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4256 | return; |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4257 | case AttributeList::AT_PreserveMost: |
| 4258 | D->addAttr(::new (S.Context) PreserveMostAttr( |
| 4259 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4260 | return; |
| 4261 | case AttributeList::AT_PreserveAll: |
| 4262 | D->addAttr(::new (S.Context) PreserveAllAttr( |
| 4263 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4264 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4265 | default: |
| 4266 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4267 | } |
| 4268 | } |
| 4269 | |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4270 | static void handleSuppressAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4271 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 4272 | return; |
| 4273 | |
| 4274 | std::vector<StringRef> DiagnosticIdentifiers; |
| 4275 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 4276 | StringRef RuleName; |
| 4277 | |
| 4278 | if (!S.checkStringLiteralArgumentAttr(Attr, I, RuleName, nullptr)) |
| 4279 | return; |
| 4280 | |
| 4281 | // FIXME: Warn if the rule name is unknown. This is tricky because only |
| 4282 | // clang-tidy knows about available rules. |
| 4283 | DiagnosticIdentifiers.push_back(RuleName); |
| 4284 | } |
| 4285 | D->addAttr(::new (S.Context) SuppressAttr( |
| 4286 | Attr.getRange(), S.Context, DiagnosticIdentifiers.data(), |
| 4287 | DiagnosticIdentifiers.size(), Attr.getAttributeSpellingListIndex())); |
| 4288 | } |
| 4289 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4290 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 4291 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4292 | if (attr.isInvalid()) |
| 4293 | return true; |
| 4294 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4295 | if (attr.hasProcessingCache()) { |
| 4296 | CC = (CallingConv) attr.getProcessingCache(); |
| 4297 | return false; |
| 4298 | } |
| 4299 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4300 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4301 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4302 | attr.setInvalid(); |
| 4303 | return true; |
| 4304 | } |
| 4305 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4306 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4307 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4308 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 4309 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 4310 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 4311 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 4312 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4313 | case AttributeList::AT_SwiftCall: CC = CC_Swift; break; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4314 | case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4315 | case AttributeList::AT_RegCall: CC = CC_X86RegCall; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4316 | case AttributeList::AT_MSABI: |
| 4317 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
Martin Storsjo | 022e782 | 2017-07-17 20:49:45 +0000 | [diff] [blame] | 4318 | CC_Win64; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4319 | break; |
| 4320 | case AttributeList::AT_SysVABI: |
| 4321 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 4322 | CC_C; |
| 4323 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4324 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 4325 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4326 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4327 | attr.setInvalid(); |
| 4328 | return true; |
| 4329 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4330 | if (StrRef == "aapcs") { |
| 4331 | CC = CC_AAPCS; |
| 4332 | break; |
| 4333 | } else if (StrRef == "aapcs-vfp") { |
| 4334 | CC = CC_AAPCS_VFP; |
| 4335 | break; |
| 4336 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4337 | |
| 4338 | attr.setInvalid(); |
| 4339 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 4340 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4341 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4342 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4343 | case AttributeList::AT_PreserveMost: CC = CC_PreserveMost; break; |
| 4344 | case AttributeList::AT_PreserveAll: CC = CC_PreserveAll; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4345 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4346 | } |
| 4347 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4348 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4349 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4350 | if (A != TargetInfo::CCCR_OK) { |
| 4351 | if (A == TargetInfo::CCCR_Warning) |
| 4352 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4353 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4354 | // This convention is not valid for the target. Use the default function or |
| 4355 | // method calling convention. |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 4356 | bool IsCXXMethod = false, IsVariadic = false; |
| 4357 | if (FD) { |
| 4358 | IsCXXMethod = FD->isCXXInstanceMember(); |
| 4359 | IsVariadic = FD->isVariadic(); |
| 4360 | } |
| 4361 | CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4362 | } |
| 4363 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4364 | attr.setProcessingCache((unsigned) CC); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4365 | return false; |
| 4366 | } |
| 4367 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4368 | /// Pointer-like types in the default address space. |
| 4369 | static bool isValidSwiftContextType(QualType type) { |
| 4370 | if (!type->hasPointerRepresentation()) |
| 4371 | return type->isDependentType(); |
| 4372 | return type->getPointeeType().getAddressSpace() == 0; |
| 4373 | } |
| 4374 | |
| 4375 | /// Pointers and references in the default address space. |
| 4376 | static bool isValidSwiftIndirectResultType(QualType type) { |
| 4377 | if (auto ptrType = type->getAs<PointerType>()) { |
| 4378 | type = ptrType->getPointeeType(); |
| 4379 | } else if (auto refType = type->getAs<ReferenceType>()) { |
| 4380 | type = refType->getPointeeType(); |
| 4381 | } else { |
| 4382 | return type->isDependentType(); |
| 4383 | } |
| 4384 | return type.getAddressSpace() == 0; |
| 4385 | } |
| 4386 | |
| 4387 | /// Pointers and references to pointers in the default address space. |
| 4388 | static bool isValidSwiftErrorResultType(QualType type) { |
| 4389 | if (auto ptrType = type->getAs<PointerType>()) { |
| 4390 | type = ptrType->getPointeeType(); |
| 4391 | } else if (auto refType = type->getAs<ReferenceType>()) { |
| 4392 | type = refType->getPointeeType(); |
| 4393 | } else { |
| 4394 | return type->isDependentType(); |
| 4395 | } |
| 4396 | if (!type.getQualifiers().empty()) |
| 4397 | return false; |
| 4398 | return isValidSwiftContextType(type); |
| 4399 | } |
| 4400 | |
| 4401 | static void handleParameterABIAttr(Sema &S, Decl *D, const AttributeList &attr, |
| 4402 | ParameterABI abi) { |
| 4403 | S.AddParameterABIAttr(attr.getRange(), D, abi, |
| 4404 | attr.getAttributeSpellingListIndex()); |
| 4405 | } |
| 4406 | |
| 4407 | void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi, |
| 4408 | unsigned spellingIndex) { |
| 4409 | |
| 4410 | QualType type = cast<ParmVarDecl>(D)->getType(); |
| 4411 | |
| 4412 | if (auto existingAttr = D->getAttr<ParameterABIAttr>()) { |
| 4413 | if (existingAttr->getABI() != abi) { |
| 4414 | Diag(range.getBegin(), diag::err_attributes_are_not_compatible) |
| 4415 | << getParameterABISpelling(abi) << existingAttr; |
| 4416 | Diag(existingAttr->getLocation(), diag::note_conflicting_attribute); |
| 4417 | return; |
| 4418 | } |
| 4419 | } |
| 4420 | |
| 4421 | switch (abi) { |
| 4422 | case ParameterABI::Ordinary: |
| 4423 | llvm_unreachable("explicit attribute for ordinary parameter ABI?"); |
| 4424 | |
| 4425 | case ParameterABI::SwiftContext: |
| 4426 | if (!isValidSwiftContextType(type)) { |
| 4427 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4428 | << getParameterABISpelling(abi) |
| 4429 | << /*pointer to pointer */ 0 << type; |
| 4430 | } |
| 4431 | D->addAttr(::new (Context) |
| 4432 | SwiftContextAttr(range, Context, spellingIndex)); |
| 4433 | return; |
| 4434 | |
| 4435 | case ParameterABI::SwiftErrorResult: |
| 4436 | if (!isValidSwiftErrorResultType(type)) { |
| 4437 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4438 | << getParameterABISpelling(abi) |
| 4439 | << /*pointer to pointer */ 1 << type; |
| 4440 | } |
| 4441 | D->addAttr(::new (Context) |
| 4442 | SwiftErrorResultAttr(range, Context, spellingIndex)); |
| 4443 | return; |
| 4444 | |
| 4445 | case ParameterABI::SwiftIndirectResult: |
| 4446 | if (!isValidSwiftIndirectResultType(type)) { |
| 4447 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4448 | << getParameterABISpelling(abi) |
| 4449 | << /*pointer*/ 0 << type; |
| 4450 | } |
| 4451 | D->addAttr(::new (Context) |
| 4452 | SwiftIndirectResultAttr(range, Context, spellingIndex)); |
| 4453 | return; |
| 4454 | } |
| 4455 | llvm_unreachable("bad parameter ABI attribute"); |
| 4456 | } |
| 4457 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4458 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4459 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4460 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 4461 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4462 | return true; |
| 4463 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 4464 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4465 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4466 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4467 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4468 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4469 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4470 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4471 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4472 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4473 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4474 | } |
| 4475 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4476 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4477 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4478 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4479 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4480 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4481 | } |
| 4482 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4483 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4484 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4485 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4486 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4487 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4488 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4489 | } |
| 4490 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4491 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4492 | } |
| 4493 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4494 | // Checks whether an argument of launch_bounds attribute is |
| 4495 | // acceptable, performs implicit conversion to Rvalue, and returns |
| 4496 | // non-nullptr Expr result on success. Otherwise, it returns nullptr |
| 4497 | // and may output an error. |
| 4498 | static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E, |
| 4499 | const CUDALaunchBoundsAttr &Attr, |
| 4500 | const unsigned Idx) { |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4501 | if (S.DiagnoseUnexpandedParameterPack(E)) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4502 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4503 | |
| 4504 | // Accept template arguments for now as they depend on something else. |
| 4505 | // We'll get to check them when they eventually get instantiated. |
| 4506 | if (E->isValueDependent()) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4507 | return E; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4508 | |
| 4509 | llvm::APSInt I(64); |
| 4510 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 4511 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
| 4512 | << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4513 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4514 | } |
| 4515 | // Make sure we can fit it in 32 bits. |
| 4516 | if (!I.isIntN(32)) { |
| 4517 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 4518 | << 32 << /* Unsigned */ 1; |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4519 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4520 | } |
| 4521 | if (I < 0) |
| 4522 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
| 4523 | << &Attr << Idx << E->getSourceRange(); |
| 4524 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4525 | // We may need to perform implicit conversion of the argument. |
| 4526 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 4527 | S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false); |
| 4528 | ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E); |
| 4529 | assert(!ValArg.isInvalid() && |
| 4530 | "Unexpected PerformCopyInitialization() failure."); |
| 4531 | |
| 4532 | return ValArg.getAs<Expr>(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
| 4535 | void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads, |
| 4536 | Expr *MinBlocks, unsigned SpellingListIndex) { |
| 4537 | CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks, |
| 4538 | SpellingListIndex); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4539 | MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0); |
| 4540 | if (MaxThreads == nullptr) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4541 | return; |
| 4542 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4543 | if (MinBlocks) { |
| 4544 | MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1); |
| 4545 | if (MinBlocks == nullptr) |
| 4546 | return; |
| 4547 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4548 | |
| 4549 | D->addAttr(::new (Context) CUDALaunchBoundsAttr( |
| 4550 | AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex)); |
| 4551 | } |
| 4552 | |
| 4553 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 4554 | const AttributeList &Attr) { |
| 4555 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1) || |
| 4556 | !checkAttributeAtMostNumArgs(S, Attr, 2)) |
| 4557 | return; |
| 4558 | |
| 4559 | S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 4560 | Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr, |
| 4561 | Attr.getAttributeSpellingListIndex()); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4564 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4565 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4566 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4567 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4568 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4569 | return; |
| 4570 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4571 | |
| 4572 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4573 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4574 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4575 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4576 | |
| 4577 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4578 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4579 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4580 | return; |
| 4581 | } |
| 4582 | |
| 4583 | uint64_t ArgumentIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4584 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 4585 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4586 | return; |
| 4587 | |
| 4588 | uint64_t TypeTagIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4589 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 4590 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4591 | return; |
| 4592 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 4593 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4594 | if (IsPointer) { |
| 4595 | // Ensure that buffer has a pointer type. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4596 | QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4597 | if (!BufferTy->isPointerType()) { |
| 4598 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 4599 | << Attr.getName() << 0; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4600 | } |
| 4601 | } |
| 4602 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4603 | D->addAttr(::new (S.Context) |
| 4604 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4605 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4606 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4610 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4611 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4612 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4613 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4614 | return; |
| 4615 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4616 | |
| 4617 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4618 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4619 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4620 | if (!isa<VarDecl>(D)) { |
| 4621 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4622 | << Attr.getName() << ExpectedVariable; |
| 4623 | return; |
| 4624 | } |
| 4625 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4626 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4627 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4628 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 4629 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4630 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4631 | D->addAttr(::new (S.Context) |
| 4632 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4633 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4634 | Attr.getLayoutCompatible(), |
| 4635 | Attr.getMustBeNull(), |
| 4636 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4637 | } |
| 4638 | |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4639 | static void handleXRayLogArgsAttr(Sema &S, Decl *D, |
| 4640 | const AttributeList &Attr) { |
| 4641 | uint64_t ArgCount; |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 4642 | |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4643 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, Attr.getArgAsExpr(0), |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 4644 | ArgCount, |
| 4645 | true /* AllowImplicitThis*/)) |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4646 | return; |
| 4647 | |
| 4648 | // ArgCount isn't a parameter index [0;n), it's a count [1;n] - hence + 1. |
| 4649 | D->addAttr(::new (S.Context) |
Dean Michael Berris | 7456a28 | 2017-06-16 03:22:09 +0000 | [diff] [blame] | 4650 | XRayLogArgsAttr(Attr.getRange(), S.Context, ++ArgCount, |
| 4651 | Attr.getAttributeSpellingListIndex())); |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4652 | } |
| 4653 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4654 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4655 | // Checker-specific attribute handlers. |
| 4656 | //===----------------------------------------------------------------------===// |
| 4657 | |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4658 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) { |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4659 | return type->isDependentType() || |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4660 | type->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4663 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4664 | return type->isDependentType() || |
| 4665 | type->isObjCObjectPointerType() || |
| 4666 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4667 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4668 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4669 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4670 | return type->isDependentType() || |
| 4671 | type->isPointerType() || |
| 4672 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4673 | } |
| 4674 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4675 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4676 | S.AddNSConsumedAttr(Attr.getRange(), D, Attr.getAttributeSpellingListIndex(), |
| 4677 | Attr.getKind() == AttributeList::AT_NSConsumed, |
| 4678 | /*template instantiation*/ false); |
| 4679 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4680 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4681 | void Sema::AddNSConsumedAttr(SourceRange attrRange, Decl *D, |
| 4682 | unsigned spellingIndex, bool isNSConsumed, |
| 4683 | bool isTemplateInstantiation) { |
| 4684 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
| 4685 | bool typeOK; |
| 4686 | |
| 4687 | if (isNSConsumed) { |
| 4688 | typeOK = isValidSubjectOfNSAttribute(*this, param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4689 | } else { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4690 | typeOK = isValidSubjectOfCFAttribute(*this, param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4691 | } |
| 4692 | |
| 4693 | if (!typeOK) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4694 | // These attributes are normally just advisory, but in ARC, ns_consumed |
| 4695 | // is significant. Allow non-dependent code to contain inappropriate |
| 4696 | // attributes even in ARC, but require template instantiations to be |
| 4697 | // set up correctly. |
| 4698 | Diag(D->getLocStart(), |
| 4699 | (isTemplateInstantiation && isNSConsumed && |
| 4700 | getLangOpts().ObjCAutoRefCount |
| 4701 | ? diag::err_ns_attribute_wrong_parameter_type |
| 4702 | : diag::warn_ns_attribute_wrong_parameter_type)) |
| 4703 | << attrRange |
| 4704 | << (isNSConsumed ? "ns_consumed" : "cf_consumed") |
| 4705 | << (isNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4706 | return; |
| 4707 | } |
| 4708 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4709 | if (isNSConsumed) |
| 4710 | param->addAttr(::new (Context) |
| 4711 | NSConsumedAttr(attrRange, Context, spellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4712 | else |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4713 | param->addAttr(::new (Context) |
| 4714 | CFConsumedAttr(attrRange, Context, spellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4715 | } |
| 4716 | |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 4717 | bool Sema::checkNSReturnsRetainedReturnType(SourceLocation loc, |
| 4718 | QualType type) { |
| 4719 | if (isValidSubjectOfNSReturnsRetainedAttribute(type)) |
| 4720 | return false; |
| 4721 | |
| 4722 | Diag(loc, diag::warn_ns_attribute_wrong_return_type) |
| 4723 | << "'ns_returns_retained'" << 0 << 0; |
| 4724 | return true; |
| 4725 | } |
| 4726 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4727 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4728 | const AttributeList &Attr) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4729 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4730 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4731 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4732 | returnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4733 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4734 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4735 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4736 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4737 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4738 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4739 | returnType = FD->getReturnType(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4740 | else if (auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 4741 | returnType = Param->getType()->getPointeeType(); |
| 4742 | if (returnType.isNull()) { |
| 4743 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4744 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4745 | << Attr.getRange(); |
| 4746 | return; |
| 4747 | } |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 4748 | } else if (Attr.isUsedAsTypeAttr()) { |
| 4749 | return; |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4750 | } else { |
| 4751 | AttributeDeclKind ExpectedDeclKind; |
| 4752 | switch (Attr.getKind()) { |
| 4753 | default: llvm_unreachable("invalid ownership attribute"); |
| 4754 | case AttributeList::AT_NSReturnsRetained: |
| 4755 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4756 | case AttributeList::AT_NSReturnsNotRetained: |
| 4757 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 4758 | break; |
| 4759 | |
| 4760 | case AttributeList::AT_CFReturnsRetained: |
| 4761 | case AttributeList::AT_CFReturnsNotRetained: |
| 4762 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 4763 | break; |
| 4764 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4765 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4766 | << Attr.getRange() << Attr.getName() << ExpectedDeclKind; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4767 | return; |
| 4768 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4769 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4770 | bool typeOK; |
| 4771 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4772 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4773 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4774 | case AttributeList::AT_NSReturnsRetained: |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4775 | typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4776 | cf = false; |
| 4777 | break; |
| 4778 | |
| 4779 | case AttributeList::AT_NSReturnsAutoreleased: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4780 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4781 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4782 | cf = false; |
| 4783 | break; |
| 4784 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4785 | case AttributeList::AT_CFReturnsRetained: |
| 4786 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4787 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4788 | cf = true; |
| 4789 | break; |
| 4790 | } |
| 4791 | |
| 4792 | if (!typeOK) { |
John McCall | 1225188 | 2017-07-15 11:06:46 +0000 | [diff] [blame] | 4793 | if (Attr.isUsedAsTypeAttr()) |
| 4794 | return; |
| 4795 | |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4796 | if (isa<ParmVarDecl>(D)) { |
| 4797 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4798 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4799 | << Attr.getRange(); |
| 4800 | } else { |
| 4801 | // Needs to be kept in sync with warn_ns_attribute_wrong_return_type. |
| 4802 | enum : unsigned { |
| 4803 | Function, |
| 4804 | Method, |
| 4805 | Property |
| 4806 | } SubjectKind = Function; |
| 4807 | if (isa<ObjCMethodDecl>(D)) |
| 4808 | SubjectKind = Method; |
| 4809 | else if (isa<ObjCPropertyDecl>(D)) |
| 4810 | SubjectKind = Property; |
| 4811 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4812 | << Attr.getName() << SubjectKind << cf |
| 4813 | << Attr.getRange(); |
| 4814 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4815 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4816 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4817 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4818 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4819 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4820 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4821 | case AttributeList::AT_NSReturnsAutoreleased: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4822 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr( |
| 4823 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4824 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4825 | case AttributeList::AT_CFReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4826 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr( |
| 4827 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4828 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4829 | case AttributeList::AT_NSReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4830 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr( |
| 4831 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4832 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4833 | case AttributeList::AT_CFReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4834 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr( |
| 4835 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4836 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4837 | case AttributeList::AT_NSReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4838 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr( |
| 4839 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4840 | return; |
| 4841 | }; |
| 4842 | } |
| 4843 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4844 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4845 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4846 | const int EP_ObjCMethod = 1; |
| 4847 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4848 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4849 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4850 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4851 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4852 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4853 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4854 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4855 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4856 | if (!resultType->isReferenceType() && |
| 4857 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4858 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4859 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4860 | << attr.getName() |
| 4861 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4862 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4863 | |
| 4864 | // Drop the attribute. |
| 4865 | return; |
| 4866 | } |
| 4867 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4868 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
| 4869 | attr.getRange(), S.Context, attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4872 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4873 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4874 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4875 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4876 | DeclContext *DC = method->getDeclContext(); |
| 4877 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4878 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4879 | << attr.getName() << 0; |
| 4880 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4881 | return; |
| 4882 | } |
| 4883 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4884 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4885 | << attr.getName() << 1; |
| 4886 | return; |
| 4887 | } |
| 4888 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4889 | method->addAttr(::new (S.Context) |
| 4890 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4891 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4892 | } |
| 4893 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4894 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 4895 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4896 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(), |
| 4897 | Attr.getName())) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4898 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4899 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4900 | D->addAttr(::new (S.Context) |
| 4901 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 4902 | Attr.getAttributeSpellingListIndex())); |
| 4903 | } |
| 4904 | |
| 4905 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 4906 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4907 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(), |
| 4908 | Attr.getName())) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4909 | return; |
| 4910 | |
| 4911 | D->addAttr(::new (S.Context) |
| 4912 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 4913 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4914 | } |
| 4915 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4916 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 4917 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4918 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4919 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4920 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4921 | 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] | 4922 | return; |
| 4923 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4924 | |
| 4925 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
| 4926 | if (auto TD = dyn_cast<TypedefNameDecl>(D)) { |
| 4927 | if (!Parm->Ident->isStr("id")) { |
| 4928 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id) |
| 4929 | << Attr.getName(); |
| 4930 | return; |
| 4931 | } |
| 4932 | |
| 4933 | // Only allow 'cv void *'. |
| 4934 | QualType T = TD->getUnderlyingType(); |
| 4935 | if (!T->isVoidPointerType()) { |
| 4936 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
| 4937 | return; |
| 4938 | } |
| 4939 | } |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4940 | |
| 4941 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4942 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4943 | Attr.getAttributeSpellingListIndex())); |
| 4944 | } |
| 4945 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4946 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 4947 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4948 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
| 4949 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4950 | if (!Parm) { |
| 4951 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4952 | return; |
| 4953 | } |
| 4954 | |
| 4955 | D->addAttr(::new (S.Context) |
| 4956 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 4957 | Attr.getAttributeSpellingListIndex())); |
| 4958 | } |
| 4959 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4960 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4961 | const AttributeList &Attr) { |
| 4962 | IdentifierInfo *RelatedClass = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4963 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4964 | if (!RelatedClass) { |
| 4965 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4966 | return; |
| 4967 | } |
| 4968 | IdentifierInfo *ClassMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4969 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4970 | IdentifierInfo *InstanceMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4971 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4972 | D->addAttr(::new (S.Context) |
| 4973 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 4974 | ClassMethod, InstanceMethod, |
| 4975 | Attr.getAttributeSpellingListIndex())); |
| 4976 | } |
| 4977 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4978 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 4979 | const AttributeList &Attr) { |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4980 | ObjCInterfaceDecl *IFace; |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4981 | if (ObjCCategoryDecl *CatDecl = |
| 4982 | dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4983 | IFace = CatDecl->getClassInterface(); |
| 4984 | else |
| 4985 | IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 4986 | |
| 4987 | if (!IFace) |
| 4988 | return; |
| 4989 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 4990 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 4991 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4992 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 4993 | Attr.getAttributeSpellingListIndex())); |
| 4994 | } |
| 4995 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4996 | static void handleObjCRuntimeName(Sema &S, Decl *D, |
| 4997 | const AttributeList &Attr) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 4998 | StringRef MetaDataName; |
| 4999 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName)) |
| 5000 | return; |
| 5001 | D->addAttr(::new (S.Context) |
| 5002 | ObjCRuntimeNameAttr(Attr.getRange(), S.Context, |
| 5003 | MetaDataName, |
| 5004 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 5005 | } |
| 5006 | |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 5007 | // When a user wants to use objc_boxable with a union or struct |
| 5008 | // but they don't have access to the declaration (legacy/third-party code) |
| 5009 | // then they can 'enable' this feature with a typedef: |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5010 | // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct; |
| 5011 | static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5012 | bool notify = false; |
| 5013 | |
| 5014 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 5015 | if (RD && RD->getDefinition()) { |
| 5016 | RD = RD->getDefinition(); |
| 5017 | notify = true; |
| 5018 | } |
| 5019 | |
| 5020 | if (RD) { |
| 5021 | ObjCBoxableAttr *BoxableAttr = ::new (S.Context) |
| 5022 | ObjCBoxableAttr(Attr.getRange(), S.Context, |
| 5023 | Attr.getAttributeSpellingListIndex()); |
| 5024 | RD->addAttr(BoxableAttr); |
| 5025 | if (notify) { |
| 5026 | // we need to notify ASTReader/ASTWriter about |
| 5027 | // modification of existing declaration |
| 5028 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 5029 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 5030 | } |
| 5031 | } |
| 5032 | } |
| 5033 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5034 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 5035 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5036 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5037 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5038 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 5039 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5040 | } |
| 5041 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5042 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 5043 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5044 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5045 | QualType type = vd->getType(); |
| 5046 | |
| 5047 | if (!type->isDependentType() && |
| 5048 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5049 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5050 | << type; |
| 5051 | return; |
| 5052 | } |
| 5053 | |
| 5054 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 5055 | |
| 5056 | // If we have no lifetime yet, check the lifetime we're presumably |
| 5057 | // going to infer. |
| 5058 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 5059 | lifetime = type->getObjCARCImplicitLifetime(); |
| 5060 | |
| 5061 | switch (lifetime) { |
| 5062 | case Qualifiers::OCL_None: |
| 5063 | assert(type->isDependentType() && |
| 5064 | "didn't infer lifetime for non-dependent type?"); |
| 5065 | break; |
| 5066 | |
| 5067 | case Qualifiers::OCL_Weak: // meaningful |
| 5068 | case Qualifiers::OCL_Strong: // meaningful |
| 5069 | break; |
| 5070 | |
| 5071 | case Qualifiers::OCL_ExplicitNone: |
| 5072 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5073 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5074 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 5075 | break; |
| 5076 | } |
| 5077 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5078 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 5079 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 5080 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5081 | } |
| 5082 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5083 | //===----------------------------------------------------------------------===// |
| 5084 | // Microsoft specific attribute handlers. |
| 5085 | //===----------------------------------------------------------------------===// |
| 5086 | |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5087 | UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range, |
| 5088 | unsigned AttrSpellingListIndex, StringRef Uuid) { |
| 5089 | if (const auto *UA = D->getAttr<UuidAttr>()) { |
Nico Weber | d58c260 | 2016-09-14 01:16:54 +0000 | [diff] [blame] | 5090 | if (UA->getGuid().equals_lower(Uuid)) |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5091 | return nullptr; |
| 5092 | Diag(UA->getLocation(), diag::err_mismatched_uuid); |
| 5093 | Diag(Range.getBegin(), diag::note_previous_uuid); |
| 5094 | D->dropAttr<UuidAttr>(); |
| 5095 | } |
| 5096 | |
| 5097 | return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex); |
| 5098 | } |
| 5099 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5100 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 5101 | if (!S.LangOpts.CPlusPlus) { |
| 5102 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 5103 | << Attr.getName() << AttributeLangSupport::C; |
| 5104 | return; |
| 5105 | } |
| 5106 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5107 | StringRef StrRef; |
| 5108 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 5109 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5110 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5111 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5112 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 5113 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5114 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 5115 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5116 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5117 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5118 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5119 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5120 | return; |
| 5121 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 5122 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5123 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5124 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5125 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5126 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5127 | return; |
| 5128 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5129 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5130 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5131 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5132 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5133 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5134 | |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5135 | // FIXME: It'd be nice to also emit a fixit removing uuid(...) (and, if it's |
| 5136 | // the only thing in the [] list, the [] too), and add an insertion of |
| 5137 | // __declspec(uuid(...)). But sadly, neither the SourceLocs of the commas |
| 5138 | // separating attributes nor of the [ and the ] are in the AST. |
Nico Weber | 0a23404 | 2017-05-05 17:15:08 +0000 | [diff] [blame] | 5139 | // Cf "SourceLocations of attribute list delimiters - [[ ... , ... ]] etc" |
Nico Weber | 469891e | 2017-05-05 17:05:56 +0000 | [diff] [blame] | 5140 | // on cfe-dev. |
| 5141 | if (Attr.isMicrosoftAttribute()) // Check for [uuid(...)] spelling. |
| 5142 | S.Diag(Attr.getLoc(), diag::warn_atl_uuid_deprecated); |
| 5143 | |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5144 | UuidAttr *UA = S.mergeUuidAttr(D, Attr.getRange(), |
| 5145 | Attr.getAttributeSpellingListIndex(), StrRef); |
| 5146 | if (UA) |
| 5147 | D->addAttr(UA); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 5148 | } |
| 5149 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5150 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5151 | if (!S.LangOpts.CPlusPlus) { |
| 5152 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 5153 | << Attr.getName() << AttributeLangSupport::C; |
| 5154 | return; |
| 5155 | } |
| 5156 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5157 | D, Attr.getRange(), /*BestCase=*/true, |
| 5158 | Attr.getAttributeSpellingListIndex(), |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5159 | (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling()); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5160 | if (IA) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5161 | D->addAttr(IA); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5162 | S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D)); |
| 5163 | } |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5164 | } |
| 5165 | |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5166 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, |
| 5167 | const AttributeList &Attr) { |
| 5168 | VarDecl *VD = cast<VarDecl>(D); |
| 5169 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
| 5170 | S.Diag(Attr.getLoc(), diag::err_thread_unsupported); |
| 5171 | return; |
| 5172 | } |
| 5173 | if (VD->getTSCSpec() != TSCS_unspecified) { |
| 5174 | S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable); |
| 5175 | return; |
| 5176 | } |
| 5177 | if (VD->hasLocalStorage()) { |
| 5178 | S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
| 5179 | return; |
| 5180 | } |
| 5181 | VD->addAttr(::new (S.Context) ThreadAttr( |
| 5182 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 5183 | } |
| 5184 | |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5185 | static void handleAbiTagAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5186 | SmallVector<StringRef, 4> Tags; |
| 5187 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 5188 | StringRef Tag; |
| 5189 | if (!S.checkStringLiteralArgumentAttr(Attr, I, Tag)) |
| 5190 | return; |
| 5191 | Tags.push_back(Tag); |
| 5192 | } |
| 5193 | |
| 5194 | if (const auto *NS = dyn_cast<NamespaceDecl>(D)) { |
| 5195 | if (!NS->isInline()) { |
| 5196 | S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 0; |
| 5197 | return; |
| 5198 | } |
| 5199 | if (NS->isAnonymousNamespace()) { |
| 5200 | S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 1; |
| 5201 | return; |
| 5202 | } |
| 5203 | if (Attr.getNumArgs() == 0) |
| 5204 | Tags.push_back(NS->getName()); |
| 5205 | } else if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5206 | return; |
| 5207 | |
| 5208 | // Store tags sorted and without duplicates. |
| 5209 | std::sort(Tags.begin(), Tags.end()); |
| 5210 | Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end()); |
| 5211 | |
| 5212 | D->addAttr(::new (S.Context) |
| 5213 | AbiTagAttr(Attr.getRange(), S.Context, Tags.data(), Tags.size(), |
| 5214 | Attr.getAttributeSpellingListIndex())); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5215 | } |
| 5216 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5217 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 5218 | const AttributeList &Attr) { |
| 5219 | // Check the attribute arguments. |
| 5220 | if (Attr.getNumArgs() > 1) { |
| 5221 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 5222 | << Attr.getName() << 1; |
| 5223 | return; |
| 5224 | } |
| 5225 | |
| 5226 | StringRef Str; |
| 5227 | SourceLocation ArgLoc; |
| 5228 | |
| 5229 | if (Attr.getNumArgs() == 0) |
| 5230 | Str = ""; |
| 5231 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 5232 | return; |
| 5233 | |
| 5234 | ARMInterruptAttr::InterruptType Kind; |
| 5235 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 5236 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 5237 | << Attr.getName() << Str << ArgLoc; |
| 5238 | return; |
| 5239 | } |
| 5240 | |
| 5241 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 5242 | D->addAttr(::new (S.Context) |
| 5243 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 5244 | } |
| 5245 | |
| 5246 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 5247 | const AttributeList &Attr) { |
| 5248 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 5249 | return; |
| 5250 | |
| 5251 | if (!Attr.isArgExpr(0)) { |
| 5252 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 5253 | << AANT_ArgumentIntegerConstant; |
| 5254 | return; |
| 5255 | } |
| 5256 | |
| 5257 | // FIXME: Check for decl - it should be void ()(void). |
| 5258 | |
| 5259 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 5260 | llvm::APSInt NumParams(32); |
| 5261 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 5262 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 5263 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 5264 | << NumParamsExpr->getSourceRange(); |
| 5265 | return; |
| 5266 | } |
| 5267 | |
| 5268 | unsigned Num = NumParams.getLimitedValue(255); |
| 5269 | if ((Num & 1) || Num > 30) { |
| 5270 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 5271 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 5272 | << NumParamsExpr->getSourceRange(); |
| 5273 | return; |
| 5274 | } |
| 5275 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5276 | D->addAttr(::new (S.Context) |
| 5277 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 5278 | Attr.getAttributeSpellingListIndex())); |
| 5279 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5280 | } |
| 5281 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5282 | static void handleMipsInterruptAttr(Sema &S, Decl *D, |
| 5283 | const AttributeList &Attr) { |
| 5284 | // Only one optional argument permitted. |
| 5285 | if (Attr.getNumArgs() > 1) { |
| 5286 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 5287 | << Attr.getName() << 1; |
| 5288 | return; |
| 5289 | } |
| 5290 | |
| 5291 | StringRef Str; |
| 5292 | SourceLocation ArgLoc; |
| 5293 | |
| 5294 | if (Attr.getNumArgs() == 0) |
| 5295 | Str = ""; |
| 5296 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 5297 | return; |
| 5298 | |
| 5299 | // Semantic checks for a function with the 'interrupt' attribute for MIPS: |
| 5300 | // a) Must be a function. |
| 5301 | // b) Must have no parameters. |
| 5302 | // c) Must have the 'void' return type. |
| 5303 | // d) Cannot have the 'mips16' attribute, as that instruction set |
| 5304 | // lacks the 'eret' instruction. |
| 5305 | // e) The attribute itself must either have no argument or one of the |
| 5306 | // valid interrupt types, see [MipsInterruptDocs]. |
| 5307 | |
| 5308 | if (!isFunctionOrMethod(D)) { |
| 5309 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5310 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 5311 | return; |
| 5312 | } |
| 5313 | |
| 5314 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 5315 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 5316 | << 0; |
| 5317 | return; |
| 5318 | } |
| 5319 | |
| 5320 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5321 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 5322 | << 1; |
| 5323 | return; |
| 5324 | } |
| 5325 | |
| 5326 | if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(), |
| 5327 | Attr.getName())) |
| 5328 | return; |
| 5329 | |
| 5330 | MipsInterruptAttr::InterruptType Kind; |
| 5331 | if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 5332 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 5333 | << Attr.getName() << "'" + std::string(Str) + "'"; |
| 5334 | return; |
| 5335 | } |
| 5336 | |
| 5337 | D->addAttr(::new (S.Context) MipsInterruptAttr( |
| 5338 | Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex())); |
| 5339 | } |
| 5340 | |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5341 | static void handleAnyX86InterruptAttr(Sema &S, Decl *D, |
| 5342 | const AttributeList &Attr) { |
| 5343 | // Semantic checks for a function with the 'interrupt' attribute. |
| 5344 | // a) Must be a function. |
| 5345 | // b) Must have the 'void' return type. |
| 5346 | // c) Must take 1 or 2 arguments. |
| 5347 | // d) The 1st argument must be a pointer. |
| 5348 | // e) The 2nd argument (if any) must be an unsigned integer. |
| 5349 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) || |
| 5350 | CXXMethodDecl::isStaticOverloadedOperator( |
| 5351 | cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) { |
| 5352 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 5353 | << Attr.getName() << ExpectedFunctionWithProtoType; |
| 5354 | return; |
| 5355 | } |
| 5356 | // Interrupt handler must have void return type. |
| 5357 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5358 | S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(), |
| 5359 | diag::err_anyx86_interrupt_attribute) |
| 5360 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5361 | ? 0 |
| 5362 | : 1) |
| 5363 | << 0; |
| 5364 | return; |
| 5365 | } |
| 5366 | // Interrupt handler must have 1 or 2 parameters. |
| 5367 | unsigned NumParams = getFunctionOrMethodNumParams(D); |
| 5368 | if (NumParams < 1 || NumParams > 2) { |
| 5369 | S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute) |
| 5370 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5371 | ? 0 |
| 5372 | : 1) |
| 5373 | << 1; |
| 5374 | return; |
| 5375 | } |
| 5376 | // The first argument must be a pointer. |
| 5377 | if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) { |
| 5378 | S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(), |
| 5379 | diag::err_anyx86_interrupt_attribute) |
| 5380 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5381 | ? 0 |
| 5382 | : 1) |
| 5383 | << 2; |
| 5384 | return; |
| 5385 | } |
| 5386 | // The second argument, if present, must be an unsigned integer. |
| 5387 | unsigned TypeSize = |
| 5388 | S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64 |
| 5389 | ? 64 |
| 5390 | : 32; |
| 5391 | if (NumParams == 2 && |
| 5392 | (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() || |
| 5393 | S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) { |
| 5394 | S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(), |
| 5395 | diag::err_anyx86_interrupt_attribute) |
| 5396 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5397 | ? 0 |
| 5398 | : 1) |
| 5399 | << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false); |
| 5400 | return; |
| 5401 | } |
| 5402 | D->addAttr(::new (S.Context) AnyX86InterruptAttr( |
| 5403 | Attr.getLoc(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 5404 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
| 5405 | } |
| 5406 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5407 | static void handleAVRInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5408 | if (!isFunctionOrMethod(D)) { |
| 5409 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5410 | << "'interrupt'" << ExpectedFunction; |
| 5411 | return; |
| 5412 | } |
| 5413 | |
| 5414 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 5415 | return; |
| 5416 | |
| 5417 | handleSimpleAttribute<AVRInterruptAttr>(S, D, Attr); |
| 5418 | } |
| 5419 | |
| 5420 | static void handleAVRSignalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5421 | if (!isFunctionOrMethod(D)) { |
| 5422 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5423 | << "'signal'" << ExpectedFunction; |
| 5424 | return; |
| 5425 | } |
| 5426 | |
| 5427 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 5428 | return; |
| 5429 | |
| 5430 | handleSimpleAttribute<AVRSignalAttr>(S, D, Attr); |
| 5431 | } |
| 5432 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5433 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5434 | // Dispatch the interrupt attribute based on the current target. |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5435 | switch (S.Context.getTargetInfo().getTriple().getArch()) { |
| 5436 | case llvm::Triple::msp430: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5437 | handleMSP430InterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5438 | break; |
| 5439 | case llvm::Triple::mipsel: |
| 5440 | case llvm::Triple::mips: |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5441 | handleMipsInterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5442 | break; |
| 5443 | case llvm::Triple::x86: |
| 5444 | case llvm::Triple::x86_64: |
| 5445 | handleAnyX86InterruptAttr(S, D, Attr); |
| 5446 | break; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5447 | case llvm::Triple::avr: |
| 5448 | handleAVRInterruptAttr(S, D, Attr); |
| 5449 | break; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5450 | default: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5451 | handleARMInterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5452 | break; |
| 5453 | } |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5454 | } |
| 5455 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5456 | static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D, |
| 5457 | const AttributeList &Attr) { |
| 5458 | uint32_t Min = 0; |
| 5459 | Expr *MinExpr = Attr.getArgAsExpr(0); |
| 5460 | if (!checkUInt32Argument(S, Attr, MinExpr, Min)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5461 | return; |
| 5462 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5463 | uint32_t Max = 0; |
| 5464 | Expr *MaxExpr = Attr.getArgAsExpr(1); |
| 5465 | if (!checkUInt32Argument(S, Attr, MaxExpr, Max)) |
| 5466 | return; |
| 5467 | |
| 5468 | if (Min == 0 && Max != 0) { |
| 5469 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5470 | << Attr.getName() << 0; |
| 5471 | return; |
| 5472 | } |
| 5473 | if (Min > Max) { |
| 5474 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5475 | << Attr.getName() << 1; |
| 5476 | return; |
| 5477 | } |
| 5478 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5479 | D->addAttr(::new (S.Context) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5480 | AMDGPUFlatWorkGroupSizeAttr(Attr.getLoc(), S.Context, Min, Max, |
| 5481 | Attr.getAttributeSpellingListIndex())); |
| 5482 | } |
| 5483 | |
| 5484 | static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, |
| 5485 | const AttributeList &Attr) { |
| 5486 | uint32_t Min = 0; |
| 5487 | Expr *MinExpr = Attr.getArgAsExpr(0); |
| 5488 | if (!checkUInt32Argument(S, Attr, MinExpr, Min)) |
| 5489 | return; |
| 5490 | |
| 5491 | uint32_t Max = 0; |
| 5492 | if (Attr.getNumArgs() == 2) { |
| 5493 | Expr *MaxExpr = Attr.getArgAsExpr(1); |
| 5494 | if (!checkUInt32Argument(S, Attr, MaxExpr, Max)) |
| 5495 | return; |
| 5496 | } |
| 5497 | |
| 5498 | if (Min == 0 && Max != 0) { |
| 5499 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5500 | << Attr.getName() << 0; |
| 5501 | return; |
| 5502 | } |
| 5503 | if (Max != 0 && Min > Max) { |
| 5504 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5505 | << Attr.getName() << 1; |
| 5506 | return; |
| 5507 | } |
| 5508 | |
| 5509 | D->addAttr(::new (S.Context) |
| 5510 | AMDGPUWavesPerEUAttr(Attr.getLoc(), S.Context, Min, Max, |
| 5511 | Attr.getAttributeSpellingListIndex())); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5512 | } |
| 5513 | |
| 5514 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, |
| 5515 | const AttributeList &Attr) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5516 | uint32_t NumSGPR = 0; |
| 5517 | Expr *NumSGPRExpr = Attr.getArgAsExpr(0); |
| 5518 | if (!checkUInt32Argument(S, Attr, NumSGPRExpr, NumSGPR)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5519 | return; |
| 5520 | |
| 5521 | D->addAttr(::new (S.Context) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5522 | AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context, NumSGPR, |
| 5523 | Attr.getAttributeSpellingListIndex())); |
| 5524 | } |
| 5525 | |
| 5526 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, |
| 5527 | const AttributeList &Attr) { |
| 5528 | uint32_t NumVGPR = 0; |
| 5529 | Expr *NumVGPRExpr = Attr.getArgAsExpr(0); |
| 5530 | if (!checkUInt32Argument(S, Attr, NumVGPRExpr, NumVGPR)) |
| 5531 | return; |
| 5532 | |
| 5533 | D->addAttr(::new (S.Context) |
| 5534 | AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context, NumVGPR, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5535 | Attr.getAttributeSpellingListIndex())); |
| 5536 | } |
| 5537 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5538 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 5539 | const AttributeList& Attr) { |
| 5540 | // If we try to apply it to a function pointer, don't warn, but don't |
| 5541 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 5542 | // special about calling a force_align_arg_pointer function. |
| 5543 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 5544 | if (VD && VD->getType()->isFunctionPointerType()) |
| 5545 | return; |
| 5546 | // Also don't warn on function pointer typedefs. |
| 5547 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 5548 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 5549 | TD->getUnderlyingType()->isFunctionType())) |
| 5550 | return; |
| 5551 | // Attribute can only be applied to function types. |
| 5552 | if (!isa<FunctionDecl>(D)) { |
| 5553 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 5554 | << Attr.getName() << /* function */0; |
| 5555 | return; |
| 5556 | } |
| 5557 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5558 | D->addAttr(::new (S.Context) |
| 5559 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 5560 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5561 | } |
| 5562 | |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5563 | static void handleLayoutVersion(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5564 | uint32_t Version; |
| 5565 | Expr *VersionExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 5566 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), Version)) |
| 5567 | return; |
| 5568 | |
| 5569 | // TODO: Investigate what happens with the next major version of MSVC. |
| 5570 | if (Version != LangOptions::MSVC2015) { |
| 5571 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 5572 | << Attr.getName() << Version << VersionExpr->getSourceRange(); |
| 5573 | return; |
| 5574 | } |
| 5575 | |
| 5576 | D->addAttr(::new (S.Context) |
| 5577 | LayoutVersionAttr(Attr.getRange(), S.Context, Version, |
| 5578 | Attr.getAttributeSpellingListIndex())); |
| 5579 | } |
| 5580 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5581 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 5582 | unsigned AttrSpellingListIndex) { |
| 5583 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5584 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5585 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5586 | } |
| 5587 | |
| 5588 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5589 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5590 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5591 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5592 | } |
| 5593 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5594 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 5595 | unsigned AttrSpellingListIndex) { |
| 5596 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5597 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5598 | D->dropAttr<DLLImportAttr>(); |
| 5599 | } |
| 5600 | |
| 5601 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5602 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5603 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5604 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5605 | } |
| 5606 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5607 | static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 5608 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 5609 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5610 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) |
| 5611 | << A.getName(); |
| 5612 | return; |
| 5613 | } |
| 5614 | |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 5615 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5616 | if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport && |
| 5617 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5618 | // MinGW doesn't allow dllimport on inline functions. |
| 5619 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
| 5620 | << A.getName(); |
| 5621 | return; |
| 5622 | } |
| 5623 | } |
| 5624 | |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 5625 | if (auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 5626 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 5627 | MD->getParent()->isLambda()) { |
| 5628 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName(); |
| 5629 | return; |
| 5630 | } |
| 5631 | } |
| 5632 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5633 | unsigned Index = A.getAttributeSpellingListIndex(); |
| 5634 | Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport |
| 5635 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 5636 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5637 | if (NewAttr) |
| 5638 | D->addAttr(NewAttr); |
| 5639 | } |
| 5640 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5641 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5642 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5643 | unsigned AttrSpellingListIndex, |
| 5644 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 5645 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 5646 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5647 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5648 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 5649 | << 1 /*previous declaration*/; |
| 5650 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 5651 | D->dropAttr<MSInheritanceAttr>(); |
| 5652 | } |
| 5653 | |
| 5654 | CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 5655 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5656 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 5657 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5658 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5659 | } |
| 5660 | } else { |
| 5661 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 5662 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5663 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5664 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5665 | } |
| 5666 | if (RD->getDescribedClassTemplate()) { |
| 5667 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5668 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5669 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5670 | } |
| 5671 | } |
| 5672 | |
| 5673 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5674 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5675 | } |
| 5676 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5677 | static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5678 | // The capability attributes take a single string parameter for the name of |
| 5679 | // the capability they represent. The lockable attribute does not take any |
| 5680 | // parameters. However, semantically, both attributes represent the same |
| 5681 | // concept, and so they use the same semantic attribute. Eventually, the |
| 5682 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5683 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 5684 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5685 | // literal will be considered a "mutex." |
| 5686 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5687 | SourceLocation LiteralLoc; |
| 5688 | if (Attr.getKind() == AttributeList::AT_Capability && |
| 5689 | !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc)) |
| 5690 | return; |
| 5691 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5692 | // Currently, there are only two names allowed for a capability: role and |
| 5693 | // mutex (case insensitive). Diagnose other capability names. |
| 5694 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 5695 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 5696 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5697 | D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N, |
| 5698 | Attr.getAttributeSpellingListIndex())); |
| 5699 | } |
| 5700 | |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5701 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, |
| 5702 | const AttributeList &Attr) { |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame^] | 5703 | SmallVector<Expr*, 1> Args; |
| 5704 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 5705 | return; |
| 5706 | |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5707 | D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context, |
Josh Gao | ec1369e | 2017-08-08 19:44:34 +0000 | [diff] [blame^] | 5708 | Args.data(), Args.size(), |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5709 | Attr.getAttributeSpellingListIndex())); |
| 5710 | } |
| 5711 | |
| 5712 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
| 5713 | const AttributeList &Attr) { |
| 5714 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5715 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5716 | return; |
| 5717 | |
| 5718 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(), |
| 5719 | S.Context, |
| 5720 | Args.data(), Args.size(), |
| 5721 | Attr.getAttributeSpellingListIndex())); |
| 5722 | } |
| 5723 | |
| 5724 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
| 5725 | const AttributeList &Attr) { |
| 5726 | SmallVector<Expr*, 2> Args; |
| 5727 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 5728 | return; |
| 5729 | |
| 5730 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(), |
| 5731 | S.Context, |
| 5732 | Attr.getArgAsExpr(0), |
| 5733 | Args.data(), |
| 5734 | Args.size(), |
| 5735 | Attr.getAttributeSpellingListIndex())); |
| 5736 | } |
| 5737 | |
| 5738 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
| 5739 | const AttributeList &Attr) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5740 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5741 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 5742 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5743 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5744 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
| 5745 | Attr.getRange(), S.Context, Args.data(), Args.size(), |
| 5746 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5747 | } |
| 5748 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5749 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
| 5750 | const AttributeList &Attr) { |
| 5751 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5752 | return; |
| 5753 | |
| 5754 | // check that all arguments are lockable objects |
| 5755 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 5756 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5757 | if (Args.empty()) |
| 5758 | return; |
| 5759 | |
| 5760 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
| 5761 | RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(), |
| 5762 | Args.size(), Attr.getAttributeSpellingListIndex()); |
| 5763 | |
| 5764 | D->addAttr(RCA); |
| 5765 | } |
| 5766 | |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5767 | static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5768 | if (auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
| 5769 | if (NSD->isAnonymousNamespace()) { |
| 5770 | S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace); |
| 5771 | // Do not want to attach the attribute to the namespace because that will |
| 5772 | // cause confusing diagnostic reports for uses of declarations within the |
| 5773 | // namespace. |
| 5774 | return; |
| 5775 | } |
| 5776 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5777 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 5778 | // Handle the cases where the attribute has a text message. |
| 5779 | StringRef Str, Replacement; |
| 5780 | if (Attr.isArgExpr(0) && Attr.getArgAsExpr(0) && |
| 5781 | !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
| 5782 | return; |
| 5783 | |
| 5784 | // Only support a single optional message for Declspec and CXX11. |
| 5785 | if (Attr.isDeclspecAttribute() || Attr.isCXX11Attribute()) |
| 5786 | checkAttributeAtMostNumArgs(S, Attr, 1); |
| 5787 | else if (Attr.isArgExpr(1) && Attr.getArgAsExpr(1) && |
| 5788 | !S.checkStringLiteralArgumentAttr(Attr, 1, Replacement)) |
| 5789 | return; |
| 5790 | |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5791 | if (!S.getLangOpts().CPlusPlus14) |
| 5792 | if (Attr.isCXX11Attribute() && |
| 5793 | !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu"))) |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5794 | S.Diag(Attr.getLoc(), diag::ext_cxx14_attr) << Attr.getName(); |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5795 | |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5796 | D->addAttr(::new (S.Context) |
| 5797 | DeprecatedAttr(Attr.getRange(), S.Context, Str, Replacement, |
| 5798 | Attr.getAttributeSpellingListIndex())); |
| 5799 | } |
| 5800 | |
| 5801 | static bool isGlobalVar(const Decl *D) { |
| 5802 | if (const auto *S = dyn_cast<VarDecl>(D)) |
| 5803 | return S->hasGlobalStorage(); |
| 5804 | return false; |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5805 | } |
| 5806 | |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5807 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5808 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5809 | return; |
| 5810 | |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 5811 | std::vector<StringRef> Sanitizers; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5812 | |
| 5813 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 5814 | StringRef SanitizerName; |
| 5815 | SourceLocation LiteralLoc; |
| 5816 | |
| 5817 | if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc)) |
| 5818 | return; |
| 5819 | |
| 5820 | if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0) |
| 5821 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5822 | else if (isGlobalVar(D) && SanitizerName != "address") |
| 5823 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5824 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5825 | Sanitizers.push_back(SanitizerName); |
| 5826 | } |
| 5827 | |
| 5828 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
| 5829 | Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(), |
| 5830 | Attr.getAttributeSpellingListIndex())); |
| 5831 | } |
| 5832 | |
| 5833 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
| 5834 | const AttributeList &Attr) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 5835 | StringRef AttrName = Attr.getName()->getName(); |
| 5836 | normalizeName(AttrName); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5837 | StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName) |
| 5838 | .Case("no_address_safety_analysis", "address") |
| 5839 | .Case("no_sanitize_address", "address") |
| 5840 | .Case("no_sanitize_thread", "thread") |
| 5841 | .Case("no_sanitize_memory", "memory"); |
| 5842 | if (isGlobalVar(D) && SanitizerName != "address") |
| 5843 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5844 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5845 | D->addAttr(::new (S.Context) |
| 5846 | NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1, |
| 5847 | Attr.getAttributeSpellingListIndex())); |
| 5848 | } |
| 5849 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 5850 | static void handleInternalLinkageAttr(Sema &S, Decl *D, |
| 5851 | const AttributeList &Attr) { |
| 5852 | if (InternalLinkageAttr *Internal = |
| 5853 | S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(), |
| 5854 | Attr.getAttributeSpellingListIndex())) |
| 5855 | D->addAttr(Internal); |
| 5856 | } |
| 5857 | |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 5858 | static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5859 | if (S.LangOpts.OpenCLVersion != 200) |
| 5860 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_opencl_version) |
| 5861 | << Attr.getName() << "2.0" << 0; |
| 5862 | else |
| 5863 | S.Diag(Attr.getLoc(), diag::warn_opencl_attr_deprecated_ignored) |
| 5864 | << Attr.getName() << "2.0"; |
| 5865 | } |
| 5866 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5867 | /// Handles semantic checking for features that are common to all attributes, |
| 5868 | /// such as checking whether a parameter was properly specified, or the correct |
| 5869 | /// number of arguments were passed, etc. |
| 5870 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 5871 | const AttributeList &Attr) { |
| 5872 | // Several attributes carry different semantics than the parsing requires, so |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5873 | // those are opted out of the common argument checks. |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5874 | // |
| 5875 | // We also bail on unknown and ignored attributes because those are handled |
| 5876 | // as part of the target-specific handling logic. |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5877 | if (Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5878 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5879 | // Check whether the attribute requires specific language extensions to be |
| 5880 | // enabled. |
| 5881 | if (!Attr.diagnoseLangOpts(S)) |
| 5882 | return true; |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5883 | // Check whether the attribute appertains to the given subject. |
| 5884 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 5885 | return true; |
| 5886 | if (Attr.hasCustomParsing()) |
| 5887 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5888 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5889 | if (Attr.getMinArgs() == Attr.getMaxArgs()) { |
| 5890 | // If there are no optional arguments, then checking for the argument count |
| 5891 | // is trivial. |
| 5892 | if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 5893 | return true; |
| 5894 | } else { |
| 5895 | // There are optional arguments, so checking is slightly more involved. |
| 5896 | if (Attr.getMinArgs() && |
| 5897 | !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs())) |
| 5898 | return true; |
| 5899 | else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() && |
| 5900 | !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs())) |
| 5901 | return true; |
| 5902 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5903 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5904 | return false; |
| 5905 | } |
| 5906 | |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5907 | static void handleOpenCLAccessAttr(Sema &S, Decl *D, |
| 5908 | const AttributeList &Attr) { |
| 5909 | if (D->isInvalidDecl()) |
| 5910 | return; |
| 5911 | |
| 5912 | // Check if there is only one access qualifier. |
| 5913 | if (D->hasAttr<OpenCLAccessAttr>()) { |
| 5914 | S.Diag(Attr.getLoc(), diag::err_opencl_multiple_access_qualifiers) |
| 5915 | << D->getSourceRange(); |
| 5916 | D->setInvalidDecl(true); |
| 5917 | return; |
| 5918 | } |
| 5919 | |
| 5920 | // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an |
| 5921 | // image object can be read and written. |
| 5922 | // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe |
| 5923 | // object. Using the read_write (or __read_write) qualifier with the pipe |
| 5924 | // qualifier is a compilation error. |
| 5925 | if (const ParmVarDecl *PDecl = dyn_cast<ParmVarDecl>(D)) { |
| 5926 | const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr(); |
| 5927 | if (Attr.getName()->getName().find("read_write") != StringRef::npos) { |
| 5928 | if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) { |
| 5929 | S.Diag(Attr.getLoc(), diag::err_opencl_invalid_read_write) |
| 5930 | << Attr.getName() << PDecl->getType() << DeclTy->isImageType(); |
| 5931 | D->setInvalidDecl(true); |
| 5932 | return; |
| 5933 | } |
| 5934 | } |
| 5935 | } |
| 5936 | |
| 5937 | D->addAttr(::new (S.Context) OpenCLAccessAttr( |
| 5938 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 5939 | } |
| 5940 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5941 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5942 | // Top Level Sema Entry Points |
| 5943 | //===----------------------------------------------------------------------===// |
| 5944 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5945 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 5946 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 5947 | /// silently ignore it if a GNU attribute. |
| 5948 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 5949 | const AttributeList &Attr, |
| 5950 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5951 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5952 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 5953 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5954 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 5955 | // instead. |
| 5956 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 5957 | return; |
| 5958 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5959 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 5960 | // which do not apply to the current target architecture are treated as |
| 5961 | // though they were unknown attributes. |
| 5962 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 5963 | !Attr.existsInTarget(S.Context.getTargetInfo())) { |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5964 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() |
| 5965 | ? diag::warn_unhandled_ms_attribute_ignored |
| 5966 | : diag::warn_unknown_attribute_ignored) |
| 5967 | << Attr.getName(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5968 | return; |
| 5969 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5970 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5971 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 5972 | return; |
| 5973 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5974 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5975 | default: |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5976 | if (!Attr.isStmtAttr()) { |
| 5977 | // Type attributes are handled elsewhere; silently move on. |
| 5978 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
| 5979 | break; |
| 5980 | } |
| 5981 | S.Diag(Attr.getLoc(), diag::err_stmt_attribute_invalid_on_decl) |
| 5982 | << Attr.getName() << D->getLocation(); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5983 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5984 | case AttributeList::AT_Interrupt: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5985 | handleInterruptAttr(S, D, Attr); |
| 5986 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5987 | case AttributeList::AT_X86ForceAlignArgPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5988 | handleX86ForceAlignArgPointerAttr(S, D, Attr); |
| 5989 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5990 | case AttributeList::AT_DLLExport: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5991 | case AttributeList::AT_DLLImport: |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5992 | handleDLLAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5993 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5994 | case AttributeList::AT_Mips16: |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 5995 | handleSimpleAttributeWithExclusions<Mips16Attr, MicroMipsAttr, |
| 5996 | MipsInterruptAttr>(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5997 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5998 | case AttributeList::AT_NoMips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5999 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); |
| 6000 | break; |
Simon Atanasyan | 2c87f53 | 2017-05-22 12:47:43 +0000 | [diff] [blame] | 6001 | case AttributeList::AT_MicroMips: |
| 6002 | handleSimpleAttributeWithExclusions<MicroMipsAttr, Mips16Attr>(S, D, Attr); |
| 6003 | break; |
| 6004 | case AttributeList::AT_NoMicroMips: |
| 6005 | handleSimpleAttribute<NoMicroMipsAttr>(S, D, Attr); |
| 6006 | break; |
Simon Atanasyan | 1a116db | 2017-07-20 20:34:18 +0000 | [diff] [blame] | 6007 | case AttributeList::AT_MipsLongCall: |
| 6008 | handleSimpleAttributeWithExclusions<MipsLongCallAttr, MipsShortCallAttr>( |
| 6009 | S, D, Attr); |
| 6010 | break; |
| 6011 | case AttributeList::AT_MipsShortCall: |
| 6012 | handleSimpleAttributeWithExclusions<MipsShortCallAttr, MipsLongCallAttr>( |
| 6013 | S, D, Attr); |
| 6014 | break; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6015 | case AttributeList::AT_AMDGPUFlatWorkGroupSize: |
| 6016 | handleAMDGPUFlatWorkGroupSizeAttr(S, D, Attr); |
| 6017 | break; |
| 6018 | case AttributeList::AT_AMDGPUWavesPerEU: |
| 6019 | handleAMDGPUWavesPerEUAttr(S, D, Attr); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 6020 | break; |
| 6021 | case AttributeList::AT_AMDGPUNumSGPR: |
| 6022 | handleAMDGPUNumSGPRAttr(S, D, Attr); |
| 6023 | break; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6024 | case AttributeList::AT_AMDGPUNumVGPR: |
| 6025 | handleAMDGPUNumVGPRAttr(S, D, Attr); |
| 6026 | break; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 6027 | case AttributeList::AT_AVRSignal: |
| 6028 | handleAVRSignalAttr(S, D, Attr); |
| 6029 | break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 6030 | case AttributeList::AT_IBAction: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6031 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); |
| 6032 | break; |
| 6033 | case AttributeList::AT_IBOutlet: |
| 6034 | handleIBOutlet(S, D, Attr); |
| 6035 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 6036 | case AttributeList::AT_IBOutletCollection: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6037 | handleIBOutletCollection(S, D, Attr); |
| 6038 | break; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 6039 | case AttributeList::AT_IFunc: |
| 6040 | handleIFuncAttr(S, D, Attr); |
| 6041 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6042 | case AttributeList::AT_Alias: |
| 6043 | handleAliasAttr(S, D, Attr); |
| 6044 | break; |
| 6045 | case AttributeList::AT_Aligned: |
| 6046 | handleAlignedAttr(S, D, Attr); |
| 6047 | break; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 6048 | case AttributeList::AT_AlignValue: |
| 6049 | handleAlignValueAttr(S, D, Attr); |
| 6050 | break; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 6051 | case AttributeList::AT_AllocSize: |
| 6052 | handleAllocSizeAttr(S, D, Attr); |
| 6053 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6054 | case AttributeList::AT_AlwaysInline: |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 6055 | handleAlwaysInlineAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6056 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6057 | case AttributeList::AT_AnalyzerNoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6058 | handleAnalyzerNoReturnAttr(S, D, Attr); |
| 6059 | break; |
| 6060 | case AttributeList::AT_TLSModel: |
| 6061 | handleTLSModelAttr(S, D, Attr); |
| 6062 | break; |
| 6063 | case AttributeList::AT_Annotate: |
| 6064 | handleAnnotateAttr(S, D, Attr); |
| 6065 | break; |
| 6066 | case AttributeList::AT_Availability: |
| 6067 | handleAvailabilityAttr(S, D, Attr); |
| 6068 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6069 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 6070 | handleDependencyAttr(S, scope, D, Attr); |
| 6071 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6072 | case AttributeList::AT_Common: |
| 6073 | handleCommonAttr(S, D, Attr); |
| 6074 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 6075 | case AttributeList::AT_CUDAConstant: |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 6076 | handleConstantAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6077 | break; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 6078 | case AttributeList::AT_PassObjectSize: |
| 6079 | handlePassObjectSizeAttr(S, D, Attr); |
| 6080 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6081 | case AttributeList::AT_Constructor: |
| 6082 | handleConstructorAttr(S, D, Attr); |
| 6083 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 6084 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6085 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); |
| 6086 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6087 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 6088 | handleDeprecatedAttr(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 6089 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6090 | case AttributeList::AT_Destructor: |
| 6091 | handleDestructorAttr(S, D, Attr); |
| 6092 | break; |
| 6093 | case AttributeList::AT_EnableIf: |
| 6094 | handleEnableIfAttr(S, D, Attr); |
| 6095 | break; |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6096 | case AttributeList::AT_DiagnoseIf: |
| 6097 | handleDiagnoseIfAttr(S, D, Attr); |
| 6098 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6099 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6100 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6101 | break; |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 6102 | case AttributeList::AT_ExternalSourceSymbol: |
| 6103 | handleExternalSourceSymbolAttr(S, D, Attr); |
| 6104 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 6105 | case AttributeList::AT_MinSize: |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 6106 | handleMinSizeAttr(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 6107 | break; |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 6108 | case AttributeList::AT_OptimizeNone: |
| 6109 | handleOptimizeNoneAttr(S, D, Attr); |
| 6110 | break; |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 6111 | case AttributeList::AT_FlagEnum: |
| 6112 | handleSimpleAttribute<FlagEnumAttr>(S, D, Attr); |
| 6113 | break; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 6114 | case AttributeList::AT_EnumExtensibility: |
| 6115 | handleEnumExtensibilityAttr(S, D, Attr); |
| 6116 | break; |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 6117 | case AttributeList::AT_Flatten: |
| 6118 | handleSimpleAttribute<FlattenAttr>(S, D, Attr); |
| 6119 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6120 | case AttributeList::AT_Format: |
| 6121 | handleFormatAttr(S, D, Attr); |
| 6122 | break; |
| 6123 | case AttributeList::AT_FormatArg: |
| 6124 | handleFormatArgAttr(S, D, Attr); |
| 6125 | break; |
| 6126 | case AttributeList::AT_CUDAGlobal: |
| 6127 | handleGlobalAttr(S, D, Attr); |
| 6128 | break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 6129 | case AttributeList::AT_CUDADevice: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 6130 | handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D, |
| 6131 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6132 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 6133 | case AttributeList::AT_CUDAHost: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 6134 | handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, |
| 6135 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6136 | break; |
| 6137 | case AttributeList::AT_GNUInline: |
| 6138 | handleGNUInlineAttr(S, D, Attr); |
| 6139 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6140 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6141 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 6142 | break; |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 6143 | case AttributeList::AT_Restrict: |
| 6144 | handleRestrictAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6145 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6146 | case AttributeList::AT_MayAlias: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6147 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); |
| 6148 | break; |
| 6149 | case AttributeList::AT_Mode: |
| 6150 | handleModeAttr(S, D, Attr); |
| 6151 | break; |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 6152 | case AttributeList::AT_NoAlias: |
| 6153 | handleSimpleAttribute<NoAliasAttr>(S, D, Attr); |
| 6154 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6155 | case AttributeList::AT_NoCommon: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6156 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); |
| 6157 | break; |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 6158 | case AttributeList::AT_NoSplitStack: |
| 6159 | handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr); |
| 6160 | break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 6161 | case AttributeList::AT_NonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6162 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 6163 | handleNonNullAttrParameter(S, PVD, Attr); |
| 6164 | else |
| 6165 | handleNonNullAttr(S, D, Attr); |
| 6166 | break; |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 6167 | case AttributeList::AT_ReturnsNonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6168 | handleReturnsNonNullAttr(S, D, Attr); |
| 6169 | break; |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 6170 | case AttributeList::AT_AssumeAligned: |
| 6171 | handleAssumeAlignedAttr(S, D, Attr); |
| 6172 | break; |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 6173 | case AttributeList::AT_AllocAlign: |
| 6174 | handleAllocAlignAttr(S, D, Attr); |
| 6175 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6176 | case AttributeList::AT_Overloadable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6177 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); |
| 6178 | break; |
| 6179 | case AttributeList::AT_Ownership: |
| 6180 | handleOwnershipAttr(S, D, Attr); |
| 6181 | break; |
| 6182 | case AttributeList::AT_Cold: |
| 6183 | handleColdAttr(S, D, Attr); |
| 6184 | break; |
| 6185 | case AttributeList::AT_Hot: |
| 6186 | handleHotAttr(S, D, Attr); |
| 6187 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6188 | case AttributeList::AT_Naked: |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 6189 | handleNakedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6190 | break; |
| 6191 | case AttributeList::AT_NoReturn: |
| 6192 | handleNoReturnAttr(S, D, Attr); |
| 6193 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 6194 | case AttributeList::AT_NoThrow: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6195 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); |
| 6196 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 6197 | case AttributeList::AT_CUDAShared: |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 6198 | handleSharedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6199 | break; |
| 6200 | case AttributeList::AT_VecReturn: |
| 6201 | handleVecReturnAttr(S, D, Attr); |
| 6202 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6203 | case AttributeList::AT_ObjCOwnership: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6204 | handleObjCOwnershipAttr(S, D, Attr); |
| 6205 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6206 | case AttributeList::AT_ObjCPreciseLifetime: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6207 | handleObjCPreciseLifetimeAttr(S, D, Attr); |
| 6208 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6209 | case AttributeList::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6210 | handleObjCReturnsInnerPointerAttr(S, D, Attr); |
| 6211 | break; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 6212 | case AttributeList::AT_ObjCRequiresSuper: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6213 | handleObjCRequiresSuperAttr(S, D, Attr); |
| 6214 | break; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 6215 | case AttributeList::AT_ObjCBridge: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6216 | handleObjCBridgeAttr(S, scope, D, Attr); |
| 6217 | break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 6218 | case AttributeList::AT_ObjCBridgeMutable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6219 | handleObjCBridgeMutableAttr(S, scope, D, Attr); |
| 6220 | break; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 6221 | case AttributeList::AT_ObjCBridgeRelated: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6222 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); |
| 6223 | break; |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 6224 | case AttributeList::AT_ObjCDesignatedInitializer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6225 | handleObjCDesignatedInitializer(S, D, Attr); |
| 6226 | break; |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6227 | case AttributeList::AT_ObjCRuntimeName: |
| 6228 | handleObjCRuntimeName(S, D, Attr); |
| 6229 | break; |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 6230 | case AttributeList::AT_ObjCRuntimeVisible: |
| 6231 | handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, Attr); |
| 6232 | break; |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 6233 | case AttributeList::AT_ObjCBoxable: |
| 6234 | handleObjCBoxable(S, D, Attr); |
| 6235 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6236 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6237 | handleCFAuditedTransferAttr(S, D, Attr); |
| 6238 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6239 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6240 | handleCFUnknownTransferAttr(S, D, Attr); |
| 6241 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6242 | case AttributeList::AT_CFConsumed: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6243 | case AttributeList::AT_NSConsumed: |
| 6244 | handleNSConsumedAttr(S, D, Attr); |
| 6245 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6246 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6247 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); |
| 6248 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6249 | case AttributeList::AT_NSReturnsAutoreleased: |
| 6250 | case AttributeList::AT_NSReturnsNotRetained: |
| 6251 | case AttributeList::AT_CFReturnsNotRetained: |
| 6252 | case AttributeList::AT_NSReturnsRetained: |
| 6253 | case AttributeList::AT_CFReturnsRetained: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6254 | handleNSReturnsRetainedAttr(S, D, Attr); |
| 6255 | break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 6256 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6257 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); |
| 6258 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6259 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6260 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); |
| 6261 | break; |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 6262 | case AttributeList::AT_OpenCLIntelReqdSubGroupSize: |
| 6263 | handleSubGroupSize(S, D, Attr); |
| 6264 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 6265 | case AttributeList::AT_VecTypeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6266 | handleVecTypeHint(S, D, Attr); |
| 6267 | break; |
Eric Fiselier | 341e825 | 2016-09-02 18:53:31 +0000 | [diff] [blame] | 6268 | case AttributeList::AT_RequireConstantInit: |
| 6269 | handleSimpleAttribute<RequireConstantInitAttr>(S, D, Attr); |
| 6270 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6271 | case AttributeList::AT_InitPriority: |
| 6272 | handleInitPriorityAttr(S, D, Attr); |
| 6273 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6274 | case AttributeList::AT_Packed: |
| 6275 | handlePackedAttr(S, D, Attr); |
| 6276 | break; |
| 6277 | case AttributeList::AT_Section: |
| 6278 | handleSectionAttr(S, D, Attr); |
| 6279 | break; |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 6280 | case AttributeList::AT_Target: |
| 6281 | handleTargetAttr(S, D, Attr); |
| 6282 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6283 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 6284 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 6285 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6286 | case AttributeList::AT_ArcWeakrefUnavailable: |
| 6287 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); |
| 6288 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6289 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6290 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); |
| 6291 | break; |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 6292 | case AttributeList::AT_ObjCSubclassingRestricted: |
| 6293 | handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, Attr); |
| 6294 | break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 6295 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 6296 | handleObjCSuppresProtocolAttr(S, D, Attr); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 6297 | break; |
| 6298 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6299 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); |
| 6300 | break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 6301 | case AttributeList::AT_Unused: |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 6302 | handleUnusedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6303 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6304 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6305 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); |
| 6306 | break; |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 6307 | case AttributeList::AT_NotTailCalled: |
| 6308 | handleNotTailCalledAttr(S, D, Attr); |
| 6309 | break; |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 6310 | case AttributeList::AT_DisableTailCalls: |
| 6311 | handleDisableTailCallsAttr(S, D, Attr); |
| 6312 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6313 | case AttributeList::AT_Used: |
| 6314 | handleUsedAttr(S, D, Attr); |
| 6315 | break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 6316 | case AttributeList::AT_Visibility: |
| 6317 | handleVisibilityAttr(S, D, Attr, false); |
| 6318 | break; |
| 6319 | case AttributeList::AT_TypeVisibility: |
| 6320 | handleVisibilityAttr(S, D, Attr, true); |
| 6321 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 6322 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6323 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); |
| 6324 | break; |
| 6325 | case AttributeList::AT_WarnUnusedResult: |
| 6326 | handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 6327 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 6328 | case AttributeList::AT_Weak: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6329 | handleSimpleAttribute<WeakAttr>(S, D, Attr); |
| 6330 | break; |
| 6331 | case AttributeList::AT_WeakRef: |
| 6332 | handleWeakRefAttr(S, D, Attr); |
| 6333 | break; |
| 6334 | case AttributeList::AT_WeakImport: |
| 6335 | handleWeakImportAttr(S, D, Attr); |
| 6336 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6337 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6338 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6339 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6340 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6341 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); |
| 6342 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6343 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6344 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 6345 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6346 | case AttributeList::AT_ObjCNSObject: |
| 6347 | handleObjCNSObject(S, D, Attr); |
| 6348 | break; |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 6349 | case AttributeList::AT_ObjCIndependentClass: |
| 6350 | handleObjCIndependentClass(S, D, Attr); |
| 6351 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6352 | case AttributeList::AT_Blocks: |
| 6353 | handleBlocksAttr(S, D, Attr); |
| 6354 | break; |
| 6355 | case AttributeList::AT_Sentinel: |
| 6356 | handleSentinelAttr(S, D, Attr); |
| 6357 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 6358 | case AttributeList::AT_Const: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6359 | handleSimpleAttribute<ConstAttr>(S, D, Attr); |
| 6360 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6361 | case AttributeList::AT_Pure: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6362 | handleSimpleAttribute<PureAttr>(S, D, Attr); |
| 6363 | break; |
| 6364 | case AttributeList::AT_Cleanup: |
| 6365 | handleCleanupAttr(S, D, Attr); |
| 6366 | break; |
| 6367 | case AttributeList::AT_NoDebug: |
| 6368 | handleNoDebugAttr(S, D, Attr); |
| 6369 | break; |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 6370 | case AttributeList::AT_NoDuplicate: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6371 | handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr); |
| 6372 | break; |
Yaxun Liu | 7d07ae7 | 2016-11-01 18:45:32 +0000 | [diff] [blame] | 6373 | case AttributeList::AT_Convergent: |
| 6374 | handleSimpleAttribute<ConvergentAttr>(S, D, Attr); |
| 6375 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6376 | case AttributeList::AT_NoInline: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6377 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); |
| 6378 | break; |
| 6379 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
| 6380 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); |
| 6381 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6382 | case AttributeList::AT_StdCall: |
| 6383 | case AttributeList::AT_CDecl: |
| 6384 | case AttributeList::AT_FastCall: |
| 6385 | case AttributeList::AT_ThisCall: |
| 6386 | case AttributeList::AT_Pascal: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 6387 | case AttributeList::AT_RegCall: |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6388 | case AttributeList::AT_SwiftCall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 6389 | case AttributeList::AT_VectorCall: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 6390 | case AttributeList::AT_MSABI: |
| 6391 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6392 | case AttributeList::AT_Pcs: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 6393 | case AttributeList::AT_IntelOclBicc: |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 6394 | case AttributeList::AT_PreserveMost: |
| 6395 | case AttributeList::AT_PreserveAll: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6396 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 6397 | break; |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 6398 | case AttributeList::AT_Suppress: |
| 6399 | handleSuppressAttr(S, D, Attr); |
| 6400 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6401 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6402 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); |
| 6403 | break; |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6404 | case AttributeList::AT_OpenCLAccess: |
| 6405 | handleOpenCLAccessAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6406 | break; |
Anastasia Stulova | fde7622 | 2016-04-01 16:05:09 +0000 | [diff] [blame] | 6407 | case AttributeList::AT_OpenCLNoSVM: |
| 6408 | handleOpenCLNoSVMAttr(S, D, Attr); |
| 6409 | break; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6410 | case AttributeList::AT_SwiftContext: |
| 6411 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftContext); |
| 6412 | break; |
| 6413 | case AttributeList::AT_SwiftErrorResult: |
| 6414 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftErrorResult); |
| 6415 | break; |
| 6416 | case AttributeList::AT_SwiftIndirectResult: |
| 6417 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftIndirectResult); |
| 6418 | break; |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 6419 | case AttributeList::AT_InternalLinkage: |
| 6420 | handleInternalLinkageAttr(S, D, Attr); |
| 6421 | break; |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 6422 | case AttributeList::AT_LTOVisibilityPublic: |
| 6423 | handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, Attr); |
| 6424 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 6425 | |
| 6426 | // Microsoft attributes: |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6427 | case AttributeList::AT_EmptyBases: |
| 6428 | handleSimpleAttribute<EmptyBasesAttr>(S, D, Attr); |
| 6429 | break; |
| 6430 | case AttributeList::AT_LayoutVersion: |
| 6431 | handleLayoutVersion(S, D, Attr); |
| 6432 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 6433 | case AttributeList::AT_MSNoVTable: |
| 6434 | handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 6435 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 6436 | case AttributeList::AT_MSStruct: |
| 6437 | handleSimpleAttribute<MSStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 6438 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6439 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6440 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 6441 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 6442 | case AttributeList::AT_MSInheritance: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6443 | handleMSInheritanceAttr(S, D, Attr); |
| 6444 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 6445 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6446 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); |
| 6447 | break; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 6448 | case AttributeList::AT_Thread: |
| 6449 | handleDeclspecThreadAttr(S, D, Attr); |
| 6450 | break; |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6451 | |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 6452 | case AttributeList::AT_AbiTag: |
| 6453 | handleAbiTagAttr(S, D, Attr); |
| 6454 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6455 | |
| 6456 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 6457 | case AttributeList::AT_AssertExclusiveLock: |
| 6458 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 6459 | break; |
| 6460 | case AttributeList::AT_AssertSharedLock: |
| 6461 | handleAssertSharedLockAttr(S, D, Attr); |
| 6462 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6463 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6464 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); |
| 6465 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6466 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6467 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6468 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6469 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6470 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); |
| 6471 | break; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6472 | case AttributeList::AT_NoSanitize: |
| 6473 | handleNoSanitizeAttr(S, D, Attr); |
| 6474 | break; |
| 6475 | case AttributeList::AT_NoSanitizeSpecific: |
| 6476 | handleNoSanitizeSpecificAttr(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 6477 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6478 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6479 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 6480 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6481 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6482 | handleGuardedByAttr(S, D, Attr); |
| 6483 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6484 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6485 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6486 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6487 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6488 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6489 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6490 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6491 | handleLockReturnedAttr(S, D, Attr); |
| 6492 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6493 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6494 | handleLocksExcludedAttr(S, D, Attr); |
| 6495 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6496 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6497 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6498 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6499 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6500 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6501 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6502 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6503 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6504 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6505 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6506 | // Capability analysis attributes. |
| 6507 | case AttributeList::AT_Capability: |
| 6508 | case AttributeList::AT_Lockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6509 | handleCapabilityAttr(S, D, Attr); |
| 6510 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6511 | case AttributeList::AT_RequiresCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6512 | handleRequiresCapabilityAttr(S, D, Attr); |
| 6513 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6514 | |
| 6515 | case AttributeList::AT_AssertCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6516 | handleAssertCapabilityAttr(S, D, Attr); |
| 6517 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6518 | case AttributeList::AT_AcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6519 | handleAcquireCapabilityAttr(S, D, Attr); |
| 6520 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6521 | case AttributeList::AT_ReleaseCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6522 | handleReleaseCapabilityAttr(S, D, Attr); |
| 6523 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6524 | case AttributeList::AT_TryAcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6525 | handleTryAcquireCapabilityAttr(S, D, Attr); |
| 6526 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6527 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 6528 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 6529 | case AttributeList::AT_Consumable: |
| 6530 | handleConsumableAttr(S, D, Attr); |
| 6531 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 6532 | case AttributeList::AT_ConsumableAutoCast: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6533 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); |
| 6534 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 6535 | case AttributeList::AT_ConsumableSetOnRead: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6536 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); |
| 6537 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 6538 | case AttributeList::AT_CallableWhen: |
| 6539 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6540 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 6541 | case AttributeList::AT_ParamTypestate: |
| 6542 | handleParamTypestateAttr(S, D, Attr); |
| 6543 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 6544 | case AttributeList::AT_ReturnTypestate: |
| 6545 | handleReturnTypestateAttr(S, D, Attr); |
| 6546 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6547 | case AttributeList::AT_SetTypestate: |
| 6548 | handleSetTypestateAttr(S, D, Attr); |
| 6549 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 6550 | case AttributeList::AT_TestTypestate: |
| 6551 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6552 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6553 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6554 | // Type safety attributes. |
| 6555 | case AttributeList::AT_ArgumentWithTypeTag: |
| 6556 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 6557 | break; |
| 6558 | case AttributeList::AT_TypeTagForDatatype: |
| 6559 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 6560 | break; |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 6561 | case AttributeList::AT_AnyX86NoCallerSavedRegisters: |
| 6562 | handleNoCallerSavedRegsAttr(S, D, Attr); |
| 6563 | break; |
Pirama Arumuga Nainar | e5d2d71 | 2016-06-10 21:51:18 +0000 | [diff] [blame] | 6564 | case AttributeList::AT_RenderScriptKernel: |
| 6565 | handleSimpleAttribute<RenderScriptKernelAttr>(S, D, Attr); |
Pirama Arumuga Nainar | 8b788d0 | 2016-06-09 23:34:20 +0000 | [diff] [blame] | 6566 | break; |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 6567 | // XRay attributes. |
| 6568 | case AttributeList::AT_XRayInstrument: |
| 6569 | handleSimpleAttribute<XRayInstrumentAttr>(S, D, Attr); |
| 6570 | break; |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 6571 | case AttributeList::AT_XRayLogArgs: |
| 6572 | handleXRayLogArgsAttr(S, D, Attr); |
| 6573 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6574 | } |
| 6575 | } |
| 6576 | |
| 6577 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 6578 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 6579 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 6580 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 6581 | bool IncludeCXX11Attributes) { |
| 6582 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6583 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6584 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6585 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6586 | // GCC accepts |
| 6587 | // static int a9 __attribute__((weakref)); |
| 6588 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6589 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 6590 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 6591 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 6592 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6593 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6594 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6595 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 6596 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 6597 | // good to have a way to specify "these attributes must appear as a group", |
| 6598 | // for these. Additionally, it would be good to have a way to specify "these |
| 6599 | // 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] | 6600 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 6601 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6602 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 6603 | // FIXME: This emits a different error message than |
| 6604 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6605 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6606 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 6607 | } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6608 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6609 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 6610 | } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6611 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6612 | D->setInvalidDecl(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6613 | } else if (Attr *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) { |
| 6614 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6615 | << A << ExpectedKernelFunction; |
| 6616 | D->setInvalidDecl(); |
| 6617 | } else if (Attr *A = D->getAttr<AMDGPUWavesPerEUAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 6618 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6619 | << A << ExpectedKernelFunction; |
| 6620 | D->setInvalidDecl(); |
| 6621 | } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6622 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6623 | << A << ExpectedKernelFunction; |
| 6624 | D->setInvalidDecl(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6625 | } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6626 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6627 | << A << ExpectedKernelFunction; |
| 6628 | D->setInvalidDecl(); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame] | 6629 | } else if (Attr *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) { |
| 6630 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
| 6631 | D->setInvalidDecl(); |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6632 | } |
| 6633 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6634 | } |
| 6635 | |
Hiroshi Inoue | 939d932 | 2017-06-30 05:40:31 +0000 | [diff] [blame] | 6636 | // Helper for delayed processing TransparentUnion attribute. |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 6637 | void Sema::ProcessDeclAttributeDelayed(Decl *D, const AttributeList *AttrList) { |
| 6638 | for (const AttributeList *Attr = AttrList; Attr; Attr = Attr->getNext()) |
| 6639 | if (Attr->getKind() == AttributeList::AT_TransparentUnion) { |
| 6640 | handleTransparentUnionAttr(*this, D, *Attr); |
| 6641 | break; |
| 6642 | } |
| 6643 | } |
| 6644 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6645 | // Annotation attributes are the only attributes allowed after an access |
| 6646 | // specifier. |
| 6647 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 6648 | const AttributeList *AttrList) { |
| 6649 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6650 | if (l->getKind() == AttributeList::AT_Annotate) { |
David Majnemer | 706f315 | 2014-12-14 01:05:01 +0000 | [diff] [blame] | 6651 | ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6652 | } else { |
| 6653 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 6654 | return true; |
| 6655 | } |
| 6656 | } |
| 6657 | |
| 6658 | return false; |
| 6659 | } |
| 6660 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6661 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 6662 | /// contains any decl attributes that we should warn about. |
| 6663 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 6664 | for ( ; A; A = A->getNext()) { |
| 6665 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 6666 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6667 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 6668 | |
| 6669 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 6670 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 6671 | << A->getName() << A->getRange(); |
| 6672 | } else { |
| 6673 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 6674 | << A->getName() << A->getRange(); |
| 6675 | } |
| 6676 | } |
| 6677 | } |
| 6678 | |
| 6679 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 6680 | /// used to build a declaration, complain about any decl attributes |
| 6681 | /// which might be lying around on it. |
| 6682 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 6683 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 6684 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 6685 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 6686 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 6687 | } |
| 6688 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6689 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6690 | /// \#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] | 6691 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 6692 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6693 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6694 | NamedDecl *NewD = nullptr; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6695 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6696 | FunctionDecl *NewFD; |
| 6697 | // FIXME: Missing call to CheckFunctionDeclaration(). |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6698 | // FIXME: Mangling? |
| 6699 | // FIXME: Is the qualifier info correct? |
| 6700 | // FIXME: Is the DeclContext correct? |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6701 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 6702 | Loc, Loc, DeclarationName(II), |
| 6703 | FD->getType(), FD->getTypeSourceInfo(), |
| 6704 | SC_None, false/*isInlineSpecified*/, |
| 6705 | FD->hasPrototype(), |
| 6706 | false/*isConstexprSpecified*/); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6707 | NewD = NewFD; |
| 6708 | |
| 6709 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 6710 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6711 | |
| 6712 | // Fake up parameter variables; they are declared as if this were |
| 6713 | // a typedef. |
| 6714 | QualType FDTy = FD->getType(); |
| 6715 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 6716 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 6717 | for (const auto &AI : FT->param_types()) { |
| 6718 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6719 | Param->setScopeInfo(0, Params.size()); |
| 6720 | Params.push_back(Param); |
| 6721 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 6722 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6723 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6724 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 6725 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6726 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 6727 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6728 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6729 | if (VD->getQualifier()) { |
| 6730 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 6731 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6732 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6733 | } |
| 6734 | return NewD; |
| 6735 | } |
| 6736 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6737 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6738 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6739 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6740 | if (W.getUsed()) return; // only do this once |
| 6741 | W.setUsed(true); |
| 6742 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 6743 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6744 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6745 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 6746 | W.getLocation())); |
| 6747 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6748 | WeakTopLevelDecl.push_back(NewD); |
| 6749 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 6750 | // to insert Decl at TU scope, sorry. |
| 6751 | DeclContext *SavedContext = CurContext; |
| 6752 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 6753 | NewD->setDeclContext(CurContext); |
| 6754 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6755 | PushOnScopeChains(NewD, S); |
| 6756 | CurContext = SavedContext; |
| 6757 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6758 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6759 | } |
| 6760 | } |
| 6761 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6762 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 6763 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 6764 | // have to do this. |
| 6765 | LoadExternalWeakUndeclaredIdentifiers(); |
| 6766 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6767 | NamedDecl *ND = nullptr; |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6768 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 6769 | if (VD->isExternC()) |
| 6770 | ND = VD; |
| 6771 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 6772 | if (FD->isExternC()) |
| 6773 | ND = FD; |
| 6774 | if (ND) { |
| 6775 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 6776 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6777 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 6778 | WeakInfo W = I->second; |
| 6779 | DeclApplyPragmaWeak(S, ND, W); |
| 6780 | WeakUndeclaredIdentifiers[Id] = W; |
| 6781 | } |
| 6782 | } |
| 6783 | } |
| 6784 | } |
| 6785 | } |
| 6786 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6787 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 6788 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 6789 | /// 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] | 6790 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6791 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6792 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6793 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6794 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6795 | // Walk the declarator structure, applying decl attributes that were in a type |
| 6796 | // position to the decl itself. This handles cases like: |
| 6797 | // int *__attr__(x)** D; |
| 6798 | // when X is a decl attribute. |
| 6799 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 6800 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6801 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6802 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6803 | // Finally, apply any attributes on the decl itself. |
| 6804 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6805 | ProcessDeclAttributeList(S, D, Attrs); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 6806 | |
| 6807 | // Apply additional attributes specified by '#pragma clang attribute'. |
| 6808 | AddPragmaAttributes(S, D); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6809 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6810 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6811 | /// Is the given declaration allowed to use a forbidden type? |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6812 | /// If so, it'll still be annotated with an attribute that makes it |
| 6813 | /// illegal to actually use. |
| 6814 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl, |
| 6815 | const DelayedDiagnostic &diag, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6816 | UnavailableAttr::ImplicitReason &reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6817 | // Private ivars are always okay. Unfortunately, people don't |
| 6818 | // always properly make their ivars private, even in system headers. |
| 6819 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 6820 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 6821 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6822 | return false; |
| 6823 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6824 | // Silently accept unsupported uses of __weak in both user and system |
| 6825 | // declarations when it's been disabled, for ease of integration with |
| 6826 | // -fno-objc-arc files. We do have to take some care against attempts |
| 6827 | // to define such things; for now, we've only done that for ivars |
| 6828 | // and properties. |
| 6829 | if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) { |
| 6830 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 6831 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 6832 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 6833 | return true; |
| 6834 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6835 | } |
| 6836 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6837 | // Allow all sorts of things in system headers. |
| 6838 | if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) { |
| 6839 | // Currently, all the failures dealt with this way are due to ARC |
| 6840 | // restrictions. |
| 6841 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 6842 | return true; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6843 | } |
| 6844 | |
| 6845 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6846 | } |
| 6847 | |
| 6848 | /// Handle a delayed forbidden-type diagnostic. |
| 6849 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 6850 | Decl *decl) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6851 | auto reason = UnavailableAttr::IR_None; |
| 6852 | if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) { |
| 6853 | assert(reason && "didn't set reason?"); |
| 6854 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason, |
| 6855 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6856 | return; |
| 6857 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6858 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6859 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 6860 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6861 | // kind of forbidden type messages on unavailable functions. |
| 6862 | if (FD->hasAttr<UnavailableAttr>() && |
| 6863 | diag.getForbiddenTypeDiagnostic() == |
| 6864 | diag::err_arc_array_param_no_ownership) { |
| 6865 | diag.Triggered = true; |
| 6866 | return; |
| 6867 | } |
| 6868 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6869 | |
| 6870 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 6871 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 6872 | diag.Triggered = true; |
| 6873 | } |
| 6874 | |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 6875 | static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context, |
| 6876 | const Decl *D) { |
| 6877 | // Check each AvailabilityAttr to find the one for this platform. |
| 6878 | for (const auto *A : D->attrs()) { |
| 6879 | if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) { |
| 6880 | // FIXME: this is copied from CheckAvailability. We should try to |
| 6881 | // de-duplicate. |
| 6882 | |
| 6883 | // Check if this is an App Extension "platform", and if so chop off |
| 6884 | // the suffix for matching with the actual platform. |
| 6885 | StringRef ActualPlatform = Avail->getPlatform()->getName(); |
| 6886 | StringRef RealizedPlatform = ActualPlatform; |
| 6887 | if (Context.getLangOpts().AppExt) { |
| 6888 | size_t suffix = RealizedPlatform.rfind("_app_extension"); |
| 6889 | if (suffix != StringRef::npos) |
| 6890 | RealizedPlatform = RealizedPlatform.slice(0, suffix); |
| 6891 | } |
| 6892 | |
| 6893 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 6894 | |
| 6895 | // Match the platform name. |
| 6896 | if (RealizedPlatform == TargetPlatform) |
| 6897 | return Avail; |
| 6898 | } |
| 6899 | } |
| 6900 | return nullptr; |
| 6901 | } |
| 6902 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 6903 | /// The diagnostic we should emit for \c D, and the declaration that |
| 6904 | /// originated it, or \c AR_Available. |
| 6905 | /// |
| 6906 | /// \param D The declaration to check. |
| 6907 | /// \param Message If non-null, this will be populated with the message from |
| 6908 | /// the availability attribute that is selected. |
| 6909 | static std::pair<AvailabilityResult, const NamedDecl *> |
| 6910 | ShouldDiagnoseAvailabilityOfDecl(const NamedDecl *D, std::string *Message) { |
| 6911 | AvailabilityResult Result = D->getAvailability(Message); |
| 6912 | |
| 6913 | // For typedefs, if the typedef declaration appears available look |
| 6914 | // to the underlying type to see if it is more restrictive. |
| 6915 | while (const TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 6916 | if (Result == AR_Available) { |
| 6917 | if (const TagType *TT = TD->getUnderlyingType()->getAs<TagType>()) { |
| 6918 | D = TT->getDecl(); |
| 6919 | Result = D->getAvailability(Message); |
| 6920 | continue; |
| 6921 | } |
| 6922 | } |
| 6923 | break; |
| 6924 | } |
| 6925 | |
| 6926 | // Forward class declarations get their attributes from their definition. |
| 6927 | if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(D)) { |
| 6928 | if (IDecl->getDefinition()) { |
| 6929 | D = IDecl->getDefinition(); |
| 6930 | Result = D->getAvailability(Message); |
| 6931 | } |
| 6932 | } |
| 6933 | |
| 6934 | if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 6935 | if (Result == AR_Available) { |
| 6936 | const DeclContext *DC = ECD->getDeclContext(); |
| 6937 | if (const auto *TheEnumDecl = dyn_cast<EnumDecl>(DC)) { |
| 6938 | Result = TheEnumDecl->getAvailability(Message); |
| 6939 | D = TheEnumDecl; |
| 6940 | } |
| 6941 | } |
| 6942 | |
| 6943 | return {Result, D}; |
| 6944 | } |
| 6945 | |
| 6946 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6947 | /// \brief whether we should emit a diagnostic for \c K and \c DeclVersion in |
| 6948 | /// the context of \c Ctx. For example, we should emit an unavailable diagnostic |
| 6949 | /// in a deprecated context, but not the other way around. |
| 6950 | static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K, |
| 6951 | VersionTuple DeclVersion, |
| 6952 | Decl *Ctx) { |
| 6953 | assert(K != AR_Available && "Expected an unavailable declaration here!"); |
| 6954 | |
| 6955 | // Checks if we should emit the availability diagnostic in the context of C. |
| 6956 | auto CheckContext = [&](const Decl *C) { |
| 6957 | if (K == AR_NotYetIntroduced) { |
| 6958 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C)) |
| 6959 | if (AA->getIntroduced() >= DeclVersion) |
| 6960 | return true; |
| 6961 | } else if (K == AR_Deprecated) |
| 6962 | if (C->isDeprecated()) |
| 6963 | return true; |
| 6964 | |
| 6965 | if (C->isUnavailable()) |
| 6966 | return true; |
| 6967 | return false; |
| 6968 | }; |
| 6969 | |
| 6970 | // FIXME: This is a temporary workaround! Some existing Apple headers depends |
| 6971 | // on nested declarations in an @interface having the availability of the |
| 6972 | // interface when they really shouldn't: they are members of the enclosing |
| 6973 | // context, and can referenced from there. |
| 6974 | if (S.OriginalLexicalContext && cast<Decl>(S.OriginalLexicalContext) != Ctx) { |
| 6975 | auto *OrigCtx = cast<Decl>(S.OriginalLexicalContext); |
| 6976 | if (CheckContext(OrigCtx)) |
| 6977 | return false; |
| 6978 | |
| 6979 | // An implementation implicitly has the availability of the interface. |
| 6980 | if (auto *CatOrImpl = dyn_cast<ObjCImplDecl>(OrigCtx)) { |
| 6981 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 6982 | if (CheckContext(Interface)) |
| 6983 | return false; |
| 6984 | } |
| 6985 | // A category implicitly has the availability of the interface. |
| 6986 | else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(OrigCtx)) |
| 6987 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 6988 | if (CheckContext(Interface)) |
| 6989 | return false; |
| 6990 | } |
| 6991 | |
| 6992 | do { |
| 6993 | if (CheckContext(Ctx)) |
| 6994 | return false; |
| 6995 | |
| 6996 | // An implementation implicitly has the availability of the interface. |
| 6997 | if (auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) { |
| 6998 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 6999 | if (CheckContext(Interface)) |
| 7000 | return false; |
| 7001 | } |
| 7002 | // A category implicitly has the availability of the interface. |
| 7003 | else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx)) |
| 7004 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 7005 | if (CheckContext(Interface)) |
| 7006 | return false; |
| 7007 | } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext()))); |
| 7008 | |
| 7009 | return true; |
| 7010 | } |
| 7011 | |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 7012 | static bool |
| 7013 | shouldDiagnoseAvailabilityByDefault(const ASTContext &Context, |
| 7014 | const VersionTuple &DeploymentVersion, |
| 7015 | const VersionTuple &DeclVersion) { |
| 7016 | const auto &Triple = Context.getTargetInfo().getTriple(); |
| 7017 | VersionTuple ForceAvailabilityFromVersion; |
| 7018 | switch (Triple.getOS()) { |
| 7019 | case llvm::Triple::IOS: |
| 7020 | case llvm::Triple::TvOS: |
| 7021 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/11); |
| 7022 | break; |
| 7023 | case llvm::Triple::WatchOS: |
| 7024 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/4); |
| 7025 | break; |
| 7026 | case llvm::Triple::Darwin: |
| 7027 | case llvm::Triple::MacOSX: |
| 7028 | ForceAvailabilityFromVersion = VersionTuple(/*Major=*/10, /*Minor=*/13); |
| 7029 | break; |
| 7030 | default: |
| 7031 | // New targets should always warn about availability. |
| 7032 | return Triple.getVendor() == llvm::Triple::Apple; |
| 7033 | } |
| 7034 | return DeploymentVersion >= ForceAvailabilityFromVersion || |
| 7035 | DeclVersion >= ForceAvailabilityFromVersion; |
| 7036 | } |
| 7037 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7038 | static NamedDecl *findEnclosingDeclToAnnotate(Decl *OrigCtx) { |
| 7039 | for (Decl *Ctx = OrigCtx; Ctx; |
| 7040 | Ctx = cast_or_null<Decl>(Ctx->getDeclContext())) { |
| 7041 | if (isa<TagDecl>(Ctx) || isa<FunctionDecl>(Ctx) || isa<ObjCMethodDecl>(Ctx)) |
| 7042 | return cast<NamedDecl>(Ctx); |
| 7043 | if (auto *CD = dyn_cast<ObjCContainerDecl>(Ctx)) { |
| 7044 | if (auto *Imp = dyn_cast<ObjCImplDecl>(Ctx)) |
| 7045 | return Imp->getClassInterface(); |
| 7046 | return CD; |
| 7047 | } |
| 7048 | } |
| 7049 | |
| 7050 | return dyn_cast<NamedDecl>(OrigCtx); |
| 7051 | } |
| 7052 | |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7053 | namespace { |
| 7054 | |
| 7055 | struct AttributeInsertion { |
| 7056 | StringRef Prefix; |
| 7057 | SourceLocation Loc; |
| 7058 | StringRef Suffix; |
| 7059 | |
| 7060 | static AttributeInsertion createInsertionAfter(const NamedDecl *D) { |
| 7061 | return {" ", D->getLocEnd(), ""}; |
| 7062 | } |
| 7063 | static AttributeInsertion createInsertionAfter(SourceLocation Loc) { |
| 7064 | return {" ", Loc, ""}; |
| 7065 | } |
| 7066 | static AttributeInsertion createInsertionBefore(const NamedDecl *D) { |
| 7067 | return {"", D->getLocStart(), "\n"}; |
| 7068 | } |
| 7069 | }; |
| 7070 | |
| 7071 | } // end anonymous namespace |
| 7072 | |
| 7073 | /// Returns a source location in which it's appropriate to insert a new |
| 7074 | /// attribute for the given declaration \D. |
| 7075 | static Optional<AttributeInsertion> |
| 7076 | createAttributeInsertion(const NamedDecl *D, const SourceManager &SM, |
| 7077 | const LangOptions &LangOpts) { |
| 7078 | if (isa<ObjCPropertyDecl>(D)) |
| 7079 | return AttributeInsertion::createInsertionAfter(D); |
| 7080 | if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 7081 | if (MD->hasBody()) |
| 7082 | return None; |
| 7083 | return AttributeInsertion::createInsertionAfter(D); |
| 7084 | } |
| 7085 | if (const auto *TD = dyn_cast<TagDecl>(D)) { |
| 7086 | SourceLocation Loc = |
| 7087 | Lexer::getLocForEndOfToken(TD->getInnerLocStart(), 0, SM, LangOpts); |
| 7088 | if (Loc.isInvalid()) |
| 7089 | return None; |
| 7090 | // Insert after the 'struct'/whatever keyword. |
| 7091 | return AttributeInsertion::createInsertionAfter(Loc); |
| 7092 | } |
| 7093 | return AttributeInsertion::createInsertionBefore(D); |
| 7094 | } |
| 7095 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7096 | /// Actually emit an availability diagnostic for a reference to an unavailable |
| 7097 | /// decl. |
| 7098 | /// |
| 7099 | /// \param Ctx The context that the reference occurred in |
| 7100 | /// \param ReferringDecl The exact declaration that was referenced. |
| 7101 | /// \param OffendingDecl A related decl to \c ReferringDecl that has an |
| 7102 | /// availability attribute corrisponding to \c K attached to it. Note that this |
| 7103 | /// may not be the same as ReferringDecl, i.e. if an EnumDecl is annotated and |
| 7104 | /// we refer to a member EnumConstantDecl, ReferringDecl is the EnumConstantDecl |
| 7105 | /// and OffendingDecl is the EnumDecl. |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7106 | static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K, |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7107 | Decl *Ctx, const NamedDecl *ReferringDecl, |
| 7108 | const NamedDecl *OffendingDecl, |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7109 | StringRef Message, SourceLocation Loc, |
| 7110 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 7111 | const ObjCPropertyDecl *ObjCProperty, |
| 7112 | bool ObjCPropertyAccess) { |
| 7113 | // Diagnostics for deprecated or unavailable. |
| 7114 | unsigned diag, diag_message, diag_fwdclass_message; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7115 | unsigned diag_available_here = diag::note_availability_specified_here; |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7116 | SourceLocation NoteLocation = OffendingDecl->getLocation(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7117 | |
| 7118 | // Matches 'diag::note_property_attribute' options. |
| 7119 | unsigned property_note_select; |
| 7120 | |
| 7121 | // Matches diag::note_availability_specified_here. |
| 7122 | unsigned available_here_select_kind; |
| 7123 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7124 | VersionTuple DeclVersion; |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7125 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, OffendingDecl)) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7126 | DeclVersion = AA->getIntroduced(); |
| 7127 | |
| 7128 | if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx)) |
| 7129 | return; |
| 7130 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7131 | switch (K) { |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7132 | case AR_Deprecated: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7133 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 7134 | : diag::warn_property_method_deprecated; |
| 7135 | diag_message = diag::warn_deprecated_message; |
| 7136 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 7137 | property_note_select = /* deprecated */ 0; |
| 7138 | available_here_select_kind = /* deprecated */ 2; |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7139 | if (const auto *attr = OffendingDecl->getAttr<DeprecatedAttr>()) |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 7140 | NoteLocation = attr->getLocation(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7141 | break; |
| 7142 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7143 | case AR_Unavailable: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7144 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 7145 | : diag::err_property_method_unavailable; |
| 7146 | diag_message = diag::err_unavailable_message; |
| 7147 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 7148 | property_note_select = /* unavailable */ 1; |
| 7149 | available_here_select_kind = /* unavailable */ 0; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7150 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7151 | if (auto attr = OffendingDecl->getAttr<UnavailableAttr>()) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7152 | if (attr->isImplicit() && attr->getImplicitReason()) { |
| 7153 | // Most of these failures are due to extra restrictions in ARC; |
| 7154 | // reflect that in the primary diagnostic when applicable. |
| 7155 | auto flagARCError = [&] { |
| 7156 | if (S.getLangOpts().ObjCAutoRefCount && |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7157 | S.getSourceManager().isInSystemHeader( |
| 7158 | OffendingDecl->getLocation())) |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 7159 | diag = diag::err_unavailable_in_arc; |
| 7160 | }; |
| 7161 | |
| 7162 | switch (attr->getImplicitReason()) { |
| 7163 | case UnavailableAttr::IR_None: break; |
| 7164 | |
| 7165 | case UnavailableAttr::IR_ARCForbiddenType: |
| 7166 | flagARCError(); |
| 7167 | diag_available_here = diag::note_arc_forbidden_type; |
| 7168 | break; |
| 7169 | |
| 7170 | case UnavailableAttr::IR_ForbiddenWeak: |
| 7171 | if (S.getLangOpts().ObjCWeakRuntime) |
| 7172 | diag_available_here = diag::note_arc_weak_disabled; |
| 7173 | else |
| 7174 | diag_available_here = diag::note_arc_weak_no_runtime; |
| 7175 | break; |
| 7176 | |
| 7177 | case UnavailableAttr::IR_ARCForbiddenConversion: |
| 7178 | flagARCError(); |
| 7179 | diag_available_here = diag::note_performs_forbidden_arc_conversion; |
| 7180 | break; |
| 7181 | |
| 7182 | case UnavailableAttr::IR_ARCInitReturnsUnrelated: |
| 7183 | flagARCError(); |
| 7184 | diag_available_here = diag::note_arc_init_returns_unrelated; |
| 7185 | break; |
| 7186 | |
| 7187 | case UnavailableAttr::IR_ARCFieldWithOwnership: |
| 7188 | flagARCError(); |
| 7189 | diag_available_here = diag::note_arc_field_with_ownership; |
| 7190 | break; |
| 7191 | } |
| 7192 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 7193 | } |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7194 | break; |
| 7195 | |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 7196 | case AR_NotYetIntroduced: { |
| 7197 | // We would like to emit the diagnostic even if -Wunguarded-availability is |
| 7198 | // not specified for deployment targets >= to iOS 11 or equivalent or |
| 7199 | // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or |
| 7200 | // later. |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7201 | const AvailabilityAttr *AA = |
| 7202 | getAttrForPlatform(S.getASTContext(), OffendingDecl); |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 7203 | VersionTuple Introduced = AA->getIntroduced(); |
| 7204 | bool NewWarning = shouldDiagnoseAvailabilityByDefault( |
| 7205 | S.Context, S.Context.getTargetInfo().getPlatformMinVersion(), |
| 7206 | Introduced); |
| 7207 | diag = NewWarning ? diag::warn_partial_availability_new |
| 7208 | : diag::warn_partial_availability; |
| 7209 | diag_message = NewWarning ? diag::warn_partial_message_new |
| 7210 | : diag::warn_partial_message; |
| 7211 | diag_fwdclass_message = NewWarning ? diag::warn_partial_fwdclass_message_new |
| 7212 | : diag::warn_partial_fwdclass_message; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 7213 | property_note_select = /* partial */ 2; |
| 7214 | available_here_select_kind = /* partial */ 3; |
| 7215 | break; |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 7216 | } |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7217 | |
| 7218 | case AR_Available: |
| 7219 | llvm_unreachable("Warning for availability of available declaration?"); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7220 | } |
| 7221 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7222 | CharSourceRange UseRange; |
| 7223 | StringRef Replacement; |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7224 | if (K == AR_Deprecated) { |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7225 | if (auto attr = OffendingDecl->getAttr<DeprecatedAttr>()) |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7226 | Replacement = attr->getReplacement(); |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7227 | if (auto attr = getAttrForPlatform(S.Context, OffendingDecl)) |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 7228 | Replacement = attr->getReplacement(); |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7229 | |
| 7230 | if (!Replacement.empty()) |
| 7231 | UseRange = |
| 7232 | CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); |
| 7233 | } |
| 7234 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7235 | if (!Message.empty()) { |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7236 | S.Diag(Loc, diag_message) << ReferringDecl << Message |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7237 | << (UseRange.isValid() ? |
| 7238 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7239 | if (ObjCProperty) |
| 7240 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 7241 | << ObjCProperty->getDeclName() << property_note_select; |
| 7242 | } else if (!UnknownObjCClass) { |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7243 | S.Diag(Loc, diag) << ReferringDecl |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7244 | << (UseRange.isValid() ? |
| 7245 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7246 | if (ObjCProperty) |
| 7247 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 7248 | << ObjCProperty->getDeclName() << property_note_select; |
| 7249 | } else { |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7250 | S.Diag(Loc, diag_fwdclass_message) << ReferringDecl |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7251 | << (UseRange.isValid() ? |
| 7252 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7253 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 7254 | } |
| 7255 | |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 7256 | // The declaration can have multiple availability attributes, we are looking |
| 7257 | // at one of them. |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7258 | const AvailabilityAttr *A = getAttrForPlatform(S.Context, OffendingDecl); |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 7259 | if (A && A->isInherited()) { |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7260 | for (const Decl *Redecl = OffendingDecl->getMostRecentDecl(); Redecl; |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 7261 | Redecl = Redecl->getPreviousDecl()) { |
| 7262 | const AvailabilityAttr *AForRedecl = getAttrForPlatform(S.Context, |
| 7263 | Redecl); |
| 7264 | if (AForRedecl && !AForRedecl->isInherited()) { |
| 7265 | // If D is a declaration with inherited attributes, the note should |
| 7266 | // point to the declaration with actual attributes. |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7267 | S.Diag(Redecl->getLocation(), diag_available_here) << OffendingDecl |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 7268 | << available_here_select_kind; |
| 7269 | break; |
| 7270 | } |
| 7271 | } |
| 7272 | } |
| 7273 | else |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 7274 | S.Diag(NoteLocation, diag_available_here) |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7275 | << OffendingDecl << available_here_select_kind; |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 7276 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7277 | if (K == AR_NotYetIntroduced) |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7278 | if (const auto *Enclosing = findEnclosingDeclToAnnotate(Ctx)) { |
| 7279 | if (auto *TD = dyn_cast<TagDecl>(Enclosing)) |
| 7280 | if (TD->getDeclName().isEmpty()) { |
| 7281 | S.Diag(TD->getLocation(), diag::note_partial_availability_silence) |
| 7282 | << /*Anonymous*/1 << TD->getKindName(); |
| 7283 | return; |
| 7284 | } |
Alex Lorenz | 727c21e | 2017-07-26 13:58:02 +0000 | [diff] [blame] | 7285 | auto FixitNoteDiag = S.Diag(Enclosing->getLocation(), |
| 7286 | diag::note_partial_availability_silence) |
| 7287 | << /*Named*/ 0 << Enclosing; |
| 7288 | // Don't offer a fixit for declarations with availability attributes. |
| 7289 | if (Enclosing->hasAttr<AvailabilityAttr>()) |
| 7290 | return; |
| 7291 | if (!S.getPreprocessor().isMacroDefined("API_AVAILABLE")) |
| 7292 | return; |
| 7293 | Optional<AttributeInsertion> Insertion = createAttributeInsertion( |
| 7294 | Enclosing, S.getSourceManager(), S.getLangOpts()); |
| 7295 | if (!Insertion) |
| 7296 | return; |
| 7297 | std::string PlatformName = |
| 7298 | AvailabilityAttr::getPlatformNameSourceSpelling( |
| 7299 | S.getASTContext().getTargetInfo().getPlatformName()) |
| 7300 | .lower(); |
| 7301 | std::string Introduced = |
| 7302 | OffendingDecl->getVersionIntroduced().getAsString(); |
| 7303 | FixitNoteDiag << FixItHint::CreateInsertion( |
| 7304 | Insertion->Loc, |
| 7305 | (llvm::Twine(Insertion->Prefix) + "API_AVAILABLE(" + PlatformName + |
| 7306 | "(" + Introduced + "))" + Insertion->Suffix) |
| 7307 | .str()); |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7308 | } |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7309 | } |
| 7310 | |
| 7311 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 7312 | Decl *Ctx) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 7313 | assert(DD.Kind == DelayedDiagnostic::Availability && |
| 7314 | "Expected an availability diagnostic here"); |
| 7315 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7316 | DD.Triggered = true; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 7317 | DoEmitAvailabilityWarning( |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7318 | S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityReferringDecl(), |
| 7319 | DD.getAvailabilityOffendingDecl(), DD.getAvailabilityMessage(), DD.Loc, |
| 7320 | DD.getUnknownObjCClass(), DD.getObjCProperty(), false); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7321 | } |
| 7322 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7323 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 7324 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7325 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7326 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7327 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7328 | // When delaying diagnostics to run in the context of a parsed |
| 7329 | // declaration, we only want to actually emit anything if parsing |
| 7330 | // succeeds. |
| 7331 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7332 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7333 | // We emit all the active diagnostics in this pool or any of its |
| 7334 | // parents. In general, we'll get one pool for the decl spec |
| 7335 | // and a child pool for each declarator; in a decl group like: |
| 7336 | // deprecated_typedef foo, *bar, baz(); |
| 7337 | // only the declarator pops will be passed decls. This is correct; |
| 7338 | // we really do need to consider delayed diagnostics from the decl spec |
| 7339 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7340 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7341 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7342 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7343 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 7344 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 7345 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7346 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7347 | continue; |
| 7348 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7349 | switch (diag.Kind) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 7350 | case DelayedDiagnostic::Availability: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 7351 | // Don't bother giving deprecation/unavailable diagnostics if |
| 7352 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 7353 | if (!decl->isInvalidDecl()) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7354 | handleDelayedAvailabilityCheck(*this, diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7355 | break; |
| 7356 | |
| 7357 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7358 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7359 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7360 | |
| 7361 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7362 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7363 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7364 | } |
| 7365 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7366 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7367 | } |
| 7368 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7369 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 7370 | /// been delayed in the current context instead of in the given pool. |
| 7371 | /// Essentially, this just moves them to the current pool. |
| 7372 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 7373 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 7374 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 7375 | curPool->steal(pool); |
| 7376 | } |
| 7377 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7378 | static void EmitAvailabilityWarning(Sema &S, AvailabilityResult AR, |
| 7379 | const NamedDecl *ReferringDecl, |
| 7380 | const NamedDecl *OffendingDecl, |
| 7381 | StringRef Message, SourceLocation Loc, |
| 7382 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 7383 | const ObjCPropertyDecl *ObjCProperty, |
| 7384 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7385 | // Delay if we're currently parsing a declaration. |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7386 | if (S.DelayedDiagnostics.shouldDelayDiagnostics()) { |
| 7387 | S.DelayedDiagnostics.add( |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7388 | DelayedDiagnostic::makeAvailability( |
| 7389 | AR, Loc, ReferringDecl, OffendingDecl, UnknownObjCClass, |
| 7390 | ObjCProperty, Message, ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7391 | return; |
| 7392 | } |
| 7393 | |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7394 | Decl *Ctx = cast<Decl>(S.getCurLexicalContext()); |
| 7395 | DoEmitAvailabilityWarning(S, AR, Ctx, ReferringDecl, OffendingDecl, |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7396 | Message, Loc, UnknownObjCClass, ObjCProperty, |
| 7397 | ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7398 | } |
Erik Pilkington | 48c7cc9 | 2016-07-29 17:37:38 +0000 | [diff] [blame] | 7399 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7400 | namespace { |
| 7401 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7402 | /// Returns true if the given statement can be a body-like child of \p Parent. |
| 7403 | bool isBodyLikeChildStmt(const Stmt *S, const Stmt *Parent) { |
| 7404 | switch (Parent->getStmtClass()) { |
| 7405 | case Stmt::IfStmtClass: |
| 7406 | return cast<IfStmt>(Parent)->getThen() == S || |
| 7407 | cast<IfStmt>(Parent)->getElse() == S; |
| 7408 | case Stmt::WhileStmtClass: |
| 7409 | return cast<WhileStmt>(Parent)->getBody() == S; |
| 7410 | case Stmt::DoStmtClass: |
| 7411 | return cast<DoStmt>(Parent)->getBody() == S; |
| 7412 | case Stmt::ForStmtClass: |
| 7413 | return cast<ForStmt>(Parent)->getBody() == S; |
| 7414 | case Stmt::CXXForRangeStmtClass: |
| 7415 | return cast<CXXForRangeStmt>(Parent)->getBody() == S; |
| 7416 | case Stmt::ObjCForCollectionStmtClass: |
| 7417 | return cast<ObjCForCollectionStmt>(Parent)->getBody() == S; |
| 7418 | case Stmt::CaseStmtClass: |
| 7419 | case Stmt::DefaultStmtClass: |
| 7420 | return cast<SwitchCase>(Parent)->getSubStmt() == S; |
| 7421 | default: |
| 7422 | return false; |
| 7423 | } |
| 7424 | } |
| 7425 | |
| 7426 | class StmtUSEFinder : public RecursiveASTVisitor<StmtUSEFinder> { |
| 7427 | const Stmt *Target; |
| 7428 | |
| 7429 | public: |
| 7430 | bool VisitStmt(Stmt *S) { return S != Target; } |
| 7431 | |
| 7432 | /// Returns true if the given statement is present in the given declaration. |
| 7433 | static bool isContained(const Stmt *Target, const Decl *D) { |
| 7434 | StmtUSEFinder Visitor; |
| 7435 | Visitor.Target = Target; |
| 7436 | return !Visitor.TraverseDecl(const_cast<Decl *>(D)); |
| 7437 | } |
| 7438 | }; |
| 7439 | |
| 7440 | /// Traverses the AST and finds the last statement that used a given |
| 7441 | /// declaration. |
| 7442 | class LastDeclUSEFinder : public RecursiveASTVisitor<LastDeclUSEFinder> { |
| 7443 | const Decl *D; |
| 7444 | |
| 7445 | public: |
| 7446 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 7447 | if (DRE->getDecl() == D) |
| 7448 | return false; |
| 7449 | return true; |
| 7450 | } |
| 7451 | |
| 7452 | static const Stmt *findLastStmtThatUsesDecl(const Decl *D, |
| 7453 | const CompoundStmt *Scope) { |
| 7454 | LastDeclUSEFinder Visitor; |
| 7455 | Visitor.D = D; |
| 7456 | for (auto I = Scope->body_rbegin(), E = Scope->body_rend(); I != E; ++I) { |
| 7457 | const Stmt *S = *I; |
| 7458 | if (!Visitor.TraverseStmt(const_cast<Stmt *>(S))) |
| 7459 | return S; |
| 7460 | } |
| 7461 | return nullptr; |
| 7462 | } |
| 7463 | }; |
| 7464 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7465 | /// \brief This class implements -Wunguarded-availability. |
| 7466 | /// |
| 7467 | /// This is done with a traversal of the AST of a function that makes reference |
| 7468 | /// to a partially available declaration. Whenever we encounter an \c if of the |
| 7469 | /// form: \c if(@available(...)), we use the version from the condition to visit |
| 7470 | /// the then statement. |
| 7471 | class DiagnoseUnguardedAvailability |
| 7472 | : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> { |
| 7473 | typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base; |
| 7474 | |
| 7475 | Sema &SemaRef; |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7476 | Decl *Ctx; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7477 | |
| 7478 | /// Stack of potentially nested 'if (@available(...))'s. |
| 7479 | SmallVector<VersionTuple, 8> AvailabilityStack; |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7480 | SmallVector<const Stmt *, 16> StmtStack; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7481 | |
| 7482 | void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range); |
| 7483 | |
| 7484 | public: |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7485 | DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx) |
| 7486 | : SemaRef(SemaRef), Ctx(Ctx) { |
| 7487 | AvailabilityStack.push_back( |
| 7488 | SemaRef.Context.getTargetInfo().getPlatformMinVersion()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7489 | } |
| 7490 | |
Alex Lorenz | 6f91112 | 2017-05-16 13:58:53 +0000 | [diff] [blame] | 7491 | bool TraverseDecl(Decl *D) { |
| 7492 | // Avoid visiting nested functions to prevent duplicate warnings. |
| 7493 | if (!D || isa<FunctionDecl>(D)) |
| 7494 | return true; |
| 7495 | return Base::TraverseDecl(D); |
| 7496 | } |
| 7497 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7498 | bool TraverseStmt(Stmt *S) { |
| 7499 | if (!S) |
| 7500 | return true; |
| 7501 | StmtStack.push_back(S); |
| 7502 | bool Result = Base::TraverseStmt(S); |
| 7503 | StmtStack.pop_back(); |
| 7504 | return Result; |
| 7505 | } |
| 7506 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7507 | void IssueDiagnostics(Stmt *S) { TraverseStmt(S); } |
| 7508 | |
| 7509 | bool TraverseIfStmt(IfStmt *If); |
| 7510 | |
Alex Lorenz | 6f91112 | 2017-05-16 13:58:53 +0000 | [diff] [blame] | 7511 | bool TraverseLambdaExpr(LambdaExpr *E) { return true; } |
| 7512 | |
Erik Pilkington | 6ac77a6 | 2017-05-22 15:41:12 +0000 | [diff] [blame] | 7513 | bool VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *PRE) { |
| 7514 | if (PRE->isClassReceiver()) |
| 7515 | DiagnoseDeclAvailability(PRE->getClassReceiver(), PRE->getReceiverLocation()); |
| 7516 | return true; |
| 7517 | } |
| 7518 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7519 | bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) { |
| 7520 | if (ObjCMethodDecl *D = Msg->getMethodDecl()) |
| 7521 | DiagnoseDeclAvailability( |
| 7522 | D, SourceRange(Msg->getSelectorStartLoc(), Msg->getLocEnd())); |
| 7523 | return true; |
| 7524 | } |
| 7525 | |
| 7526 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 7527 | DiagnoseDeclAvailability(DRE->getDecl(), |
| 7528 | SourceRange(DRE->getLocStart(), DRE->getLocEnd())); |
| 7529 | return true; |
| 7530 | } |
| 7531 | |
| 7532 | bool VisitMemberExpr(MemberExpr *ME) { |
| 7533 | DiagnoseDeclAvailability(ME->getMemberDecl(), |
| 7534 | SourceRange(ME->getLocStart(), ME->getLocEnd())); |
| 7535 | return true; |
| 7536 | } |
| 7537 | |
Alex Lorenz | 0a484ba | 2017-05-24 15:15:29 +0000 | [diff] [blame] | 7538 | bool VisitObjCAvailabilityCheckExpr(ObjCAvailabilityCheckExpr *E) { |
| 7539 | SemaRef.Diag(E->getLocStart(), diag::warn_at_available_unchecked_use) |
| 7540 | << (!SemaRef.getLangOpts().ObjC1); |
| 7541 | return true; |
| 7542 | } |
| 7543 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7544 | bool VisitTypeLoc(TypeLoc Ty); |
| 7545 | }; |
| 7546 | |
| 7547 | void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability( |
| 7548 | NamedDecl *D, SourceRange Range) { |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7549 | AvailabilityResult Result; |
| 7550 | const NamedDecl *OffendingDecl; |
| 7551 | std::tie(Result, OffendingDecl) = |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7552 | ShouldDiagnoseAvailabilityOfDecl(D, nullptr); |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7553 | if (Result != AR_Available) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7554 | // All other diagnostic kinds have already been handled in |
| 7555 | // DiagnoseAvailabilityOfDecl. |
| 7556 | if (Result != AR_NotYetIntroduced) |
| 7557 | return; |
| 7558 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7559 | const AvailabilityAttr *AA = |
| 7560 | getAttrForPlatform(SemaRef.getASTContext(), OffendingDecl); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7561 | VersionTuple Introduced = AA->getIntroduced(); |
| 7562 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7563 | if (AvailabilityStack.back() >= Introduced) |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7564 | return; |
| 7565 | |
| 7566 | // If the context of this function is less available than D, we should not |
| 7567 | // emit a diagnostic. |
| 7568 | if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx)) |
| 7569 | return; |
| 7570 | |
Alex Lorenz | c9a369f | 2017-06-22 17:02:24 +0000 | [diff] [blame] | 7571 | // We would like to emit the diagnostic even if -Wunguarded-availability is |
| 7572 | // not specified for deployment targets >= to iOS 11 or equivalent or |
| 7573 | // for declarations that were introduced in iOS 11 (macOS 10.13, ...) or |
| 7574 | // later. |
| 7575 | unsigned DiagKind = |
| 7576 | shouldDiagnoseAvailabilityByDefault( |
| 7577 | SemaRef.Context, |
| 7578 | SemaRef.Context.getTargetInfo().getPlatformMinVersion(), Introduced) |
| 7579 | ? diag::warn_unguarded_availability_new |
| 7580 | : diag::warn_unguarded_availability; |
| 7581 | |
| 7582 | SemaRef.Diag(Range.getBegin(), DiagKind) |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7583 | << Range << D |
| 7584 | << AvailabilityAttr::getPrettyPlatformName( |
| 7585 | SemaRef.getASTContext().getTargetInfo().getPlatformName()) |
| 7586 | << Introduced.getAsString(); |
| 7587 | |
Erik Pilkington | 4042f3c | 2017-07-05 17:08:56 +0000 | [diff] [blame] | 7588 | SemaRef.Diag(OffendingDecl->getLocation(), |
| 7589 | diag::note_availability_specified_here) |
| 7590 | << OffendingDecl << /* partial */ 3; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7591 | |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7592 | auto FixitDiag = |
| 7593 | SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence) |
| 7594 | << Range << D |
| 7595 | << (SemaRef.getLangOpts().ObjC1 ? /*@available*/ 0 |
| 7596 | : /*__builtin_available*/ 1); |
| 7597 | |
| 7598 | // Find the statement which should be enclosed in the if @available check. |
| 7599 | if (StmtStack.empty()) |
| 7600 | return; |
| 7601 | const Stmt *StmtOfUse = StmtStack.back(); |
| 7602 | const CompoundStmt *Scope = nullptr; |
| 7603 | for (const Stmt *S : llvm::reverse(StmtStack)) { |
| 7604 | if (const auto *CS = dyn_cast<CompoundStmt>(S)) { |
| 7605 | Scope = CS; |
| 7606 | break; |
| 7607 | } |
| 7608 | if (isBodyLikeChildStmt(StmtOfUse, S)) { |
| 7609 | // The declaration won't be seen outside of the statement, so we don't |
| 7610 | // have to wrap the uses of any declared variables in if (@available). |
| 7611 | // Therefore we can avoid setting Scope here. |
| 7612 | break; |
| 7613 | } |
| 7614 | StmtOfUse = S; |
| 7615 | } |
| 7616 | const Stmt *LastStmtOfUse = nullptr; |
| 7617 | if (isa<DeclStmt>(StmtOfUse) && Scope) { |
| 7618 | for (const Decl *D : cast<DeclStmt>(StmtOfUse)->decls()) { |
| 7619 | if (StmtUSEFinder::isContained(StmtStack.back(), D)) { |
| 7620 | LastStmtOfUse = LastDeclUSEFinder::findLastStmtThatUsesDecl(D, Scope); |
| 7621 | break; |
| 7622 | } |
| 7623 | } |
| 7624 | } |
| 7625 | |
| 7626 | const SourceManager &SM = SemaRef.getSourceManager(); |
| 7627 | SourceLocation IfInsertionLoc = |
| 7628 | SM.getExpansionLoc(StmtOfUse->getLocStart()); |
| 7629 | SourceLocation StmtEndLoc = |
| 7630 | SM.getExpansionRange( |
| 7631 | (LastStmtOfUse ? LastStmtOfUse : StmtOfUse)->getLocEnd()) |
| 7632 | .second; |
| 7633 | if (SM.getFileID(IfInsertionLoc) != SM.getFileID(StmtEndLoc)) |
| 7634 | return; |
| 7635 | |
| 7636 | StringRef Indentation = Lexer::getIndentationForLine(IfInsertionLoc, SM); |
| 7637 | const char *ExtraIndentation = " "; |
| 7638 | std::string FixItString; |
| 7639 | llvm::raw_string_ostream FixItOS(FixItString); |
| 7640 | FixItOS << "if (" << (SemaRef.getLangOpts().ObjC1 ? "@available" |
| 7641 | : "__builtin_available") |
Alex Lorenz | e1fb64e | 2017-05-09 15:34:46 +0000 | [diff] [blame] | 7642 | << "(" |
| 7643 | << AvailabilityAttr::getPlatformNameSourceSpelling( |
| 7644 | SemaRef.getASTContext().getTargetInfo().getPlatformName()) |
Alex Lorenz | 9c5c2bf | 2017-05-05 16:42:44 +0000 | [diff] [blame] | 7645 | << " " << Introduced.getAsString() << ", *)) {\n" |
| 7646 | << Indentation << ExtraIndentation; |
| 7647 | FixitDiag << FixItHint::CreateInsertion(IfInsertionLoc, FixItOS.str()); |
| 7648 | SourceLocation ElseInsertionLoc = Lexer::findLocationAfterToken( |
| 7649 | StmtEndLoc, tok::semi, SM, SemaRef.getLangOpts(), |
| 7650 | /*SkipTrailingWhitespaceAndNewLine=*/false); |
| 7651 | if (ElseInsertionLoc.isInvalid()) |
| 7652 | ElseInsertionLoc = |
| 7653 | Lexer::getLocForEndOfToken(StmtEndLoc, 0, SM, SemaRef.getLangOpts()); |
| 7654 | FixItOS.str().clear(); |
| 7655 | FixItOS << "\n" |
| 7656 | << Indentation << "} else {\n" |
| 7657 | << Indentation << ExtraIndentation |
| 7658 | << "// Fallback on earlier versions\n" |
| 7659 | << Indentation << "}"; |
| 7660 | FixitDiag << FixItHint::CreateInsertion(ElseInsertionLoc, FixItOS.str()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7661 | } |
| 7662 | } |
| 7663 | |
| 7664 | bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) { |
| 7665 | const Type *TyPtr = Ty.getTypePtr(); |
| 7666 | SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()}; |
| 7667 | |
Erik Pilkington | 6ac77a6 | 2017-05-22 15:41:12 +0000 | [diff] [blame] | 7668 | if (Range.isInvalid()) |
| 7669 | return true; |
| 7670 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7671 | if (const TagType *TT = dyn_cast<TagType>(TyPtr)) { |
| 7672 | TagDecl *TD = TT->getDecl(); |
| 7673 | DiagnoseDeclAvailability(TD, Range); |
| 7674 | |
| 7675 | } else if (const TypedefType *TD = dyn_cast<TypedefType>(TyPtr)) { |
| 7676 | TypedefNameDecl *D = TD->getDecl(); |
| 7677 | DiagnoseDeclAvailability(D, Range); |
| 7678 | |
| 7679 | } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) { |
| 7680 | if (NamedDecl *D = ObjCO->getInterface()) |
| 7681 | DiagnoseDeclAvailability(D, Range); |
| 7682 | } |
| 7683 | |
| 7684 | return true; |
| 7685 | } |
| 7686 | |
| 7687 | bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) { |
| 7688 | VersionTuple CondVersion; |
| 7689 | if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) { |
| 7690 | CondVersion = E->getVersion(); |
| 7691 | |
| 7692 | // If we're using the '*' case here or if this check is redundant, then we |
| 7693 | // use the enclosing version to check both branches. |
| 7694 | if (CondVersion.empty() || CondVersion <= AvailabilityStack.back()) |
| 7695 | return Base::TraverseStmt(If->getThen()) && |
| 7696 | Base::TraverseStmt(If->getElse()); |
| 7697 | } else { |
| 7698 | // This isn't an availability checking 'if', we can just continue. |
| 7699 | return Base::TraverseIfStmt(If); |
| 7700 | } |
| 7701 | |
| 7702 | AvailabilityStack.push_back(CondVersion); |
| 7703 | bool ShouldContinue = TraverseStmt(If->getThen()); |
| 7704 | AvailabilityStack.pop_back(); |
| 7705 | |
| 7706 | return ShouldContinue && TraverseStmt(If->getElse()); |
| 7707 | } |
| 7708 | |
| 7709 | } // end anonymous namespace |
| 7710 | |
| 7711 | void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) { |
| 7712 | Stmt *Body = nullptr; |
| 7713 | |
| 7714 | if (auto *FD = D->getAsFunction()) { |
| 7715 | // FIXME: We only examine the pattern decl for availability violations now, |
| 7716 | // but we should also examine instantiated templates. |
| 7717 | if (FD->isTemplateInstantiation()) |
| 7718 | return; |
| 7719 | |
| 7720 | Body = FD->getBody(); |
| 7721 | } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7722 | Body = MD->getBody(); |
Alex Lorenz | 28559ce | 2017-04-26 14:20:02 +0000 | [diff] [blame] | 7723 | else if (auto *BD = dyn_cast<BlockDecl>(D)) |
| 7724 | Body = BD->getBody(); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7725 | |
| 7726 | assert(Body && "Need a body here!"); |
| 7727 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7728 | DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7729 | } |
Erik Pilkington | 9f866a7 | 2017-07-18 20:32:07 +0000 | [diff] [blame] | 7730 | |
| 7731 | void Sema::DiagnoseAvailabilityOfDecl(NamedDecl *D, SourceLocation Loc, |
| 7732 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 7733 | bool ObjCPropertyAccess, |
| 7734 | bool AvoidPartialAvailabilityChecks) { |
| 7735 | std::string Message; |
| 7736 | AvailabilityResult Result; |
| 7737 | const NamedDecl* OffendingDecl; |
| 7738 | // See if this declaration is unavailable, deprecated, or partial. |
| 7739 | std::tie(Result, OffendingDecl) = ShouldDiagnoseAvailabilityOfDecl(D, &Message); |
| 7740 | if (Result == AR_Available) |
| 7741 | return; |
| 7742 | |
| 7743 | if (Result == AR_NotYetIntroduced) { |
| 7744 | if (AvoidPartialAvailabilityChecks) |
| 7745 | return; |
| 7746 | |
| 7747 | // We need to know the @available context in the current function to |
| 7748 | // diagnose this use, let DiagnoseUnguardedAvailabilityViolations do that |
| 7749 | // when we're done parsing the current function. |
| 7750 | if (getCurFunctionOrMethodDecl()) { |
| 7751 | getEnclosingFunction()->HasPotentialAvailabilityViolations = true; |
| 7752 | return; |
| 7753 | } else if (getCurBlock() || getCurLambda()) { |
| 7754 | getCurFunction()->HasPotentialAvailabilityViolations = true; |
| 7755 | return; |
| 7756 | } |
| 7757 | } |
| 7758 | |
| 7759 | const ObjCPropertyDecl *ObjCPDecl = nullptr; |
| 7760 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 7761 | if (const ObjCPropertyDecl *PD = MD->findPropertyDecl()) { |
| 7762 | AvailabilityResult PDeclResult = PD->getAvailability(nullptr); |
| 7763 | if (PDeclResult == Result) |
| 7764 | ObjCPDecl = PD; |
| 7765 | } |
| 7766 | } |
| 7767 | |
| 7768 | EmitAvailabilityWarning(*this, Result, D, OffendingDecl, Message, Loc, |
| 7769 | UnknownObjCClass, ObjCPDecl, ObjCPropertyAccess); |
| 7770 | } |