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 | } |
| 241 | const IdentifierInfo *getAttrName(const clang::AttributeList &Attr) { |
| 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( |
| 316 | Sema &S, const Decl *D, const AttrInfo& Attr, |
| 317 | unsigned AttrArgNum, const Expr *IdxExpr, uint64_t &Idx) { |
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. |
| 344 | if (HasImplicitThisParam) { |
| 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; |
| 952 | if (!Cond->isValueDependent() && |
| 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 | |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1040 | auto *FD = cast<FunctionDecl>(D); |
| 1041 | bool ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond); |
| 1042 | D->addAttr(::new (S.Context) DiagnoseIfAttr( |
| 1043 | Attr.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent, FD, |
| 1044 | Attr.getAttributeSpellingListIndex())); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1047 | static void handlePassObjectSizeAttr(Sema &S, Decl *D, |
| 1048 | const AttributeList &Attr) { |
| 1049 | if (D->hasAttr<PassObjectSizeAttr>()) { |
| 1050 | S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter) |
| 1051 | << Attr.getName(); |
| 1052 | return; |
| 1053 | } |
| 1054 | |
| 1055 | Expr *E = Attr.getArgAsExpr(0); |
| 1056 | uint32_t Type; |
| 1057 | if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1)) |
| 1058 | return; |
| 1059 | |
| 1060 | // pass_object_size's argument is passed in as the second argument of |
| 1061 | // __builtin_object_size. So, it has the same constraints as that second |
| 1062 | // argument; namely, it must be in the range [0, 3]. |
| 1063 | if (Type > 3) { |
| 1064 | S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range) |
| 1065 | << Attr.getName() << 0 << 3 << E->getSourceRange(); |
| 1066 | return; |
| 1067 | } |
| 1068 | |
| 1069 | // pass_object_size is only supported on constant pointer parameters; as a |
| 1070 | // kindness to users, we allow the parameter to be non-const for declarations. |
| 1071 | // At this point, we have no clue if `D` belongs to a function declaration or |
| 1072 | // definition, so we defer the constness check until later. |
| 1073 | if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) { |
| 1074 | S.Diag(D->getLocStart(), diag::err_attribute_pointers_only) |
| 1075 | << Attr.getName() << 1; |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | D->addAttr(::new (S.Context) |
| 1080 | PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type, |
| 1081 | Attr.getAttributeSpellingListIndex())); |
| 1082 | } |
| 1083 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1084 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1085 | ConsumableAttr::ConsumedState DefaultState; |
| 1086 | |
| 1087 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1088 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1089 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1090 | DefaultState)) { |
| 1091 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1092 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1093 | return; |
| 1094 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1095 | } else { |
| 1096 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1097 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 1098 | return; |
| 1099 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1100 | |
| 1101 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1102 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1103 | Attr.getAttributeSpellingListIndex())); |
| 1104 | } |
| 1105 | |
| 1106 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 1107 | const AttributeList &Attr) { |
| 1108 | ASTContext &CurrContext = S.getASTContext(); |
| 1109 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 1110 | |
| 1111 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1112 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 1113 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 1114 | RD->getNameAsString(); |
| 1115 | |
| 1116 | return false; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | return true; |
| 1121 | } |
| 1122 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1123 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 1124 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1125 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1126 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1127 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1128 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1129 | return; |
| 1130 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1131 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 1132 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 1133 | CallableWhenAttr::ConsumedState CallableState; |
| 1134 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1135 | StringRef StateString; |
| 1136 | SourceLocation Loc; |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 1137 | if (Attr.isArgIdent(ArgIndex)) { |
| 1138 | IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex); |
| 1139 | StateString = Ident->Ident->getName(); |
| 1140 | Loc = Ident->Loc; |
| 1141 | } else { |
| 1142 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 1143 | return; |
| 1144 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1145 | |
| 1146 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1147 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1148 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 1149 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1150 | return; |
| 1151 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1152 | |
| 1153 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1156 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1157 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 1158 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1161 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 1162 | const AttributeList &Attr) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1163 | ParamTypestateAttr::ConsumedState ParamState; |
| 1164 | |
| 1165 | if (Attr.isArgIdent(0)) { |
| 1166 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1167 | StringRef StateString = Ident->Ident->getName(); |
| 1168 | |
| 1169 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 1170 | ParamState)) { |
| 1171 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1172 | << Attr.getName() << StateString; |
| 1173 | return; |
| 1174 | } |
| 1175 | } else { |
| 1176 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1177 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1178 | return; |
| 1179 | } |
| 1180 | |
| 1181 | // FIXME: This check is currently being done in the analysis. It can be |
| 1182 | // enabled here only after the parser propagates attributes at |
| 1183 | // template specialization definition, not declaration. |
| 1184 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 1185 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1186 | // |
| 1187 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1188 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1189 | // ReturnType.getAsString(); |
| 1190 | // return; |
| 1191 | //} |
| 1192 | |
| 1193 | D->addAttr(::new (S.Context) |
| 1194 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 1195 | Attr.getAttributeSpellingListIndex())); |
| 1196 | } |
| 1197 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1198 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 1199 | const AttributeList &Attr) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1200 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 1201 | |
| 1202 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1203 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1204 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1205 | ReturnState)) { |
| 1206 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1207 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1208 | return; |
| 1209 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1210 | } else { |
| 1211 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1212 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1213 | return; |
| 1214 | } |
| 1215 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1216 | // FIXME: This check is currently being done in the analysis. It can be |
| 1217 | // enabled here only after the parser propagates attributes at |
| 1218 | // template specialization definition, not declaration. |
| 1219 | //QualType ReturnType; |
| 1220 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1221 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1222 | // ReturnType = Param->getType(); |
| 1223 | // |
| 1224 | //} else if (const CXXConstructorDecl *Constructor = |
| 1225 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1226 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1227 | // |
| 1228 | //} else { |
| 1229 | // |
| 1230 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1231 | //} |
| 1232 | // |
| 1233 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1234 | // |
| 1235 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1236 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1237 | // ReturnType.getAsString(); |
| 1238 | // return; |
| 1239 | //} |
| 1240 | |
| 1241 | D->addAttr(::new (S.Context) |
| 1242 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1243 | Attr.getAttributeSpellingListIndex())); |
| 1244 | } |
| 1245 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1246 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1247 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1248 | return; |
| 1249 | |
| 1250 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1251 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1252 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1253 | StringRef Param = Ident->Ident->getName(); |
| 1254 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1255 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1256 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1257 | return; |
| 1258 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1259 | } else { |
| 1260 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1261 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1262 | return; |
| 1263 | } |
| 1264 | |
| 1265 | D->addAttr(::new (S.Context) |
| 1266 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1267 | Attr.getAttributeSpellingListIndex())); |
| 1268 | } |
| 1269 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1270 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1271 | const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1272 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1273 | return; |
| 1274 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1275 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1276 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1277 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1278 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1279 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1280 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1281 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1282 | return; |
| 1283 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1284 | } else { |
| 1285 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1286 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1287 | return; |
| 1288 | } |
| 1289 | |
| 1290 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1291 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1292 | Attr.getAttributeSpellingListIndex())); |
| 1293 | } |
| 1294 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1295 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1296 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1297 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1298 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1301 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1302 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1303 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1304 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1305 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1306 | // Report warning about changed offset in the newer compiler versions. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1307 | if (!FD->getType()->isDependentType() && |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1308 | !FD->getType()->isIncompleteType() && FD->isBitField() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1309 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1310 | S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield); |
| 1311 | |
| 1312 | FD->addAttr(::new (S.Context) PackedAttr( |
| 1313 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1314 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1315 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1318 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1319 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1320 | // variables or properties of Objective-C classes. The outlet must also |
| 1321 | // have an object reference type. |
| 1322 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1323 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1324 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1325 | << Attr.getName() << VD->getType() << 0; |
| 1326 | return false; |
| 1327 | } |
| 1328 | } |
| 1329 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1330 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1331 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1332 | << Attr.getName() << PD->getType() << 1; |
| 1333 | return false; |
| 1334 | } |
| 1335 | } |
| 1336 | else { |
| 1337 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1338 | return false; |
| 1339 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1340 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1341 | return true; |
| 1342 | } |
| 1343 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1344 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1345 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1346 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1347 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1348 | D->addAttr(::new (S.Context) |
| 1349 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1350 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1353 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1354 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1355 | |
| 1356 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1357 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1358 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1359 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1360 | return; |
| 1361 | } |
| 1362 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1363 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1364 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1365 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1366 | ParsedType PT; |
| 1367 | |
| 1368 | if (Attr.hasParsedType()) |
| 1369 | PT = Attr.getTypeArg(); |
| 1370 | else { |
| 1371 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1372 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1373 | if (!PT) { |
| 1374 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1375 | return; |
| 1376 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1377 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1378 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1379 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1380 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1381 | if (!QTLoc) |
| 1382 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1383 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1384 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1385 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1386 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1387 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1388 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1389 | S.Diag(Attr.getLoc(), |
| 1390 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1391 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1392 | return; |
| 1393 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1394 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1395 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1396 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1397 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1400 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1401 | if (RefOkay) { |
| 1402 | if (T->isReferenceType()) |
| 1403 | return true; |
| 1404 | } else { |
| 1405 | T = T.getNonReferenceType(); |
| 1406 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1407 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1408 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1409 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1410 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1411 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1412 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1413 | for (const auto *I : UD->fields()) { |
| 1414 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1415 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1416 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1417 | } |
| 1418 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1424 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1425 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1426 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1427 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1428 | if (!S.isValidPointerAttrType(T)) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1429 | if (isReturnValue) |
| 1430 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1431 | << Attr.getName() << AttrParmRange << TypeRange; |
| 1432 | else |
| 1433 | S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only) |
| 1434 | << Attr.getName() << AttrParmRange << TypeRange << 0; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1435 | return false; |
| 1436 | } |
| 1437 | return true; |
| 1438 | } |
| 1439 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1440 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1441 | SmallVector<unsigned, 8> NonNullArgs; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1442 | for (unsigned I = 0; I < Attr.getNumArgs(); ++I) { |
| 1443 | Expr *Ex = Attr.getArgAsExpr(I); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1444 | uint64_t Idx; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1445 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1446 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1447 | |
| 1448 | // Is the function argument a pointer type? |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1449 | if (Idx < getFunctionOrMethodNumParams(D) && |
| 1450 | !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1451 | Ex->getSourceRange(), |
| 1452 | getFunctionOrMethodParamRange(D, Idx))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1453 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1454 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1455 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1456 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1457 | |
| 1458 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1459 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1460 | // check if the attribute came from a macro expansion or a template |
| 1461 | // instantiation. |
| 1462 | if (NonNullArgs.empty() && Attr.getLoc().isFileID() && |
Richard Smith | 51ec0cf | 2017-02-21 01:17:38 +0000 | [diff] [blame] | 1463 | !S.inTemplateInstantiation()) { |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1464 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1465 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1466 | I != E && !AnyPointers; ++I) { |
| 1467 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1468 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1469 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1470 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1471 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1472 | if (!AnyPointers) |
| 1473 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1474 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1475 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1476 | unsigned *Start = NonNullArgs.data(); |
| 1477 | unsigned Size = NonNullArgs.size(); |
| 1478 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1479 | D->addAttr(::new (S.Context) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1480 | NonNullAttr(Attr.getRange(), S.Context, Start, Size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1481 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1484 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1485 | const AttributeList &Attr) { |
| 1486 | if (Attr.getNumArgs() > 0) { |
| 1487 | if (D->getFunctionType()) { |
| 1488 | handleNonNullAttr(S, D, Attr); |
| 1489 | } else { |
| 1490 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1491 | << D->getSourceRange(); |
| 1492 | } |
| 1493 | return; |
| 1494 | } |
| 1495 | |
| 1496 | // Is the argument a pointer type? |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1497 | if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(), |
| 1498 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1499 | return; |
| 1500 | |
| 1501 | D->addAttr(::new (S.Context) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1502 | NonNullAttr(Attr.getRange(), S.Context, nullptr, 0, |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1503 | Attr.getAttributeSpellingListIndex())); |
| 1504 | } |
| 1505 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1506 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1507 | const AttributeList &Attr) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1508 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1509 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1510 | if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1511 | /* isReturnValue */ true)) |
| 1512 | return; |
| 1513 | |
| 1514 | D->addAttr(::new (S.Context) |
| 1515 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1516 | Attr.getAttributeSpellingListIndex())); |
| 1517 | } |
| 1518 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1519 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, |
| 1520 | const AttributeList &Attr) { |
| 1521 | Expr *E = Attr.getArgAsExpr(0), |
| 1522 | *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr; |
| 1523 | S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE, |
| 1524 | Attr.getAttributeSpellingListIndex()); |
| 1525 | } |
| 1526 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1527 | static void handleAllocAlignAttr(Sema &S, Decl *D, |
| 1528 | const AttributeList &Attr) { |
| 1529 | S.AddAllocAlignAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 1530 | Attr.getAttributeSpellingListIndex()); |
| 1531 | } |
| 1532 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1533 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1534 | Expr *OE, unsigned SpellingListIndex) { |
| 1535 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1536 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1537 | |
| 1538 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1539 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1540 | |
| 1541 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1542 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1543 | << &TmpAttr << AttrRange << SR; |
| 1544 | return; |
| 1545 | } |
| 1546 | |
| 1547 | if (!E->isValueDependent()) { |
| 1548 | llvm::APSInt I(64); |
| 1549 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1550 | if (OE) |
| 1551 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1552 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1553 | << E->getSourceRange(); |
| 1554 | else |
| 1555 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1556 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1557 | << E->getSourceRange(); |
| 1558 | return; |
| 1559 | } |
| 1560 | |
| 1561 | if (!I.isPowerOf2()) { |
| 1562 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1563 | << E->getSourceRange(); |
| 1564 | return; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | if (OE) { |
| 1569 | if (!OE->isValueDependent()) { |
| 1570 | llvm::APSInt I(64); |
| 1571 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1572 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1573 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1574 | << OE->getSourceRange(); |
| 1575 | return; |
| 1576 | } |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | D->addAttr(::new (Context) |
| 1581 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1582 | } |
| 1583 | |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 1584 | void Sema::AddAllocAlignAttr(SourceRange AttrRange, Decl *D, Expr *ParamExpr, |
| 1585 | unsigned SpellingListIndex) { |
| 1586 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1587 | |
| 1588 | AllocAlignAttr TmpAttr(AttrRange, Context, 0, SpellingListIndex); |
| 1589 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1590 | |
| 1591 | if (!ResultType->isDependentType() && |
| 1592 | !isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1593 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1594 | << &TmpAttr << AttrRange << getFunctionOrMethodResultSourceRange(D); |
| 1595 | return; |
| 1596 | } |
| 1597 | |
| 1598 | uint64_t IndexVal; |
| 1599 | const auto *FuncDecl = cast<FunctionDecl>(D); |
| 1600 | if (!checkFunctionOrMethodParameterIndex(*this, FuncDecl, TmpAttr, |
| 1601 | /*AttrArgNo=*/1, ParamExpr, |
| 1602 | IndexVal)) |
| 1603 | return; |
| 1604 | |
| 1605 | QualType Ty = getFunctionOrMethodParamType(D, IndexVal); |
| 1606 | if (!Ty->isDependentType() && !Ty->isIntegralType(Context)) { |
| 1607 | Diag(ParamExpr->getLocStart(), diag::err_attribute_integers_only) |
| 1608 | << &TmpAttr << FuncDecl->getParamDecl(IndexVal)->getSourceRange(); |
| 1609 | return; |
| 1610 | } |
| 1611 | |
| 1612 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
| 1613 | // because that has corrected for the implicit this parameter, and is zero- |
| 1614 | // based. The attribute expects what the user wrote explicitly. |
| 1615 | llvm::APSInt Val; |
| 1616 | ParamExpr->EvaluateAsInt(Val, Context); |
| 1617 | |
| 1618 | D->addAttr(::new (Context) AllocAlignAttr( |
| 1619 | AttrRange, Context, Val.getZExtValue(), SpellingListIndex)); |
| 1620 | } |
| 1621 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1622 | /// Normalize the attribute, __foo__ becomes foo. |
| 1623 | /// Returns true if normalization was applied. |
| 1624 | static bool normalizeName(StringRef &AttrName) { |
Aaron Ballman | 6269236 | 2015-10-09 13:53:24 +0000 | [diff] [blame] | 1625 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1626 | AttrName.endswith("__")) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1627 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1628 | return true; |
| 1629 | } |
| 1630 | return false; |
| 1631 | } |
| 1632 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1633 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1634 | // This attribute must be applied to a function declaration. The first |
| 1635 | // argument to the attribute must be an identifier, the name of the resource, |
| 1636 | // for example: malloc. The following arguments must be argument indexes, the |
| 1637 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1638 | // 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] | 1639 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1640 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1641 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1642 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1643 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1644 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1645 | return; |
| 1646 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1647 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1648 | // Figure out our Kind. |
| 1649 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1650 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1651 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1652 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1653 | // Check arguments. |
| 1654 | switch (K) { |
| 1655 | case OwnershipAttr::Takes: |
| 1656 | case OwnershipAttr::Holds: |
| 1657 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1658 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1659 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1660 | return; |
| 1661 | } |
| 1662 | break; |
| 1663 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1664 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1665 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1666 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1667 | return; |
| 1668 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1669 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1672 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1673 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1674 | StringRef ModuleName = Module->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1675 | if (normalizeName(ModuleName)) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1676 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1677 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1678 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1679 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1680 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1681 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1682 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1683 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1684 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1685 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1686 | // Is the function argument a pointer type? |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1687 | QualType T = getFunctionOrMethodParamType(D, Idx); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1688 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1689 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1690 | case OwnershipAttr::Takes: |
| 1691 | case OwnershipAttr::Holds: |
| 1692 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1693 | Err = 0; |
| 1694 | break; |
| 1695 | case OwnershipAttr::Returns: |
| 1696 | if (!T->isIntegerType()) |
| 1697 | Err = 1; |
| 1698 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1699 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1700 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1701 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1702 | << Ex->getSourceRange(); |
| 1703 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1704 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1705 | |
| 1706 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1707 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1708 | // Cannot have two ownership attributes of different kinds for the same |
| 1709 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1710 | if (I->getOwnKind() != K && I->args_end() != |
| 1711 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1712 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1713 | << AL.getName() << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1714 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1715 | } else if (K == OwnershipAttr::Returns && |
| 1716 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1717 | // A returns attribute conflicts with any other returns attribute using |
| 1718 | // a different index. Note, diagnostic reporting is 1-based, but stored |
| 1719 | // argument indexes are 0-based. |
| 1720 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1721 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
| 1722 | << *(I->args_begin()) + 1; |
| 1723 | if (I->args_size()) |
| 1724 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
| 1725 | << (unsigned)Idx + 1 << Ex->getSourceRange(); |
| 1726 | return; |
| 1727 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1728 | } |
| 1729 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1730 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | unsigned* start = OwnershipArgs.data(); |
| 1734 | unsigned size = OwnershipArgs.size(); |
| 1735 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1736 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1737 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1738 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1739 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1742 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1743 | // Check the attribute arguments. |
| 1744 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1745 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1746 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1747 | return; |
| 1748 | } |
| 1749 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1750 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1751 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1752 | // gcc rejects |
| 1753 | // class c { |
| 1754 | // static int a __attribute__((weakref ("v2"))); |
| 1755 | // static int b() __attribute__((weakref ("f3"))); |
| 1756 | // }; |
| 1757 | // and ignores the attributes of |
| 1758 | // void f(void) { |
| 1759 | // static int a __attribute__((weakref ("v2"))); |
| 1760 | // } |
| 1761 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1762 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1763 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1764 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1765 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1766 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | // The GCC manual says |
| 1770 | // |
| 1771 | // At present, a declaration to which `weakref' is attached can only |
| 1772 | // be `static'. |
| 1773 | // |
| 1774 | // It also says |
| 1775 | // |
| 1776 | // Without a TARGET, |
| 1777 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1778 | // equivalent to `weak'. |
| 1779 | // |
| 1780 | // gcc 4.4.1 will accept |
| 1781 | // int a7 __attribute__((weakref)); |
| 1782 | // as |
| 1783 | // int a7 __attribute__((weak)); |
| 1784 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1785 | // it if this behaviour is actually used. |
| 1786 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1787 | // GCC rejects |
| 1788 | // static ((alias ("y"), weakref)). |
| 1789 | // Should we? How to check that weakref is before or after alias? |
| 1790 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1791 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1792 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1793 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1794 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1795 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1796 | // GCC will accept anything as the argument of weakref. Should we |
| 1797 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1798 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1799 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1800 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1801 | D->addAttr(::new (S.Context) |
| 1802 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1803 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1806 | static void handleIFuncAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1807 | StringRef Str; |
| 1808 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
| 1809 | return; |
| 1810 | |
| 1811 | // Aliases should be on declarations, not definitions. |
| 1812 | const auto *FD = cast<FunctionDecl>(D); |
| 1813 | if (FD->isThisDeclarationADefinition()) { |
| 1814 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD << 1; |
| 1815 | return; |
| 1816 | } |
| 1817 | // FIXME: it should be handled as a target specific attribute. |
| 1818 | if (S.Context.getTargetInfo().getTriple().getObjectFormat() != |
| 1819 | llvm::Triple::ELF) { |
| 1820 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1821 | return; |
| 1822 | } |
| 1823 | |
| 1824 | D->addAttr(::new (S.Context) IFuncAttr(Attr.getRange(), S.Context, Str, |
| 1825 | Attr.getAttributeSpellingListIndex())); |
| 1826 | } |
| 1827 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1828 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1829 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1830 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1831 | return; |
| 1832 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1833 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1834 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1835 | return; |
| 1836 | } |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1837 | if (S.Context.getTargetInfo().getTriple().isNVPTX()) { |
| 1838 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_nvptx); |
| 1839 | } |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1840 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1841 | // Aliases should be on declarations, not definitions. |
| 1842 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1843 | if (FD->isThisDeclarationADefinition()) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1844 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1845 | return; |
| 1846 | } |
| 1847 | } else { |
| 1848 | const auto *VD = cast<VarDecl>(D); |
| 1849 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1850 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1851 | return; |
| 1852 | } |
| 1853 | } |
| 1854 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1855 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1856 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1857 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1858 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1859 | } |
| 1860 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1861 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1862 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1863 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1864 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1865 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1866 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1870 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1871 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1872 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1873 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1874 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1877 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1878 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1879 | StringRef Model; |
| 1880 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1881 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1882 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1883 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1884 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1885 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1886 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1887 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1888 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1889 | return; |
| 1890 | } |
| 1891 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1892 | D->addAttr(::new (S.Context) |
| 1893 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1894 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1897 | static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1898 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1899 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1900 | D->addAttr(::new (S.Context) RestrictAttr( |
| 1901 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1902 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1905 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1906 | << Attr.getName() << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1907 | } |
| 1908 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1909 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1910 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1911 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1912 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1913 | return; |
| 1914 | } |
| 1915 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1916 | if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(), |
| 1917 | Attr.getAttributeSpellingListIndex())) |
| 1918 | D->addAttr(CA); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1919 | } |
| 1920 | |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 1921 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1922 | if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(), |
| 1923 | Attr.getName())) |
| 1924 | return; |
| 1925 | |
Saleem Abdulrasool | b51bcaf | 2017-04-07 15:13:47 +0000 | [diff] [blame] | 1926 | if (Attr.isDeclspecAttribute()) { |
| 1927 | const auto &Triple = S.getASTContext().getTargetInfo().getTriple(); |
| 1928 | const auto &Arch = Triple.getArch(); |
| 1929 | if (Arch != llvm::Triple::x86 && |
| 1930 | (Arch != llvm::Triple::arm && Arch != llvm::Triple::thumb)) { |
| 1931 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_on_arch) |
| 1932 | << Attr.getName() << Triple.getArchName(); |
| 1933 | return; |
| 1934 | } |
| 1935 | } |
| 1936 | |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 1937 | D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context, |
| 1938 | Attr.getAttributeSpellingListIndex())); |
| 1939 | } |
| 1940 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1941 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1942 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1943 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1944 | if (S.CheckNoReturnAttr(attr)) |
| 1945 | return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1946 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1947 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1948 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1949 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1950 | return; |
| 1951 | } |
| 1952 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1953 | D->addAttr(::new (S.Context) NoReturnAttr( |
| 1954 | attr.getRange(), S.Context, attr.getAttributeSpellingListIndex())); |
| 1955 | } |
| 1956 | |
| 1957 | static void handleNoCallerSavedRegsAttr(Sema &S, Decl *D, |
| 1958 | const AttributeList &Attr) { |
| 1959 | if (S.CheckNoCallerSavedRegsAttr(Attr)) |
| 1960 | return; |
| 1961 | |
| 1962 | D->addAttr(::new (S.Context) AnyX86NoCallerSavedRegistersAttr( |
| 1963 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1967 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1968 | attr.setInvalid(); |
| 1969 | return true; |
| 1970 | } |
| 1971 | |
| 1972 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 1975 | bool Sema::CheckNoCallerSavedRegsAttr(const AttributeList &Attr) { |
| 1976 | // Check whether the attribute is valid on the current target. |
| 1977 | if (!Attr.existsInTarget(Context.getTargetInfo())) { |
| 1978 | Diag(Attr.getLoc(), diag::warn_unknown_attribute_ignored) << Attr.getName(); |
| 1979 | Attr.setInvalid(); |
| 1980 | return true; |
| 1981 | } |
| 1982 | |
| 1983 | if (!checkAttributeNumArgs(*this, Attr, 0)) { |
| 1984 | Attr.setInvalid(); |
| 1985 | return true; |
| 1986 | } |
| 1987 | |
| 1988 | return false; |
| 1989 | } |
| 1990 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1991 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1992 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1993 | |
| 1994 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1995 | // because 'analyzer_noreturn' does not impact the type. |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 1996 | if (!isFunctionOrMethodOrBlock(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1997 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1998 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 1999 | !VD->getType()->isFunctionPointerType())) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 2000 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 2001 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 2002 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2003 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 2004 | return; |
| 2005 | } |
| 2006 | } |
| 2007 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2008 | D->addAttr(::new (S.Context) |
| 2009 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 2010 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2013 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2014 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2015 | /* |
| 2016 | Returning a Vector Class in Registers |
| 2017 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2018 | According to the PPU ABI specifications, a class with a single member of |
| 2019 | vector type is returned in memory when used as the return value of a function. |
| 2020 | This results in inefficient code when implementing vector classes. To return |
| 2021 | the value in a single vector register, add the vecreturn attribute to the |
| 2022 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2023 | |
| 2024 | Example: |
| 2025 | |
| 2026 | struct Vector |
| 2027 | { |
| 2028 | __vector float xyzw; |
| 2029 | } __attribute__((vecreturn)); |
| 2030 | |
| 2031 | Vector Add(Vector lhs, Vector rhs) |
| 2032 | { |
| 2033 | Vector result; |
| 2034 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 2035 | return result; // This will be returned in a register |
| 2036 | } |
| 2037 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 2038 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 2039 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2040 | return; |
| 2041 | } |
| 2042 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2043 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2044 | int count = 0; |
| 2045 | |
| 2046 | if (!isa<CXXRecordDecl>(record)) { |
| 2047 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 2048 | return; |
| 2049 | } |
| 2050 | |
| 2051 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 2052 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 2053 | return; |
| 2054 | } |
| 2055 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 2056 | for (const auto *I : record->fields()) { |
| 2057 | if ((count == 1) || !I->getType()->isVectorType()) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 2058 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 2059 | return; |
| 2060 | } |
| 2061 | count++; |
| 2062 | } |
| 2063 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2064 | D->addAttr(::new (S.Context) |
| 2065 | VecReturnAttr(Attr.getRange(), S.Context, |
| 2066 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 2067 | } |
| 2068 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2069 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 2070 | const AttributeList &Attr) { |
| 2071 | if (isa<ParmVarDecl>(D)) { |
| 2072 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 2073 | // parameter of a function declaration or lambda. |
| 2074 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 2075 | S.Diag(Attr.getLoc(), |
| 2076 | diag::err_carries_dependency_param_not_function_decl); |
| 2077 | return; |
| 2078 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2079 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 2080 | |
| 2081 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 2082 | Attr.getRange(), S.Context, |
| 2083 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 2086 | static void handleNotTailCalledAttr(Sema &S, Decl *D, |
| 2087 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 2088 | if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(), |
| 2089 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 2090 | return; |
| 2091 | |
| 2092 | D->addAttr(::new (S.Context) NotTailCalledAttr( |
| 2093 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 2094 | } |
| 2095 | |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 2096 | static void handleDisableTailCallsAttr(Sema &S, Decl *D, |
| 2097 | const AttributeList &Attr) { |
| 2098 | if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(), |
| 2099 | Attr.getName())) |
| 2100 | return; |
| 2101 | |
| 2102 | D->addAttr(::new (S.Context) DisableTailCallsAttr( |
| 2103 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 2104 | } |
| 2105 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2106 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2107 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 2108 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 2109 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2110 | return; |
| 2111 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2112 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2113 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2114 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2115 | return; |
| 2116 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2117 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2118 | D->addAttr(::new (S.Context) |
| 2119 | UsedAttr(Attr.getRange(), S.Context, |
| 2120 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2123 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2124 | bool IsCXX1zAttr = Attr.isCXX11Attribute() && !Attr.getScopeName(); |
| 2125 | |
| 2126 | if (IsCXX1zAttr && isa<VarDecl>(D)) { |
| 2127 | // The C++1z spelling of this attribute cannot be applied to a static data |
| 2128 | // member per [dcl.attr.unused]p2. |
| 2129 | if (cast<VarDecl>(D)->isStaticDataMember()) { |
| 2130 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2131 | << Attr.getName() << ExpectedForMaybeUnused; |
| 2132 | return; |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | // If this is spelled as the standard C++1z attribute, but not in C++1z, warn |
| 2137 | // about using it as an extension. |
| 2138 | if (!S.getLangOpts().CPlusPlus1z && IsCXX1zAttr) |
| 2139 | S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName(); |
| 2140 | |
| 2141 | D->addAttr(::new (S.Context) UnusedAttr( |
| 2142 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 2143 | } |
| 2144 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2145 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2146 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 2147 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2148 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 2149 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2150 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2151 | D->addAttr(::new (S.Context) |
| 2152 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 2153 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2156 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 2157 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 2158 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2159 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 2160 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2161 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2162 | D->addAttr(::new (S.Context) |
| 2163 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2164 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2167 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2168 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2169 | const AttributeList &Attr) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2170 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2171 | StringRef Str; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 2172 | if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2173 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2174 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2175 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2176 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2179 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2180 | const AttributeList &Attr) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2181 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 2182 | S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
| 2183 | << Attr.getName() << Attr.getRange(); |
| 2184 | return; |
| 2185 | } |
| 2186 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2187 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 2188 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 2189 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2190 | } |
| 2191 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2192 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2193 | IdentifierInfo *Platform, |
| 2194 | VersionTuple Introduced, |
| 2195 | VersionTuple Deprecated, |
| 2196 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2197 | StringRef PlatformName |
| 2198 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2199 | if (PlatformName.empty()) |
| 2200 | PlatformName = Platform->getName(); |
| 2201 | |
| 2202 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2203 | // of these steps are needed). |
| 2204 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2205 | !(Introduced <= Deprecated)) { |
| 2206 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2207 | << 1 << PlatformName << Deprecated.getAsString() |
| 2208 | << 0 << Introduced.getAsString(); |
| 2209 | return true; |
| 2210 | } |
| 2211 | |
| 2212 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2213 | !(Introduced <= Obsoleted)) { |
| 2214 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2215 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2216 | << 0 << Introduced.getAsString(); |
| 2217 | return true; |
| 2218 | } |
| 2219 | |
| 2220 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2221 | !(Deprecated <= Obsoleted)) { |
| 2222 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2223 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2224 | << 1 << Deprecated.getAsString(); |
| 2225 | return true; |
| 2226 | } |
| 2227 | |
| 2228 | return false; |
| 2229 | } |
| 2230 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2231 | /// \brief Check whether the two versions match. |
| 2232 | /// |
| 2233 | /// If either version tuple is empty, then they are assumed to match. If |
| 2234 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2235 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2236 | bool BeforeIsOkay) { |
| 2237 | if (X.empty() || Y.empty()) |
| 2238 | return true; |
| 2239 | |
| 2240 | if (X == Y) |
| 2241 | return true; |
| 2242 | |
| 2243 | if (BeforeIsOkay && X < Y) |
| 2244 | return true; |
| 2245 | |
| 2246 | return false; |
| 2247 | } |
| 2248 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2249 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2250 | IdentifierInfo *Platform, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2251 | bool Implicit, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2252 | VersionTuple Introduced, |
| 2253 | VersionTuple Deprecated, |
| 2254 | VersionTuple Obsoleted, |
| 2255 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2256 | StringRef Message, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2257 | bool IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2258 | StringRef Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2259 | AvailabilityMergeKind AMK, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2260 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2261 | VersionTuple MergedIntroduced = Introduced; |
| 2262 | VersionTuple MergedDeprecated = Deprecated; |
| 2263 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2264 | bool FoundAny = false; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2265 | bool OverrideOrImpl = false; |
| 2266 | switch (AMK) { |
| 2267 | case AMK_None: |
| 2268 | case AMK_Redeclaration: |
| 2269 | OverrideOrImpl = false; |
| 2270 | break; |
| 2271 | |
| 2272 | case AMK_Override: |
| 2273 | case AMK_ProtocolImplementation: |
| 2274 | OverrideOrImpl = true; |
| 2275 | break; |
| 2276 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2277 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2278 | if (D->hasAttrs()) { |
| 2279 | AttrVec &Attrs = D->getAttrs(); |
| 2280 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2281 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2282 | if (!OldAA) { |
| 2283 | ++i; |
| 2284 | continue; |
| 2285 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2286 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2287 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2288 | if (OldPlatform != Platform) { |
| 2289 | ++i; |
| 2290 | continue; |
| 2291 | } |
| 2292 | |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2293 | // If there is an existing availability attribute for this platform that |
| 2294 | // is explicit and the new one is implicit use the explicit one and |
| 2295 | // discard the new implicit attribute. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2296 | if (!OldAA->isImplicit() && Implicit) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2297 | return nullptr; |
| 2298 | } |
| 2299 | |
| 2300 | // If there is an existing attribute for this platform that is implicit |
| 2301 | // and the new attribute is explicit then erase the old one and |
| 2302 | // continue processing the attributes. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2303 | if (!Implicit && OldAA->isImplicit()) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2304 | Attrs.erase(Attrs.begin() + i); |
| 2305 | --e; |
| 2306 | continue; |
| 2307 | } |
| 2308 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2309 | FoundAny = true; |
| 2310 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2311 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2312 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2313 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2314 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2315 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 2316 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 2317 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2318 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2319 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 2320 | if (OverrideOrImpl) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2321 | int Which = -1; |
| 2322 | VersionTuple FirstVersion; |
| 2323 | VersionTuple SecondVersion; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2324 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2325 | Which = 0; |
| 2326 | FirstVersion = OldIntroduced; |
| 2327 | SecondVersion = Introduced; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2328 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2329 | Which = 1; |
| 2330 | FirstVersion = Deprecated; |
| 2331 | SecondVersion = OldDeprecated; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2332 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2333 | Which = 2; |
| 2334 | FirstVersion = Obsoleted; |
| 2335 | SecondVersion = OldObsoleted; |
| 2336 | } |
| 2337 | |
| 2338 | if (Which == -1) { |
| 2339 | Diag(OldAA->getLocation(), |
| 2340 | diag::warn_mismatched_availability_override_unavail) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2341 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2342 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2343 | } else { |
| 2344 | Diag(OldAA->getLocation(), |
| 2345 | diag::warn_mismatched_availability_override) |
| 2346 | << Which |
| 2347 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2348 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 2349 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2350 | } |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2351 | if (AMK == AMK_Override) |
| 2352 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2353 | else |
| 2354 | Diag(Range.getBegin(), diag::note_protocol_method); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2355 | } else { |
| 2356 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2357 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2358 | } |
| 2359 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2360 | Attrs.erase(Attrs.begin() + i); |
| 2361 | --e; |
| 2362 | continue; |
| 2363 | } |
| 2364 | |
| 2365 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2366 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2367 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2368 | |
| 2369 | if (MergedIntroduced2.empty()) |
| 2370 | MergedIntroduced2 = OldIntroduced; |
| 2371 | if (MergedDeprecated2.empty()) |
| 2372 | MergedDeprecated2 = OldDeprecated; |
| 2373 | if (MergedObsoleted2.empty()) |
| 2374 | MergedObsoleted2 = OldObsoleted; |
| 2375 | |
| 2376 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2377 | MergedIntroduced2, MergedDeprecated2, |
| 2378 | MergedObsoleted2)) { |
| 2379 | Attrs.erase(Attrs.begin() + i); |
| 2380 | --e; |
| 2381 | continue; |
| 2382 | } |
| 2383 | |
| 2384 | MergedIntroduced = MergedIntroduced2; |
| 2385 | MergedDeprecated = MergedDeprecated2; |
| 2386 | MergedObsoleted = MergedObsoleted2; |
| 2387 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2388 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | if (FoundAny && |
| 2392 | MergedIntroduced == Introduced && |
| 2393 | MergedDeprecated == Deprecated && |
| 2394 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2395 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2396 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2397 | // Only create a new attribute if !OverrideOrImpl, but we want to do |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2398 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2399 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2400 | MergedDeprecated, MergedObsoleted) && |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2401 | !OverrideOrImpl) { |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2402 | auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2403 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2404 | Obsoleted, IsUnavailable, Message, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2405 | IsStrict, Replacement, |
| 2406 | AttrSpellingListIndex); |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2407 | Avail->setImplicit(Implicit); |
| 2408 | return Avail; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2409 | } |
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 | } |
| 2412 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2413 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2414 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2415 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2416 | return; |
| 2417 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2418 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2419 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2420 | IdentifierInfo *II = Platform->Ident; |
| 2421 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2422 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2423 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2424 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2425 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
Alex Lorenz | 472cc79 | 2017-04-20 09:35:02 +0000 | [diff] [blame] | 2426 | if (!ND) // We warned about this already, so just return. |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2427 | return; |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2428 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2429 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2430 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2431 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2432 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2433 | bool IsStrict = Attr.getStrictLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2434 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 2435 | if (const StringLiteral *SE = |
| 2436 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2437 | Str = SE->getString(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2438 | StringRef Replacement; |
| 2439 | if (const StringLiteral *SE = |
| 2440 | dyn_cast_or_null<StringLiteral>(Attr.getReplacementExpr())) |
| 2441 | Replacement = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2442 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2443 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2444 | false/*Implicit*/, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2445 | Introduced.Version, |
| 2446 | Deprecated.Version, |
| 2447 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2448 | IsUnavailable, Str, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2449 | IsStrict, Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2450 | Sema::AMK_None, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2451 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2452 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2453 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2454 | |
| 2455 | // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning |
| 2456 | // matches before the start of the watchOS platform. |
| 2457 | if (S.Context.getTargetInfo().getTriple().isWatchOS()) { |
| 2458 | IdentifierInfo *NewII = nullptr; |
| 2459 | if (II->getName() == "ios") |
| 2460 | NewII = &S.Context.Idents.get("watchos"); |
| 2461 | else if (II->getName() == "ios_app_extension") |
| 2462 | NewII = &S.Context.Idents.get("watchos_app_extension"); |
| 2463 | |
| 2464 | if (NewII) { |
| 2465 | auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple { |
| 2466 | if (Version.empty()) |
| 2467 | return Version; |
| 2468 | auto Major = Version.getMajor(); |
| 2469 | auto NewMajor = Major >= 9 ? Major - 7 : 0; |
| 2470 | if (NewMajor >= 2) { |
| 2471 | if (Version.getMinor().hasValue()) { |
| 2472 | if (Version.getSubminor().hasValue()) |
| 2473 | return VersionTuple(NewMajor, Version.getMinor().getValue(), |
| 2474 | Version.getSubminor().getValue()); |
| 2475 | else |
| 2476 | return VersionTuple(NewMajor, Version.getMinor().getValue()); |
| 2477 | } |
| 2478 | } |
| 2479 | |
| 2480 | return VersionTuple(2, 0); |
| 2481 | }; |
| 2482 | |
| 2483 | auto NewIntroduced = adjustWatchOSVersion(Introduced.Version); |
| 2484 | auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version); |
| 2485 | auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version); |
| 2486 | |
| 2487 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2488 | Attr.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2489 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2490 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2491 | NewIntroduced, |
| 2492 | NewDeprecated, |
| 2493 | NewObsoleted, |
| 2494 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2495 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2496 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2497 | Sema::AMK_None, |
| 2498 | Index); |
| 2499 | if (NewAttr) |
| 2500 | D->addAttr(NewAttr); |
| 2501 | } |
| 2502 | } else if (S.Context.getTargetInfo().getTriple().isTvOS()) { |
| 2503 | // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning |
| 2504 | // matches before the start of the tvOS platform. |
| 2505 | IdentifierInfo *NewII = nullptr; |
| 2506 | if (II->getName() == "ios") |
| 2507 | NewII = &S.Context.Idents.get("tvos"); |
| 2508 | else if (II->getName() == "ios_app_extension") |
| 2509 | NewII = &S.Context.Idents.get("tvos_app_extension"); |
| 2510 | |
| 2511 | if (NewII) { |
| 2512 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2513 | Attr.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2514 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2515 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2516 | Introduced.Version, |
| 2517 | Deprecated.Version, |
| 2518 | Obsoleted.Version, |
| 2519 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2520 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2521 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2522 | Sema::AMK_None, |
| 2523 | Index); |
| 2524 | if (NewAttr) |
| 2525 | D->addAttr(NewAttr); |
| 2526 | } |
| 2527 | } |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2530 | static void handleExternalSourceSymbolAttr(Sema &S, Decl *D, |
| 2531 | const AttributeList &Attr) { |
| 2532 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 2533 | return; |
| 2534 | assert(checkAttributeAtMostNumArgs(S, Attr, 3) && |
| 2535 | "Invalid number of arguments in an external_source_symbol attribute"); |
| 2536 | |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 2537 | StringRef Language; |
| 2538 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(Attr.getArgAsExpr(0))) |
| 2539 | Language = SE->getString(); |
| 2540 | StringRef DefinedIn; |
| 2541 | if (const auto *SE = dyn_cast_or_null<StringLiteral>(Attr.getArgAsExpr(1))) |
| 2542 | DefinedIn = SE->getString(); |
| 2543 | bool IsGeneratedDeclaration = Attr.getArgAsIdent(2) != nullptr; |
| 2544 | |
| 2545 | D->addAttr(::new (S.Context) ExternalSourceSymbolAttr( |
| 2546 | Attr.getRange(), S.Context, Language, DefinedIn, IsGeneratedDeclaration, |
| 2547 | Attr.getAttributeSpellingListIndex())); |
| 2548 | } |
| 2549 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2550 | template <class T> |
| 2551 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2552 | typename T::VisibilityType value, |
| 2553 | unsigned attrSpellingListIndex) { |
| 2554 | T *existingAttr = D->getAttr<T>(); |
| 2555 | if (existingAttr) { |
| 2556 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2557 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2558 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2559 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2560 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2561 | D->dropAttr<T>(); |
| 2562 | } |
| 2563 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2564 | } |
| 2565 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2566 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2567 | VisibilityAttr::VisibilityType Vis, |
| 2568 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2569 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2570 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2571 | } |
| 2572 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2573 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2574 | TypeVisibilityAttr::VisibilityType Vis, |
| 2575 | unsigned AttrSpellingListIndex) { |
| 2576 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2577 | AttrSpellingListIndex); |
| 2578 | } |
| 2579 | |
| 2580 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2581 | bool isTypeVisibility) { |
| 2582 | // Visibility attributes don't mean anything on a typedef. |
| 2583 | if (isa<TypedefNameDecl>(D)) { |
| 2584 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2585 | << Attr.getName(); |
| 2586 | return; |
| 2587 | } |
| 2588 | |
| 2589 | // 'type_visibility' can only go on a type or namespace. |
| 2590 | if (isTypeVisibility && |
| 2591 | !(isa<TagDecl>(D) || |
| 2592 | isa<ObjCInterfaceDecl>(D) || |
| 2593 | isa<NamespaceDecl>(D))) { |
| 2594 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2595 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2596 | return; |
| 2597 | } |
| 2598 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2599 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2600 | StringRef TypeStr; |
| 2601 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2602 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2603 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2604 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2605 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2606 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2607 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2608 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2609 | return; |
| 2610 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2611 | |
| 2612 | // Complain about attempts to use protected visibility on targets |
| 2613 | // (like Darwin) that don't support it. |
| 2614 | if (type == VisibilityAttr::Protected && |
| 2615 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2616 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2617 | type = VisibilityAttr::Default; |
| 2618 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2619 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2620 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2621 | clang::Attr *newAttr; |
| 2622 | if (isTypeVisibility) { |
| 2623 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2624 | (TypeVisibilityAttr::VisibilityType) type, |
| 2625 | Index); |
| 2626 | } else { |
| 2627 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2628 | } |
| 2629 | if (newAttr) |
| 2630 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2633 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2634 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2635 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2636 | if (!Attr.isArgIdent(0)) { |
| 2637 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2638 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2639 | return; |
| 2640 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2641 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2642 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2643 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2644 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2645 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2646 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2647 | return; |
| 2648 | } |
| 2649 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2650 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 2651 | !method->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2652 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2653 | << method->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2654 | // Ignore the attribute. |
| 2655 | return; |
| 2656 | } |
| 2657 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2658 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2659 | S.Context, F, |
| 2660 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2663 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2664 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2665 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2666 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2667 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2668 | return; |
| 2669 | } |
| 2670 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2671 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2672 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2673 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2674 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2675 | return; |
| 2676 | } |
| 2677 | } |
| 2678 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2679 | // It is okay to include this attribute on properties, e.g.: |
| 2680 | // |
| 2681 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2682 | // |
| 2683 | // In this case it follows tradition and suppresses an error in the above |
| 2684 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2685 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2686 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2687 | D->addAttr(::new (S.Context) |
| 2688 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2689 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2692 | static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2693 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 2694 | QualType T = TD->getUnderlyingType(); |
| 2695 | if (!T->isObjCObjectPointerType()) { |
| 2696 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2697 | return; |
| 2698 | } |
| 2699 | } else { |
| 2700 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2701 | return; |
| 2702 | } |
| 2703 | D->addAttr(::new (S.Context) |
| 2704 | ObjCIndependentClassAttr(Attr.getRange(), S.Context, |
| 2705 | Attr.getAttributeSpellingListIndex())); |
| 2706 | } |
| 2707 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2708 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2709 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2710 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2711 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2712 | return; |
| 2713 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2714 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2715 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2716 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2717 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2718 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2719 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2720 | return; |
| 2721 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2722 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2723 | D->addAttr(::new (S.Context) |
| 2724 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2725 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2728 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2729 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2730 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2731 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2732 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2733 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2734 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2735 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2736 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2737 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2738 | return; |
| 2739 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2740 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2741 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2742 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2743 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2744 | return; |
| 2745 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2746 | |
| 2747 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2748 | } |
| 2749 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2750 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2751 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2752 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2753 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2754 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2755 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2756 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2757 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2758 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2759 | return; |
| 2760 | } |
| 2761 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2762 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2763 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2764 | // FIXME: This error message could be improved, it would be nice |
| 2765 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2766 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2767 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2768 | return; |
| 2769 | } |
| 2770 | } |
| 2771 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2772 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2773 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2774 | if (isa<FunctionNoProtoType>(FT)) { |
| 2775 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2776 | return; |
| 2777 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2778 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2779 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2780 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2781 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2782 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2783 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2784 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2785 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2786 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2787 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2788 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2789 | if (!BD->isVariadic()) { |
| 2790 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2791 | return; |
| 2792 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2793 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2794 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2795 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2796 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2797 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2798 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2799 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2800 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2801 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2802 | return; |
| 2803 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2804 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2805 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2806 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2807 | return; |
| 2808 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2809 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2810 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2811 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2812 | return; |
| 2813 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2814 | D->addAttr(::new (S.Context) |
| 2815 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2816 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2819 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2820 | if (D->getFunctionType() && |
| 2821 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2822 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2823 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2824 | return; |
| 2825 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2826 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2827 | if (MD->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2828 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2829 | << Attr.getName() << 1; |
| 2830 | return; |
| 2831 | } |
| 2832 | |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2833 | // If this is spelled as the standard C++1z attribute, but not in C++1z, warn |
| 2834 | // about using it as an extension. |
| 2835 | if (!S.getLangOpts().CPlusPlus1z && Attr.isCXX11Attribute() && |
| 2836 | !Attr.getScopeName()) |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 2837 | S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName(); |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2838 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2839 | D->addAttr(::new (S.Context) |
| 2840 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2841 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2844 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2845 | // weak_import only applies to variable & function declarations. |
| 2846 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2847 | if (!D->canBeWeakImported(isDef)) { |
| 2848 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2849 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2850 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2851 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2852 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2853 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2854 | // Nothing to warn about here. |
| 2855 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2856 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2857 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2858 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2859 | return; |
| 2860 | } |
| 2861 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2862 | D->addAttr(::new (S.Context) |
| 2863 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2864 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2867 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2868 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2869 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2870 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2871 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2872 | for (unsigned i = 0; i < 3; ++i) { |
| 2873 | const Expr *E = Attr.getArgAsExpr(i); |
| 2874 | if (!checkUInt32Argument(S, Attr, E, WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2875 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2876 | if (WGSize[i] == 0) { |
| 2877 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero) |
| 2878 | << Attr.getName() << E->getSourceRange(); |
| 2879 | return; |
| 2880 | } |
| 2881 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2882 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2883 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2884 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2885 | Existing->getYDim() == WGSize[1] && |
| 2886 | Existing->getZDim() == WGSize[2])) |
| 2887 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2888 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2889 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2890 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2891 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame^] | 2894 | // Handles intel_reqd_sub_group_size. |
| 2895 | static void handleSubGroupSize(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2896 | uint32_t SGSize; |
| 2897 | const Expr *E = Attr.getArgAsExpr(0); |
| 2898 | if (!checkUInt32Argument(S, Attr, E, SGSize)) |
| 2899 | return; |
| 2900 | if (SGSize == 0) { |
| 2901 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero) |
| 2902 | << Attr.getName() << E->getSourceRange(); |
| 2903 | return; |
| 2904 | } |
| 2905 | |
| 2906 | OpenCLIntelReqdSubGroupSizeAttr *Existing = |
| 2907 | D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>(); |
| 2908 | if (Existing && Existing->getSubGroupSize() != SGSize) |
| 2909 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2910 | |
| 2911 | D->addAttr(::new (S.Context) OpenCLIntelReqdSubGroupSizeAttr( |
| 2912 | Attr.getRange(), S.Context, SGSize, |
| 2913 | Attr.getAttributeSpellingListIndex())); |
| 2914 | } |
| 2915 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2916 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2917 | if (!Attr.hasParsedType()) { |
| 2918 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2919 | << Attr.getName() << 1; |
| 2920 | return; |
| 2921 | } |
| 2922 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2923 | TypeSourceInfo *ParmTSI = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2924 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2925 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2926 | |
| 2927 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2928 | (ParmType->isBooleanType() || |
| 2929 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2930 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2931 | << ParmType; |
| 2932 | return; |
| 2933 | } |
| 2934 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2935 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2936 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2937 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2938 | return; |
| 2939 | } |
| 2940 | } |
| 2941 | |
| 2942 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2943 | ParmTSI, |
| 2944 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2947 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2948 | StringRef Name, |
| 2949 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2950 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2951 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2952 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2953 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2954 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2955 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2956 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2957 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2958 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2959 | } |
| 2960 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2961 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2962 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2963 | if (!Error.empty()) { |
| 2964 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error; |
| 2965 | return false; |
| 2966 | } |
| 2967 | return true; |
| 2968 | } |
| 2969 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2970 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2971 | // Make sure that there is a string literal as the sections's single |
| 2972 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2973 | StringRef Str; |
| 2974 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2975 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2976 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2977 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2978 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2979 | return; |
| 2980 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2981 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2982 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2983 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2984 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2985 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2986 | return; |
| 2987 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2988 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2989 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2990 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2991 | if (NewAttr) |
| 2992 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2993 | } |
| 2994 | |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2995 | // Check for things we'd like to warn about, no errors or validation for now. |
| 2996 | // TODO: Validation should use a backend target library that specifies |
| 2997 | // the allowable subtarget features and cpus. We could use something like a |
| 2998 | // TargetCodeGenInfo hook here to do validation. |
| 2999 | void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 3000 | for (auto Str : {"tune=", "fpmath="}) |
| 3001 | if (AttrStr.find(Str) != StringRef::npos) |
| 3002 | Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str; |
| 3003 | } |
| 3004 | |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3005 | static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3006 | StringRef Str; |
| 3007 | SourceLocation LiteralLoc; |
| 3008 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
| 3009 | return; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 3010 | S.checkTargetAttr(LiteralLoc, Str); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 3011 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 3012 | TargetAttr *NewAttr = |
| 3013 | ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index); |
| 3014 | D->addAttr(NewAttr); |
| 3015 | } |
| 3016 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3017 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3018 | VarDecl *VD = cast<VarDecl>(D); |
| 3019 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3020 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3021 | return; |
| 3022 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3023 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3024 | Expr *E = Attr.getArgAsExpr(0); |
| 3025 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3026 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3027 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3028 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3029 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 3030 | // will warn the user. |
| 3031 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 3032 | if (DRE->hasQualifier()) |
| 3033 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 3034 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 3035 | NI = DRE->getNameInfo(); |
| 3036 | if (!FD) { |
| 3037 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 3038 | << NI.getName(); |
| 3039 | return; |
| 3040 | } |
| 3041 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 3042 | if (ULE->hasExplicitTemplateArgs()) |
| 3043 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3044 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 3045 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 3046 | if (!FD) { |
| 3047 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 3048 | << NI.getName(); |
| 3049 | if (ULE->getType() == S.Context.OverloadTy) |
| 3050 | S.NoteAllOverloadCandidates(ULE); |
| 3051 | return; |
| 3052 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3053 | } else { |
| 3054 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3055 | return; |
| 3056 | } |
| 3057 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3058 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3059 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 3060 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3061 | return; |
| 3062 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3063 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3064 | // We're currently more strict than GCC about what function types we accept. |
| 3065 | // If this ever proves to be a problem it should be easy to fix. |
| 3066 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 3067 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 3068 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 3069 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 3070 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 3071 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 3072 | return; |
| 3073 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3074 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3075 | D->addAttr(::new (S.Context) |
| 3076 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 3077 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 3080 | static void handleEnumExtensibilityAttr(Sema &S, Decl *D, |
| 3081 | const AttributeList &Attr) { |
| 3082 | if (!Attr.isArgIdent(0)) { |
| 3083 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 3084 | << Attr.getName() << 0 << AANT_ArgumentIdentifier; |
| 3085 | return; |
| 3086 | } |
| 3087 | |
| 3088 | EnumExtensibilityAttr::Kind ExtensibilityKind; |
| 3089 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 3090 | if (!EnumExtensibilityAttr::ConvertStrToKind(II->getName(), |
| 3091 | ExtensibilityKind)) { |
| 3092 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 3093 | << Attr.getName() << II; |
| 3094 | return; |
| 3095 | } |
| 3096 | |
| 3097 | D->addAttr(::new (S.Context) EnumExtensibilityAttr( |
| 3098 | Attr.getRange(), S.Context, ExtensibilityKind, |
| 3099 | Attr.getAttributeSpellingListIndex())); |
| 3100 | } |
| 3101 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3102 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3103 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3104 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3105 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3106 | uint64_t Idx; |
| 3107 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3108 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3109 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3110 | // Make sure the format string is really a string. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3111 | QualType Ty = getFunctionOrMethodParamType(D, Idx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3112 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3113 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 3114 | if (NotNSStringTy && |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3115 | !isCFStringType(Ty, S.Context) && |
| 3116 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3117 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3118 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3119 | << "a string type" << IdxExpr->getSourceRange() |
| 3120 | << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3121 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3122 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3123 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3124 | if (!isNSStringType(Ty, S.Context) && |
| 3125 | !isCFStringType(Ty, S.Context) && |
| 3126 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3127 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3128 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 3129 | << (NotNSStringTy ? "string type" : "NSString") |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 3130 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3131 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3132 | } |
| 3133 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3134 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 3135 | // because that has corrected for the implicit this parameter, and is zero- |
| 3136 | // based. The attribute expects what the user wrote explicitly. |
| 3137 | llvm::APSInt Val; |
| 3138 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 3139 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3140 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 3141 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3142 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 3143 | } |
| 3144 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3145 | enum FormatAttrKind { |
| 3146 | CFStringFormat, |
| 3147 | NSStringFormat, |
| 3148 | StrftimeFormat, |
| 3149 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3150 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3151 | InvalidFormat |
| 3152 | }; |
| 3153 | |
| 3154 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 3155 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3156 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 3157 | return llvm::StringSwitch<FormatAttrKind>(Format) |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3158 | // Check for formats that get handled specially. |
| 3159 | .Case("NSString", NSStringFormat) |
| 3160 | .Case("CFString", CFStringFormat) |
| 3161 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3162 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3163 | // Otherwise, check for supported formats. |
| 3164 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 3165 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 3166 | .Case("kprintf", SupportedFormat) // OpenBSD. |
| 3167 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
| 3168 | .Case("os_trace", SupportedFormat) |
| 3169 | .Case("os_log", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3170 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 3171 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 3172 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3175 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3176 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3177 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 3178 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3179 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3180 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 3181 | return; |
| 3182 | } |
| 3183 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3184 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3185 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3186 | Attr.setInvalid(); |
| 3187 | return; |
| 3188 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3189 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3190 | if (S.Context.getAsArrayType(T)) |
| 3191 | T = S.Context.getBaseElementType(T); |
| 3192 | if (!T->getAs<RecordType>()) { |
| 3193 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3194 | Attr.setInvalid(); |
| 3195 | return; |
| 3196 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3197 | |
| 3198 | Expr *E = Attr.getArgAsExpr(0); |
| 3199 | uint32_t prioritynum; |
| 3200 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3201 | Attr.setInvalid(); |
| 3202 | return; |
| 3203 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3204 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3205 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3206 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3207 | << E->getSourceRange() << Attr.getName() << 101 << 65535; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3208 | Attr.setInvalid(); |
| 3209 | return; |
| 3210 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3211 | D->addAttr(::new (S.Context) |
| 3212 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3213 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3214 | } |
| 3215 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3216 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3217 | IdentifierInfo *Format, int FormatIdx, |
| 3218 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3219 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3220 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3221 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 3222 | if (F->getType() == Format && |
| 3223 | F->getFormatIdx() == FormatIdx && |
| 3224 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3225 | // If we don't have a valid location for this attribute, adopt the |
| 3226 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3227 | if (F->getLocation().isInvalid()) |
| 3228 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3229 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3230 | } |
| 3231 | } |
| 3232 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3233 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3234 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3237 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3238 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3239 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3240 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3241 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3242 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3243 | return; |
| 3244 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3245 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3246 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3247 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3248 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3249 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3250 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3251 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 3252 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3253 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 3254 | if (normalizeName(Format)) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3255 | // If we've modified the string name, we need a new identifier for it. |
| 3256 | II = &S.Context.Idents.get(Format); |
| 3257 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3258 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3259 | // Check for supported formats. |
| 3260 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3261 | |
| 3262 | if (Kind == IgnoredFormat) |
| 3263 | return; |
| 3264 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3265 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3266 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 3267 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3268 | return; |
| 3269 | } |
| 3270 | |
| 3271 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3272 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3273 | uint32_t Idx; |
| 3274 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3275 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3276 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3277 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3278 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3279 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3280 | return; |
| 3281 | } |
| 3282 | |
| 3283 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3284 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3285 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3286 | if (HasImplicitThisParam) { |
| 3287 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3288 | S.Diag(Attr.getLoc(), |
| 3289 | diag::err_format_attribute_implicit_this_format_string) |
| 3290 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3291 | return; |
| 3292 | } |
| 3293 | ArgIdx--; |
| 3294 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3295 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3296 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3297 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3298 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3299 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3300 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3301 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3302 | << "a CFString" << IdxExpr->getSourceRange() |
| 3303 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3304 | return; |
| 3305 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3306 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3307 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3308 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3309 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3310 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3311 | << "an NSString" << IdxExpr->getSourceRange() |
| 3312 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3313 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3314 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3315 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3316 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3317 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3318 | << "a string type" << IdxExpr->getSourceRange() |
| 3319 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3320 | return; |
| 3321 | } |
| 3322 | |
| 3323 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3324 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3325 | uint32_t FirstArg; |
| 3326 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3327 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3328 | |
| 3329 | // check if the function is variadic if the 3rd argument non-zero |
| 3330 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3331 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3332 | ++NumArgs; // +1 for ... |
| 3333 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3334 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3335 | return; |
| 3336 | } |
| 3337 | } |
| 3338 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3339 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3340 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3341 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3342 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3343 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3344 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3345 | return; |
| 3346 | } |
| 3347 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3348 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3349 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3350 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3351 | return; |
| 3352 | } |
| 3353 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3354 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3355 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3356 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3357 | if (NewAttr) |
| 3358 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3361 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3362 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3363 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3364 | RecordDecl *RD = nullptr; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3365 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3366 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3367 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3368 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3369 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3370 | |
| 3371 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3372 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3373 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3374 | return; |
| 3375 | } |
| 3376 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3377 | if (!RD->isCompleteDefinition()) { |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 3378 | if (!RD->isBeingDefined()) |
| 3379 | S.Diag(Attr.getLoc(), |
| 3380 | diag::warn_transparent_union_attribute_not_definition); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3381 | return; |
| 3382 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3383 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3384 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3385 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3386 | if (Field == FieldEnd) { |
| 3387 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3388 | return; |
| 3389 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3390 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3391 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3392 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3393 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3394 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3395 | diag::warn_transparent_union_attribute_floating) |
| 3396 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3397 | return; |
| 3398 | } |
| 3399 | |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3400 | if (FirstType->isIncompleteType()) |
| 3401 | return; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3402 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3403 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3404 | for (; Field != FieldEnd; ++Field) { |
| 3405 | QualType FieldType = Field->getType(); |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3406 | if (FieldType->isIncompleteType()) |
| 3407 | return; |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3408 | // FIXME: this isn't fully correct; we also need to test whether the |
| 3409 | // members of the union would all have the same calling convention as the |
| 3410 | // first member of the union. Checking just the size and alignment isn't |
| 3411 | // sufficient (consider structs passed on the stack instead of in registers |
| 3412 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3413 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3414 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3415 | // Warn if we drop the attribute. |
| 3416 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3417 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3418 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3419 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3420 | diag::warn_transparent_union_attribute_field_size_align) |
| 3421 | << isSize << Field->getDeclName() << FieldBits; |
| 3422 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3423 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3424 | diag::note_transparent_union_first_field_size_align) |
| 3425 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3426 | return; |
| 3427 | } |
| 3428 | } |
| 3429 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3430 | RD->addAttr(::new (S.Context) |
| 3431 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3432 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3433 | } |
| 3434 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3435 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3436 | // Make sure that there is a string literal as the annotation's single |
| 3437 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3438 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3439 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3440 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3441 | |
| 3442 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3443 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 3444 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3445 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3446 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3447 | |
| 3448 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3449 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3450 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3453 | static void handleAlignValueAttr(Sema &S, Decl *D, |
| 3454 | const AttributeList &Attr) { |
| 3455 | S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 3456 | Attr.getAttributeSpellingListIndex()); |
| 3457 | } |
| 3458 | |
| 3459 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3460 | unsigned SpellingListIndex) { |
| 3461 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 3462 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3463 | |
| 3464 | QualType T; |
| 3465 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3466 | T = TD->getUnderlyingType(); |
| 3467 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3468 | T = VD->getType(); |
| 3469 | else |
| 3470 | llvm_unreachable("Unknown decl type for align_value"); |
| 3471 | |
| 3472 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 3473 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 3474 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 3475 | << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange(); |
| 3476 | return; |
| 3477 | } |
| 3478 | |
| 3479 | if (!E->isValueDependent()) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3480 | llvm::APSInt Alignment; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3481 | ExprResult ICE |
| 3482 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3483 | diag::err_align_value_attribute_argument_not_int, |
| 3484 | /*AllowFold*/ false); |
| 3485 | if (ICE.isInvalid()) |
| 3486 | return; |
| 3487 | |
| 3488 | if (!Alignment.isPowerOf2()) { |
| 3489 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3490 | << E->getSourceRange(); |
| 3491 | return; |
| 3492 | } |
| 3493 | |
| 3494 | D->addAttr(::new (Context) |
| 3495 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 3496 | SpellingListIndex)); |
| 3497 | return; |
| 3498 | } |
| 3499 | |
| 3500 | // Save dependent expressions in the AST to be instantiated. |
| 3501 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3504 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3505 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3506 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3507 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3508 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3509 | return; |
| 3510 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3511 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3512 | if (Attr.getNumArgs() == 0) { |
| 3513 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3514 | true, nullptr, Attr.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3515 | return; |
| 3516 | } |
| 3517 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3518 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3519 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3520 | S.Diag(Attr.getEllipsisLoc(), |
| 3521 | diag::err_pack_expansion_without_parameter_packs); |
| 3522 | return; |
| 3523 | } |
| 3524 | |
| 3525 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3526 | return; |
| 3527 | |
David Majnemer | 26a1e0e | 2015-04-07 02:37:09 +0000 | [diff] [blame] | 3528 | if (E->isValueDependent()) { |
| 3529 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 3530 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 3531 | S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name) |
| 3532 | << E->getSourceRange(); |
| 3533 | return; |
| 3534 | } |
| 3535 | } |
| 3536 | } |
| 3537 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3538 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3539 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3543 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3544 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3545 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3546 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3547 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3548 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3549 | // C++11 [dcl.align]p1: |
| 3550 | // An alignment-specifier may be applied to a variable or to a class |
| 3551 | // data member, but it shall not be applied to a bit-field, a function |
| 3552 | // parameter, the formal parameter of a catch clause, or a variable |
| 3553 | // declared with the register storage class specifier. An |
| 3554 | // alignment-specifier may also be applied to the declaration of a class |
| 3555 | // or enumeration type. |
| 3556 | // C11 6.7.5/2: |
| 3557 | // An alignment attribute shall not be specified in a declaration of |
| 3558 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3559 | // object declared with the register storage-class specifier. |
| 3560 | int DiagKind = -1; |
| 3561 | if (isa<ParmVarDecl>(D)) { |
| 3562 | DiagKind = 0; |
| 3563 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3564 | if (VD->getStorageClass() == SC_Register) |
| 3565 | DiagKind = 1; |
| 3566 | if (VD->isExceptionVariable()) |
| 3567 | DiagKind = 2; |
| 3568 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3569 | if (FD->isBitField()) |
| 3570 | DiagKind = 3; |
| 3571 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3572 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3573 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3574 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3575 | return; |
| 3576 | } |
| 3577 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3578 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3579 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3580 | return; |
| 3581 | } |
| 3582 | } |
| 3583 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3584 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3585 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3586 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3587 | AA->setPackExpansion(IsPackExpansion); |
| 3588 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3589 | return; |
| 3590 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3591 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3592 | // FIXME: Cache the number on the Attr object? |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3593 | llvm::APSInt Alignment; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3594 | ExprResult ICE |
| 3595 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3596 | diag::err_aligned_attribute_argument_not_int, |
| 3597 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3598 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3599 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3600 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3601 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3602 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3603 | // C++11 [dcl.align]p2: |
| 3604 | // -- if the constant expression evaluates to zero, the alignment |
| 3605 | // specifier shall have no effect |
| 3606 | // C11 6.7.5p6: |
| 3607 | // An alignment specification of zero has no effect. |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3608 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3609 | if (!llvm::isPowerOf2_64(AlignVal)) { |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3610 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3611 | << E->getSourceRange(); |
| 3612 | return; |
| 3613 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3614 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3615 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3616 | // Alignment calculations can wrap around if it's greater than 2**28. |
David Majnemer | 29c69db | 2015-07-26 01:48:59 +0000 | [diff] [blame] | 3617 | unsigned MaxValidAlignment = |
| 3618 | Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192 |
| 3619 | : 268435456; |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3620 | if (AlignVal > MaxValidAlignment) { |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3621 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 3622 | << E->getSourceRange(); |
| 3623 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3624 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3625 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3626 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3627 | unsigned MaxTLSAlign = |
| 3628 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3629 | .getQuantity(); |
| 3630 | auto *VD = dyn_cast<VarDecl>(D); |
| 3631 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3632 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3633 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3634 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3635 | return; |
| 3636 | } |
| 3637 | } |
| 3638 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3639 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3640 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3641 | AA->setPackExpansion(IsPackExpansion); |
| 3642 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3645 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3646 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3647 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3648 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3649 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3650 | SpellingListIndex); |
| 3651 | AA->setPackExpansion(IsPackExpansion); |
| 3652 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3653 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3654 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3655 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3656 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3657 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3658 | QualType UnderlyingTy, DiagTy; |
| 3659 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 3660 | UnderlyingTy = DiagTy = VD->getType(); |
| 3661 | } else { |
| 3662 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
| 3663 | if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3664 | UnderlyingTy = ED->getIntegerType(); |
| 3665 | } |
| 3666 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3667 | return; |
| 3668 | |
| 3669 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3670 | // The combined effect of all alignment attributes in a declaration shall |
| 3671 | // not specify an alignment that is less strict than the alignment that |
| 3672 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3673 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3674 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3675 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3676 | if (I->isAlignmentDependent()) |
| 3677 | return; |
| 3678 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3679 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3680 | Align = std::max(Align, I->getAlignment(Context)); |
| 3681 | } |
| 3682 | |
| 3683 | if (AlignasAttr && Align) { |
| 3684 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3685 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3686 | if (NaturalAlign > RequestedAlign) |
| 3687 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3688 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3689 | } |
| 3690 | } |
| 3691 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3692 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3693 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3694 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3695 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3696 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3697 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3698 | // have to wait until the record is defined to catch any mismatches. |
| 3699 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3700 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3701 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3702 | // The unspecified model never matches what a definition could need. |
| 3703 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3704 | return false; |
| 3705 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3706 | if (BestCase) { |
| 3707 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3708 | return false; |
| 3709 | } else { |
| 3710 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3711 | return false; |
| 3712 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3713 | |
| 3714 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3715 | << 0 /*definition*/; |
| 3716 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3717 | << RD->getNameAsString(); |
| 3718 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3721 | /// parseModeAttrArg - Parses attribute mode string and returns parsed type |
| 3722 | /// attribute. |
| 3723 | static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, |
| 3724 | bool &IntegerMode, bool &ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3725 | IntegerMode = true; |
| 3726 | ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3727 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3728 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3729 | switch (Str[0]) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3730 | case 'Q': |
| 3731 | DestWidth = 8; |
| 3732 | break; |
| 3733 | case 'H': |
| 3734 | DestWidth = 16; |
| 3735 | break; |
| 3736 | case 'S': |
| 3737 | DestWidth = 32; |
| 3738 | break; |
| 3739 | case 'D': |
| 3740 | DestWidth = 64; |
| 3741 | break; |
| 3742 | case 'X': |
| 3743 | DestWidth = 96; |
| 3744 | break; |
| 3745 | case 'T': |
| 3746 | DestWidth = 128; |
| 3747 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3748 | } |
| 3749 | if (Str[1] == 'F') { |
| 3750 | IntegerMode = false; |
| 3751 | } else if (Str[1] == 'C') { |
| 3752 | IntegerMode = false; |
| 3753 | ComplexMode = true; |
| 3754 | } else if (Str[1] != 'I') { |
| 3755 | DestWidth = 0; |
| 3756 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3757 | break; |
| 3758 | case 4: |
| 3759 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3760 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3761 | if (Str == "word") |
Reid Kleckner | f27e752 | 2016-02-01 18:58:24 +0000 | [diff] [blame] | 3762 | DestWidth = S.Context.getTargetInfo().getRegisterWidth(); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3763 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3764 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3765 | break; |
| 3766 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3767 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3768 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3769 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3770 | case 11: |
| 3771 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3772 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3773 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3774 | } |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
| 3778 | /// type. |
| 3779 | /// |
| 3780 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3781 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3782 | /// HImode, not an intermediate pointer. |
| 3783 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3784 | // This attribute isn't documented, but glibc uses it. It changes |
| 3785 | // the width of an int or unsigned int to the specified size. |
| 3786 | if (!Attr.isArgIdent(0)) { |
| 3787 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3788 | << AANT_ArgumentIdentifier; |
| 3789 | return; |
| 3790 | } |
| 3791 | |
| 3792 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3793 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3794 | S.AddModeAttr(Attr.getRange(), D, Name, Attr.getAttributeSpellingListIndex()); |
| 3795 | } |
| 3796 | |
| 3797 | void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name, |
| 3798 | unsigned SpellingListIndex, bool InInstantiation) { |
| 3799 | StringRef Str = Name->getName(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3800 | normalizeName(Str); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3801 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3802 | |
| 3803 | unsigned DestWidth = 0; |
| 3804 | bool IntegerMode = true; |
| 3805 | bool ComplexMode = false; |
| 3806 | llvm::APInt VectorSize(64, 0); |
| 3807 | if (Str.size() >= 4 && Str[0] == 'V') { |
| 3808 | // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2). |
| 3809 | size_t StrSize = Str.size(); |
| 3810 | size_t VectorStringLength = 0; |
| 3811 | while ((VectorStringLength + 1) < StrSize && |
| 3812 | isdigit(Str[VectorStringLength + 1])) |
| 3813 | ++VectorStringLength; |
| 3814 | if (VectorStringLength && |
| 3815 | !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) && |
| 3816 | VectorSize.isPowerOf2()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3817 | parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth, |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3818 | IntegerMode, ComplexMode); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3819 | // Avoid duplicate warning from template instantiation. |
| 3820 | if (!InInstantiation) |
| 3821 | Diag(AttrLoc, diag::warn_vector_mode_deprecated); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3822 | } else { |
| 3823 | VectorSize = 0; |
| 3824 | } |
| 3825 | } |
| 3826 | |
| 3827 | if (!VectorSize) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3828 | parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode); |
| 3829 | |
| 3830 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3831 | // and friends, at least with glibc. |
| 3832 | // FIXME: Make sure floating-point mappings are accurate |
| 3833 | // FIXME: Support XF and TF types |
| 3834 | if (!DestWidth) { |
| 3835 | Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
| 3836 | return; |
| 3837 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3838 | |
| 3839 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3840 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3841 | OldTy = TD->getUnderlyingType(); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3842 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 3843 | // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'. |
| 3844 | // Try to get type from enum declaration, default to int. |
| 3845 | OldTy = ED->getIntegerType(); |
| 3846 | if (OldTy.isNull()) |
| 3847 | OldTy = Context.IntTy; |
| 3848 | } else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3849 | OldTy = cast<ValueDecl>(D)->getType(); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3850 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3851 | if (OldTy->isDependentType()) { |
| 3852 | D->addAttr(::new (Context) |
| 3853 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
| 3854 | return; |
| 3855 | } |
| 3856 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3857 | // Base type can also be a vector type (see PR17453). |
| 3858 | // Distinguish between base type and base element type. |
| 3859 | QualType OldElemTy = OldTy; |
| 3860 | if (const VectorType *VT = OldTy->getAs<VectorType>()) |
| 3861 | OldElemTy = VT->getElementType(); |
| 3862 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3863 | // GCC allows 'mode' attribute on enumeration types (even incomplete), except |
| 3864 | // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete |
| 3865 | // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected. |
| 3866 | if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) && |
| 3867 | VectorSize.getBoolValue()) { |
| 3868 | Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange; |
| 3869 | return; |
| 3870 | } |
| 3871 | bool IntegralOrAnyEnumType = |
| 3872 | OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>(); |
| 3873 | |
| 3874 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() && |
| 3875 | !IntegralOrAnyEnumType) |
| 3876 | Diag(AttrLoc, diag::err_mode_not_primitive); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3877 | else if (IntegerMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3878 | if (!IntegralOrAnyEnumType) |
| 3879 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3880 | } else if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3881 | if (!OldElemTy->isComplexType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3882 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3883 | } else { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3884 | if (!OldElemTy->isFloatingType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3885 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3886 | } |
| 3887 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3888 | QualType NewElemTy; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3889 | |
| 3890 | if (IntegerMode) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3891 | NewElemTy = Context.getIntTypeForBitwidth(DestWidth, |
| 3892 | OldElemTy->isSignedIntegerType()); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3893 | else |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3894 | NewElemTy = Context.getRealTypeForBitwidth(DestWidth); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3895 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3896 | if (NewElemTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3897 | Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3898 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3901 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3902 | NewElemTy = Context.getComplexType(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
| 3905 | QualType NewTy = NewElemTy; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3906 | if (VectorSize.getBoolValue()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3907 | NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(), |
| 3908 | VectorType::GenericVector); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3909 | } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3910 | // Complex machine mode does not support base vector types. |
| 3911 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3912 | Diag(AttrLoc, diag::err_complex_mode_vector_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3913 | return; |
| 3914 | } |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3915 | unsigned NumElements = Context.getTypeSize(OldElemTy) * |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3916 | OldVT->getNumElements() / |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3917 | Context.getTypeSize(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3918 | NewTy = |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3919 | Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | if (NewTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3923 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3924 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3928 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3929 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3930 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3931 | ED->setIntegerType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3932 | else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3933 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3934 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3935 | D->addAttr(::new (Context) |
| 3936 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3937 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3938 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3939 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3940 | D->addAttr(::new (S.Context) |
| 3941 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3942 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3943 | } |
| 3944 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3945 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3946 | IdentifierInfo *Ident, |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3947 | unsigned AttrSpellingListIndex) { |
| 3948 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3949 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3950 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3951 | return nullptr; |
| 3952 | } |
| 3953 | |
| 3954 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 3955 | return nullptr; |
| 3956 | |
| 3957 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 3958 | AttrSpellingListIndex); |
| 3959 | } |
| 3960 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3961 | CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range, |
| 3962 | IdentifierInfo *Ident, |
| 3963 | unsigned AttrSpellingListIndex) { |
| 3964 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident)) |
| 3965 | return nullptr; |
| 3966 | |
| 3967 | return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex); |
| 3968 | } |
| 3969 | |
| 3970 | InternalLinkageAttr * |
| 3971 | Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range, |
| 3972 | IdentifierInfo *Ident, |
| 3973 | unsigned AttrSpellingListIndex) { |
| 3974 | if (auto VD = dyn_cast<VarDecl>(D)) { |
| 3975 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 3976 | // ImplicitParm or VarTemplateSpecialization). |
| 3977 | if (VD->getKind() != Decl::Var) { |
| 3978 | Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type) |
| 3979 | << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 3980 | : ExpectedVariableOrFunction); |
| 3981 | return nullptr; |
| 3982 | } |
| 3983 | // Attribute does not apply to non-static local variables. |
| 3984 | if (VD->hasLocalStorage()) { |
| 3985 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 3986 | return nullptr; |
| 3987 | } |
| 3988 | } |
| 3989 | |
| 3990 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident)) |
| 3991 | return nullptr; |
| 3992 | |
| 3993 | return ::new (Context) |
| 3994 | InternalLinkageAttr(Range, Context, AttrSpellingListIndex); |
| 3995 | } |
| 3996 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3997 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 3998 | unsigned AttrSpellingListIndex) { |
| 3999 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 4000 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 4001 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 4002 | return nullptr; |
| 4003 | } |
| 4004 | |
| 4005 | if (D->hasAttr<MinSizeAttr>()) |
| 4006 | return nullptr; |
| 4007 | |
| 4008 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 4009 | } |
| 4010 | |
| 4011 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 4012 | unsigned AttrSpellingListIndex) { |
| 4013 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 4014 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 4015 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4016 | D->dropAttr<AlwaysInlineAttr>(); |
| 4017 | } |
| 4018 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 4019 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 4020 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 4021 | D->dropAttr<MinSizeAttr>(); |
| 4022 | } |
| 4023 | |
| 4024 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 4025 | return nullptr; |
| 4026 | |
| 4027 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 4028 | AttrSpellingListIndex); |
| 4029 | } |
| 4030 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4031 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 4032 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4033 | if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(), |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 4034 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 4035 | return; |
| 4036 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4037 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
| 4038 | D, Attr.getRange(), Attr.getName(), |
| 4039 | Attr.getAttributeSpellingListIndex())) |
| 4040 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4041 | } |
| 4042 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4043 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4044 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
| 4045 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 4046 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4049 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, |
| 4050 | const AttributeList &Attr) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 4051 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
| 4052 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 4053 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 4056 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4057 | if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, Attr.getRange(), |
| 4058 | Attr.getName())) |
| 4059 | return; |
| 4060 | auto *VD = cast<VarDecl>(D); |
| 4061 | if (!VD->hasGlobalStorage()) { |
| 4062 | S.Diag(Attr.getLoc(), diag::err_cuda_nonglobal_constant); |
| 4063 | return; |
| 4064 | } |
| 4065 | D->addAttr(::new (S.Context) CUDAConstantAttr( |
| 4066 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4067 | } |
| 4068 | |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4069 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4070 | if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, Attr.getRange(), |
| 4071 | Attr.getName())) |
| 4072 | return; |
| 4073 | auto *VD = cast<VarDecl>(D); |
Justin Lebar | 281ce2a | 2016-10-02 15:24:50 +0000 | [diff] [blame] | 4074 | // extern __shared__ is only allowed on arrays with no length (e.g. |
| 4075 | // "int x[]"). |
| 4076 | if (VD->hasExternalStorage() && !isa<IncompleteArrayType>(VD->getType())) { |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4077 | S.Diag(Attr.getLoc(), diag::err_cuda_extern_shared) << VD; |
| 4078 | return; |
| 4079 | } |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 4080 | if (S.getLangOpts().CUDA && VD->hasLocalStorage() && |
| 4081 | S.CUDADiagIfHostCode(Attr.getLoc(), diag::err_cuda_host_shared) |
| 4082 | << S.CurrentCUDATarget()) |
| 4083 | return; |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 4084 | D->addAttr(::new (S.Context) CUDASharedAttr( |
| 4085 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4086 | } |
| 4087 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4088 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 4089 | if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, Attr.getRange(), |
| 4090 | Attr.getName()) || |
| 4091 | checkAttrMutualExclusion<CUDAHostAttr>(S, D, Attr.getRange(), |
| 4092 | Attr.getName())) { |
| 4093 | return; |
| 4094 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4095 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4096 | if (!FD->getReturnType()->isVoidType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4097 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 4098 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4099 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 4100 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 4101 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4102 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4103 | } |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 4104 | if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) { |
| 4105 | if (Method->isInstance()) { |
| 4106 | S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method) |
| 4107 | << Method; |
| 4108 | return; |
| 4109 | } |
| 4110 | S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method; |
| 4111 | } |
| 4112 | // Only warn for "inline" when compiling for host, to cut down on noise. |
| 4113 | if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice) |
| 4114 | S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4115 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4116 | D->addAttr(::new (S.Context) |
| 4117 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Eli Bendersky | 8386004 | 2014-09-02 22:00:06 +0000 | [diff] [blame] | 4118 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4121 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4122 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 4123 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 4124 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 4125 | return; |
| 4126 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4127 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4128 | D->addAttr(::new (S.Context) |
| 4129 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 4130 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4133 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4134 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4135 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4136 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4137 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 4138 | CallingConv CC; |
Richard Smith | eec7cb1 | 2015-05-28 23:38:53 +0000 | [diff] [blame] | 4139 | if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4140 | return; |
| 4141 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4142 | if (!isa<ObjCMethodDecl>(D)) { |
| 4143 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 4144 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4145 | return; |
| 4146 | } |
| 4147 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4148 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4149 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4150 | D->addAttr(::new (S.Context) |
| 4151 | FastCallAttr(Attr.getRange(), S.Context, |
| 4152 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4153 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4154 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4155 | D->addAttr(::new (S.Context) |
| 4156 | StdCallAttr(Attr.getRange(), S.Context, |
| 4157 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4158 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4159 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4160 | D->addAttr(::new (S.Context) |
| 4161 | ThisCallAttr(Attr.getRange(), S.Context, |
| 4162 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 4163 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4164 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4165 | D->addAttr(::new (S.Context) |
| 4166 | CDeclAttr(Attr.getRange(), S.Context, |
| 4167 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4168 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4169 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4170 | D->addAttr(::new (S.Context) |
| 4171 | PascalAttr(Attr.getRange(), S.Context, |
| 4172 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4173 | return; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4174 | case AttributeList::AT_SwiftCall: |
| 4175 | D->addAttr(::new (S.Context) |
| 4176 | SwiftCallAttr(Attr.getRange(), S.Context, |
| 4177 | Attr.getAttributeSpellingListIndex())); |
| 4178 | return; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4179 | case AttributeList::AT_VectorCall: |
| 4180 | D->addAttr(::new (S.Context) |
| 4181 | VectorCallAttr(Attr.getRange(), S.Context, |
| 4182 | Attr.getAttributeSpellingListIndex())); |
| 4183 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4184 | case AttributeList::AT_MSABI: |
| 4185 | D->addAttr(::new (S.Context) |
| 4186 | MSABIAttr(Attr.getRange(), S.Context, |
| 4187 | Attr.getAttributeSpellingListIndex())); |
| 4188 | return; |
| 4189 | case AttributeList::AT_SysVABI: |
| 4190 | D->addAttr(::new (S.Context) |
| 4191 | SysVABIAttr(Attr.getRange(), S.Context, |
| 4192 | Attr.getAttributeSpellingListIndex())); |
| 4193 | return; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4194 | case AttributeList::AT_RegCall: |
| 4195 | D->addAttr(::new (S.Context) RegCallAttr( |
| 4196 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4197 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4198 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4199 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4200 | switch (CC) { |
| 4201 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4202 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4203 | break; |
| 4204 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4205 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4206 | break; |
| 4207 | default: |
| 4208 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4211 | D->addAttr(::new (S.Context) |
| 4212 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 4213 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4214 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4215 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4216 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4217 | D->addAttr(::new (S.Context) |
| 4218 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 4219 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4220 | return; |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4221 | case AttributeList::AT_PreserveMost: |
| 4222 | D->addAttr(::new (S.Context) PreserveMostAttr( |
| 4223 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4224 | return; |
| 4225 | case AttributeList::AT_PreserveAll: |
| 4226 | D->addAttr(::new (S.Context) PreserveAllAttr( |
| 4227 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4228 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4229 | default: |
| 4230 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4231 | } |
| 4232 | } |
| 4233 | |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 4234 | static void handleSuppressAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4235 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 4236 | return; |
| 4237 | |
| 4238 | std::vector<StringRef> DiagnosticIdentifiers; |
| 4239 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 4240 | StringRef RuleName; |
| 4241 | |
| 4242 | if (!S.checkStringLiteralArgumentAttr(Attr, I, RuleName, nullptr)) |
| 4243 | return; |
| 4244 | |
| 4245 | // FIXME: Warn if the rule name is unknown. This is tricky because only |
| 4246 | // clang-tidy knows about available rules. |
| 4247 | DiagnosticIdentifiers.push_back(RuleName); |
| 4248 | } |
| 4249 | D->addAttr(::new (S.Context) SuppressAttr( |
| 4250 | Attr.getRange(), S.Context, DiagnosticIdentifiers.data(), |
| 4251 | DiagnosticIdentifiers.size(), Attr.getAttributeSpellingListIndex())); |
| 4252 | } |
| 4253 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4254 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 4255 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4256 | if (attr.isInvalid()) |
| 4257 | return true; |
| 4258 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4259 | if (attr.hasProcessingCache()) { |
| 4260 | CC = (CallingConv) attr.getProcessingCache(); |
| 4261 | return false; |
| 4262 | } |
| 4263 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4264 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4265 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4266 | attr.setInvalid(); |
| 4267 | return true; |
| 4268 | } |
| 4269 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4270 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4271 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4272 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 4273 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 4274 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 4275 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 4276 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4277 | case AttributeList::AT_SwiftCall: CC = CC_Swift; break; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4278 | case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4279 | case AttributeList::AT_RegCall: CC = CC_X86RegCall; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4280 | case AttributeList::AT_MSABI: |
| 4281 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 4282 | CC_X86_64Win64; |
| 4283 | break; |
| 4284 | case AttributeList::AT_SysVABI: |
| 4285 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 4286 | CC_C; |
| 4287 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4288 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 4289 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4290 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4291 | attr.setInvalid(); |
| 4292 | return true; |
| 4293 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4294 | if (StrRef == "aapcs") { |
| 4295 | CC = CC_AAPCS; |
| 4296 | break; |
| 4297 | } else if (StrRef == "aapcs-vfp") { |
| 4298 | CC = CC_AAPCS_VFP; |
| 4299 | break; |
| 4300 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4301 | |
| 4302 | attr.setInvalid(); |
| 4303 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 4304 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4305 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4306 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4307 | case AttributeList::AT_PreserveMost: CC = CC_PreserveMost; break; |
| 4308 | case AttributeList::AT_PreserveAll: CC = CC_PreserveAll; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4309 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4310 | } |
| 4311 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4312 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4313 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4314 | if (A != TargetInfo::CCCR_OK) { |
| 4315 | if (A == TargetInfo::CCCR_Warning) |
| 4316 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4317 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4318 | // This convention is not valid for the target. Use the default function or |
| 4319 | // method calling convention. |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 4320 | bool IsCXXMethod = false, IsVariadic = false; |
| 4321 | if (FD) { |
| 4322 | IsCXXMethod = FD->isCXXInstanceMember(); |
| 4323 | IsVariadic = FD->isVariadic(); |
| 4324 | } |
| 4325 | CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4326 | } |
| 4327 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4328 | attr.setProcessingCache((unsigned) CC); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4329 | return false; |
| 4330 | } |
| 4331 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4332 | /// Pointer-like types in the default address space. |
| 4333 | static bool isValidSwiftContextType(QualType type) { |
| 4334 | if (!type->hasPointerRepresentation()) |
| 4335 | return type->isDependentType(); |
| 4336 | return type->getPointeeType().getAddressSpace() == 0; |
| 4337 | } |
| 4338 | |
| 4339 | /// Pointers and references in the default address space. |
| 4340 | static bool isValidSwiftIndirectResultType(QualType type) { |
| 4341 | if (auto ptrType = type->getAs<PointerType>()) { |
| 4342 | type = ptrType->getPointeeType(); |
| 4343 | } else if (auto refType = type->getAs<ReferenceType>()) { |
| 4344 | type = refType->getPointeeType(); |
| 4345 | } else { |
| 4346 | return type->isDependentType(); |
| 4347 | } |
| 4348 | return type.getAddressSpace() == 0; |
| 4349 | } |
| 4350 | |
| 4351 | /// Pointers and references to pointers in the default address space. |
| 4352 | static bool isValidSwiftErrorResultType(QualType type) { |
| 4353 | if (auto ptrType = type->getAs<PointerType>()) { |
| 4354 | type = ptrType->getPointeeType(); |
| 4355 | } else if (auto refType = type->getAs<ReferenceType>()) { |
| 4356 | type = refType->getPointeeType(); |
| 4357 | } else { |
| 4358 | return type->isDependentType(); |
| 4359 | } |
| 4360 | if (!type.getQualifiers().empty()) |
| 4361 | return false; |
| 4362 | return isValidSwiftContextType(type); |
| 4363 | } |
| 4364 | |
| 4365 | static void handleParameterABIAttr(Sema &S, Decl *D, const AttributeList &attr, |
| 4366 | ParameterABI abi) { |
| 4367 | S.AddParameterABIAttr(attr.getRange(), D, abi, |
| 4368 | attr.getAttributeSpellingListIndex()); |
| 4369 | } |
| 4370 | |
| 4371 | void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi, |
| 4372 | unsigned spellingIndex) { |
| 4373 | |
| 4374 | QualType type = cast<ParmVarDecl>(D)->getType(); |
| 4375 | |
| 4376 | if (auto existingAttr = D->getAttr<ParameterABIAttr>()) { |
| 4377 | if (existingAttr->getABI() != abi) { |
| 4378 | Diag(range.getBegin(), diag::err_attributes_are_not_compatible) |
| 4379 | << getParameterABISpelling(abi) << existingAttr; |
| 4380 | Diag(existingAttr->getLocation(), diag::note_conflicting_attribute); |
| 4381 | return; |
| 4382 | } |
| 4383 | } |
| 4384 | |
| 4385 | switch (abi) { |
| 4386 | case ParameterABI::Ordinary: |
| 4387 | llvm_unreachable("explicit attribute for ordinary parameter ABI?"); |
| 4388 | |
| 4389 | case ParameterABI::SwiftContext: |
| 4390 | if (!isValidSwiftContextType(type)) { |
| 4391 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4392 | << getParameterABISpelling(abi) |
| 4393 | << /*pointer to pointer */ 0 << type; |
| 4394 | } |
| 4395 | D->addAttr(::new (Context) |
| 4396 | SwiftContextAttr(range, Context, spellingIndex)); |
| 4397 | return; |
| 4398 | |
| 4399 | case ParameterABI::SwiftErrorResult: |
| 4400 | if (!isValidSwiftErrorResultType(type)) { |
| 4401 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4402 | << getParameterABISpelling(abi) |
| 4403 | << /*pointer to pointer */ 1 << type; |
| 4404 | } |
| 4405 | D->addAttr(::new (Context) |
| 4406 | SwiftErrorResultAttr(range, Context, spellingIndex)); |
| 4407 | return; |
| 4408 | |
| 4409 | case ParameterABI::SwiftIndirectResult: |
| 4410 | if (!isValidSwiftIndirectResultType(type)) { |
| 4411 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4412 | << getParameterABISpelling(abi) |
| 4413 | << /*pointer*/ 0 << type; |
| 4414 | } |
| 4415 | D->addAttr(::new (Context) |
| 4416 | SwiftIndirectResultAttr(range, Context, spellingIndex)); |
| 4417 | return; |
| 4418 | } |
| 4419 | llvm_unreachable("bad parameter ABI attribute"); |
| 4420 | } |
| 4421 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4422 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4423 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4424 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 4425 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4426 | return true; |
| 4427 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 4428 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4429 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4430 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4431 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4432 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4433 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4434 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4435 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4436 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4437 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4438 | } |
| 4439 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4440 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4441 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4442 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4443 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4444 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4445 | } |
| 4446 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4447 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4448 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4449 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4450 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4451 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4452 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4453 | } |
| 4454 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4455 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4458 | // Checks whether an argument of launch_bounds attribute is |
| 4459 | // acceptable, performs implicit conversion to Rvalue, and returns |
| 4460 | // non-nullptr Expr result on success. Otherwise, it returns nullptr |
| 4461 | // and may output an error. |
| 4462 | static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E, |
| 4463 | const CUDALaunchBoundsAttr &Attr, |
| 4464 | const unsigned Idx) { |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4465 | if (S.DiagnoseUnexpandedParameterPack(E)) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4466 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4467 | |
| 4468 | // Accept template arguments for now as they depend on something else. |
| 4469 | // We'll get to check them when they eventually get instantiated. |
| 4470 | if (E->isValueDependent()) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4471 | return E; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4472 | |
| 4473 | llvm::APSInt I(64); |
| 4474 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 4475 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
| 4476 | << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4477 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4478 | } |
| 4479 | // Make sure we can fit it in 32 bits. |
| 4480 | if (!I.isIntN(32)) { |
| 4481 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 4482 | << 32 << /* Unsigned */ 1; |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4483 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4484 | } |
| 4485 | if (I < 0) |
| 4486 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
| 4487 | << &Attr << Idx << E->getSourceRange(); |
| 4488 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4489 | // We may need to perform implicit conversion of the argument. |
| 4490 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 4491 | S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false); |
| 4492 | ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E); |
| 4493 | assert(!ValArg.isInvalid() && |
| 4494 | "Unexpected PerformCopyInitialization() failure."); |
| 4495 | |
| 4496 | return ValArg.getAs<Expr>(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads, |
| 4500 | Expr *MinBlocks, unsigned SpellingListIndex) { |
| 4501 | CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks, |
| 4502 | SpellingListIndex); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4503 | MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0); |
| 4504 | if (MaxThreads == nullptr) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4505 | return; |
| 4506 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4507 | if (MinBlocks) { |
| 4508 | MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1); |
| 4509 | if (MinBlocks == nullptr) |
| 4510 | return; |
| 4511 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4512 | |
| 4513 | D->addAttr(::new (Context) CUDALaunchBoundsAttr( |
| 4514 | AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex)); |
| 4515 | } |
| 4516 | |
| 4517 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 4518 | const AttributeList &Attr) { |
| 4519 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1) || |
| 4520 | !checkAttributeAtMostNumArgs(S, Attr, 2)) |
| 4521 | return; |
| 4522 | |
| 4523 | S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 4524 | Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr, |
| 4525 | Attr.getAttributeSpellingListIndex()); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4526 | } |
| 4527 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4528 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4529 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4530 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4531 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4532 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4533 | return; |
| 4534 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4535 | |
| 4536 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4537 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4538 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4539 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4540 | |
| 4541 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4542 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4543 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4544 | return; |
| 4545 | } |
| 4546 | |
| 4547 | uint64_t ArgumentIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4548 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 4549 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4550 | return; |
| 4551 | |
| 4552 | uint64_t TypeTagIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4553 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 4554 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4555 | return; |
| 4556 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 4557 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4558 | if (IsPointer) { |
| 4559 | // Ensure that buffer has a pointer type. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4560 | QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4561 | if (!BufferTy->isPointerType()) { |
| 4562 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 4563 | << Attr.getName() << 0; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4564 | } |
| 4565 | } |
| 4566 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4567 | D->addAttr(::new (S.Context) |
| 4568 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4569 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4570 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
| 4573 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4574 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4575 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4576 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4577 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4578 | return; |
| 4579 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4580 | |
| 4581 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4582 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4583 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4584 | if (!isa<VarDecl>(D)) { |
| 4585 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4586 | << Attr.getName() << ExpectedVariable; |
| 4587 | return; |
| 4588 | } |
| 4589 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4590 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4591 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4592 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 4593 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4594 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4595 | D->addAttr(::new (S.Context) |
| 4596 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4597 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4598 | Attr.getLayoutCompatible(), |
| 4599 | Attr.getMustBeNull(), |
| 4600 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 4603 | static void handleXRayLogArgsAttr(Sema &S, Decl *D, |
| 4604 | const AttributeList &Attr) { |
| 4605 | uint64_t ArgCount; |
| 4606 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, Attr.getArgAsExpr(0), |
| 4607 | ArgCount)) |
| 4608 | return; |
| 4609 | |
| 4610 | // ArgCount isn't a parameter index [0;n), it's a count [1;n] - hence + 1. |
| 4611 | D->addAttr(::new (S.Context) |
| 4612 | XRayLogArgsAttr(Attr.getRange(), S.Context, ++ArgCount, |
| 4613 | Attr.getAttributeSpellingListIndex())); |
| 4614 | } |
| 4615 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4616 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4617 | // Checker-specific attribute handlers. |
| 4618 | //===----------------------------------------------------------------------===// |
| 4619 | |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4620 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) { |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4621 | return type->isDependentType() || |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4622 | type->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4625 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4626 | return type->isDependentType() || |
| 4627 | type->isObjCObjectPointerType() || |
| 4628 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4629 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4630 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4631 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4632 | return type->isDependentType() || |
| 4633 | type->isPointerType() || |
| 4634 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4637 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4638 | S.AddNSConsumedAttr(Attr.getRange(), D, Attr.getAttributeSpellingListIndex(), |
| 4639 | Attr.getKind() == AttributeList::AT_NSConsumed, |
| 4640 | /*template instantiation*/ false); |
| 4641 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4642 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4643 | void Sema::AddNSConsumedAttr(SourceRange attrRange, Decl *D, |
| 4644 | unsigned spellingIndex, bool isNSConsumed, |
| 4645 | bool isTemplateInstantiation) { |
| 4646 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
| 4647 | bool typeOK; |
| 4648 | |
| 4649 | if (isNSConsumed) { |
| 4650 | typeOK = isValidSubjectOfNSAttribute(*this, param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4651 | } else { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4652 | typeOK = isValidSubjectOfCFAttribute(*this, param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4653 | } |
| 4654 | |
| 4655 | if (!typeOK) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4656 | // These attributes are normally just advisory, but in ARC, ns_consumed |
| 4657 | // is significant. Allow non-dependent code to contain inappropriate |
| 4658 | // attributes even in ARC, but require template instantiations to be |
| 4659 | // set up correctly. |
| 4660 | Diag(D->getLocStart(), |
| 4661 | (isTemplateInstantiation && isNSConsumed && |
| 4662 | getLangOpts().ObjCAutoRefCount |
| 4663 | ? diag::err_ns_attribute_wrong_parameter_type |
| 4664 | : diag::warn_ns_attribute_wrong_parameter_type)) |
| 4665 | << attrRange |
| 4666 | << (isNSConsumed ? "ns_consumed" : "cf_consumed") |
| 4667 | << (isNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4668 | return; |
| 4669 | } |
| 4670 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4671 | if (isNSConsumed) |
| 4672 | param->addAttr(::new (Context) |
| 4673 | NSConsumedAttr(attrRange, Context, spellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4674 | else |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4675 | param->addAttr(::new (Context) |
| 4676 | CFConsumedAttr(attrRange, Context, spellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4677 | } |
| 4678 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4679 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4680 | const AttributeList &Attr) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4681 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4682 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4683 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4684 | returnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4685 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4686 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4687 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4688 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4689 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4690 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4691 | returnType = FD->getReturnType(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4692 | else if (auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 4693 | returnType = Param->getType()->getPointeeType(); |
| 4694 | if (returnType.isNull()) { |
| 4695 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4696 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4697 | << Attr.getRange(); |
| 4698 | return; |
| 4699 | } |
| 4700 | } else { |
| 4701 | AttributeDeclKind ExpectedDeclKind; |
| 4702 | switch (Attr.getKind()) { |
| 4703 | default: llvm_unreachable("invalid ownership attribute"); |
| 4704 | case AttributeList::AT_NSReturnsRetained: |
| 4705 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4706 | case AttributeList::AT_NSReturnsNotRetained: |
| 4707 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 4708 | break; |
| 4709 | |
| 4710 | case AttributeList::AT_CFReturnsRetained: |
| 4711 | case AttributeList::AT_CFReturnsNotRetained: |
| 4712 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 4713 | break; |
| 4714 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4715 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4716 | << Attr.getRange() << Attr.getName() << ExpectedDeclKind; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4717 | return; |
| 4718 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4719 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4720 | bool typeOK; |
| 4721 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4722 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4723 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4724 | case AttributeList::AT_NSReturnsRetained: |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4725 | typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4726 | cf = false; |
| 4727 | break; |
| 4728 | |
| 4729 | case AttributeList::AT_NSReturnsAutoreleased: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4730 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4731 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4732 | cf = false; |
| 4733 | break; |
| 4734 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4735 | case AttributeList::AT_CFReturnsRetained: |
| 4736 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4737 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4738 | cf = true; |
| 4739 | break; |
| 4740 | } |
| 4741 | |
| 4742 | if (!typeOK) { |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4743 | if (isa<ParmVarDecl>(D)) { |
| 4744 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4745 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4746 | << Attr.getRange(); |
| 4747 | } else { |
| 4748 | // Needs to be kept in sync with warn_ns_attribute_wrong_return_type. |
| 4749 | enum : unsigned { |
| 4750 | Function, |
| 4751 | Method, |
| 4752 | Property |
| 4753 | } SubjectKind = Function; |
| 4754 | if (isa<ObjCMethodDecl>(D)) |
| 4755 | SubjectKind = Method; |
| 4756 | else if (isa<ObjCPropertyDecl>(D)) |
| 4757 | SubjectKind = Property; |
| 4758 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4759 | << Attr.getName() << SubjectKind << cf |
| 4760 | << Attr.getRange(); |
| 4761 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4762 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4763 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4764 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4765 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4766 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4767 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4768 | case AttributeList::AT_NSReturnsAutoreleased: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4769 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr( |
| 4770 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4771 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4772 | case AttributeList::AT_CFReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4773 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr( |
| 4774 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4775 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4776 | case AttributeList::AT_NSReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4777 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr( |
| 4778 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4779 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4780 | case AttributeList::AT_CFReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4781 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr( |
| 4782 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4783 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4784 | case AttributeList::AT_NSReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4785 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr( |
| 4786 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4787 | return; |
| 4788 | }; |
| 4789 | } |
| 4790 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4791 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4792 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4793 | const int EP_ObjCMethod = 1; |
| 4794 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4795 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4796 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4797 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4798 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4799 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4800 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4801 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4802 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4803 | if (!resultType->isReferenceType() && |
| 4804 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4805 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4806 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4807 | << attr.getName() |
| 4808 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4809 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4810 | |
| 4811 | // Drop the attribute. |
| 4812 | return; |
| 4813 | } |
| 4814 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4815 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
| 4816 | attr.getRange(), S.Context, attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4819 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4820 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4821 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4822 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4823 | DeclContext *DC = method->getDeclContext(); |
| 4824 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4825 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4826 | << attr.getName() << 0; |
| 4827 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4828 | return; |
| 4829 | } |
| 4830 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4831 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4832 | << attr.getName() << 1; |
| 4833 | return; |
| 4834 | } |
| 4835 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4836 | method->addAttr(::new (S.Context) |
| 4837 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4838 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4839 | } |
| 4840 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4841 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 4842 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4843 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(), |
| 4844 | Attr.getName())) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4845 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4846 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4847 | D->addAttr(::new (S.Context) |
| 4848 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 4849 | Attr.getAttributeSpellingListIndex())); |
| 4850 | } |
| 4851 | |
| 4852 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 4853 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4854 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(), |
| 4855 | Attr.getName())) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4856 | return; |
| 4857 | |
| 4858 | D->addAttr(::new (S.Context) |
| 4859 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 4860 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4861 | } |
| 4862 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4863 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 4864 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4865 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4866 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4867 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4868 | 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] | 4869 | return; |
| 4870 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4871 | |
| 4872 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
| 4873 | if (auto TD = dyn_cast<TypedefNameDecl>(D)) { |
| 4874 | if (!Parm->Ident->isStr("id")) { |
| 4875 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id) |
| 4876 | << Attr.getName(); |
| 4877 | return; |
| 4878 | } |
| 4879 | |
| 4880 | // Only allow 'cv void *'. |
| 4881 | QualType T = TD->getUnderlyingType(); |
| 4882 | if (!T->isVoidPointerType()) { |
| 4883 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
| 4884 | return; |
| 4885 | } |
| 4886 | } |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4887 | |
| 4888 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4889 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4890 | Attr.getAttributeSpellingListIndex())); |
| 4891 | } |
| 4892 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4893 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 4894 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4895 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
| 4896 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4897 | if (!Parm) { |
| 4898 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4899 | return; |
| 4900 | } |
| 4901 | |
| 4902 | D->addAttr(::new (S.Context) |
| 4903 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 4904 | Attr.getAttributeSpellingListIndex())); |
| 4905 | } |
| 4906 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4907 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4908 | const AttributeList &Attr) { |
| 4909 | IdentifierInfo *RelatedClass = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4910 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4911 | if (!RelatedClass) { |
| 4912 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4913 | return; |
| 4914 | } |
| 4915 | IdentifierInfo *ClassMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4916 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4917 | IdentifierInfo *InstanceMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4918 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4919 | D->addAttr(::new (S.Context) |
| 4920 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 4921 | ClassMethod, InstanceMethod, |
| 4922 | Attr.getAttributeSpellingListIndex())); |
| 4923 | } |
| 4924 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4925 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 4926 | const AttributeList &Attr) { |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4927 | ObjCInterfaceDecl *IFace; |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4928 | if (ObjCCategoryDecl *CatDecl = |
| 4929 | dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4930 | IFace = CatDecl->getClassInterface(); |
| 4931 | else |
| 4932 | IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 4933 | |
| 4934 | if (!IFace) |
| 4935 | return; |
| 4936 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 4937 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 4938 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4939 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 4940 | Attr.getAttributeSpellingListIndex())); |
| 4941 | } |
| 4942 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4943 | static void handleObjCRuntimeName(Sema &S, Decl *D, |
| 4944 | const AttributeList &Attr) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 4945 | StringRef MetaDataName; |
| 4946 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName)) |
| 4947 | return; |
| 4948 | D->addAttr(::new (S.Context) |
| 4949 | ObjCRuntimeNameAttr(Attr.getRange(), S.Context, |
| 4950 | MetaDataName, |
| 4951 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4952 | } |
| 4953 | |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 4954 | // When a user wants to use objc_boxable with a union or struct |
| 4955 | // but they don't have access to the declaration (legacy/third-party code) |
| 4956 | // then they can 'enable' this feature with a typedef: |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4957 | // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct; |
| 4958 | static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4959 | bool notify = false; |
| 4960 | |
| 4961 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4962 | if (RD && RD->getDefinition()) { |
| 4963 | RD = RD->getDefinition(); |
| 4964 | notify = true; |
| 4965 | } |
| 4966 | |
| 4967 | if (RD) { |
| 4968 | ObjCBoxableAttr *BoxableAttr = ::new (S.Context) |
| 4969 | ObjCBoxableAttr(Attr.getRange(), S.Context, |
| 4970 | Attr.getAttributeSpellingListIndex()); |
| 4971 | RD->addAttr(BoxableAttr); |
| 4972 | if (notify) { |
| 4973 | // we need to notify ASTReader/ASTWriter about |
| 4974 | // modification of existing declaration |
| 4975 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 4976 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 4977 | } |
| 4978 | } |
| 4979 | } |
| 4980 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4981 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4982 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4983 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4984 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4985 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4986 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4987 | } |
| 4988 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4989 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4990 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4991 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4992 | QualType type = vd->getType(); |
| 4993 | |
| 4994 | if (!type->isDependentType() && |
| 4995 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4996 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4997 | << type; |
| 4998 | return; |
| 4999 | } |
| 5000 | |
| 5001 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 5002 | |
| 5003 | // If we have no lifetime yet, check the lifetime we're presumably |
| 5004 | // going to infer. |
| 5005 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 5006 | lifetime = type->getObjCARCImplicitLifetime(); |
| 5007 | |
| 5008 | switch (lifetime) { |
| 5009 | case Qualifiers::OCL_None: |
| 5010 | assert(type->isDependentType() && |
| 5011 | "didn't infer lifetime for non-dependent type?"); |
| 5012 | break; |
| 5013 | |
| 5014 | case Qualifiers::OCL_Weak: // meaningful |
| 5015 | case Qualifiers::OCL_Strong: // meaningful |
| 5016 | break; |
| 5017 | |
| 5018 | case Qualifiers::OCL_ExplicitNone: |
| 5019 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5020 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5021 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 5022 | break; |
| 5023 | } |
| 5024 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 5025 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 5026 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 5027 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5028 | } |
| 5029 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5030 | //===----------------------------------------------------------------------===// |
| 5031 | // Microsoft specific attribute handlers. |
| 5032 | //===----------------------------------------------------------------------===// |
| 5033 | |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5034 | UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range, |
| 5035 | unsigned AttrSpellingListIndex, StringRef Uuid) { |
| 5036 | if (const auto *UA = D->getAttr<UuidAttr>()) { |
Nico Weber | d58c260 | 2016-09-14 01:16:54 +0000 | [diff] [blame] | 5037 | if (UA->getGuid().equals_lower(Uuid)) |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5038 | return nullptr; |
| 5039 | Diag(UA->getLocation(), diag::err_mismatched_uuid); |
| 5040 | Diag(Range.getBegin(), diag::note_previous_uuid); |
| 5041 | D->dropAttr<UuidAttr>(); |
| 5042 | } |
| 5043 | |
| 5044 | return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex); |
| 5045 | } |
| 5046 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5047 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 5048 | if (!S.LangOpts.CPlusPlus) { |
| 5049 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 5050 | << Attr.getName() << AttributeLangSupport::C; |
| 5051 | return; |
| 5052 | } |
| 5053 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5054 | StringRef StrRef; |
| 5055 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 5056 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5057 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5058 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5059 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 5060 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5061 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 5062 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5063 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5064 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5065 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5066 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5067 | return; |
| 5068 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 5069 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5070 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5071 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5072 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5073 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5074 | return; |
| 5075 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 5076 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 5077 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5078 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 5079 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 5080 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 5081 | |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 5082 | UuidAttr *UA = S.mergeUuidAttr(D, Attr.getRange(), |
| 5083 | Attr.getAttributeSpellingListIndex(), StrRef); |
| 5084 | if (UA) |
| 5085 | D->addAttr(UA); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 5086 | } |
| 5087 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5088 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5089 | if (!S.LangOpts.CPlusPlus) { |
| 5090 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 5091 | << Attr.getName() << AttributeLangSupport::C; |
| 5092 | return; |
| 5093 | } |
| 5094 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5095 | D, Attr.getRange(), /*BestCase=*/true, |
| 5096 | Attr.getAttributeSpellingListIndex(), |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5097 | (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling()); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5098 | if (IA) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5099 | D->addAttr(IA); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 5100 | S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D)); |
| 5101 | } |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5102 | } |
| 5103 | |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 5104 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, |
| 5105 | const AttributeList &Attr) { |
| 5106 | VarDecl *VD = cast<VarDecl>(D); |
| 5107 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
| 5108 | S.Diag(Attr.getLoc(), diag::err_thread_unsupported); |
| 5109 | return; |
| 5110 | } |
| 5111 | if (VD->getTSCSpec() != TSCS_unspecified) { |
| 5112 | S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable); |
| 5113 | return; |
| 5114 | } |
| 5115 | if (VD->hasLocalStorage()) { |
| 5116 | S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
| 5117 | return; |
| 5118 | } |
| 5119 | VD->addAttr(::new (S.Context) ThreadAttr( |
| 5120 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 5121 | } |
| 5122 | |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5123 | static void handleAbiTagAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5124 | SmallVector<StringRef, 4> Tags; |
| 5125 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 5126 | StringRef Tag; |
| 5127 | if (!S.checkStringLiteralArgumentAttr(Attr, I, Tag)) |
| 5128 | return; |
| 5129 | Tags.push_back(Tag); |
| 5130 | } |
| 5131 | |
| 5132 | if (const auto *NS = dyn_cast<NamespaceDecl>(D)) { |
| 5133 | if (!NS->isInline()) { |
| 5134 | S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 0; |
| 5135 | return; |
| 5136 | } |
| 5137 | if (NS->isAnonymousNamespace()) { |
| 5138 | S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 1; |
| 5139 | return; |
| 5140 | } |
| 5141 | if (Attr.getNumArgs() == 0) |
| 5142 | Tags.push_back(NS->getName()); |
| 5143 | } else if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5144 | return; |
| 5145 | |
| 5146 | // Store tags sorted and without duplicates. |
| 5147 | std::sort(Tags.begin(), Tags.end()); |
| 5148 | Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end()); |
| 5149 | |
| 5150 | D->addAttr(::new (S.Context) |
| 5151 | AbiTagAttr(Attr.getRange(), S.Context, Tags.data(), Tags.size(), |
| 5152 | Attr.getAttributeSpellingListIndex())); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 5153 | } |
| 5154 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5155 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 5156 | const AttributeList &Attr) { |
| 5157 | // Check the attribute arguments. |
| 5158 | if (Attr.getNumArgs() > 1) { |
| 5159 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 5160 | << Attr.getName() << 1; |
| 5161 | return; |
| 5162 | } |
| 5163 | |
| 5164 | StringRef Str; |
| 5165 | SourceLocation ArgLoc; |
| 5166 | |
| 5167 | if (Attr.getNumArgs() == 0) |
| 5168 | Str = ""; |
| 5169 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 5170 | return; |
| 5171 | |
| 5172 | ARMInterruptAttr::InterruptType Kind; |
| 5173 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 5174 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 5175 | << Attr.getName() << Str << ArgLoc; |
| 5176 | return; |
| 5177 | } |
| 5178 | |
| 5179 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 5180 | D->addAttr(::new (S.Context) |
| 5181 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 5182 | } |
| 5183 | |
| 5184 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 5185 | const AttributeList &Attr) { |
| 5186 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 5187 | return; |
| 5188 | |
| 5189 | if (!Attr.isArgExpr(0)) { |
| 5190 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 5191 | << AANT_ArgumentIntegerConstant; |
| 5192 | return; |
| 5193 | } |
| 5194 | |
| 5195 | // FIXME: Check for decl - it should be void ()(void). |
| 5196 | |
| 5197 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 5198 | llvm::APSInt NumParams(32); |
| 5199 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 5200 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 5201 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 5202 | << NumParamsExpr->getSourceRange(); |
| 5203 | return; |
| 5204 | } |
| 5205 | |
| 5206 | unsigned Num = NumParams.getLimitedValue(255); |
| 5207 | if ((Num & 1) || Num > 30) { |
| 5208 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 5209 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 5210 | << NumParamsExpr->getSourceRange(); |
| 5211 | return; |
| 5212 | } |
| 5213 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5214 | D->addAttr(::new (S.Context) |
| 5215 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 5216 | Attr.getAttributeSpellingListIndex())); |
| 5217 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5218 | } |
| 5219 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5220 | static void handleMipsInterruptAttr(Sema &S, Decl *D, |
| 5221 | const AttributeList &Attr) { |
| 5222 | // Only one optional argument permitted. |
| 5223 | if (Attr.getNumArgs() > 1) { |
| 5224 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 5225 | << Attr.getName() << 1; |
| 5226 | return; |
| 5227 | } |
| 5228 | |
| 5229 | StringRef Str; |
| 5230 | SourceLocation ArgLoc; |
| 5231 | |
| 5232 | if (Attr.getNumArgs() == 0) |
| 5233 | Str = ""; |
| 5234 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 5235 | return; |
| 5236 | |
| 5237 | // Semantic checks for a function with the 'interrupt' attribute for MIPS: |
| 5238 | // a) Must be a function. |
| 5239 | // b) Must have no parameters. |
| 5240 | // c) Must have the 'void' return type. |
| 5241 | // d) Cannot have the 'mips16' attribute, as that instruction set |
| 5242 | // lacks the 'eret' instruction. |
| 5243 | // e) The attribute itself must either have no argument or one of the |
| 5244 | // valid interrupt types, see [MipsInterruptDocs]. |
| 5245 | |
| 5246 | if (!isFunctionOrMethod(D)) { |
| 5247 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5248 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 5249 | return; |
| 5250 | } |
| 5251 | |
| 5252 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 5253 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 5254 | << 0; |
| 5255 | return; |
| 5256 | } |
| 5257 | |
| 5258 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5259 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 5260 | << 1; |
| 5261 | return; |
| 5262 | } |
| 5263 | |
| 5264 | if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(), |
| 5265 | Attr.getName())) |
| 5266 | return; |
| 5267 | |
| 5268 | MipsInterruptAttr::InterruptType Kind; |
| 5269 | if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 5270 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 5271 | << Attr.getName() << "'" + std::string(Str) + "'"; |
| 5272 | return; |
| 5273 | } |
| 5274 | |
| 5275 | D->addAttr(::new (S.Context) MipsInterruptAttr( |
| 5276 | Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex())); |
| 5277 | } |
| 5278 | |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5279 | static void handleAnyX86InterruptAttr(Sema &S, Decl *D, |
| 5280 | const AttributeList &Attr) { |
| 5281 | // Semantic checks for a function with the 'interrupt' attribute. |
| 5282 | // a) Must be a function. |
| 5283 | // b) Must have the 'void' return type. |
| 5284 | // c) Must take 1 or 2 arguments. |
| 5285 | // d) The 1st argument must be a pointer. |
| 5286 | // e) The 2nd argument (if any) must be an unsigned integer. |
| 5287 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) || |
| 5288 | CXXMethodDecl::isStaticOverloadedOperator( |
| 5289 | cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) { |
| 5290 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 5291 | << Attr.getName() << ExpectedFunctionWithProtoType; |
| 5292 | return; |
| 5293 | } |
| 5294 | // Interrupt handler must have void return type. |
| 5295 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5296 | S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(), |
| 5297 | diag::err_anyx86_interrupt_attribute) |
| 5298 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5299 | ? 0 |
| 5300 | : 1) |
| 5301 | << 0; |
| 5302 | return; |
| 5303 | } |
| 5304 | // Interrupt handler must have 1 or 2 parameters. |
| 5305 | unsigned NumParams = getFunctionOrMethodNumParams(D); |
| 5306 | if (NumParams < 1 || NumParams > 2) { |
| 5307 | S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute) |
| 5308 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5309 | ? 0 |
| 5310 | : 1) |
| 5311 | << 1; |
| 5312 | return; |
| 5313 | } |
| 5314 | // The first argument must be a pointer. |
| 5315 | if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) { |
| 5316 | S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(), |
| 5317 | diag::err_anyx86_interrupt_attribute) |
| 5318 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5319 | ? 0 |
| 5320 | : 1) |
| 5321 | << 2; |
| 5322 | return; |
| 5323 | } |
| 5324 | // The second argument, if present, must be an unsigned integer. |
| 5325 | unsigned TypeSize = |
| 5326 | S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64 |
| 5327 | ? 64 |
| 5328 | : 32; |
| 5329 | if (NumParams == 2 && |
| 5330 | (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() || |
| 5331 | S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) { |
| 5332 | S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(), |
| 5333 | diag::err_anyx86_interrupt_attribute) |
| 5334 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5335 | ? 0 |
| 5336 | : 1) |
| 5337 | << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false); |
| 5338 | return; |
| 5339 | } |
| 5340 | D->addAttr(::new (S.Context) AnyX86InterruptAttr( |
| 5341 | Attr.getLoc(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 5342 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
| 5343 | } |
| 5344 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5345 | static void handleAVRInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5346 | if (!isFunctionOrMethod(D)) { |
| 5347 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5348 | << "'interrupt'" << ExpectedFunction; |
| 5349 | return; |
| 5350 | } |
| 5351 | |
| 5352 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 5353 | return; |
| 5354 | |
| 5355 | handleSimpleAttribute<AVRInterruptAttr>(S, D, Attr); |
| 5356 | } |
| 5357 | |
| 5358 | static void handleAVRSignalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5359 | if (!isFunctionOrMethod(D)) { |
| 5360 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5361 | << "'signal'" << ExpectedFunction; |
| 5362 | return; |
| 5363 | } |
| 5364 | |
| 5365 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 5366 | return; |
| 5367 | |
| 5368 | handleSimpleAttribute<AVRSignalAttr>(S, D, Attr); |
| 5369 | } |
| 5370 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5371 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5372 | // Dispatch the interrupt attribute based on the current target. |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5373 | switch (S.Context.getTargetInfo().getTriple().getArch()) { |
| 5374 | case llvm::Triple::msp430: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5375 | handleMSP430InterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5376 | break; |
| 5377 | case llvm::Triple::mipsel: |
| 5378 | case llvm::Triple::mips: |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5379 | handleMipsInterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5380 | break; |
| 5381 | case llvm::Triple::x86: |
| 5382 | case llvm::Triple::x86_64: |
| 5383 | handleAnyX86InterruptAttr(S, D, Attr); |
| 5384 | break; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5385 | case llvm::Triple::avr: |
| 5386 | handleAVRInterruptAttr(S, D, Attr); |
| 5387 | break; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5388 | default: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5389 | handleARMInterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5390 | break; |
| 5391 | } |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5394 | static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D, |
| 5395 | const AttributeList &Attr) { |
| 5396 | uint32_t Min = 0; |
| 5397 | Expr *MinExpr = Attr.getArgAsExpr(0); |
| 5398 | if (!checkUInt32Argument(S, Attr, MinExpr, Min)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5399 | return; |
| 5400 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5401 | uint32_t Max = 0; |
| 5402 | Expr *MaxExpr = Attr.getArgAsExpr(1); |
| 5403 | if (!checkUInt32Argument(S, Attr, MaxExpr, Max)) |
| 5404 | return; |
| 5405 | |
| 5406 | if (Min == 0 && Max != 0) { |
| 5407 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5408 | << Attr.getName() << 0; |
| 5409 | return; |
| 5410 | } |
| 5411 | if (Min > Max) { |
| 5412 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5413 | << Attr.getName() << 1; |
| 5414 | return; |
| 5415 | } |
| 5416 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5417 | D->addAttr(::new (S.Context) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5418 | AMDGPUFlatWorkGroupSizeAttr(Attr.getLoc(), S.Context, Min, Max, |
| 5419 | Attr.getAttributeSpellingListIndex())); |
| 5420 | } |
| 5421 | |
| 5422 | static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, |
| 5423 | const AttributeList &Attr) { |
| 5424 | uint32_t Min = 0; |
| 5425 | Expr *MinExpr = Attr.getArgAsExpr(0); |
| 5426 | if (!checkUInt32Argument(S, Attr, MinExpr, Min)) |
| 5427 | return; |
| 5428 | |
| 5429 | uint32_t Max = 0; |
| 5430 | if (Attr.getNumArgs() == 2) { |
| 5431 | Expr *MaxExpr = Attr.getArgAsExpr(1); |
| 5432 | if (!checkUInt32Argument(S, Attr, MaxExpr, Max)) |
| 5433 | return; |
| 5434 | } |
| 5435 | |
| 5436 | if (Min == 0 && Max != 0) { |
| 5437 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5438 | << Attr.getName() << 0; |
| 5439 | return; |
| 5440 | } |
| 5441 | if (Max != 0 && Min > Max) { |
| 5442 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5443 | << Attr.getName() << 1; |
| 5444 | return; |
| 5445 | } |
| 5446 | |
| 5447 | D->addAttr(::new (S.Context) |
| 5448 | AMDGPUWavesPerEUAttr(Attr.getLoc(), S.Context, Min, Max, |
| 5449 | Attr.getAttributeSpellingListIndex())); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, |
| 5453 | const AttributeList &Attr) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5454 | uint32_t NumSGPR = 0; |
| 5455 | Expr *NumSGPRExpr = Attr.getArgAsExpr(0); |
| 5456 | if (!checkUInt32Argument(S, Attr, NumSGPRExpr, NumSGPR)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5457 | return; |
| 5458 | |
| 5459 | D->addAttr(::new (S.Context) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5460 | AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context, NumSGPR, |
| 5461 | Attr.getAttributeSpellingListIndex())); |
| 5462 | } |
| 5463 | |
| 5464 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, |
| 5465 | const AttributeList &Attr) { |
| 5466 | uint32_t NumVGPR = 0; |
| 5467 | Expr *NumVGPRExpr = Attr.getArgAsExpr(0); |
| 5468 | if (!checkUInt32Argument(S, Attr, NumVGPRExpr, NumVGPR)) |
| 5469 | return; |
| 5470 | |
| 5471 | D->addAttr(::new (S.Context) |
| 5472 | AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context, NumVGPR, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5473 | Attr.getAttributeSpellingListIndex())); |
| 5474 | } |
| 5475 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5476 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 5477 | const AttributeList& Attr) { |
| 5478 | // If we try to apply it to a function pointer, don't warn, but don't |
| 5479 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 5480 | // special about calling a force_align_arg_pointer function. |
| 5481 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 5482 | if (VD && VD->getType()->isFunctionPointerType()) |
| 5483 | return; |
| 5484 | // Also don't warn on function pointer typedefs. |
| 5485 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 5486 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 5487 | TD->getUnderlyingType()->isFunctionType())) |
| 5488 | return; |
| 5489 | // Attribute can only be applied to function types. |
| 5490 | if (!isa<FunctionDecl>(D)) { |
| 5491 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 5492 | << Attr.getName() << /* function */0; |
| 5493 | return; |
| 5494 | } |
| 5495 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5496 | D->addAttr(::new (S.Context) |
| 5497 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 5498 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5499 | } |
| 5500 | |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5501 | static void handleLayoutVersion(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5502 | uint32_t Version; |
| 5503 | Expr *VersionExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 5504 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), Version)) |
| 5505 | return; |
| 5506 | |
| 5507 | // TODO: Investigate what happens with the next major version of MSVC. |
| 5508 | if (Version != LangOptions::MSVC2015) { |
| 5509 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 5510 | << Attr.getName() << Version << VersionExpr->getSourceRange(); |
| 5511 | return; |
| 5512 | } |
| 5513 | |
| 5514 | D->addAttr(::new (S.Context) |
| 5515 | LayoutVersionAttr(Attr.getRange(), S.Context, Version, |
| 5516 | Attr.getAttributeSpellingListIndex())); |
| 5517 | } |
| 5518 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5519 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 5520 | unsigned AttrSpellingListIndex) { |
| 5521 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5522 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5523 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5524 | } |
| 5525 | |
| 5526 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5527 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5528 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5529 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5530 | } |
| 5531 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5532 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 5533 | unsigned AttrSpellingListIndex) { |
| 5534 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5535 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5536 | D->dropAttr<DLLImportAttr>(); |
| 5537 | } |
| 5538 | |
| 5539 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5540 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5541 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5542 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5543 | } |
| 5544 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5545 | static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 5546 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 5547 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5548 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) |
| 5549 | << A.getName(); |
| 5550 | return; |
| 5551 | } |
| 5552 | |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 5553 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5554 | if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport && |
| 5555 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5556 | // MinGW doesn't allow dllimport on inline functions. |
| 5557 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
| 5558 | << A.getName(); |
| 5559 | return; |
| 5560 | } |
| 5561 | } |
| 5562 | |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 5563 | if (auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 5564 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 5565 | MD->getParent()->isLambda()) { |
| 5566 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName(); |
| 5567 | return; |
| 5568 | } |
| 5569 | } |
| 5570 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5571 | unsigned Index = A.getAttributeSpellingListIndex(); |
| 5572 | Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport |
| 5573 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 5574 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5575 | if (NewAttr) |
| 5576 | D->addAttr(NewAttr); |
| 5577 | } |
| 5578 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5579 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5580 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5581 | unsigned AttrSpellingListIndex, |
| 5582 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 5583 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 5584 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5585 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5586 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 5587 | << 1 /*previous declaration*/; |
| 5588 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 5589 | D->dropAttr<MSInheritanceAttr>(); |
| 5590 | } |
| 5591 | |
| 5592 | CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 5593 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5594 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 5595 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5596 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5597 | } |
| 5598 | } else { |
| 5599 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 5600 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5601 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5602 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5603 | } |
| 5604 | if (RD->getDescribedClassTemplate()) { |
| 5605 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5606 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5607 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5608 | } |
| 5609 | } |
| 5610 | |
| 5611 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5612 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5613 | } |
| 5614 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5615 | static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5616 | // The capability attributes take a single string parameter for the name of |
| 5617 | // the capability they represent. The lockable attribute does not take any |
| 5618 | // parameters. However, semantically, both attributes represent the same |
| 5619 | // concept, and so they use the same semantic attribute. Eventually, the |
| 5620 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5621 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 5622 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5623 | // literal will be considered a "mutex." |
| 5624 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5625 | SourceLocation LiteralLoc; |
| 5626 | if (Attr.getKind() == AttributeList::AT_Capability && |
| 5627 | !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc)) |
| 5628 | return; |
| 5629 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5630 | // Currently, there are only two names allowed for a capability: role and |
| 5631 | // mutex (case insensitive). Diagnose other capability names. |
| 5632 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 5633 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 5634 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5635 | D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N, |
| 5636 | Attr.getAttributeSpellingListIndex())); |
| 5637 | } |
| 5638 | |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5639 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, |
| 5640 | const AttributeList &Attr) { |
| 5641 | D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context, |
| 5642 | Attr.getArgAsExpr(0), |
| 5643 | Attr.getAttributeSpellingListIndex())); |
| 5644 | } |
| 5645 | |
| 5646 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
| 5647 | const AttributeList &Attr) { |
| 5648 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5649 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5650 | return; |
| 5651 | |
| 5652 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(), |
| 5653 | S.Context, |
| 5654 | Args.data(), Args.size(), |
| 5655 | Attr.getAttributeSpellingListIndex())); |
| 5656 | } |
| 5657 | |
| 5658 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
| 5659 | const AttributeList &Attr) { |
| 5660 | SmallVector<Expr*, 2> Args; |
| 5661 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 5662 | return; |
| 5663 | |
| 5664 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(), |
| 5665 | S.Context, |
| 5666 | Attr.getArgAsExpr(0), |
| 5667 | Args.data(), |
| 5668 | Args.size(), |
| 5669 | Attr.getAttributeSpellingListIndex())); |
| 5670 | } |
| 5671 | |
| 5672 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
| 5673 | const AttributeList &Attr) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5674 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5675 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 5676 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5677 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5678 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
| 5679 | Attr.getRange(), S.Context, Args.data(), Args.size(), |
| 5680 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5681 | } |
| 5682 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5683 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
| 5684 | const AttributeList &Attr) { |
| 5685 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5686 | return; |
| 5687 | |
| 5688 | // check that all arguments are lockable objects |
| 5689 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 5690 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5691 | if (Args.empty()) |
| 5692 | return; |
| 5693 | |
| 5694 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
| 5695 | RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(), |
| 5696 | Args.size(), Attr.getAttributeSpellingListIndex()); |
| 5697 | |
| 5698 | D->addAttr(RCA); |
| 5699 | } |
| 5700 | |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5701 | static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5702 | if (auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
| 5703 | if (NSD->isAnonymousNamespace()) { |
| 5704 | S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace); |
| 5705 | // Do not want to attach the attribute to the namespace because that will |
| 5706 | // cause confusing diagnostic reports for uses of declarations within the |
| 5707 | // namespace. |
| 5708 | return; |
| 5709 | } |
| 5710 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5711 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 5712 | // Handle the cases where the attribute has a text message. |
| 5713 | StringRef Str, Replacement; |
| 5714 | if (Attr.isArgExpr(0) && Attr.getArgAsExpr(0) && |
| 5715 | !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
| 5716 | return; |
| 5717 | |
| 5718 | // Only support a single optional message for Declspec and CXX11. |
| 5719 | if (Attr.isDeclspecAttribute() || Attr.isCXX11Attribute()) |
| 5720 | checkAttributeAtMostNumArgs(S, Attr, 1); |
| 5721 | else if (Attr.isArgExpr(1) && Attr.getArgAsExpr(1) && |
| 5722 | !S.checkStringLiteralArgumentAttr(Attr, 1, Replacement)) |
| 5723 | return; |
| 5724 | |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5725 | if (!S.getLangOpts().CPlusPlus14) |
| 5726 | if (Attr.isCXX11Attribute() && |
| 5727 | !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu"))) |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5728 | S.Diag(Attr.getLoc(), diag::ext_cxx14_attr) << Attr.getName(); |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5729 | |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5730 | D->addAttr(::new (S.Context) |
| 5731 | DeprecatedAttr(Attr.getRange(), S.Context, Str, Replacement, |
| 5732 | Attr.getAttributeSpellingListIndex())); |
| 5733 | } |
| 5734 | |
| 5735 | static bool isGlobalVar(const Decl *D) { |
| 5736 | if (const auto *S = dyn_cast<VarDecl>(D)) |
| 5737 | return S->hasGlobalStorage(); |
| 5738 | return false; |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5739 | } |
| 5740 | |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5741 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5742 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5743 | return; |
| 5744 | |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 5745 | std::vector<StringRef> Sanitizers; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5746 | |
| 5747 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 5748 | StringRef SanitizerName; |
| 5749 | SourceLocation LiteralLoc; |
| 5750 | |
| 5751 | if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc)) |
| 5752 | return; |
| 5753 | |
| 5754 | if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0) |
| 5755 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5756 | else if (isGlobalVar(D) && SanitizerName != "address") |
| 5757 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5758 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5759 | Sanitizers.push_back(SanitizerName); |
| 5760 | } |
| 5761 | |
| 5762 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
| 5763 | Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(), |
| 5764 | Attr.getAttributeSpellingListIndex())); |
| 5765 | } |
| 5766 | |
| 5767 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
| 5768 | const AttributeList &Attr) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 5769 | StringRef AttrName = Attr.getName()->getName(); |
| 5770 | normalizeName(AttrName); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5771 | StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName) |
| 5772 | .Case("no_address_safety_analysis", "address") |
| 5773 | .Case("no_sanitize_address", "address") |
| 5774 | .Case("no_sanitize_thread", "thread") |
| 5775 | .Case("no_sanitize_memory", "memory"); |
| 5776 | if (isGlobalVar(D) && SanitizerName != "address") |
| 5777 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5778 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5779 | D->addAttr(::new (S.Context) |
| 5780 | NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1, |
| 5781 | Attr.getAttributeSpellingListIndex())); |
| 5782 | } |
| 5783 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 5784 | static void handleInternalLinkageAttr(Sema &S, Decl *D, |
| 5785 | const AttributeList &Attr) { |
| 5786 | if (InternalLinkageAttr *Internal = |
| 5787 | S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(), |
| 5788 | Attr.getAttributeSpellingListIndex())) |
| 5789 | D->addAttr(Internal); |
| 5790 | } |
| 5791 | |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 5792 | static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5793 | if (S.LangOpts.OpenCLVersion != 200) |
| 5794 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_opencl_version) |
| 5795 | << Attr.getName() << "2.0" << 0; |
| 5796 | else |
| 5797 | S.Diag(Attr.getLoc(), diag::warn_opencl_attr_deprecated_ignored) |
| 5798 | << Attr.getName() << "2.0"; |
| 5799 | } |
| 5800 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5801 | /// Handles semantic checking for features that are common to all attributes, |
| 5802 | /// such as checking whether a parameter was properly specified, or the correct |
| 5803 | /// number of arguments were passed, etc. |
| 5804 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 5805 | const AttributeList &Attr) { |
| 5806 | // Several attributes carry different semantics than the parsing requires, so |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5807 | // those are opted out of the common argument checks. |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5808 | // |
| 5809 | // We also bail on unknown and ignored attributes because those are handled |
| 5810 | // as part of the target-specific handling logic. |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5811 | if (Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5812 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5813 | // Check whether the attribute requires specific language extensions to be |
| 5814 | // enabled. |
| 5815 | if (!Attr.diagnoseLangOpts(S)) |
| 5816 | return true; |
Alex Lorenz | 24952fb | 2017-04-19 15:52:11 +0000 | [diff] [blame] | 5817 | // Check whether the attribute appertains to the given subject. |
| 5818 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 5819 | return true; |
| 5820 | if (Attr.hasCustomParsing()) |
| 5821 | return false; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5822 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5823 | if (Attr.getMinArgs() == Attr.getMaxArgs()) { |
| 5824 | // If there are no optional arguments, then checking for the argument count |
| 5825 | // is trivial. |
| 5826 | if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 5827 | return true; |
| 5828 | } else { |
| 5829 | // There are optional arguments, so checking is slightly more involved. |
| 5830 | if (Attr.getMinArgs() && |
| 5831 | !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs())) |
| 5832 | return true; |
| 5833 | else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() && |
| 5834 | !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs())) |
| 5835 | return true; |
| 5836 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5837 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5838 | return false; |
| 5839 | } |
| 5840 | |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5841 | static void handleOpenCLAccessAttr(Sema &S, Decl *D, |
| 5842 | const AttributeList &Attr) { |
| 5843 | if (D->isInvalidDecl()) |
| 5844 | return; |
| 5845 | |
| 5846 | // Check if there is only one access qualifier. |
| 5847 | if (D->hasAttr<OpenCLAccessAttr>()) { |
| 5848 | S.Diag(Attr.getLoc(), diag::err_opencl_multiple_access_qualifiers) |
| 5849 | << D->getSourceRange(); |
| 5850 | D->setInvalidDecl(true); |
| 5851 | return; |
| 5852 | } |
| 5853 | |
| 5854 | // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an |
| 5855 | // image object can be read and written. |
| 5856 | // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe |
| 5857 | // object. Using the read_write (or __read_write) qualifier with the pipe |
| 5858 | // qualifier is a compilation error. |
| 5859 | if (const ParmVarDecl *PDecl = dyn_cast<ParmVarDecl>(D)) { |
| 5860 | const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr(); |
| 5861 | if (Attr.getName()->getName().find("read_write") != StringRef::npos) { |
| 5862 | if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) { |
| 5863 | S.Diag(Attr.getLoc(), diag::err_opencl_invalid_read_write) |
| 5864 | << Attr.getName() << PDecl->getType() << DeclTy->isImageType(); |
| 5865 | D->setInvalidDecl(true); |
| 5866 | return; |
| 5867 | } |
| 5868 | } |
| 5869 | } |
| 5870 | |
| 5871 | D->addAttr(::new (S.Context) OpenCLAccessAttr( |
| 5872 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 5873 | } |
| 5874 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5875 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5876 | // Top Level Sema Entry Points |
| 5877 | //===----------------------------------------------------------------------===// |
| 5878 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5879 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 5880 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 5881 | /// silently ignore it if a GNU attribute. |
| 5882 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 5883 | const AttributeList &Attr, |
| 5884 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5885 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5886 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 5887 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5888 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 5889 | // instead. |
| 5890 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 5891 | return; |
| 5892 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5893 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 5894 | // which do not apply to the current target architecture are treated as |
| 5895 | // though they were unknown attributes. |
| 5896 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 5897 | !Attr.existsInTarget(S.Context.getTargetInfo())) { |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5898 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() |
| 5899 | ? diag::warn_unhandled_ms_attribute_ignored |
| 5900 | : diag::warn_unknown_attribute_ignored) |
| 5901 | << Attr.getName(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5902 | return; |
| 5903 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5904 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5905 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 5906 | return; |
| 5907 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5908 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5909 | default: |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5910 | if (!Attr.isStmtAttr()) { |
| 5911 | // Type attributes are handled elsewhere; silently move on. |
| 5912 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
| 5913 | break; |
| 5914 | } |
| 5915 | S.Diag(Attr.getLoc(), diag::err_stmt_attribute_invalid_on_decl) |
| 5916 | << Attr.getName() << D->getLocation(); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5917 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5918 | case AttributeList::AT_Interrupt: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5919 | handleInterruptAttr(S, D, Attr); |
| 5920 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5921 | case AttributeList::AT_X86ForceAlignArgPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5922 | handleX86ForceAlignArgPointerAttr(S, D, Attr); |
| 5923 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5924 | case AttributeList::AT_DLLExport: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5925 | case AttributeList::AT_DLLImport: |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5926 | handleDLLAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5927 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5928 | case AttributeList::AT_Mips16: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5929 | handleSimpleAttributeWithExclusions<Mips16Attr, MipsInterruptAttr>(S, D, |
| 5930 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5931 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5932 | case AttributeList::AT_NoMips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5933 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); |
| 5934 | break; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5935 | case AttributeList::AT_AMDGPUFlatWorkGroupSize: |
| 5936 | handleAMDGPUFlatWorkGroupSizeAttr(S, D, Attr); |
| 5937 | break; |
| 5938 | case AttributeList::AT_AMDGPUWavesPerEU: |
| 5939 | handleAMDGPUWavesPerEUAttr(S, D, Attr); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5940 | break; |
| 5941 | case AttributeList::AT_AMDGPUNumSGPR: |
| 5942 | handleAMDGPUNumSGPRAttr(S, D, Attr); |
| 5943 | break; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5944 | case AttributeList::AT_AMDGPUNumVGPR: |
| 5945 | handleAMDGPUNumVGPRAttr(S, D, Attr); |
| 5946 | break; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5947 | case AttributeList::AT_AVRSignal: |
| 5948 | handleAVRSignalAttr(S, D, Attr); |
| 5949 | break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 5950 | case AttributeList::AT_IBAction: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5951 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); |
| 5952 | break; |
| 5953 | case AttributeList::AT_IBOutlet: |
| 5954 | handleIBOutlet(S, D, Attr); |
| 5955 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5956 | case AttributeList::AT_IBOutletCollection: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5957 | handleIBOutletCollection(S, D, Attr); |
| 5958 | break; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 5959 | case AttributeList::AT_IFunc: |
| 5960 | handleIFuncAttr(S, D, Attr); |
| 5961 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5962 | case AttributeList::AT_Alias: |
| 5963 | handleAliasAttr(S, D, Attr); |
| 5964 | break; |
| 5965 | case AttributeList::AT_Aligned: |
| 5966 | handleAlignedAttr(S, D, Attr); |
| 5967 | break; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 5968 | case AttributeList::AT_AlignValue: |
| 5969 | handleAlignValueAttr(S, D, Attr); |
| 5970 | break; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5971 | case AttributeList::AT_AllocSize: |
| 5972 | handleAllocSizeAttr(S, D, Attr); |
| 5973 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5974 | case AttributeList::AT_AlwaysInline: |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 5975 | handleAlwaysInlineAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5976 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5977 | case AttributeList::AT_AnalyzerNoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5978 | handleAnalyzerNoReturnAttr(S, D, Attr); |
| 5979 | break; |
| 5980 | case AttributeList::AT_TLSModel: |
| 5981 | handleTLSModelAttr(S, D, Attr); |
| 5982 | break; |
| 5983 | case AttributeList::AT_Annotate: |
| 5984 | handleAnnotateAttr(S, D, Attr); |
| 5985 | break; |
| 5986 | case AttributeList::AT_Availability: |
| 5987 | handleAvailabilityAttr(S, D, Attr); |
| 5988 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5989 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5990 | handleDependencyAttr(S, scope, D, Attr); |
| 5991 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5992 | case AttributeList::AT_Common: |
| 5993 | handleCommonAttr(S, D, Attr); |
| 5994 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5995 | case AttributeList::AT_CUDAConstant: |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 5996 | handleConstantAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5997 | break; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5998 | case AttributeList::AT_PassObjectSize: |
| 5999 | handlePassObjectSizeAttr(S, D, Attr); |
| 6000 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6001 | case AttributeList::AT_Constructor: |
| 6002 | handleConstructorAttr(S, D, Attr); |
| 6003 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 6004 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6005 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); |
| 6006 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6007 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 6008 | handleDeprecatedAttr(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 6009 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6010 | case AttributeList::AT_Destructor: |
| 6011 | handleDestructorAttr(S, D, Attr); |
| 6012 | break; |
| 6013 | case AttributeList::AT_EnableIf: |
| 6014 | handleEnableIfAttr(S, D, Attr); |
| 6015 | break; |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 6016 | case AttributeList::AT_DiagnoseIf: |
| 6017 | handleDiagnoseIfAttr(S, D, Attr); |
| 6018 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6019 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6020 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6021 | break; |
Alex Lorenz | d5d27e1 | 2017-03-01 18:06:25 +0000 | [diff] [blame] | 6022 | case AttributeList::AT_ExternalSourceSymbol: |
| 6023 | handleExternalSourceSymbolAttr(S, D, Attr); |
| 6024 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 6025 | case AttributeList::AT_MinSize: |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 6026 | handleMinSizeAttr(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 6027 | break; |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 6028 | case AttributeList::AT_OptimizeNone: |
| 6029 | handleOptimizeNoneAttr(S, D, Attr); |
| 6030 | break; |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 6031 | case AttributeList::AT_FlagEnum: |
| 6032 | handleSimpleAttribute<FlagEnumAttr>(S, D, Attr); |
| 6033 | break; |
Akira Hatanaka | 3c268af | 2017-03-21 02:23:00 +0000 | [diff] [blame] | 6034 | case AttributeList::AT_EnumExtensibility: |
| 6035 | handleEnumExtensibilityAttr(S, D, Attr); |
| 6036 | break; |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 6037 | case AttributeList::AT_Flatten: |
| 6038 | handleSimpleAttribute<FlattenAttr>(S, D, Attr); |
| 6039 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6040 | case AttributeList::AT_Format: |
| 6041 | handleFormatAttr(S, D, Attr); |
| 6042 | break; |
| 6043 | case AttributeList::AT_FormatArg: |
| 6044 | handleFormatArgAttr(S, D, Attr); |
| 6045 | break; |
| 6046 | case AttributeList::AT_CUDAGlobal: |
| 6047 | handleGlobalAttr(S, D, Attr); |
| 6048 | break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 6049 | case AttributeList::AT_CUDADevice: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 6050 | handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D, |
| 6051 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6052 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 6053 | case AttributeList::AT_CUDAHost: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 6054 | handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, |
| 6055 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6056 | break; |
| 6057 | case AttributeList::AT_GNUInline: |
| 6058 | handleGNUInlineAttr(S, D, Attr); |
| 6059 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6060 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6061 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 6062 | break; |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 6063 | case AttributeList::AT_Restrict: |
| 6064 | handleRestrictAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6065 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6066 | case AttributeList::AT_MayAlias: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6067 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); |
| 6068 | break; |
| 6069 | case AttributeList::AT_Mode: |
| 6070 | handleModeAttr(S, D, Attr); |
| 6071 | break; |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 6072 | case AttributeList::AT_NoAlias: |
| 6073 | handleSimpleAttribute<NoAliasAttr>(S, D, Attr); |
| 6074 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6075 | case AttributeList::AT_NoCommon: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6076 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); |
| 6077 | break; |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 6078 | case AttributeList::AT_NoSplitStack: |
| 6079 | handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr); |
| 6080 | break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 6081 | case AttributeList::AT_NonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6082 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 6083 | handleNonNullAttrParameter(S, PVD, Attr); |
| 6084 | else |
| 6085 | handleNonNullAttr(S, D, Attr); |
| 6086 | break; |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 6087 | case AttributeList::AT_ReturnsNonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6088 | handleReturnsNonNullAttr(S, D, Attr); |
| 6089 | break; |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 6090 | case AttributeList::AT_AssumeAligned: |
| 6091 | handleAssumeAlignedAttr(S, D, Attr); |
| 6092 | break; |
Erich Keane | 623efd8 | 2017-03-30 21:48:55 +0000 | [diff] [blame] | 6093 | case AttributeList::AT_AllocAlign: |
| 6094 | handleAllocAlignAttr(S, D, Attr); |
| 6095 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6096 | case AttributeList::AT_Overloadable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6097 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); |
| 6098 | break; |
| 6099 | case AttributeList::AT_Ownership: |
| 6100 | handleOwnershipAttr(S, D, Attr); |
| 6101 | break; |
| 6102 | case AttributeList::AT_Cold: |
| 6103 | handleColdAttr(S, D, Attr); |
| 6104 | break; |
| 6105 | case AttributeList::AT_Hot: |
| 6106 | handleHotAttr(S, D, Attr); |
| 6107 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6108 | case AttributeList::AT_Naked: |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 6109 | handleNakedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6110 | break; |
| 6111 | case AttributeList::AT_NoReturn: |
| 6112 | handleNoReturnAttr(S, D, Attr); |
| 6113 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 6114 | case AttributeList::AT_NoThrow: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6115 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); |
| 6116 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 6117 | case AttributeList::AT_CUDAShared: |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 6118 | handleSharedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6119 | break; |
| 6120 | case AttributeList::AT_VecReturn: |
| 6121 | handleVecReturnAttr(S, D, Attr); |
| 6122 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6123 | case AttributeList::AT_ObjCOwnership: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6124 | handleObjCOwnershipAttr(S, D, Attr); |
| 6125 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6126 | case AttributeList::AT_ObjCPreciseLifetime: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6127 | handleObjCPreciseLifetimeAttr(S, D, Attr); |
| 6128 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6129 | case AttributeList::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6130 | handleObjCReturnsInnerPointerAttr(S, D, Attr); |
| 6131 | break; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 6132 | case AttributeList::AT_ObjCRequiresSuper: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6133 | handleObjCRequiresSuperAttr(S, D, Attr); |
| 6134 | break; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 6135 | case AttributeList::AT_ObjCBridge: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6136 | handleObjCBridgeAttr(S, scope, D, Attr); |
| 6137 | break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 6138 | case AttributeList::AT_ObjCBridgeMutable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6139 | handleObjCBridgeMutableAttr(S, scope, D, Attr); |
| 6140 | break; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 6141 | case AttributeList::AT_ObjCBridgeRelated: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6142 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); |
| 6143 | break; |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 6144 | case AttributeList::AT_ObjCDesignatedInitializer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6145 | handleObjCDesignatedInitializer(S, D, Attr); |
| 6146 | break; |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 6147 | case AttributeList::AT_ObjCRuntimeName: |
| 6148 | handleObjCRuntimeName(S, D, Attr); |
| 6149 | break; |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 6150 | case AttributeList::AT_ObjCRuntimeVisible: |
| 6151 | handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, Attr); |
| 6152 | break; |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 6153 | case AttributeList::AT_ObjCBoxable: |
| 6154 | handleObjCBoxable(S, D, Attr); |
| 6155 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6156 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6157 | handleCFAuditedTransferAttr(S, D, Attr); |
| 6158 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6159 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6160 | handleCFUnknownTransferAttr(S, D, Attr); |
| 6161 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6162 | case AttributeList::AT_CFConsumed: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6163 | case AttributeList::AT_NSConsumed: |
| 6164 | handleNSConsumedAttr(S, D, Attr); |
| 6165 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6166 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6167 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); |
| 6168 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6169 | case AttributeList::AT_NSReturnsAutoreleased: |
| 6170 | case AttributeList::AT_NSReturnsNotRetained: |
| 6171 | case AttributeList::AT_CFReturnsNotRetained: |
| 6172 | case AttributeList::AT_NSReturnsRetained: |
| 6173 | case AttributeList::AT_CFReturnsRetained: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6174 | handleNSReturnsRetainedAttr(S, D, Attr); |
| 6175 | break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 6176 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6177 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); |
| 6178 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6179 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6180 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); |
| 6181 | break; |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame^] | 6182 | case AttributeList::AT_OpenCLIntelReqdSubGroupSize: |
| 6183 | handleSubGroupSize(S, D, Attr); |
| 6184 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 6185 | case AttributeList::AT_VecTypeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6186 | handleVecTypeHint(S, D, Attr); |
| 6187 | break; |
Eric Fiselier | 341e825 | 2016-09-02 18:53:31 +0000 | [diff] [blame] | 6188 | case AttributeList::AT_RequireConstantInit: |
| 6189 | handleSimpleAttribute<RequireConstantInitAttr>(S, D, Attr); |
| 6190 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6191 | case AttributeList::AT_InitPriority: |
| 6192 | handleInitPriorityAttr(S, D, Attr); |
| 6193 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6194 | case AttributeList::AT_Packed: |
| 6195 | handlePackedAttr(S, D, Attr); |
| 6196 | break; |
| 6197 | case AttributeList::AT_Section: |
| 6198 | handleSectionAttr(S, D, Attr); |
| 6199 | break; |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 6200 | case AttributeList::AT_Target: |
| 6201 | handleTargetAttr(S, D, Attr); |
| 6202 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6203 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 6204 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 6205 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6206 | case AttributeList::AT_ArcWeakrefUnavailable: |
| 6207 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); |
| 6208 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6209 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6210 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); |
| 6211 | break; |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 6212 | case AttributeList::AT_ObjCSubclassingRestricted: |
| 6213 | handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, Attr); |
| 6214 | break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 6215 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 6216 | handleObjCSuppresProtocolAttr(S, D, Attr); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 6217 | break; |
| 6218 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6219 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); |
| 6220 | break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 6221 | case AttributeList::AT_Unused: |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 6222 | handleUnusedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6223 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6224 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6225 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); |
| 6226 | break; |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 6227 | case AttributeList::AT_NotTailCalled: |
| 6228 | handleNotTailCalledAttr(S, D, Attr); |
| 6229 | break; |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 6230 | case AttributeList::AT_DisableTailCalls: |
| 6231 | handleDisableTailCallsAttr(S, D, Attr); |
| 6232 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6233 | case AttributeList::AT_Used: |
| 6234 | handleUsedAttr(S, D, Attr); |
| 6235 | break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 6236 | case AttributeList::AT_Visibility: |
| 6237 | handleVisibilityAttr(S, D, Attr, false); |
| 6238 | break; |
| 6239 | case AttributeList::AT_TypeVisibility: |
| 6240 | handleVisibilityAttr(S, D, Attr, true); |
| 6241 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 6242 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6243 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); |
| 6244 | break; |
| 6245 | case AttributeList::AT_WarnUnusedResult: |
| 6246 | handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 6247 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 6248 | case AttributeList::AT_Weak: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6249 | handleSimpleAttribute<WeakAttr>(S, D, Attr); |
| 6250 | break; |
| 6251 | case AttributeList::AT_WeakRef: |
| 6252 | handleWeakRefAttr(S, D, Attr); |
| 6253 | break; |
| 6254 | case AttributeList::AT_WeakImport: |
| 6255 | handleWeakImportAttr(S, D, Attr); |
| 6256 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6257 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6258 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6259 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6260 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6261 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); |
| 6262 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6263 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6264 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 6265 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6266 | case AttributeList::AT_ObjCNSObject: |
| 6267 | handleObjCNSObject(S, D, Attr); |
| 6268 | break; |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 6269 | case AttributeList::AT_ObjCIndependentClass: |
| 6270 | handleObjCIndependentClass(S, D, Attr); |
| 6271 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6272 | case AttributeList::AT_Blocks: |
| 6273 | handleBlocksAttr(S, D, Attr); |
| 6274 | break; |
| 6275 | case AttributeList::AT_Sentinel: |
| 6276 | handleSentinelAttr(S, D, Attr); |
| 6277 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 6278 | case AttributeList::AT_Const: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6279 | handleSimpleAttribute<ConstAttr>(S, D, Attr); |
| 6280 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6281 | case AttributeList::AT_Pure: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6282 | handleSimpleAttribute<PureAttr>(S, D, Attr); |
| 6283 | break; |
| 6284 | case AttributeList::AT_Cleanup: |
| 6285 | handleCleanupAttr(S, D, Attr); |
| 6286 | break; |
| 6287 | case AttributeList::AT_NoDebug: |
| 6288 | handleNoDebugAttr(S, D, Attr); |
| 6289 | break; |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 6290 | case AttributeList::AT_NoDuplicate: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6291 | handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr); |
| 6292 | break; |
Yaxun Liu | 7d07ae7 | 2016-11-01 18:45:32 +0000 | [diff] [blame] | 6293 | case AttributeList::AT_Convergent: |
| 6294 | handleSimpleAttribute<ConvergentAttr>(S, D, Attr); |
| 6295 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6296 | case AttributeList::AT_NoInline: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6297 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); |
| 6298 | break; |
| 6299 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
| 6300 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); |
| 6301 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6302 | case AttributeList::AT_StdCall: |
| 6303 | case AttributeList::AT_CDecl: |
| 6304 | case AttributeList::AT_FastCall: |
| 6305 | case AttributeList::AT_ThisCall: |
| 6306 | case AttributeList::AT_Pascal: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 6307 | case AttributeList::AT_RegCall: |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6308 | case AttributeList::AT_SwiftCall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 6309 | case AttributeList::AT_VectorCall: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 6310 | case AttributeList::AT_MSABI: |
| 6311 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6312 | case AttributeList::AT_Pcs: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 6313 | case AttributeList::AT_IntelOclBicc: |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 6314 | case AttributeList::AT_PreserveMost: |
| 6315 | case AttributeList::AT_PreserveAll: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6316 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 6317 | break; |
Matthias Gehre | 01a6338 | 2017-03-27 19:45:24 +0000 | [diff] [blame] | 6318 | case AttributeList::AT_Suppress: |
| 6319 | handleSuppressAttr(S, D, Attr); |
| 6320 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6321 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6322 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); |
| 6323 | break; |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6324 | case AttributeList::AT_OpenCLAccess: |
| 6325 | handleOpenCLAccessAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6326 | break; |
Anastasia Stulova | fde7622 | 2016-04-01 16:05:09 +0000 | [diff] [blame] | 6327 | case AttributeList::AT_OpenCLNoSVM: |
| 6328 | handleOpenCLNoSVMAttr(S, D, Attr); |
| 6329 | break; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6330 | case AttributeList::AT_SwiftContext: |
| 6331 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftContext); |
| 6332 | break; |
| 6333 | case AttributeList::AT_SwiftErrorResult: |
| 6334 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftErrorResult); |
| 6335 | break; |
| 6336 | case AttributeList::AT_SwiftIndirectResult: |
| 6337 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftIndirectResult); |
| 6338 | break; |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 6339 | case AttributeList::AT_InternalLinkage: |
| 6340 | handleInternalLinkageAttr(S, D, Attr); |
| 6341 | break; |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 6342 | case AttributeList::AT_LTOVisibilityPublic: |
| 6343 | handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, Attr); |
| 6344 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 6345 | |
| 6346 | // Microsoft attributes: |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6347 | case AttributeList::AT_EmptyBases: |
| 6348 | handleSimpleAttribute<EmptyBasesAttr>(S, D, Attr); |
| 6349 | break; |
| 6350 | case AttributeList::AT_LayoutVersion: |
| 6351 | handleLayoutVersion(S, D, Attr); |
| 6352 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 6353 | case AttributeList::AT_MSNoVTable: |
| 6354 | handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 6355 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 6356 | case AttributeList::AT_MSStruct: |
| 6357 | handleSimpleAttribute<MSStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 6358 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6359 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6360 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 6361 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 6362 | case AttributeList::AT_MSInheritance: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6363 | handleMSInheritanceAttr(S, D, Attr); |
| 6364 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 6365 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6366 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); |
| 6367 | break; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 6368 | case AttributeList::AT_Thread: |
| 6369 | handleDeclspecThreadAttr(S, D, Attr); |
| 6370 | break; |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6371 | |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 6372 | case AttributeList::AT_AbiTag: |
| 6373 | handleAbiTagAttr(S, D, Attr); |
| 6374 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6375 | |
| 6376 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 6377 | case AttributeList::AT_AssertExclusiveLock: |
| 6378 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 6379 | break; |
| 6380 | case AttributeList::AT_AssertSharedLock: |
| 6381 | handleAssertSharedLockAttr(S, D, Attr); |
| 6382 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6383 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6384 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); |
| 6385 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6386 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6387 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6388 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6389 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6390 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); |
| 6391 | break; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6392 | case AttributeList::AT_NoSanitize: |
| 6393 | handleNoSanitizeAttr(S, D, Attr); |
| 6394 | break; |
| 6395 | case AttributeList::AT_NoSanitizeSpecific: |
| 6396 | handleNoSanitizeSpecificAttr(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 6397 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6398 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6399 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 6400 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6401 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6402 | handleGuardedByAttr(S, D, Attr); |
| 6403 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6404 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6405 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6406 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6407 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6408 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6409 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6410 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6411 | handleLockReturnedAttr(S, D, Attr); |
| 6412 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6413 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6414 | handleLocksExcludedAttr(S, D, Attr); |
| 6415 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6416 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6417 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6418 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6419 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6420 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6421 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6422 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6423 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6424 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6425 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6426 | // Capability analysis attributes. |
| 6427 | case AttributeList::AT_Capability: |
| 6428 | case AttributeList::AT_Lockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6429 | handleCapabilityAttr(S, D, Attr); |
| 6430 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6431 | case AttributeList::AT_RequiresCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6432 | handleRequiresCapabilityAttr(S, D, Attr); |
| 6433 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6434 | |
| 6435 | case AttributeList::AT_AssertCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6436 | handleAssertCapabilityAttr(S, D, Attr); |
| 6437 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6438 | case AttributeList::AT_AcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6439 | handleAcquireCapabilityAttr(S, D, Attr); |
| 6440 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6441 | case AttributeList::AT_ReleaseCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6442 | handleReleaseCapabilityAttr(S, D, Attr); |
| 6443 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6444 | case AttributeList::AT_TryAcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6445 | handleTryAcquireCapabilityAttr(S, D, Attr); |
| 6446 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6447 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 6448 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 6449 | case AttributeList::AT_Consumable: |
| 6450 | handleConsumableAttr(S, D, Attr); |
| 6451 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 6452 | case AttributeList::AT_ConsumableAutoCast: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6453 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); |
| 6454 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 6455 | case AttributeList::AT_ConsumableSetOnRead: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6456 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); |
| 6457 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 6458 | case AttributeList::AT_CallableWhen: |
| 6459 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6460 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 6461 | case AttributeList::AT_ParamTypestate: |
| 6462 | handleParamTypestateAttr(S, D, Attr); |
| 6463 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 6464 | case AttributeList::AT_ReturnTypestate: |
| 6465 | handleReturnTypestateAttr(S, D, Attr); |
| 6466 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6467 | case AttributeList::AT_SetTypestate: |
| 6468 | handleSetTypestateAttr(S, D, Attr); |
| 6469 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 6470 | case AttributeList::AT_TestTypestate: |
| 6471 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6472 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6473 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6474 | // Type safety attributes. |
| 6475 | case AttributeList::AT_ArgumentWithTypeTag: |
| 6476 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 6477 | break; |
| 6478 | case AttributeList::AT_TypeTagForDatatype: |
| 6479 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 6480 | break; |
Oren Ben Simhon | 318a6ea | 2017-04-27 12:01:00 +0000 | [diff] [blame] | 6481 | case AttributeList::AT_AnyX86NoCallerSavedRegisters: |
| 6482 | handleNoCallerSavedRegsAttr(S, D, Attr); |
| 6483 | break; |
Pirama Arumuga Nainar | e5d2d71 | 2016-06-10 21:51:18 +0000 | [diff] [blame] | 6484 | case AttributeList::AT_RenderScriptKernel: |
| 6485 | handleSimpleAttribute<RenderScriptKernelAttr>(S, D, Attr); |
Pirama Arumuga Nainar | 8b788d0 | 2016-06-09 23:34:20 +0000 | [diff] [blame] | 6486 | break; |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 6487 | // XRay attributes. |
| 6488 | case AttributeList::AT_XRayInstrument: |
| 6489 | handleSimpleAttribute<XRayInstrumentAttr>(S, D, Attr); |
| 6490 | break; |
Dean Michael Berris | 418da3f | 2017-03-06 07:08:21 +0000 | [diff] [blame] | 6491 | case AttributeList::AT_XRayLogArgs: |
| 6492 | handleXRayLogArgsAttr(S, D, Attr); |
| 6493 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6494 | } |
| 6495 | } |
| 6496 | |
| 6497 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 6498 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 6499 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 6500 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 6501 | bool IncludeCXX11Attributes) { |
| 6502 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6503 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6504 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6505 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6506 | // GCC accepts |
| 6507 | // static int a9 __attribute__((weakref)); |
| 6508 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6509 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 6510 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 6511 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 6512 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6513 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6514 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6515 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 6516 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 6517 | // good to have a way to specify "these attributes must appear as a group", |
| 6518 | // for these. Additionally, it would be good to have a way to specify "these |
| 6519 | // 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] | 6520 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 6521 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6522 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 6523 | // FIXME: This emits a different error message than |
| 6524 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6525 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6526 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 6527 | } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6528 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6529 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 6530 | } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6531 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6532 | D->setInvalidDecl(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6533 | } else if (Attr *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) { |
| 6534 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6535 | << A << ExpectedKernelFunction; |
| 6536 | D->setInvalidDecl(); |
| 6537 | } else if (Attr *A = D->getAttr<AMDGPUWavesPerEUAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 6538 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6539 | << A << ExpectedKernelFunction; |
| 6540 | D->setInvalidDecl(); |
| 6541 | } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6542 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6543 | << A << ExpectedKernelFunction; |
| 6544 | D->setInvalidDecl(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6545 | } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6546 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6547 | << A << ExpectedKernelFunction; |
| 6548 | D->setInvalidDecl(); |
Xiuli Pan | be6da4b | 2017-05-04 07:31:20 +0000 | [diff] [blame^] | 6549 | } else if (Attr *A = D->getAttr<OpenCLIntelReqdSubGroupSizeAttr>()) { |
| 6550 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
| 6551 | D->setInvalidDecl(); |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6552 | } |
| 6553 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6554 | } |
| 6555 | |
Erich Keane | 2fe684b | 2017-02-28 20:44:39 +0000 | [diff] [blame] | 6556 | // Helper for delayed proccessing TransparentUnion attribute. |
| 6557 | void Sema::ProcessDeclAttributeDelayed(Decl *D, const AttributeList *AttrList) { |
| 6558 | for (const AttributeList *Attr = AttrList; Attr; Attr = Attr->getNext()) |
| 6559 | if (Attr->getKind() == AttributeList::AT_TransparentUnion) { |
| 6560 | handleTransparentUnionAttr(*this, D, *Attr); |
| 6561 | break; |
| 6562 | } |
| 6563 | } |
| 6564 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6565 | // Annotation attributes are the only attributes allowed after an access |
| 6566 | // specifier. |
| 6567 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 6568 | const AttributeList *AttrList) { |
| 6569 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6570 | if (l->getKind() == AttributeList::AT_Annotate) { |
David Majnemer | 706f315 | 2014-12-14 01:05:01 +0000 | [diff] [blame] | 6571 | ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6572 | } else { |
| 6573 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 6574 | return true; |
| 6575 | } |
| 6576 | } |
| 6577 | |
| 6578 | return false; |
| 6579 | } |
| 6580 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6581 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 6582 | /// contains any decl attributes that we should warn about. |
| 6583 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 6584 | for ( ; A; A = A->getNext()) { |
| 6585 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 6586 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6587 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 6588 | |
| 6589 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 6590 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 6591 | << A->getName() << A->getRange(); |
| 6592 | } else { |
| 6593 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 6594 | << A->getName() << A->getRange(); |
| 6595 | } |
| 6596 | } |
| 6597 | } |
| 6598 | |
| 6599 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 6600 | /// used to build a declaration, complain about any decl attributes |
| 6601 | /// which might be lying around on it. |
| 6602 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 6603 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 6604 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 6605 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 6606 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 6607 | } |
| 6608 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6609 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6610 | /// \#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] | 6611 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 6612 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6613 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6614 | NamedDecl *NewD = nullptr; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6615 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6616 | FunctionDecl *NewFD; |
| 6617 | // FIXME: Missing call to CheckFunctionDeclaration(). |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6618 | // FIXME: Mangling? |
| 6619 | // FIXME: Is the qualifier info correct? |
| 6620 | // FIXME: Is the DeclContext correct? |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6621 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 6622 | Loc, Loc, DeclarationName(II), |
| 6623 | FD->getType(), FD->getTypeSourceInfo(), |
| 6624 | SC_None, false/*isInlineSpecified*/, |
| 6625 | FD->hasPrototype(), |
| 6626 | false/*isConstexprSpecified*/); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6627 | NewD = NewFD; |
| 6628 | |
| 6629 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 6630 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6631 | |
| 6632 | // Fake up parameter variables; they are declared as if this were |
| 6633 | // a typedef. |
| 6634 | QualType FDTy = FD->getType(); |
| 6635 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 6636 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 6637 | for (const auto &AI : FT->param_types()) { |
| 6638 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6639 | Param->setScopeInfo(0, Params.size()); |
| 6640 | Params.push_back(Param); |
| 6641 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 6642 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6643 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6644 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 6645 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6646 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 6647 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6648 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6649 | if (VD->getQualifier()) { |
| 6650 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 6651 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6652 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6653 | } |
| 6654 | return NewD; |
| 6655 | } |
| 6656 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6657 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6658 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6659 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6660 | if (W.getUsed()) return; // only do this once |
| 6661 | W.setUsed(true); |
| 6662 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 6663 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6664 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6665 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 6666 | W.getLocation())); |
| 6667 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6668 | WeakTopLevelDecl.push_back(NewD); |
| 6669 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 6670 | // to insert Decl at TU scope, sorry. |
| 6671 | DeclContext *SavedContext = CurContext; |
| 6672 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 6673 | NewD->setDeclContext(CurContext); |
| 6674 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6675 | PushOnScopeChains(NewD, S); |
| 6676 | CurContext = SavedContext; |
| 6677 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6678 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6679 | } |
| 6680 | } |
| 6681 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6682 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 6683 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 6684 | // have to do this. |
| 6685 | LoadExternalWeakUndeclaredIdentifiers(); |
| 6686 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6687 | NamedDecl *ND = nullptr; |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6688 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 6689 | if (VD->isExternC()) |
| 6690 | ND = VD; |
| 6691 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 6692 | if (FD->isExternC()) |
| 6693 | ND = FD; |
| 6694 | if (ND) { |
| 6695 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 6696 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6697 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 6698 | WeakInfo W = I->second; |
| 6699 | DeclApplyPragmaWeak(S, ND, W); |
| 6700 | WeakUndeclaredIdentifiers[Id] = W; |
| 6701 | } |
| 6702 | } |
| 6703 | } |
| 6704 | } |
| 6705 | } |
| 6706 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6707 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 6708 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 6709 | /// 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] | 6710 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6711 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6712 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6713 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6714 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6715 | // Walk the declarator structure, applying decl attributes that were in a type |
| 6716 | // position to the decl itself. This handles cases like: |
| 6717 | // int *__attr__(x)** D; |
| 6718 | // when X is a decl attribute. |
| 6719 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 6720 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6721 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6722 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6723 | // Finally, apply any attributes on the decl itself. |
| 6724 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6725 | ProcessDeclAttributeList(S, D, Attrs); |
Alex Lorenz | 9e7bf16 | 2017-04-18 14:33:39 +0000 | [diff] [blame] | 6726 | |
| 6727 | // Apply additional attributes specified by '#pragma clang attribute'. |
| 6728 | AddPragmaAttributes(S, D); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6729 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6730 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6731 | /// Is the given declaration allowed to use a forbidden type? |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6732 | /// If so, it'll still be annotated with an attribute that makes it |
| 6733 | /// illegal to actually use. |
| 6734 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl, |
| 6735 | const DelayedDiagnostic &diag, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6736 | UnavailableAttr::ImplicitReason &reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6737 | // Private ivars are always okay. Unfortunately, people don't |
| 6738 | // always properly make their ivars private, even in system headers. |
| 6739 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 6740 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 6741 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6742 | return false; |
| 6743 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6744 | // Silently accept unsupported uses of __weak in both user and system |
| 6745 | // declarations when it's been disabled, for ease of integration with |
| 6746 | // -fno-objc-arc files. We do have to take some care against attempts |
| 6747 | // to define such things; for now, we've only done that for ivars |
| 6748 | // and properties. |
| 6749 | if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) { |
| 6750 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 6751 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 6752 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 6753 | return true; |
| 6754 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6755 | } |
| 6756 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6757 | // Allow all sorts of things in system headers. |
| 6758 | if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) { |
| 6759 | // Currently, all the failures dealt with this way are due to ARC |
| 6760 | // restrictions. |
| 6761 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 6762 | return true; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6763 | } |
| 6764 | |
| 6765 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6766 | } |
| 6767 | |
| 6768 | /// Handle a delayed forbidden-type diagnostic. |
| 6769 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 6770 | Decl *decl) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6771 | auto reason = UnavailableAttr::IR_None; |
| 6772 | if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) { |
| 6773 | assert(reason && "didn't set reason?"); |
| 6774 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason, |
| 6775 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6776 | return; |
| 6777 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6778 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6779 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 6780 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6781 | // kind of forbidden type messages on unavailable functions. |
| 6782 | if (FD->hasAttr<UnavailableAttr>() && |
| 6783 | diag.getForbiddenTypeDiagnostic() == |
| 6784 | diag::err_arc_array_param_no_ownership) { |
| 6785 | diag.Triggered = true; |
| 6786 | return; |
| 6787 | } |
| 6788 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6789 | |
| 6790 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 6791 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 6792 | diag.Triggered = true; |
| 6793 | } |
| 6794 | |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 6795 | static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context, |
| 6796 | const Decl *D) { |
| 6797 | // Check each AvailabilityAttr to find the one for this platform. |
| 6798 | for (const auto *A : D->attrs()) { |
| 6799 | if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) { |
| 6800 | // FIXME: this is copied from CheckAvailability. We should try to |
| 6801 | // de-duplicate. |
| 6802 | |
| 6803 | // Check if this is an App Extension "platform", and if so chop off |
| 6804 | // the suffix for matching with the actual platform. |
| 6805 | StringRef ActualPlatform = Avail->getPlatform()->getName(); |
| 6806 | StringRef RealizedPlatform = ActualPlatform; |
| 6807 | if (Context.getLangOpts().AppExt) { |
| 6808 | size_t suffix = RealizedPlatform.rfind("_app_extension"); |
| 6809 | if (suffix != StringRef::npos) |
| 6810 | RealizedPlatform = RealizedPlatform.slice(0, suffix); |
| 6811 | } |
| 6812 | |
| 6813 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 6814 | |
| 6815 | // Match the platform name. |
| 6816 | if (RealizedPlatform == TargetPlatform) |
| 6817 | return Avail; |
| 6818 | } |
| 6819 | } |
| 6820 | return nullptr; |
| 6821 | } |
| 6822 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6823 | /// \brief whether we should emit a diagnostic for \c K and \c DeclVersion in |
| 6824 | /// the context of \c Ctx. For example, we should emit an unavailable diagnostic |
| 6825 | /// in a deprecated context, but not the other way around. |
| 6826 | static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K, |
| 6827 | VersionTuple DeclVersion, |
| 6828 | Decl *Ctx) { |
| 6829 | assert(K != AR_Available && "Expected an unavailable declaration here!"); |
| 6830 | |
| 6831 | // Checks if we should emit the availability diagnostic in the context of C. |
| 6832 | auto CheckContext = [&](const Decl *C) { |
| 6833 | if (K == AR_NotYetIntroduced) { |
| 6834 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C)) |
| 6835 | if (AA->getIntroduced() >= DeclVersion) |
| 6836 | return true; |
| 6837 | } else if (K == AR_Deprecated) |
| 6838 | if (C->isDeprecated()) |
| 6839 | return true; |
| 6840 | |
| 6841 | if (C->isUnavailable()) |
| 6842 | return true; |
| 6843 | return false; |
| 6844 | }; |
| 6845 | |
| 6846 | // FIXME: This is a temporary workaround! Some existing Apple headers depends |
| 6847 | // on nested declarations in an @interface having the availability of the |
| 6848 | // interface when they really shouldn't: they are members of the enclosing |
| 6849 | // context, and can referenced from there. |
| 6850 | if (S.OriginalLexicalContext && cast<Decl>(S.OriginalLexicalContext) != Ctx) { |
| 6851 | auto *OrigCtx = cast<Decl>(S.OriginalLexicalContext); |
| 6852 | if (CheckContext(OrigCtx)) |
| 6853 | return false; |
| 6854 | |
| 6855 | // An implementation implicitly has the availability of the interface. |
| 6856 | if (auto *CatOrImpl = dyn_cast<ObjCImplDecl>(OrigCtx)) { |
| 6857 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 6858 | if (CheckContext(Interface)) |
| 6859 | return false; |
| 6860 | } |
| 6861 | // A category implicitly has the availability of the interface. |
| 6862 | else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(OrigCtx)) |
| 6863 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 6864 | if (CheckContext(Interface)) |
| 6865 | return false; |
| 6866 | } |
| 6867 | |
| 6868 | do { |
| 6869 | if (CheckContext(Ctx)) |
| 6870 | return false; |
| 6871 | |
| 6872 | // An implementation implicitly has the availability of the interface. |
| 6873 | if (auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) { |
| 6874 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 6875 | if (CheckContext(Interface)) |
| 6876 | return false; |
| 6877 | } |
| 6878 | // A category implicitly has the availability of the interface. |
| 6879 | else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx)) |
| 6880 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 6881 | if (CheckContext(Interface)) |
| 6882 | return false; |
| 6883 | } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext()))); |
| 6884 | |
| 6885 | return true; |
| 6886 | } |
| 6887 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6888 | static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K, |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6889 | Decl *Ctx, const NamedDecl *D, |
| 6890 | StringRef Message, SourceLocation Loc, |
| 6891 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 6892 | const ObjCPropertyDecl *ObjCProperty, |
| 6893 | bool ObjCPropertyAccess) { |
| 6894 | // Diagnostics for deprecated or unavailable. |
| 6895 | unsigned diag, diag_message, diag_fwdclass_message; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6896 | unsigned diag_available_here = diag::note_availability_specified_here; |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 6897 | SourceLocation NoteLocation = D->getLocation(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6898 | |
| 6899 | // Matches 'diag::note_property_attribute' options. |
| 6900 | unsigned property_note_select; |
| 6901 | |
| 6902 | // Matches diag::note_availability_specified_here. |
| 6903 | unsigned available_here_select_kind; |
| 6904 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6905 | VersionTuple DeclVersion; |
| 6906 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, D)) |
| 6907 | DeclVersion = AA->getIntroduced(); |
| 6908 | |
| 6909 | if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx)) |
| 6910 | return; |
| 6911 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6912 | switch (K) { |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6913 | case AR_Deprecated: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6914 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 6915 | : diag::warn_property_method_deprecated; |
| 6916 | diag_message = diag::warn_deprecated_message; |
| 6917 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 6918 | property_note_select = /* deprecated */ 0; |
| 6919 | available_here_select_kind = /* deprecated */ 2; |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 6920 | if (const auto *attr = D->getAttr<DeprecatedAttr>()) |
| 6921 | NoteLocation = attr->getLocation(); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6922 | break; |
| 6923 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6924 | case AR_Unavailable: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6925 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 6926 | : diag::err_property_method_unavailable; |
| 6927 | diag_message = diag::err_unavailable_message; |
| 6928 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 6929 | property_note_select = /* unavailable */ 1; |
| 6930 | available_here_select_kind = /* unavailable */ 0; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6931 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6932 | if (auto attr = D->getAttr<UnavailableAttr>()) { |
| 6933 | if (attr->isImplicit() && attr->getImplicitReason()) { |
| 6934 | // Most of these failures are due to extra restrictions in ARC; |
| 6935 | // reflect that in the primary diagnostic when applicable. |
| 6936 | auto flagARCError = [&] { |
| 6937 | if (S.getLangOpts().ObjCAutoRefCount && |
| 6938 | S.getSourceManager().isInSystemHeader(D->getLocation())) |
| 6939 | diag = diag::err_unavailable_in_arc; |
| 6940 | }; |
| 6941 | |
| 6942 | switch (attr->getImplicitReason()) { |
| 6943 | case UnavailableAttr::IR_None: break; |
| 6944 | |
| 6945 | case UnavailableAttr::IR_ARCForbiddenType: |
| 6946 | flagARCError(); |
| 6947 | diag_available_here = diag::note_arc_forbidden_type; |
| 6948 | break; |
| 6949 | |
| 6950 | case UnavailableAttr::IR_ForbiddenWeak: |
| 6951 | if (S.getLangOpts().ObjCWeakRuntime) |
| 6952 | diag_available_here = diag::note_arc_weak_disabled; |
| 6953 | else |
| 6954 | diag_available_here = diag::note_arc_weak_no_runtime; |
| 6955 | break; |
| 6956 | |
| 6957 | case UnavailableAttr::IR_ARCForbiddenConversion: |
| 6958 | flagARCError(); |
| 6959 | diag_available_here = diag::note_performs_forbidden_arc_conversion; |
| 6960 | break; |
| 6961 | |
| 6962 | case UnavailableAttr::IR_ARCInitReturnsUnrelated: |
| 6963 | flagARCError(); |
| 6964 | diag_available_here = diag::note_arc_init_returns_unrelated; |
| 6965 | break; |
| 6966 | |
| 6967 | case UnavailableAttr::IR_ARCFieldWithOwnership: |
| 6968 | flagARCError(); |
| 6969 | diag_available_here = diag::note_arc_field_with_ownership; |
| 6970 | break; |
| 6971 | } |
| 6972 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6973 | } |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6974 | break; |
| 6975 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6976 | case AR_NotYetIntroduced: |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6977 | diag = diag::warn_partial_availability; |
| 6978 | diag_message = diag::warn_partial_message; |
| 6979 | diag_fwdclass_message = diag::warn_partial_fwdclass_message; |
| 6980 | property_note_select = /* partial */ 2; |
| 6981 | available_here_select_kind = /* partial */ 3; |
| 6982 | break; |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6983 | |
| 6984 | case AR_Available: |
| 6985 | llvm_unreachable("Warning for availability of available declaration?"); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6986 | } |
| 6987 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6988 | CharSourceRange UseRange; |
| 6989 | StringRef Replacement; |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6990 | if (K == AR_Deprecated) { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6991 | if (auto attr = D->getAttr<DeprecatedAttr>()) |
| 6992 | Replacement = attr->getReplacement(); |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 6993 | if (auto attr = getAttrForPlatform(S.Context, D)) |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 6994 | Replacement = attr->getReplacement(); |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6995 | |
| 6996 | if (!Replacement.empty()) |
| 6997 | UseRange = |
| 6998 | CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); |
| 6999 | } |
| 7000 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7001 | if (!Message.empty()) { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7002 | S.Diag(Loc, diag_message) << D << Message |
| 7003 | << (UseRange.isValid() ? |
| 7004 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7005 | if (ObjCProperty) |
| 7006 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 7007 | << ObjCProperty->getDeclName() << property_note_select; |
| 7008 | } else if (!UnknownObjCClass) { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7009 | S.Diag(Loc, diag) << D |
| 7010 | << (UseRange.isValid() ? |
| 7011 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7012 | if (ObjCProperty) |
| 7013 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 7014 | << ObjCProperty->getDeclName() << property_note_select; |
| 7015 | } else { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 7016 | S.Diag(Loc, diag_fwdclass_message) << D |
| 7017 | << (UseRange.isValid() ? |
| 7018 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7019 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 7020 | } |
| 7021 | |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 7022 | // The declaration can have multiple availability attributes, we are looking |
| 7023 | // at one of them. |
| 7024 | const AvailabilityAttr *A = getAttrForPlatform(S.Context, D); |
| 7025 | if (A && A->isInherited()) { |
| 7026 | for (const Decl *Redecl = D->getMostRecentDecl(); Redecl; |
| 7027 | Redecl = Redecl->getPreviousDecl()) { |
| 7028 | const AvailabilityAttr *AForRedecl = getAttrForPlatform(S.Context, |
| 7029 | Redecl); |
| 7030 | if (AForRedecl && !AForRedecl->isInherited()) { |
| 7031 | // If D is a declaration with inherited attributes, the note should |
| 7032 | // point to the declaration with actual attributes. |
| 7033 | S.Diag(Redecl->getLocation(), diag_available_here) << D |
| 7034 | << available_here_select_kind; |
| 7035 | break; |
| 7036 | } |
| 7037 | } |
| 7038 | } |
| 7039 | else |
Erich Keane | a32910d | 2017-03-23 18:51:54 +0000 | [diff] [blame] | 7040 | S.Diag(NoteLocation, diag_available_here) |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 7041 | << D << available_here_select_kind; |
| 7042 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7043 | if (K == AR_NotYetIntroduced) |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 7044 | S.Diag(Loc, diag::note_partial_availability_silence) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7045 | } |
| 7046 | |
| 7047 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 7048 | Decl *Ctx) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 7049 | assert(DD.Kind == DelayedDiagnostic::Availability && |
| 7050 | "Expected an availability diagnostic here"); |
| 7051 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7052 | DD.Triggered = true; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 7053 | DoEmitAvailabilityWarning( |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 7054 | S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityDecl(), |
| 7055 | DD.getAvailabilityMessage(), DD.Loc, DD.getUnknownObjCClass(), |
| 7056 | DD.getObjCProperty(), false); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7057 | } |
| 7058 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7059 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 7060 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7061 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7062 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7063 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7064 | // When delaying diagnostics to run in the context of a parsed |
| 7065 | // declaration, we only want to actually emit anything if parsing |
| 7066 | // succeeds. |
| 7067 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7068 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7069 | // We emit all the active diagnostics in this pool or any of its |
| 7070 | // parents. In general, we'll get one pool for the decl spec |
| 7071 | // and a child pool for each declarator; in a decl group like: |
| 7072 | // deprecated_typedef foo, *bar, baz(); |
| 7073 | // only the declarator pops will be passed decls. This is correct; |
| 7074 | // we really do need to consider delayed diagnostics from the decl spec |
| 7075 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7076 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7077 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7078 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7079 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 7080 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 7081 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7082 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7083 | continue; |
| 7084 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 7085 | switch (diag.Kind) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 7086 | case DelayedDiagnostic::Availability: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 7087 | // Don't bother giving deprecation/unavailable diagnostics if |
| 7088 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 7089 | if (!decl->isInvalidDecl()) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 7090 | handleDelayedAvailabilityCheck(*this, diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7091 | break; |
| 7092 | |
| 7093 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7094 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7095 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7096 | |
| 7097 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7098 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7099 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 7100 | } |
| 7101 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 7102 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7103 | } |
| 7104 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 7105 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 7106 | /// been delayed in the current context instead of in the given pool. |
| 7107 | /// Essentially, this just moves them to the current pool. |
| 7108 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 7109 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 7110 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 7111 | curPool->steal(pool); |
| 7112 | } |
| 7113 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7114 | void Sema::EmitAvailabilityWarning(AvailabilityResult AR, |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 7115 | NamedDecl *D, StringRef Message, |
| 7116 | SourceLocation Loc, |
| 7117 | const ObjCInterfaceDecl *UnknownObjCClass, |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame] | 7118 | const ObjCPropertyDecl *ObjCProperty, |
| 7119 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7120 | // Delay if we're currently parsing a declaration. |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 7121 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 7122 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability( |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7123 | AR, Loc, D, UnknownObjCClass, ObjCProperty, Message, |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 7124 | ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7125 | return; |
| 7126 | } |
| 7127 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 7128 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 7129 | DoEmitAvailabilityWarning(*this, AR, Ctx, D, Message, Loc, UnknownObjCClass, |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 7130 | ObjCProperty, ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 7131 | } |
Erik Pilkington | 48c7cc9 | 2016-07-29 17:37:38 +0000 | [diff] [blame] | 7132 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7133 | namespace { |
| 7134 | |
| 7135 | /// \brief This class implements -Wunguarded-availability. |
| 7136 | /// |
| 7137 | /// This is done with a traversal of the AST of a function that makes reference |
| 7138 | /// to a partially available declaration. Whenever we encounter an \c if of the |
| 7139 | /// form: \c if(@available(...)), we use the version from the condition to visit |
| 7140 | /// the then statement. |
| 7141 | class DiagnoseUnguardedAvailability |
| 7142 | : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> { |
| 7143 | typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base; |
| 7144 | |
| 7145 | Sema &SemaRef; |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7146 | Decl *Ctx; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7147 | |
| 7148 | /// Stack of potentially nested 'if (@available(...))'s. |
| 7149 | SmallVector<VersionTuple, 8> AvailabilityStack; |
| 7150 | |
| 7151 | void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range); |
| 7152 | |
| 7153 | public: |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7154 | DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx) |
| 7155 | : SemaRef(SemaRef), Ctx(Ctx) { |
| 7156 | AvailabilityStack.push_back( |
| 7157 | SemaRef.Context.getTargetInfo().getPlatformMinVersion()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7158 | } |
| 7159 | |
| 7160 | void IssueDiagnostics(Stmt *S) { TraverseStmt(S); } |
| 7161 | |
| 7162 | bool TraverseIfStmt(IfStmt *If); |
| 7163 | |
| 7164 | bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) { |
| 7165 | if (ObjCMethodDecl *D = Msg->getMethodDecl()) |
| 7166 | DiagnoseDeclAvailability( |
| 7167 | D, SourceRange(Msg->getSelectorStartLoc(), Msg->getLocEnd())); |
| 7168 | return true; |
| 7169 | } |
| 7170 | |
| 7171 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 7172 | DiagnoseDeclAvailability(DRE->getDecl(), |
| 7173 | SourceRange(DRE->getLocStart(), DRE->getLocEnd())); |
| 7174 | return true; |
| 7175 | } |
| 7176 | |
| 7177 | bool VisitMemberExpr(MemberExpr *ME) { |
| 7178 | DiagnoseDeclAvailability(ME->getMemberDecl(), |
| 7179 | SourceRange(ME->getLocStart(), ME->getLocEnd())); |
| 7180 | return true; |
| 7181 | } |
| 7182 | |
| 7183 | bool VisitTypeLoc(TypeLoc Ty); |
| 7184 | }; |
| 7185 | |
| 7186 | void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability( |
| 7187 | NamedDecl *D, SourceRange Range) { |
| 7188 | |
| 7189 | VersionTuple ContextVersion = AvailabilityStack.back(); |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7190 | if (AvailabilityResult Result = |
| 7191 | SemaRef.ShouldDiagnoseAvailabilityOfDecl(D, nullptr)) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7192 | // All other diagnostic kinds have already been handled in |
| 7193 | // DiagnoseAvailabilityOfDecl. |
| 7194 | if (Result != AR_NotYetIntroduced) |
| 7195 | return; |
| 7196 | |
| 7197 | const AvailabilityAttr *AA = getAttrForPlatform(SemaRef.getASTContext(), D); |
| 7198 | VersionTuple Introduced = AA->getIntroduced(); |
| 7199 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7200 | if (ContextVersion >= Introduced) |
| 7201 | return; |
| 7202 | |
| 7203 | // If the context of this function is less available than D, we should not |
| 7204 | // emit a diagnostic. |
| 7205 | if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx)) |
| 7206 | return; |
| 7207 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7208 | SemaRef.Diag(Range.getBegin(), diag::warn_unguarded_availability) |
| 7209 | << Range << D |
| 7210 | << AvailabilityAttr::getPrettyPlatformName( |
| 7211 | SemaRef.getASTContext().getTargetInfo().getPlatformName()) |
| 7212 | << Introduced.getAsString(); |
| 7213 | |
| 7214 | SemaRef.Diag(D->getLocation(), diag::note_availability_specified_here) |
| 7215 | << D << /* partial */ 3; |
| 7216 | |
| 7217 | // FIXME: Replace this with a fixit diagnostic. |
| 7218 | SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence) |
| 7219 | << Range << D; |
| 7220 | } |
| 7221 | } |
| 7222 | |
| 7223 | bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) { |
| 7224 | const Type *TyPtr = Ty.getTypePtr(); |
| 7225 | SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()}; |
| 7226 | |
| 7227 | if (const TagType *TT = dyn_cast<TagType>(TyPtr)) { |
| 7228 | TagDecl *TD = TT->getDecl(); |
| 7229 | DiagnoseDeclAvailability(TD, Range); |
| 7230 | |
| 7231 | } else if (const TypedefType *TD = dyn_cast<TypedefType>(TyPtr)) { |
| 7232 | TypedefNameDecl *D = TD->getDecl(); |
| 7233 | DiagnoseDeclAvailability(D, Range); |
| 7234 | |
| 7235 | } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) { |
| 7236 | if (NamedDecl *D = ObjCO->getInterface()) |
| 7237 | DiagnoseDeclAvailability(D, Range); |
| 7238 | } |
| 7239 | |
| 7240 | return true; |
| 7241 | } |
| 7242 | |
| 7243 | bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) { |
| 7244 | VersionTuple CondVersion; |
| 7245 | if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) { |
| 7246 | CondVersion = E->getVersion(); |
| 7247 | |
| 7248 | // If we're using the '*' case here or if this check is redundant, then we |
| 7249 | // use the enclosing version to check both branches. |
| 7250 | if (CondVersion.empty() || CondVersion <= AvailabilityStack.back()) |
| 7251 | return Base::TraverseStmt(If->getThen()) && |
| 7252 | Base::TraverseStmt(If->getElse()); |
| 7253 | } else { |
| 7254 | // This isn't an availability checking 'if', we can just continue. |
| 7255 | return Base::TraverseIfStmt(If); |
| 7256 | } |
| 7257 | |
| 7258 | AvailabilityStack.push_back(CondVersion); |
| 7259 | bool ShouldContinue = TraverseStmt(If->getThen()); |
| 7260 | AvailabilityStack.pop_back(); |
| 7261 | |
| 7262 | return ShouldContinue && TraverseStmt(If->getElse()); |
| 7263 | } |
| 7264 | |
| 7265 | } // end anonymous namespace |
| 7266 | |
| 7267 | void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) { |
| 7268 | Stmt *Body = nullptr; |
| 7269 | |
| 7270 | if (auto *FD = D->getAsFunction()) { |
| 7271 | // FIXME: We only examine the pattern decl for availability violations now, |
| 7272 | // but we should also examine instantiated templates. |
| 7273 | if (FD->isTemplateInstantiation()) |
| 7274 | return; |
| 7275 | |
| 7276 | Body = FD->getBody(); |
| 7277 | } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7278 | Body = MD->getBody(); |
Alex Lorenz | 28559ce | 2017-04-26 14:20:02 +0000 | [diff] [blame] | 7279 | else if (auto *BD = dyn_cast<BlockDecl>(D)) |
| 7280 | Body = BD->getBody(); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7281 | |
| 7282 | assert(Body && "Need a body here!"); |
| 7283 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7284 | DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7285 | } |