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 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 221 | /// \brief If Expr is a valid integer constant, get the value of the integer |
| 222 | /// expression and return success or failure. May output an error. |
| 223 | static bool checkUInt32Argument(Sema &S, const AttributeList &Attr, |
| 224 | const Expr *Expr, uint32_t &Val, |
| 225 | unsigned Idx = UINT_MAX) { |
| 226 | llvm::APSInt I(32); |
| 227 | if (Expr->isTypeDependent() || Expr->isValueDependent() || |
| 228 | !Expr->isIntegerConstantExpr(I, S.Context)) { |
| 229 | if (Idx != UINT_MAX) |
| 230 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 231 | << Attr.getName() << Idx << AANT_ArgumentIntegerConstant |
| 232 | << Expr->getSourceRange(); |
| 233 | else |
| 234 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 235 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 236 | << Expr->getSourceRange(); |
| 237 | return false; |
| 238 | } |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 239 | |
| 240 | if (!I.isIntN(32)) { |
Aaron Ballman | 31f4231 | 2014-07-24 14:51:23 +0000 | [diff] [blame] | 241 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 242 | << I.toString(10, false) << 32 << /* Unsigned */ 1; |
Aaron Ballman | adfdde5 | 2014-07-22 14:09:34 +0000 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 246 | Val = (uint32_t)I.getZExtValue(); |
| 247 | return true; |
| 248 | } |
| 249 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 250 | /// \brief Wrapper around checkUInt32Argument, with an extra check to be sure |
| 251 | /// that the result will fit into a regular (signed) int. All args have the same |
| 252 | /// purpose as they do in checkUInt32Argument. |
| 253 | static bool checkPositiveIntArgument(Sema &S, const AttributeList &Attr, |
| 254 | const Expr *Expr, int &Val, |
| 255 | unsigned Idx = UINT_MAX) { |
| 256 | uint32_t UVal; |
| 257 | if (!checkUInt32Argument(S, Attr, Expr, UVal, Idx)) |
| 258 | return false; |
| 259 | |
George Burgess IV | a804957 | 2016-12-22 19:00:31 +0000 | [diff] [blame] | 260 | if (UVal > (uint32_t)std::numeric_limits<int>::max()) { |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 261 | llvm::APSInt I(32); // for toString |
| 262 | I = UVal; |
| 263 | S.Diag(Expr->getExprLoc(), diag::err_ice_too_large) |
| 264 | << I.toString(10, false) << 32 << /* Unsigned */ 0; |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | Val = UVal; |
| 269 | return true; |
| 270 | } |
| 271 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 272 | /// \brief Diagnose mutually exclusive attributes when present on a given |
| 273 | /// declaration. Returns true if diagnosed. |
| 274 | template <typename AttrTy> |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 275 | static bool checkAttrMutualExclusion(Sema &S, Decl *D, SourceRange Range, |
| 276 | IdentifierInfo *Ident) { |
Aaron Ballman | 2cfbc00 | 2014-01-03 16:23:46 +0000 | [diff] [blame] | 277 | if (AttrTy *A = D->getAttr<AttrTy>()) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 278 | S.Diag(Range.getBegin(), diag::err_attributes_are_not_compatible) << Ident |
| 279 | << A; |
| 280 | S.Diag(A->getLocation(), diag::note_conflicting_attribute); |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 281 | return true; |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 286 | /// \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] | 287 | /// instance method D. May output an error. |
| 288 | /// |
| 289 | /// \returns true if IdxExpr is a valid index. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 290 | static bool checkFunctionOrMethodParameterIndex(Sema &S, const Decl *D, |
| 291 | const AttributeList &Attr, |
| 292 | unsigned AttrArgNum, |
| 293 | const Expr *IdxExpr, |
| 294 | uint64_t &Idx) { |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 295 | assert(isFunctionOrMethodOrBlock(D)); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 296 | |
| 297 | // In C++ the implicit 'this' function parameter also counts. |
| 298 | // Parameters are counted from one. |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 299 | bool HP = hasFunctionProto(D); |
| 300 | bool HasImplicitThisParam = isInstanceMethod(D); |
| 301 | bool IV = HP && isFunctionOrMethodVariadic(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 302 | unsigned NumParams = |
| 303 | (HP ? getFunctionOrMethodNumParams(D) : 0) + HasImplicitThisParam; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 304 | |
| 305 | llvm::APSInt IdxInt; |
| 306 | if (IdxExpr->isTypeDependent() || IdxExpr->isValueDependent() || |
| 307 | !IdxExpr->isIntegerConstantExpr(IdxInt, S.Context)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 308 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 309 | << Attr.getName() << AttrArgNum << AANT_ArgumentIntegerConstant |
| 310 | << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 311 | return false; |
| 312 | } |
| 313 | |
| 314 | Idx = IdxInt.getLimitedValue(); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 315 | if (Idx < 1 || (!IV && Idx > NumParams)) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 316 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 317 | << Attr.getName() << AttrArgNum << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 318 | return false; |
| 319 | } |
| 320 | Idx--; // Convert to zero-based. |
| 321 | if (HasImplicitThisParam) { |
| 322 | if (Idx == 0) { |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 323 | S.Diag(Attr.getLoc(), |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 324 | diag::err_attribute_invalid_implicit_this_argument) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 325 | << Attr.getName() << IdxExpr->getSourceRange(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 326 | return false; |
| 327 | } |
| 328 | --Idx; |
| 329 | } |
| 330 | |
| 331 | return true; |
| 332 | } |
| 333 | |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 334 | /// \brief Check if the argument \p ArgNum of \p Attr is a ASCII string literal. |
| 335 | /// If not emit an error and return false. If the argument is an identifier it |
| 336 | /// will emit an error with a fixit hint and treat it as if it was a string |
| 337 | /// literal. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 338 | bool Sema::checkStringLiteralArgumentAttr(const AttributeList &Attr, |
| 339 | unsigned ArgNum, StringRef &Str, |
Tim Northover | a484bc0 | 2013-10-01 14:34:25 +0000 | [diff] [blame] | 340 | SourceLocation *ArgLocation) { |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 341 | // Look for identifiers. If we have one emit a hint to fix it to a literal. |
| 342 | if (Attr.isArgIdent(ArgNum)) { |
| 343 | IdentifierLoc *Loc = Attr.getArgAsIdent(ArgNum); |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 344 | Diag(Loc->Loc, diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 345 | << Attr.getName() << AANT_ArgumentString |
| 346 | << FixItHint::CreateInsertion(Loc->Loc, "\"") |
Craig Topper | 07fa176 | 2015-11-15 02:31:46 +0000 | [diff] [blame] | 347 | << FixItHint::CreateInsertion(getLocForEndOfToken(Loc->Loc), "\""); |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 348 | Str = Loc->Ident->getName(); |
| 349 | if (ArgLocation) |
| 350 | *ArgLocation = Loc->Loc; |
| 351 | return true; |
| 352 | } |
| 353 | |
| 354 | // Now check for an actual string literal. |
| 355 | Expr *ArgExpr = Attr.getArgAsExpr(ArgNum); |
| 356 | StringLiteral *Literal = dyn_cast<StringLiteral>(ArgExpr->IgnoreParenCasts()); |
| 357 | if (ArgLocation) |
| 358 | *ArgLocation = ArgExpr->getLocStart(); |
| 359 | |
| 360 | if (!Literal || !Literal->isAscii()) { |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 361 | Diag(ArgExpr->getLocStart(), diag::err_attribute_argument_type) |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 362 | << Attr.getName() << AANT_ArgumentString; |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | Str = Literal->getString(); |
| 367 | return true; |
| 368 | } |
| 369 | |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 370 | /// \brief Applies the given attribute to the Decl without performing any |
| 371 | /// additional semantic checking. |
| 372 | template <typename AttrType> |
| 373 | static void handleSimpleAttribute(Sema &S, Decl *D, |
| 374 | const AttributeList &Attr) { |
| 375 | D->addAttr(::new (S.Context) AttrType(Attr.getRange(), S.Context, |
| 376 | Attr.getAttributeSpellingListIndex())); |
| 377 | } |
| 378 | |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 379 | template <typename AttrType> |
| 380 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
| 381 | const AttributeList &Attr) { |
| 382 | handleSimpleAttribute<AttrType>(S, D, Attr); |
| 383 | } |
| 384 | |
| 385 | /// \brief Applies the given attribute to the Decl so long as the Decl doesn't |
| 386 | /// already have one of the given incompatible attributes. |
| 387 | template <typename AttrType, typename IncompatibleAttrType, |
| 388 | typename... IncompatibleAttrTypes> |
| 389 | static void handleSimpleAttributeWithExclusions(Sema &S, Decl *D, |
| 390 | const AttributeList &Attr) { |
| 391 | if (checkAttrMutualExclusion<IncompatibleAttrType>(S, D, Attr.getRange(), |
| 392 | Attr.getName())) |
| 393 | return; |
| 394 | handleSimpleAttributeWithExclusions<AttrType, IncompatibleAttrTypes...>(S, D, |
| 395 | Attr); |
| 396 | } |
| 397 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 398 | /// \brief Check if the passed-in expression is of type int or bool. |
| 399 | static bool isIntOrBool(Expr *Exp) { |
| 400 | QualType QT = Exp->getType(); |
| 401 | return QT->isBooleanType() || QT->isIntegerType(); |
| 402 | } |
| 403 | |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 404 | |
| 405 | // Check to see if the type is a smart pointer of some kind. We assume |
| 406 | // it's a smart pointer if it defines both operator-> and operator*. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 407 | static bool threadSafetyCheckIsSmartPointer(Sema &S, const RecordType* RT) { |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 408 | DeclContextLookupResult Res1 = RT->getDecl()->lookup( |
| 409 | S.Context.DeclarationNames.getCXXOperatorName(OO_Star)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 410 | if (Res1.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 411 | return false; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 412 | |
Richard Smith | cf4bdde | 2015-02-21 02:45:19 +0000 | [diff] [blame] | 413 | DeclContextLookupResult Res2 = RT->getDecl()->lookup( |
| 414 | S.Context.DeclarationNames.getCXXOperatorName(OO_Arrow)); |
David Blaikie | ff7d47a | 2012-12-19 00:45:41 +0000 | [diff] [blame] | 415 | if (Res2.empty()) |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 416 | return false; |
| 417 | |
| 418 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 421 | /// \brief Check if passed in Decl is a pointer type. |
| 422 | /// Note that this function may produce an error message. |
| 423 | /// \return true if the Decl is a pointer type; false otherwise |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 424 | static bool threadSafetyCheckIsPointer(Sema &S, const Decl *D, |
| 425 | const AttributeList &Attr) { |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 426 | const ValueDecl *vd = cast<ValueDecl>(D); |
| 427 | QualType QT = vd->getType(); |
| 428 | if (QT->isAnyPointerType()) |
| 429 | return true; |
| 430 | |
| 431 | if (const RecordType *RT = QT->getAs<RecordType>()) { |
| 432 | // If it's an incomplete type, it could be a smart pointer; skip it. |
| 433 | // (We don't want to force template instantiation if we can avoid it, |
| 434 | // since that would alter the order in which templates are instantiated.) |
| 435 | if (RT->isIncompleteType()) |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 436 | return true; |
DeLesley Hutchins | e09be23 | 2012-04-23 18:39:55 +0000 | [diff] [blame] | 437 | |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 438 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
| 439 | return true; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 440 | } |
Aaron Ballman | 553e681 | 2013-12-26 14:54:11 +0000 | [diff] [blame] | 441 | |
| 442 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_pointer) |
Aaron Ballman | 6828945 | 2013-12-26 15:06:01 +0000 | [diff] [blame] | 443 | << Attr.getName() << QT; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 444 | return false; |
| 445 | } |
| 446 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 447 | /// \brief Checks that the passed in QualType either is of RecordType or points |
| 448 | /// to RecordType. Returns the relevant RecordType, null if it does not exit. |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 449 | static const RecordType *getRecordType(QualType QT) { |
| 450 | if (const RecordType *RT = QT->getAs<RecordType>()) |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 451 | return RT; |
Benjamin Kramer | 56b675f | 2011-08-19 04:18:11 +0000 | [diff] [blame] | 452 | |
| 453 | // Now check if we point to record type. |
| 454 | if (const PointerType *PT = QT->getAs<PointerType>()) |
| 455 | return PT->getPointeeType()->getAs<RecordType>(); |
| 456 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 457 | return nullptr; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 460 | static bool checkRecordTypeForCapability(Sema &S, QualType Ty) { |
DeLesley Hutchins | 481d5ab | 2012-04-06 20:02:30 +0000 | [diff] [blame] | 461 | const RecordType *RT = getRecordType(Ty); |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 462 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 463 | if (!RT) |
| 464 | return false; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 465 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 466 | // 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] | 467 | if (RT->isIncompleteType()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 468 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 469 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 470 | // Allow smart pointers to be used as capability objects. |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 471 | // FIXME -- Check the type that the smart pointer points to. |
| 472 | if (threadSafetyCheckIsSmartPointer(S, RT)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 473 | return true; |
DeLesley Hutchins | 90ff468 | 2012-05-02 22:18:42 +0000 | [diff] [blame] | 474 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 475 | // Check if the record itself has a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 476 | RecordDecl *RD = RT->getDecl(); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 477 | if (RD->hasAttr<CapabilityAttr>()) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 478 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 479 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 480 | // Else check if any base classes have a capability. |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 481 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 482 | CXXBasePaths BPaths(false, false); |
Benjamin Kramer | 6e4f6e1 | 2015-07-25 15:07:25 +0000 | [diff] [blame] | 483 | if (CRD->lookupInBases([](const CXXBaseSpecifier *BS, CXXBasePath &) { |
| 484 | const auto *Type = BS->getType()->getAs<RecordType>(); |
| 485 | return Type->getDecl()->hasAttr<CapabilityAttr>(); |
| 486 | }, BPaths)) |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 487 | return true; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 488 | } |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 489 | return false; |
| 490 | } |
| 491 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 492 | static bool checkTypedefTypeForCapability(QualType Ty) { |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 493 | const auto *TD = Ty->getAs<TypedefType>(); |
| 494 | if (!TD) |
| 495 | return false; |
| 496 | |
| 497 | TypedefNameDecl *TN = TD->getDecl(); |
| 498 | if (!TN) |
| 499 | return false; |
| 500 | |
| 501 | return TN->hasAttr<CapabilityAttr>(); |
| 502 | } |
| 503 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 504 | static bool typeHasCapability(Sema &S, QualType Ty) { |
| 505 | if (checkTypedefTypeForCapability(Ty)) |
| 506 | return true; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 507 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 508 | if (checkRecordTypeForCapability(S, Ty)) |
| 509 | return true; |
DeLesley Hutchins | 5ff430c | 2012-05-04 16:28:38 +0000 | [diff] [blame] | 510 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 511 | return false; |
| 512 | } |
| 513 | |
| 514 | static bool isCapabilityExpr(Sema &S, const Expr *Ex) { |
| 515 | // Capability expressions are simple expressions involving the boolean logic |
| 516 | // operators &&, || or !, a simple DeclRefExpr, CastExpr or a ParenExpr. Once |
| 517 | // a DeclRefExpr is found, its type should be checked to determine whether it |
| 518 | // is a capability or not. |
| 519 | |
| 520 | if (const auto *E = dyn_cast<DeclRefExpr>(Ex)) |
| 521 | return typeHasCapability(S, E->getType()); |
| 522 | else if (const auto *E = dyn_cast<CastExpr>(Ex)) |
| 523 | return isCapabilityExpr(S, E->getSubExpr()); |
| 524 | else if (const auto *E = dyn_cast<ParenExpr>(Ex)) |
| 525 | return isCapabilityExpr(S, E->getSubExpr()); |
| 526 | else if (const auto *E = dyn_cast<UnaryOperator>(Ex)) { |
| 527 | if (E->getOpcode() == UO_LNot) |
| 528 | return isCapabilityExpr(S, E->getSubExpr()); |
| 529 | return false; |
| 530 | } else if (const auto *E = dyn_cast<BinaryOperator>(Ex)) { |
| 531 | if (E->getOpcode() == BO_LAnd || E->getOpcode() == BO_LOr) |
| 532 | return isCapabilityExpr(S, E->getLHS()) && |
| 533 | isCapabilityExpr(S, E->getRHS()); |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | return false; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 540 | /// \brief Checks that all attribute arguments, starting from Sidx, resolve to |
| 541 | /// a capability object. |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 542 | /// \param Sidx The attribute argument index to start checking with. |
| 543 | /// \param ParamIdxOk Whether an argument can be indexing into a function |
| 544 | /// parameter list. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 545 | static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D, |
| 546 | const AttributeList &Attr, |
| 547 | SmallVectorImpl<Expr *> &Args, |
| 548 | int Sidx = 0, |
| 549 | bool ParamIdxOk = false) { |
| 550 | for (unsigned Idx = Sidx; Idx < Attr.getNumArgs(); ++Idx) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 551 | Expr *ArgExp = Attr.getArgAsExpr(Idx); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 552 | |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 553 | if (ArgExp->isTypeDependent()) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 554 | // FIXME -- need to check this again on template instantiation |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 555 | Args.push_back(ArgExp); |
| 556 | continue; |
| 557 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 558 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 559 | if (StringLiteral *StrLit = dyn_cast<StringLiteral>(ArgExp)) { |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 560 | if (StrLit->getLength() == 0 || |
Benjamin Kramer | ca9fe14 | 2013-09-13 16:30:12 +0000 | [diff] [blame] | 561 | (StrLit->isAscii() && StrLit->getString() == StringRef("*"))) { |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 562 | // Pass empty strings to the analyzer without warnings. |
DeLesley Hutchins | a5a00e8 | 2012-09-07 17:34:53 +0000 | [diff] [blame] | 563 | // Treat "*" as the universal lock. |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 564 | Args.push_back(ArgExp); |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 565 | continue; |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 566 | } |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 567 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 568 | // We allow constant strings to be used as a placeholder for expressions |
| 569 | // that are not valid C++ syntax, but warn that they are ignored. |
| 570 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_ignored) << |
| 571 | Attr.getName(); |
DeLesley Hutchins | 3c3d57b | 2012-08-31 21:57:32 +0000 | [diff] [blame] | 572 | Args.push_back(ArgExp); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 573 | continue; |
| 574 | } |
| 575 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 576 | QualType ArgTy = ArgExp->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 577 | |
DeLesley Hutchins | 70b5e8e | 2012-04-23 16:45:01 +0000 | [diff] [blame] | 578 | // A pointer to member expression of the form &MyClass::mu is treated |
| 579 | // specially -- we need to look at the type of the member. |
| 580 | if (UnaryOperator *UOp = dyn_cast<UnaryOperator>(ArgExp)) |
| 581 | if (UOp->getOpcode() == UO_AddrOf) |
| 582 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(UOp->getSubExpr())) |
| 583 | if (DRE->getDecl()->isCXXInstanceMember()) |
| 584 | ArgTy = DRE->getDecl()->getType(); |
| 585 | |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 586 | // 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] | 587 | const RecordType *RT = getRecordType(ArgTy); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 588 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 589 | // Now check if we index into a record type function param. |
| 590 | if(!RT && ParamIdxOk) { |
| 591 | FunctionDecl *FD = dyn_cast<FunctionDecl>(D); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 592 | IntegerLiteral *IL = dyn_cast<IntegerLiteral>(ArgExp); |
| 593 | if(FD && IL) { |
| 594 | unsigned int NumParams = FD->getNumParams(); |
| 595 | llvm::APInt ArgValue = IL->getValue(); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 596 | uint64_t ParamIdxFromOne = ArgValue.getZExtValue(); |
| 597 | uint64_t ParamIdxFromZero = ParamIdxFromOne - 1; |
| 598 | if(!ArgValue.isStrictlyPositive() || ParamIdxFromOne > NumParams) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 599 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_range) |
| 600 | << Attr.getName() << Idx + 1 << NumParams; |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 601 | continue; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 602 | } |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 603 | ArgTy = FD->getParamDecl(ParamIdxFromZero)->getType(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | |
Aaron Ballman | 7605072 | 2014-04-04 15:13:57 +0000 | [diff] [blame] | 607 | // If the type does not have a capability, see if the components of the |
| 608 | // expression have capabilities. This allows for writing C code where the |
| 609 | // capability may be on the type, and the expression is a capability |
| 610 | // boolean logic expression. Eg) requires_capability(A || B && !C) |
| 611 | if (!typeHasCapability(S, ArgTy) && !isCapabilityExpr(S, ArgExp)) |
| 612 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_argument_not_lockable) |
| 613 | << Attr.getName() << ArgTy; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 614 | |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 615 | Args.push_back(ArgExp); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 616 | } |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 619 | //===----------------------------------------------------------------------===// |
Chris Lattner | 58418ff | 2008-06-29 00:16:31 +0000 | [diff] [blame] | 620 | // Attribute Implementations |
| 621 | //===----------------------------------------------------------------------===// |
| 622 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 623 | static void handlePtGuardedVarAttr(Sema &S, Decl *D, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 624 | const AttributeList &Attr) { |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 625 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 626 | return; |
| 627 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 628 | D->addAttr(::new (S.Context) |
| 629 | PtGuardedVarAttr(Attr.getRange(), S.Context, |
| 630 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 631 | } |
| 632 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 633 | static bool checkGuardedByAttrCommon(Sema &S, Decl *D, |
| 634 | const AttributeList &Attr, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 635 | Expr* &Arg) { |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 636 | SmallVector<Expr*, 1> Args; |
| 637 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 638 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 639 | unsigned Size = Args.size(); |
| 640 | if (Size != 1) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 641 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 642 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 643 | Arg = Args[0]; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 644 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 645 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 648 | static void handleGuardedByAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 649 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 650 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 651 | return; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 652 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 653 | D->addAttr(::new (S.Context) GuardedByAttr(Attr.getRange(), S.Context, Arg, |
| 654 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 657 | static void handlePtGuardedByAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 658 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 659 | Expr *Arg = nullptr; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 660 | if (!checkGuardedByAttrCommon(S, D, Attr, Arg)) |
| 661 | return; |
| 662 | |
| 663 | if (!threadSafetyCheckIsPointer(S, D, Attr)) |
| 664 | return; |
| 665 | |
| 666 | D->addAttr(::new (S.Context) PtGuardedByAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 667 | S.Context, Arg, |
| 668 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 671 | static bool checkAcquireOrderAttrCommon(Sema &S, Decl *D, |
| 672 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 673 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 674 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 675 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 676 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 677 | // Check that this attribute only applies to lockable types. |
Aaron Ballman | e61b8b8 | 2013-12-02 15:02:49 +0000 | [diff] [blame] | 678 | QualType QT = cast<ValueDecl>(D)->getType(); |
DeLesley Hutchins | 2b504dc | 2015-09-29 16:24:18 +0000 | [diff] [blame] | 679 | if (!QT->isDependentType() && !typeHasCapability(S, QT)) { |
| 680 | S.Diag(Attr.getLoc(), diag::warn_thread_attribute_decl_not_lockable) |
| 681 | << Attr.getName(); |
| 682 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 683 | } |
| 684 | |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 685 | // Check that all arguments are lockable objects. |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 686 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Dmitri Gribenko | 8f8930f | 2013-05-03 15:05:50 +0000 | [diff] [blame] | 687 | if (Args.empty()) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 688 | return false; |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 689 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 690 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 691 | } |
| 692 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 693 | static void handleAcquiredAfterAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 694 | const AttributeList &Attr) { |
| 695 | SmallVector<Expr*, 1> Args; |
| 696 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 697 | return; |
| 698 | |
| 699 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 700 | D->addAttr(::new (S.Context) |
| 701 | AcquiredAfterAttr(Attr.getRange(), S.Context, |
| 702 | StartArg, Args.size(), |
| 703 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 706 | static void handleAcquiredBeforeAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 707 | const AttributeList &Attr) { |
| 708 | SmallVector<Expr*, 1> Args; |
| 709 | if (!checkAcquireOrderAttrCommon(S, D, Attr, Args)) |
| 710 | return; |
| 711 | |
| 712 | Expr **StartArg = &Args[0]; |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 713 | D->addAttr(::new (S.Context) |
| 714 | AcquiredBeforeAttr(Attr.getRange(), S.Context, |
| 715 | StartArg, Args.size(), |
| 716 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 719 | static bool checkLockFunAttrCommon(Sema &S, Decl *D, |
| 720 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 721 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 722 | // zero or more arguments ok |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 723 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 724 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, /*ParamIdxOk=*/true); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 725 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 726 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 727 | } |
| 728 | |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 729 | static void handleAssertSharedLockAttr(Sema &S, Decl *D, |
| 730 | const AttributeList &Attr) { |
| 731 | SmallVector<Expr*, 1> Args; |
| 732 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 733 | return; |
| 734 | |
| 735 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 736 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 737 | D->addAttr(::new (S.Context) |
| 738 | AssertSharedLockAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 739 | Attr.getAttributeSpellingListIndex())); |
| 740 | } |
| 741 | |
| 742 | static void handleAssertExclusiveLockAttr(Sema &S, Decl *D, |
| 743 | const AttributeList &Attr) { |
| 744 | SmallVector<Expr*, 1> Args; |
| 745 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
| 746 | return; |
| 747 | |
| 748 | unsigned Size = Args.size(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 749 | Expr **StartArg = Size == 0 ? nullptr : &Args[0]; |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 750 | D->addAttr(::new (S.Context) |
| 751 | AssertExclusiveLockAttr(Attr.getRange(), S.Context, |
| 752 | StartArg, Size, |
| 753 | Attr.getAttributeSpellingListIndex())); |
| 754 | } |
| 755 | |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 756 | /// \brief Checks to be sure that the given parameter number is inbounds, and is |
| 757 | /// an some integral type. Will emit appropriate diagnostics if this returns |
| 758 | /// false. |
| 759 | /// |
| 760 | /// FuncParamNo is expected to be from the user, so is base-1. AttrArgNo is used |
| 761 | /// to actually retrieve the argument, so it's base-0. |
| 762 | static bool checkParamIsIntegerType(Sema &S, const FunctionDecl *FD, |
| 763 | const AttributeList &Attr, |
| 764 | unsigned FuncParamNo, unsigned AttrArgNo) { |
| 765 | assert(Attr.isArgExpr(AttrArgNo) && "Expected expression argument"); |
| 766 | uint64_t Idx; |
| 767 | if (!checkFunctionOrMethodParameterIndex(S, FD, Attr, FuncParamNo, |
| 768 | Attr.getArgAsExpr(AttrArgNo), Idx)) |
| 769 | return false; |
| 770 | |
| 771 | const ParmVarDecl *Param = FD->getParamDecl(Idx); |
| 772 | if (!Param->getType()->isIntegerType() && !Param->getType()->isCharType()) { |
| 773 | SourceLocation SrcLoc = Attr.getArgAsExpr(AttrArgNo)->getLocStart(); |
| 774 | S.Diag(SrcLoc, diag::err_attribute_integers_only) |
| 775 | << Attr.getName() << Param->getSourceRange(); |
| 776 | return false; |
| 777 | } |
| 778 | return true; |
| 779 | } |
| 780 | |
| 781 | static void handleAllocSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 782 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1) || |
| 783 | !checkAttributeAtMostNumArgs(S, Attr, 2)) |
| 784 | return; |
| 785 | |
| 786 | const auto *FD = cast<FunctionDecl>(D); |
| 787 | if (!FD->getReturnType()->isPointerType()) { |
| 788 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 789 | << Attr.getName(); |
| 790 | return; |
| 791 | } |
| 792 | |
| 793 | const Expr *SizeExpr = Attr.getArgAsExpr(0); |
| 794 | int SizeArgNo; |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame^] | 795 | // Parameter indices are 1-indexed, hence Index=1 |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 796 | if (!checkPositiveIntArgument(S, Attr, SizeExpr, SizeArgNo, /*Index=*/1)) |
| 797 | return; |
| 798 | |
| 799 | if (!checkParamIsIntegerType(S, FD, Attr, SizeArgNo, /*AttrArgNo=*/0)) |
| 800 | return; |
| 801 | |
| 802 | // Args are 1-indexed, so 0 implies that the arg was not present |
| 803 | int NumberArgNo = 0; |
| 804 | if (Attr.getNumArgs() == 2) { |
| 805 | const Expr *NumberExpr = Attr.getArgAsExpr(1); |
Simon Pilgrim | 27cc054 | 2017-02-15 15:12:06 +0000 | [diff] [blame^] | 806 | // Parameter indices are 1-based, hence Index=2 |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 807 | if (!checkPositiveIntArgument(S, Attr, NumberExpr, NumberArgNo, |
| 808 | /*Index=*/2)) |
| 809 | return; |
| 810 | |
| 811 | if (!checkParamIsIntegerType(S, FD, Attr, NumberArgNo, /*AttrArgNo=*/1)) |
| 812 | return; |
| 813 | } |
| 814 | |
| 815 | D->addAttr(::new (S.Context) AllocSizeAttr( |
| 816 | Attr.getRange(), S.Context, SizeArgNo, NumberArgNo, |
| 817 | Attr.getAttributeSpellingListIndex())); |
| 818 | } |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 819 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 820 | static bool checkTryLockFunAttrCommon(Sema &S, Decl *D, |
| 821 | const AttributeList &Attr, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 822 | SmallVectorImpl<Expr *> &Args) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 823 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 824 | return false; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 825 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 826 | if (!isIntOrBool(Attr.getArgAsExpr(0))) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 827 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 828 | << Attr.getName() << 1 << AANT_ArgumentIntOrBool; |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 829 | return false; |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | // check that all arguments are lockable objects |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 833 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 1); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 834 | |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 835 | return true; |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 836 | } |
| 837 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 838 | static void handleSharedTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 839 | const AttributeList &Attr) { |
| 840 | SmallVector<Expr*, 2> Args; |
| 841 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 842 | return; |
| 843 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 844 | D->addAttr(::new (S.Context) |
| 845 | SharedTrylockFunctionAttr(Attr.getRange(), S.Context, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 846 | Attr.getArgAsExpr(0), |
| 847 | Args.data(), Args.size(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 848 | Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Michael Han | a9171bc | 2012-08-03 17:40:43 +0000 | [diff] [blame] | 851 | static void handleExclusiveTrylockFunctionAttr(Sema &S, Decl *D, |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 852 | const AttributeList &Attr) { |
| 853 | SmallVector<Expr*, 2> Args; |
| 854 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 855 | return; |
| 856 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 857 | D->addAttr(::new (S.Context) ExclusiveTrylockFunctionAttr( |
| 858 | Attr.getRange(), S.Context, Attr.getArgAsExpr(0), Args.data(), |
| 859 | Args.size(), Attr.getAttributeSpellingListIndex())); |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 862 | static void handleLockReturnedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 863 | const AttributeList &Attr) { |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 864 | // check that the argument is lockable object |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 865 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 866 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
DeLesley Hutchins | d96b46a | 2012-05-02 17:38:37 +0000 | [diff] [blame] | 867 | unsigned Size = Args.size(); |
| 868 | if (Size == 0) |
| 869 | return; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 870 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 871 | D->addAttr(::new (S.Context) |
| 872 | LockReturnedAttr(Attr.getRange(), S.Context, Args[0], |
| 873 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | static void handleLocksExcludedAttr(Sema &S, Decl *D, |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 877 | const AttributeList &Attr) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 878 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 879 | return; |
| 880 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 881 | // check that all arguments are lockable objects |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 882 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 883 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 884 | unsigned Size = Args.size(); |
DeLesley Hutchins | 8d11c79 | 2012-04-19 16:10:44 +0000 | [diff] [blame] | 885 | if (Size == 0) |
| 886 | return; |
| 887 | Expr **StartArg = &Args[0]; |
Caitlin Sadowski | afbbd8e | 2011-08-23 18:46:34 +0000 | [diff] [blame] | 888 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 889 | D->addAttr(::new (S.Context) |
| 890 | LocksExcludedAttr(Attr.getRange(), S.Context, StartArg, Size, |
| 891 | Attr.getAttributeSpellingListIndex())); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 892 | } |
| 893 | |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 894 | static bool checkFunctionConditionAttr(Sema &S, Decl *D, |
| 895 | const AttributeList &Attr, |
| 896 | Expr *&Cond, StringRef &Msg) { |
| 897 | Cond = Attr.getArgAsExpr(0); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 898 | if (!Cond->isTypeDependent()) { |
| 899 | ExprResult Converted = S.PerformContextuallyConvertToBool(Cond); |
| 900 | if (Converted.isInvalid()) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 901 | return false; |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 902 | Cond = Converted.get(); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 903 | } |
| 904 | |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 905 | if (!S.checkStringLiteralArgumentAttr(Attr, 1, Msg)) |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 906 | return false; |
| 907 | |
| 908 | if (Msg.empty()) |
| 909 | Msg = "<no message provided>"; |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 910 | |
| 911 | SmallVector<PartialDiagnosticAt, 8> Diags; |
| 912 | if (!Cond->isValueDependent() && |
| 913 | !Expr::isPotentialConstantExprUnevaluated(Cond, cast<FunctionDecl>(D), |
| 914 | Diags)) { |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 915 | S.Diag(Attr.getLoc(), diag::err_attr_cond_never_constant_expr) |
| 916 | << Attr.getName(); |
George Burgess IV | 0d54653 | 2016-11-10 21:47:12 +0000 | [diff] [blame] | 917 | for (const PartialDiagnosticAt &PDiag : Diags) |
| 918 | S.Diag(PDiag.first, PDiag.second); |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 919 | return false; |
| 920 | } |
| 921 | return true; |
| 922 | } |
| 923 | |
| 924 | static void handleEnableIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 925 | S.Diag(Attr.getLoc(), diag::ext_clang_enable_if); |
| 926 | |
| 927 | Expr *Cond; |
| 928 | StringRef Msg; |
| 929 | if (checkFunctionConditionAttr(S, D, Attr, Cond, Msg)) |
| 930 | D->addAttr(::new (S.Context) |
| 931 | EnableIfAttr(Attr.getRange(), S.Context, Cond, Msg, |
| 932 | Attr.getAttributeSpellingListIndex())); |
| 933 | } |
| 934 | |
| 935 | namespace { |
| 936 | /// Determines if a given Expr references any of the given function's |
| 937 | /// ParmVarDecls, or the function's implicit `this` parameter (if applicable). |
| 938 | class ArgumentDependenceChecker |
| 939 | : public RecursiveASTVisitor<ArgumentDependenceChecker> { |
| 940 | #ifndef NDEBUG |
| 941 | const CXXRecordDecl *ClassType; |
| 942 | #endif |
| 943 | llvm::SmallPtrSet<const ParmVarDecl *, 16> Parms; |
| 944 | bool Result; |
| 945 | |
| 946 | public: |
| 947 | ArgumentDependenceChecker(const FunctionDecl *FD) { |
| 948 | #ifndef NDEBUG |
| 949 | if (const auto *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 950 | ClassType = MD->getParent(); |
| 951 | else |
| 952 | ClassType = nullptr; |
| 953 | #endif |
| 954 | Parms.insert(FD->param_begin(), FD->param_end()); |
| 955 | } |
| 956 | |
| 957 | bool referencesArgs(Expr *E) { |
| 958 | Result = false; |
| 959 | TraverseStmt(E); |
| 960 | return Result; |
| 961 | } |
| 962 | |
| 963 | bool VisitCXXThisExpr(CXXThisExpr *E) { |
| 964 | assert(E->getType()->getPointeeCXXRecordDecl() == ClassType && |
| 965 | "`this` doesn't refer to the enclosing class?"); |
| 966 | Result = true; |
| 967 | return false; |
| 968 | } |
| 969 | |
| 970 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 971 | if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) |
| 972 | if (Parms.count(PVD)) { |
| 973 | Result = true; |
| 974 | return false; |
| 975 | } |
| 976 | return true; |
| 977 | } |
| 978 | }; |
| 979 | } |
| 980 | |
| 981 | static void handleDiagnoseIfAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 982 | S.Diag(Attr.getLoc(), diag::ext_clang_diagnose_if); |
| 983 | |
| 984 | Expr *Cond; |
| 985 | StringRef Msg; |
| 986 | if (!checkFunctionConditionAttr(S, D, Attr, Cond, Msg)) |
| 987 | return; |
| 988 | |
| 989 | StringRef DiagTypeStr; |
| 990 | if (!S.checkStringLiteralArgumentAttr(Attr, 2, DiagTypeStr)) |
| 991 | return; |
| 992 | |
| 993 | DiagnoseIfAttr::DiagnosticType DiagType; |
| 994 | if (!DiagnoseIfAttr::ConvertStrToDiagnosticType(DiagTypeStr, DiagType)) { |
| 995 | S.Diag(Attr.getArgAsExpr(2)->getLocStart(), |
| 996 | diag::err_diagnose_if_invalid_diagnostic_type); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 997 | return; |
| 998 | } |
| 999 | |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 1000 | auto *FD = cast<FunctionDecl>(D); |
| 1001 | bool ArgDependent = ArgumentDependenceChecker(FD).referencesArgs(Cond); |
| 1002 | D->addAttr(::new (S.Context) DiagnoseIfAttr( |
| 1003 | Attr.getRange(), S.Context, Cond, Msg, DiagType, ArgDependent, FD, |
| 1004 | Attr.getAttributeSpellingListIndex())); |
Nick Lewycky | 35a6ef4 | 2014-01-11 02:50:57 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1007 | static void handlePassObjectSizeAttr(Sema &S, Decl *D, |
| 1008 | const AttributeList &Attr) { |
| 1009 | if (D->hasAttr<PassObjectSizeAttr>()) { |
| 1010 | S.Diag(D->getLocStart(), diag::err_attribute_only_once_per_parameter) |
| 1011 | << Attr.getName(); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | Expr *E = Attr.getArgAsExpr(0); |
| 1016 | uint32_t Type; |
| 1017 | if (!checkUInt32Argument(S, Attr, E, Type, /*Idx=*/1)) |
| 1018 | return; |
| 1019 | |
| 1020 | // pass_object_size's argument is passed in as the second argument of |
| 1021 | // __builtin_object_size. So, it has the same constraints as that second |
| 1022 | // argument; namely, it must be in the range [0, 3]. |
| 1023 | if (Type > 3) { |
| 1024 | S.Diag(E->getLocStart(), diag::err_attribute_argument_outof_range) |
| 1025 | << Attr.getName() << 0 << 3 << E->getSourceRange(); |
| 1026 | return; |
| 1027 | } |
| 1028 | |
| 1029 | // pass_object_size is only supported on constant pointer parameters; as a |
| 1030 | // kindness to users, we allow the parameter to be non-const for declarations. |
| 1031 | // At this point, we have no clue if `D` belongs to a function declaration or |
| 1032 | // definition, so we defer the constness check until later. |
| 1033 | if (!cast<ParmVarDecl>(D)->getType()->isPointerType()) { |
| 1034 | S.Diag(D->getLocStart(), diag::err_attribute_pointers_only) |
| 1035 | << Attr.getName() << 1; |
| 1036 | return; |
| 1037 | } |
| 1038 | |
| 1039 | D->addAttr(::new (S.Context) |
| 1040 | PassObjectSizeAttr(Attr.getRange(), S.Context, (int)Type, |
| 1041 | Attr.getAttributeSpellingListIndex())); |
| 1042 | } |
| 1043 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1044 | static void handleConsumableAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1045 | ConsumableAttr::ConsumedState DefaultState; |
| 1046 | |
| 1047 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1048 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1049 | if (!ConsumableAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1050 | DefaultState)) { |
| 1051 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1052 | << Attr.getName() << IL->Ident; |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1053 | return; |
| 1054 | } |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1055 | } else { |
| 1056 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 1057 | << Attr.getName() << AANT_ArgumentIdentifier; |
| 1058 | return; |
| 1059 | } |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1060 | |
| 1061 | D->addAttr(::new (S.Context) |
David Blaikie | 16f76d2 | 2013-09-06 01:28:43 +0000 | [diff] [blame] | 1062 | ConsumableAttr(Attr.getRange(), S.Context, DefaultState, |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1063 | Attr.getAttributeSpellingListIndex())); |
| 1064 | } |
| 1065 | |
| 1066 | static bool checkForConsumableClass(Sema &S, const CXXMethodDecl *MD, |
| 1067 | const AttributeList &Attr) { |
| 1068 | ASTContext &CurrContext = S.getASTContext(); |
| 1069 | QualType ThisType = MD->getThisType(CurrContext)->getPointeeType(); |
| 1070 | |
| 1071 | if (const CXXRecordDecl *RD = ThisType->getAsCXXRecordDecl()) { |
| 1072 | if (!RD->hasAttr<ConsumableAttr>()) { |
| 1073 | S.Diag(Attr.getLoc(), diag::warn_attr_on_unconsumable_class) << |
| 1074 | RD->getNameAsString(); |
| 1075 | |
| 1076 | return false; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | return true; |
| 1081 | } |
| 1082 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1083 | static void handleCallableWhenAttr(Sema &S, Decl *D, |
| 1084 | const AttributeList &Attr) { |
Aaron Ballman | dbd586f | 2013-10-14 23:26:04 +0000 | [diff] [blame] | 1085 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 1086 | return; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1087 | |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 1088 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1089 | return; |
| 1090 | |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1091 | SmallVector<CallableWhenAttr::ConsumedState, 3> States; |
| 1092 | for (unsigned ArgIndex = 0; ArgIndex < Attr.getNumArgs(); ++ArgIndex) { |
| 1093 | CallableWhenAttr::ConsumedState CallableState; |
| 1094 | |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1095 | StringRef StateString; |
| 1096 | SourceLocation Loc; |
Aaron Ballman | 55ef151 | 2014-12-19 16:42:04 +0000 | [diff] [blame] | 1097 | if (Attr.isArgIdent(ArgIndex)) { |
| 1098 | IdentifierLoc *Ident = Attr.getArgAsIdent(ArgIndex); |
| 1099 | StateString = Ident->Ident->getName(); |
| 1100 | Loc = Ident->Loc; |
| 1101 | } else { |
| 1102 | if (!S.checkStringLiteralArgumentAttr(Attr, ArgIndex, StateString, &Loc)) |
| 1103 | return; |
| 1104 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1105 | |
| 1106 | if (!CallableWhenAttr::ConvertStrToConsumedState(StateString, |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1107 | CallableState)) { |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1108 | S.Diag(Loc, diag::warn_attribute_type_not_supported) |
| 1109 | << Attr.getName() << StateString; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1110 | return; |
| 1111 | } |
Aaron Ballman | 4c9b7dc | 2013-10-05 22:45:34 +0000 | [diff] [blame] | 1112 | |
| 1113 | States.push_back(CallableState); |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1116 | D->addAttr(::new (S.Context) |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 1117 | CallableWhenAttr(Attr.getRange(), S.Context, States.data(), |
| 1118 | States.size(), Attr.getAttributeSpellingListIndex())); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1121 | static void handleParamTypestateAttr(Sema &S, Decl *D, |
| 1122 | const AttributeList &Attr) { |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 1123 | ParamTypestateAttr::ConsumedState ParamState; |
| 1124 | |
| 1125 | if (Attr.isArgIdent(0)) { |
| 1126 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1127 | StringRef StateString = Ident->Ident->getName(); |
| 1128 | |
| 1129 | if (!ParamTypestateAttr::ConvertStrToConsumedState(StateString, |
| 1130 | ParamState)) { |
| 1131 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1132 | << Attr.getName() << StateString; |
| 1133 | return; |
| 1134 | } |
| 1135 | } else { |
| 1136 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1137 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | // FIXME: This check is currently being done in the analysis. It can be |
| 1142 | // enabled here only after the parser propagates attributes at |
| 1143 | // template specialization definition, not declaration. |
| 1144 | //QualType ReturnType = cast<ParmVarDecl>(D)->getType(); |
| 1145 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1146 | // |
| 1147 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1148 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1149 | // ReturnType.getAsString(); |
| 1150 | // return; |
| 1151 | //} |
| 1152 | |
| 1153 | D->addAttr(::new (S.Context) |
| 1154 | ParamTypestateAttr(Attr.getRange(), S.Context, ParamState, |
| 1155 | Attr.getAttributeSpellingListIndex())); |
| 1156 | } |
| 1157 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1158 | static void handleReturnTypestateAttr(Sema &S, Decl *D, |
| 1159 | const AttributeList &Attr) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1160 | ReturnTypestateAttr::ConsumedState ReturnState; |
| 1161 | |
| 1162 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 1163 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 1164 | if (!ReturnTypestateAttr::ConvertStrToConsumedState(IL->Ident->getName(), |
| 1165 | ReturnState)) { |
| 1166 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) |
| 1167 | << Attr.getName() << IL->Ident; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1168 | return; |
| 1169 | } |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1170 | } else { |
| 1171 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1172 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1173 | return; |
| 1174 | } |
| 1175 | |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1176 | // FIXME: This check is currently being done in the analysis. It can be |
| 1177 | // enabled here only after the parser propagates attributes at |
| 1178 | // template specialization definition, not declaration. |
| 1179 | //QualType ReturnType; |
| 1180 | // |
DeLesley Hutchins | 36ea1dd | 2013-10-17 22:53:04 +0000 | [diff] [blame] | 1181 | //if (const ParmVarDecl *Param = dyn_cast<ParmVarDecl>(D)) { |
| 1182 | // ReturnType = Param->getType(); |
| 1183 | // |
| 1184 | //} else if (const CXXConstructorDecl *Constructor = |
| 1185 | // dyn_cast<CXXConstructorDecl>(D)) { |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 1186 | // ReturnType = Constructor->getThisType(S.getASTContext())->getPointeeType(); |
| 1187 | // |
| 1188 | //} else { |
| 1189 | // |
| 1190 | // ReturnType = cast<FunctionDecl>(D)->getCallResultType(); |
| 1191 | //} |
| 1192 | // |
| 1193 | //const CXXRecordDecl *RD = ReturnType->getAsCXXRecordDecl(); |
| 1194 | // |
| 1195 | //if (!RD || !RD->hasAttr<ConsumableAttr>()) { |
| 1196 | // S.Diag(Attr.getLoc(), diag::warn_return_state_for_unconsumable_type) << |
| 1197 | // ReturnType.getAsString(); |
| 1198 | // return; |
| 1199 | //} |
| 1200 | |
| 1201 | D->addAttr(::new (S.Context) |
| 1202 | ReturnTypestateAttr(Attr.getRange(), S.Context, ReturnState, |
| 1203 | Attr.getAttributeSpellingListIndex())); |
| 1204 | } |
| 1205 | |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1206 | static void handleSetTypestateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1207 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1208 | return; |
| 1209 | |
| 1210 | SetTypestateAttr::ConsumedState NewState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1211 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1212 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1213 | StringRef Param = Ident->Ident->getName(); |
| 1214 | if (!SetTypestateAttr::ConvertStrToConsumedState(Param, NewState)) { |
| 1215 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1216 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1217 | return; |
| 1218 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1219 | } else { |
| 1220 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1221 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1222 | return; |
| 1223 | } |
| 1224 | |
| 1225 | D->addAttr(::new (S.Context) |
| 1226 | SetTypestateAttr(Attr.getRange(), S.Context, NewState, |
| 1227 | Attr.getAttributeSpellingListIndex())); |
| 1228 | } |
| 1229 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1230 | static void handleTestTypestateAttr(Sema &S, Decl *D, |
| 1231 | const AttributeList &Attr) { |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1232 | if (!checkForConsumableClass(S, cast<CXXMethodDecl>(D), Attr)) |
| 1233 | return; |
| 1234 | |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1235 | TestTypestateAttr::ConsumedState TestState; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1236 | if (Attr.isArgIdent(0)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1237 | IdentifierLoc *Ident = Attr.getArgAsIdent(0); |
| 1238 | StringRef Param = Ident->Ident->getName(); |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1239 | if (!TestTypestateAttr::ConvertStrToConsumedState(Param, TestState)) { |
Aaron Ballman | 91c98e1 | 2013-10-14 23:22:37 +0000 | [diff] [blame] | 1240 | S.Diag(Ident->Loc, diag::warn_attribute_type_not_supported) |
| 1241 | << Attr.getName() << Param; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1242 | return; |
| 1243 | } |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1244 | } else { |
| 1245 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << |
| 1246 | Attr.getName() << AANT_ArgumentIdentifier; |
| 1247 | return; |
| 1248 | } |
| 1249 | |
| 1250 | D->addAttr(::new (S.Context) |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 1251 | TestTypestateAttr(Attr.getRange(), S.Context, TestState, |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 1252 | Attr.getAttributeSpellingListIndex())); |
| 1253 | } |
| 1254 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1255 | static void handleExtVectorTypeAttr(Sema &S, Scope *scope, Decl *D, |
| 1256 | const AttributeList &Attr) { |
Richard Smith | 1f5a432 | 2013-01-13 02:11:23 +0000 | [diff] [blame] | 1257 | // Remember this typedef decl, we will need it later for diagnostics. |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 1258 | S.ExtVectorDecls.push_back(cast<TypedefNameDecl>(D)); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1261 | static void handlePackedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1262 | if (TagDecl *TD = dyn_cast<TagDecl>(D)) |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 1263 | TD->addAttr(::new (S.Context) PackedAttr(Attr.getRange(), S.Context, |
| 1264 | Attr.getAttributeSpellingListIndex())); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1265 | else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1266 | // Report warning about changed offset in the newer compiler versions. |
Eli Friedman | c087c3f | 2012-11-07 00:35:20 +0000 | [diff] [blame] | 1267 | if (!FD->getType()->isDependentType() && |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1268 | !FD->getType()->isIncompleteType() && FD->isBitField() && |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 1269 | S.Context.getTypeAlign(FD->getType()) <= 8) |
Alexey Bataev | 830dfcc | 2015-12-03 09:34:49 +0000 | [diff] [blame] | 1270 | S.Diag(Attr.getLoc(), diag::warn_attribute_packed_for_bitfield); |
| 1271 | |
| 1272 | FD->addAttr(::new (S.Context) PackedAttr( |
| 1273 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1274 | } else |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 1275 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1278 | static bool checkIBOutletCommon(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1279 | // The IBOutlet/IBOutletCollection attributes only apply to instance |
| 1280 | // variables or properties of Objective-C classes. The outlet must also |
| 1281 | // have an object reference type. |
| 1282 | if (const ObjCIvarDecl *VD = dyn_cast<ObjCIvarDecl>(D)) { |
| 1283 | if (!VD->getType()->getAs<ObjCObjectPointerType>()) { |
Ted Kremenek | 5d6044e | 2011-11-01 18:08:35 +0000 | [diff] [blame] | 1284 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1285 | << Attr.getName() << VD->getType() << 0; |
| 1286 | return false; |
| 1287 | } |
| 1288 | } |
| 1289 | else if (const ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 1290 | if (!PD->getType()->getAs<ObjCObjectPointerType>()) { |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1291 | S.Diag(Attr.getLoc(), diag::warn_iboutlet_object_type) |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1292 | << Attr.getName() << PD->getType() << 1; |
| 1293 | return false; |
| 1294 | } |
| 1295 | } |
| 1296 | else { |
| 1297 | S.Diag(Attr.getLoc(), diag::warn_attribute_iboutlet) << Attr.getName(); |
| 1298 | return false; |
| 1299 | } |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 1300 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1301 | return true; |
| 1302 | } |
| 1303 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1304 | static void handleIBOutlet(Sema &S, Decl *D, const AttributeList &Attr) { |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1305 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1306 | return; |
Ted Kremenek | 1f67282 | 2010-02-18 03:08:58 +0000 | [diff] [blame] | 1307 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1308 | D->addAttr(::new (S.Context) |
| 1309 | IBOutletAttr(Attr.getRange(), S.Context, |
| 1310 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 8e3704d | 2008-07-15 22:26:48 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1313 | static void handleIBOutletCollection(Sema &S, Decl *D, |
| 1314 | const AttributeList &Attr) { |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1315 | |
| 1316 | // The iboutletcollection attribute can have zero or one arguments. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1317 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1318 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1319 | << Attr.getName() << 1; |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1320 | return; |
| 1321 | } |
| 1322 | |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1323 | if (!checkIBOutletCommon(S, D, Attr)) |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1324 | return; |
Ted Kremenek | 7fd1723 | 2011-09-29 07:02:25 +0000 | [diff] [blame] | 1325 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1326 | ParsedType PT; |
| 1327 | |
| 1328 | if (Attr.hasParsedType()) |
| 1329 | PT = Attr.getTypeArg(); |
| 1330 | else { |
| 1331 | PT = S.getTypeName(S.Context.Idents.get("NSObject"), Attr.getLoc(), |
| 1332 | S.getScopeForContext(D->getDeclContext()->getParent())); |
| 1333 | if (!PT) { |
| 1334 | S.Diag(Attr.getLoc(), diag::err_iboutletcollection_type) << "NSObject"; |
| 1335 | return; |
| 1336 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1337 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1338 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1339 | TypeSourceInfo *QTLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1340 | QualType QT = S.GetTypeFromParser(PT, &QTLoc); |
| 1341 | if (!QTLoc) |
| 1342 | QTLoc = S.Context.getTrivialTypeSourceInfo(QT, Attr.getLoc()); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1343 | |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1344 | // Diagnose use of non-object type in iboutletcollection attribute. |
| 1345 | // FIXME. Gnu attribute extension ignores use of builtin types in |
| 1346 | // attributes. So, __attribute__((iboutletcollection(char))) will be |
| 1347 | // treated as __attribute__((iboutletcollection())). |
Fariborz Jahanian | 2f31b33 | 2011-10-18 19:54:31 +0000 | [diff] [blame] | 1348 | if (!QT->isObjCIdType() && !QT->isObjCObjectType()) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1349 | S.Diag(Attr.getLoc(), |
| 1350 | QT->isBuiltinType() ? diag::err_iboutletcollection_builtintype |
| 1351 | : diag::err_iboutletcollection_type) << QT; |
Fariborz Jahanian | b5d59b6 | 2010-08-17 20:23:12 +0000 | [diff] [blame] | 1352 | return; |
| 1353 | } |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 1354 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1355 | D->addAttr(::new (S.Context) |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 1356 | IBOutletCollectionAttr(Attr.getRange(), S.Context, QTLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1357 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 26bde77 | 2010-05-19 17:38:06 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1360 | bool Sema::isValidPointerAttrType(QualType T, bool RefOkay) { |
| 1361 | if (RefOkay) { |
| 1362 | if (T->isReferenceType()) |
| 1363 | return true; |
| 1364 | } else { |
| 1365 | T = T.getNonReferenceType(); |
| 1366 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1367 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1368 | // The nonnull attribute, and other similar attributes, can be applied to a |
| 1369 | // transparent union that contains a pointer type. |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1370 | if (const RecordType *UT = T->getAsUnionType()) { |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1371 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) { |
| 1372 | RecordDecl *UD = UT->getDecl(); |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1373 | for (const auto *I : UD->fields()) { |
| 1374 | QualType QT = I->getType(); |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1375 | if (QT->isAnyPointerType() || QT->isBlockPointerType()) |
| 1376 | return true; |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1377 | } |
| 1378 | } |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | return T->isAnyPointerType() || T->isBlockPointerType(); |
Fariborz Jahanian | f4aa279 | 2011-06-27 21:12:03 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1384 | static bool attrNonNullArgCheck(Sema &S, QualType T, const AttributeList &Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1385 | SourceRange AttrParmRange, |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1386 | SourceRange TypeRange, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1387 | bool isReturnValue = false) { |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1388 | if (!S.isValidPointerAttrType(T)) { |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 1389 | if (isReturnValue) |
| 1390 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1391 | << Attr.getName() << AttrParmRange << TypeRange; |
| 1392 | else |
| 1393 | S.Diag(Attr.getLoc(), diag::warn_attribute_pointers_only) |
| 1394 | << Attr.getName() << AttrParmRange << TypeRange << 0; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 1395 | return false; |
| 1396 | } |
| 1397 | return true; |
| 1398 | } |
| 1399 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1400 | static void handleNonNullAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1401 | SmallVector<unsigned, 8> NonNullArgs; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1402 | for (unsigned I = 0; I < Attr.getNumArgs(); ++I) { |
| 1403 | Expr *Ex = Attr.getArgAsExpr(I); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1404 | uint64_t Idx; |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1405 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, I + 1, Ex, Idx)) |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1406 | return; |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1407 | |
| 1408 | // Is the function argument a pointer type? |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1409 | if (Idx < getFunctionOrMethodNumParams(D) && |
| 1410 | !attrNonNullArgCheck(S, getFunctionOrMethodParamType(D, Idx), Attr, |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1411 | Ex->getSourceRange(), |
| 1412 | getFunctionOrMethodParamRange(D, Idx))) |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1413 | continue; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1414 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1415 | NonNullArgs.push_back(Idx); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1416 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1417 | |
| 1418 | // If no arguments were specified to __attribute__((nonnull)) then all pointer |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1419 | // arguments have a nonnull attribute; warn if there aren't any. Skip this |
| 1420 | // check if the attribute came from a macro expansion or a template |
| 1421 | // instantiation. |
| 1422 | if (NonNullArgs.empty() && Attr.getLoc().isFileID() && |
| 1423 | S.ActiveTemplateInstantiations.empty()) { |
| 1424 | bool AnyPointers = isFunctionOrMethodVariadic(D); |
| 1425 | for (unsigned I = 0, E = getFunctionOrMethodNumParams(D); |
| 1426 | I != E && !AnyPointers; ++I) { |
| 1427 | QualType T = getFunctionOrMethodParamType(D, I); |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1428 | if (T->isDependentType() || S.isValidPointerAttrType(T)) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1429 | AnyPointers = true; |
Ted Kremenek | 5fa5052 | 2008-11-18 06:52:58 +0000 | [diff] [blame] | 1430 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1431 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1432 | if (!AnyPointers) |
| 1433 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_no_pointers); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1434 | } |
Ted Kremenek | c4f6d90 | 2008-09-01 19:57:52 +0000 | [diff] [blame] | 1435 | |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1436 | unsigned *Start = NonNullArgs.data(); |
| 1437 | unsigned Size = NonNullArgs.size(); |
| 1438 | llvm::array_pod_sort(Start, Start + Size); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1439 | D->addAttr(::new (S.Context) |
Richard Smith | 588bd9b | 2014-08-27 04:59:42 +0000 | [diff] [blame] | 1440 | NonNullAttr(Attr.getRange(), S.Context, Start, Size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1441 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 2d63bc1 | 2008-07-21 21:53:04 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1444 | static void handleNonNullAttrParameter(Sema &S, ParmVarDecl *D, |
| 1445 | const AttributeList &Attr) { |
| 1446 | if (Attr.getNumArgs() > 0) { |
| 1447 | if (D->getFunctionType()) { |
| 1448 | handleNonNullAttr(S, D, Attr); |
| 1449 | } else { |
| 1450 | S.Diag(Attr.getLoc(), diag::warn_attribute_nonnull_parm_no_args) |
| 1451 | << D->getSourceRange(); |
| 1452 | } |
| 1453 | return; |
| 1454 | } |
| 1455 | |
| 1456 | // Is the argument a pointer type? |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1457 | if (!attrNonNullArgCheck(S, D->getType(), Attr, SourceRange(), |
| 1458 | D->getSourceRange())) |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1459 | return; |
| 1460 | |
| 1461 | D->addAttr(::new (S.Context) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1462 | NonNullAttr(Attr.getRange(), S.Context, nullptr, 0, |
Jordan Rose | c939907 | 2014-02-11 17:27:59 +0000 | [diff] [blame] | 1463 | Attr.getAttributeSpellingListIndex())); |
| 1464 | } |
| 1465 | |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1466 | static void handleReturnsNonNullAttr(Sema &S, Decl *D, |
| 1467 | const AttributeList &Attr) { |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 1468 | QualType ResultType = getFunctionOrMethodResultType(D); |
Aaron Ballman | 4bfa0de | 2014-08-01 12:58:11 +0000 | [diff] [blame] | 1469 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1470 | if (!attrNonNullArgCheck(S, ResultType, Attr, SourceRange(), SR, |
Ted Kremenek | dbf62e3 | 2014-01-20 05:50:47 +0000 | [diff] [blame] | 1471 | /* isReturnValue */ true)) |
| 1472 | return; |
| 1473 | |
| 1474 | D->addAttr(::new (S.Context) |
| 1475 | ReturnsNonNullAttr(Attr.getRange(), S.Context, |
| 1476 | Attr.getAttributeSpellingListIndex())); |
| 1477 | } |
| 1478 | |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 1479 | static void handleAssumeAlignedAttr(Sema &S, Decl *D, |
| 1480 | const AttributeList &Attr) { |
| 1481 | Expr *E = Attr.getArgAsExpr(0), |
| 1482 | *OE = Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr; |
| 1483 | S.AddAssumeAlignedAttr(Attr.getRange(), D, E, OE, |
| 1484 | Attr.getAttributeSpellingListIndex()); |
| 1485 | } |
| 1486 | |
| 1487 | void Sema::AddAssumeAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 1488 | Expr *OE, unsigned SpellingListIndex) { |
| 1489 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1490 | SourceRange SR = getFunctionOrMethodResultSourceRange(D); |
| 1491 | |
| 1492 | AssumeAlignedAttr TmpAttr(AttrRange, Context, E, OE, SpellingListIndex); |
| 1493 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 1494 | |
| 1495 | if (!isValidPointerAttrType(ResultType, /* RefOkay */ true)) { |
| 1496 | Diag(AttrLoc, diag::warn_attribute_return_pointers_refs_only) |
| 1497 | << &TmpAttr << AttrRange << SR; |
| 1498 | return; |
| 1499 | } |
| 1500 | |
| 1501 | if (!E->isValueDependent()) { |
| 1502 | llvm::APSInt I(64); |
| 1503 | if (!E->isIntegerConstantExpr(I, Context)) { |
| 1504 | if (OE) |
| 1505 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1506 | << &TmpAttr << 1 << AANT_ArgumentIntegerConstant |
| 1507 | << E->getSourceRange(); |
| 1508 | else |
| 1509 | Diag(AttrLoc, diag::err_attribute_argument_type) |
| 1510 | << &TmpAttr << AANT_ArgumentIntegerConstant |
| 1511 | << E->getSourceRange(); |
| 1512 | return; |
| 1513 | } |
| 1514 | |
| 1515 | if (!I.isPowerOf2()) { |
| 1516 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 1517 | << E->getSourceRange(); |
| 1518 | return; |
| 1519 | } |
| 1520 | } |
| 1521 | |
| 1522 | if (OE) { |
| 1523 | if (!OE->isValueDependent()) { |
| 1524 | llvm::APSInt I(64); |
| 1525 | if (!OE->isIntegerConstantExpr(I, Context)) { |
| 1526 | Diag(AttrLoc, diag::err_attribute_argument_n_type) |
| 1527 | << &TmpAttr << 2 << AANT_ArgumentIntegerConstant |
| 1528 | << OE->getSourceRange(); |
| 1529 | return; |
| 1530 | } |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | D->addAttr(::new (Context) |
| 1535 | AssumeAlignedAttr(AttrRange, Context, E, OE, SpellingListIndex)); |
| 1536 | } |
| 1537 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1538 | /// Normalize the attribute, __foo__ becomes foo. |
| 1539 | /// Returns true if normalization was applied. |
| 1540 | static bool normalizeName(StringRef &AttrName) { |
Aaron Ballman | 6269236 | 2015-10-09 13:53:24 +0000 | [diff] [blame] | 1541 | if (AttrName.size() > 4 && AttrName.startswith("__") && |
| 1542 | AttrName.endswith("__")) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1543 | AttrName = AttrName.drop_front(2).drop_back(2); |
| 1544 | return true; |
| 1545 | } |
| 1546 | return false; |
| 1547 | } |
| 1548 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1549 | static void handleOwnershipAttr(Sema &S, Decl *D, const AttributeList &AL) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1550 | // This attribute must be applied to a function declaration. The first |
| 1551 | // argument to the attribute must be an identifier, the name of the resource, |
| 1552 | // for example: malloc. The following arguments must be argument indexes, the |
| 1553 | // arguments must be of integer type for Returns, otherwise of pointer type. |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1554 | // 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] | 1555 | // after being held. free() should be __attribute((ownership_takes)), whereas |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1556 | // a list append function may well be __attribute((ownership_holds)). |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1557 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1558 | if (!AL.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 1559 | S.Diag(AL.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1560 | << AL.getName() << 1 << AANT_ArgumentIdentifier; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1561 | return; |
| 1562 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1563 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1564 | // Figure out our Kind. |
| 1565 | OwnershipAttr::OwnershipKind K = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1566 | OwnershipAttr(AL.getLoc(), S.Context, nullptr, nullptr, 0, |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1567 | AL.getAttributeSpellingListIndex()).getOwnKind(); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1568 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1569 | // Check arguments. |
| 1570 | switch (K) { |
| 1571 | case OwnershipAttr::Takes: |
| 1572 | case OwnershipAttr::Holds: |
| 1573 | if (AL.getNumArgs() < 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1574 | S.Diag(AL.getLoc(), diag::err_attribute_too_few_arguments) |
| 1575 | << AL.getName() << 2; |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1576 | return; |
| 1577 | } |
| 1578 | break; |
| 1579 | case OwnershipAttr::Returns: |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1580 | if (AL.getNumArgs() > 2) { |
Aaron Ballman | 05e420a | 2014-01-02 21:26:14 +0000 | [diff] [blame] | 1581 | S.Diag(AL.getLoc(), diag::err_attribute_too_many_arguments) |
| 1582 | << AL.getName() << 1; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1583 | return; |
| 1584 | } |
Jordy Rose | 5af0e3c | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 1585 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1588 | IdentifierInfo *Module = AL.getArgAsIdent(0)->Ident; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1589 | |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1590 | StringRef ModuleName = Module->getName(); |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 1591 | if (normalizeName(ModuleName)) { |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1592 | Module = &S.PP.getIdentifierTable().get(ModuleName); |
| 1593 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1594 | |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1595 | SmallVector<unsigned, 8> OwnershipArgs; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1596 | for (unsigned i = 1; i < AL.getNumArgs(); ++i) { |
| 1597 | Expr *Ex = AL.getArgAsExpr(i); |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1598 | uint64_t Idx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1599 | if (!checkFunctionOrMethodParameterIndex(S, D, AL, i, Ex, Idx)) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1600 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 1601 | |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1602 | // Is the function argument a pointer type? |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 1603 | QualType T = getFunctionOrMethodParamType(D, Idx); |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1604 | int Err = -1; // No error |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1605 | switch (K) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1606 | case OwnershipAttr::Takes: |
| 1607 | case OwnershipAttr::Holds: |
| 1608 | if (!T->isAnyPointerType() && !T->isBlockPointerType()) |
| 1609 | Err = 0; |
| 1610 | break; |
| 1611 | case OwnershipAttr::Returns: |
| 1612 | if (!T->isIntegerType()) |
| 1613 | Err = 1; |
| 1614 | break; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1615 | } |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1616 | if (-1 != Err) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 1617 | S.Diag(AL.getLoc(), diag::err_ownership_type) << AL.getName() << Err |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1618 | << Ex->getSourceRange(); |
| 1619 | return; |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1620 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1621 | |
| 1622 | // Check we don't have a conflict with another ownership attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1623 | for (const auto *I : D->specific_attrs<OwnershipAttr>()) { |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1624 | // Cannot have two ownership attributes of different kinds for the same |
| 1625 | // index. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1626 | if (I->getOwnKind() != K && I->args_end() != |
| 1627 | std::find(I->args_begin(), I->args_end(), Idx)) { |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1628 | S.Diag(AL.getLoc(), diag::err_attributes_are_not_compatible) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 1629 | << AL.getName() << I; |
Aaron Ballman | 6e2dd7b | 2013-09-16 18:11:41 +0000 | [diff] [blame] | 1630 | return; |
Aaron Ballman | ef7aef8 | 2014-07-31 20:44:26 +0000 | [diff] [blame] | 1631 | } else if (K == OwnershipAttr::Returns && |
| 1632 | I->getOwnKind() == OwnershipAttr::Returns) { |
| 1633 | // A returns attribute conflicts with any other returns attribute using |
| 1634 | // a different index. Note, diagnostic reporting is 1-based, but stored |
| 1635 | // argument indexes are 0-based. |
| 1636 | if (std::find(I->args_begin(), I->args_end(), Idx) == I->args_end()) { |
| 1637 | S.Diag(I->getLocation(), diag::err_ownership_returns_index_mismatch) |
| 1638 | << *(I->args_begin()) + 1; |
| 1639 | if (I->args_size()) |
| 1640 | S.Diag(AL.getLoc(), diag::note_ownership_returns_index_mismatch) |
| 1641 | << (unsigned)Idx + 1 << Ex->getSourceRange(); |
| 1642 | return; |
| 1643 | } |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1644 | } |
| 1645 | } |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 1646 | OwnershipArgs.push_back(Idx); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
| 1649 | unsigned* start = OwnershipArgs.data(); |
| 1650 | unsigned size = OwnershipArgs.size(); |
| 1651 | llvm::array_pod_sort(start, start + size); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 1652 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1653 | D->addAttr(::new (S.Context) |
Richard Smith | 852e9ce | 2013-11-27 01:46:48 +0000 | [diff] [blame] | 1654 | OwnershipAttr(AL.getLoc(), S.Context, Module, start, size, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1655 | AL.getAttributeSpellingListIndex())); |
Ted Kremenek | d21139a | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1658 | static void handleWeakRefAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1659 | // Check the attribute arguments. |
| 1660 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 1661 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 1662 | << Attr.getName() << 1; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1663 | return; |
| 1664 | } |
| 1665 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1666 | NamedDecl *nd = cast<NamedDecl>(D); |
John McCall | 7a198ce | 2011-02-08 22:35:49 +0000 | [diff] [blame] | 1667 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1668 | // gcc rejects |
| 1669 | // class c { |
| 1670 | // static int a __attribute__((weakref ("v2"))); |
| 1671 | // static int b() __attribute__((weakref ("f3"))); |
| 1672 | // }; |
| 1673 | // and ignores the attributes of |
| 1674 | // void f(void) { |
| 1675 | // static int a __attribute__((weakref ("v2"))); |
| 1676 | // } |
| 1677 | // we reject them |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1678 | const DeclContext *Ctx = D->getDeclContext()->getRedeclContext(); |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1679 | if (!Ctx->isFileContext()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 1680 | S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) |
| 1681 | << nd; |
Sebastian Redl | 50c6825 | 2010-08-31 00:36:30 +0000 | [diff] [blame] | 1682 | return; |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
| 1685 | // The GCC manual says |
| 1686 | // |
| 1687 | // At present, a declaration to which `weakref' is attached can only |
| 1688 | // be `static'. |
| 1689 | // |
| 1690 | // It also says |
| 1691 | // |
| 1692 | // Without a TARGET, |
| 1693 | // given as an argument to `weakref' or to `alias', `weakref' is |
| 1694 | // equivalent to `weak'. |
| 1695 | // |
| 1696 | // gcc 4.4.1 will accept |
| 1697 | // int a7 __attribute__((weakref)); |
| 1698 | // as |
| 1699 | // int a7 __attribute__((weak)); |
| 1700 | // This looks like a bug in gcc. We reject that for now. We should revisit |
| 1701 | // it if this behaviour is actually used. |
| 1702 | |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1703 | // GCC rejects |
| 1704 | // static ((alias ("y"), weakref)). |
| 1705 | // Should we? How to check that weakref is before or after alias? |
| 1706 | |
Aaron Ballman | febff0c | 2013-09-09 23:40:31 +0000 | [diff] [blame] | 1707 | // FIXME: it would be good for us to keep the WeakRefAttr as-written instead |
| 1708 | // of transforming it into an AliasAttr. The WeakRefAttr never uses the |
| 1709 | // StringRef parameter it was given anyway. |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1710 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1711 | if (Attr.getNumArgs() && S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1712 | // GCC will accept anything as the argument of weakref. Should we |
| 1713 | // check for an existing decl? |
Aaron Ballman | 3b1dde6 | 2013-09-13 19:35:18 +0000 | [diff] [blame] | 1714 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
| 1715 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1716 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1717 | D->addAttr(::new (S.Context) |
| 1718 | WeakRefAttr(Attr.getRange(), S.Context, |
| 1719 | Attr.getAttributeSpellingListIndex())); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 1720 | } |
| 1721 | |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1722 | static void handleIFuncAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1723 | StringRef Str; |
| 1724 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
| 1725 | return; |
| 1726 | |
| 1727 | // Aliases should be on declarations, not definitions. |
| 1728 | const auto *FD = cast<FunctionDecl>(D); |
| 1729 | if (FD->isThisDeclarationADefinition()) { |
| 1730 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD << 1; |
| 1731 | return; |
| 1732 | } |
| 1733 | // FIXME: it should be handled as a target specific attribute. |
| 1734 | if (S.Context.getTargetInfo().getTriple().getObjectFormat() != |
| 1735 | llvm::Triple::ELF) { |
| 1736 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 1737 | return; |
| 1738 | } |
| 1739 | |
| 1740 | D->addAttr(::new (S.Context) IFuncAttr(Attr.getRange(), S.Context, Str, |
| 1741 | Attr.getAttributeSpellingListIndex())); |
| 1742 | } |
| 1743 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1744 | static void handleAliasAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1745 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1746 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1747 | return; |
| 1748 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 1749 | if (S.Context.getTargetInfo().getTriple().isOSDarwin()) { |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1750 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_darwin); |
| 1751 | return; |
| 1752 | } |
Justin Lebar | a8f0254b | 2016-01-23 21:28:10 +0000 | [diff] [blame] | 1753 | if (S.Context.getTargetInfo().getTriple().isNVPTX()) { |
| 1754 | S.Diag(Attr.getLoc(), diag::err_alias_not_supported_on_nvptx); |
| 1755 | } |
Rafael Espindola | 0017c5f | 2010-12-07 15:23:23 +0000 | [diff] [blame] | 1756 | |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1757 | // Aliases should be on declarations, not definitions. |
| 1758 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| 1759 | if (FD->isThisDeclarationADefinition()) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1760 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << FD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1761 | return; |
| 1762 | } |
| 1763 | } else { |
| 1764 | const auto *VD = cast<VarDecl>(D); |
| 1765 | if (VD->isThisDeclarationADefinition() && VD->isExternallyVisible()) { |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 1766 | S.Diag(Attr.getLoc(), diag::err_alias_is_definition) << VD << 0; |
David Majnemer | 2dc8146 | 2015-01-19 09:00:28 +0000 | [diff] [blame] | 1767 | return; |
| 1768 | } |
| 1769 | } |
| 1770 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1771 | // FIXME: check if target symbol exists in current file |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1772 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1773 | D->addAttr(::new (S.Context) AliasAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1774 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1775 | } |
| 1776 | |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1777 | static void handleColdAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1778 | if (checkAttrMutualExclusion<HotAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1779 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1780 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1781 | D->addAttr(::new (S.Context) ColdAttr(Attr.getRange(), S.Context, |
| 1782 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | static void handleHotAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1786 | if (checkAttrMutualExclusion<ColdAttr>(S, D, Attr.getRange(), Attr.getName())) |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1787 | return; |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1788 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1789 | D->addAttr(::new (S.Context) HotAttr(Attr.getRange(), S.Context, |
| 1790 | Attr.getAttributeSpellingListIndex())); |
Benjamin Kramer | 29c2b43 | 2012-05-12 21:10:52 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1793 | static void handleTLSModelAttr(Sema &S, Decl *D, |
| 1794 | const AttributeList &Attr) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1795 | StringRef Model; |
| 1796 | SourceLocation LiteralLoc; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1797 | // Check that it is a string. |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 1798 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Model, &LiteralLoc)) |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1799 | return; |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1800 | |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1801 | // Check that the value. |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1802 | if (Model != "global-dynamic" && Model != "local-dynamic" |
| 1803 | && Model != "initial-exec" && Model != "local-exec") { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 1804 | S.Diag(LiteralLoc, diag::err_attr_tlsmodel_arg); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1805 | return; |
| 1806 | } |
| 1807 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1808 | D->addAttr(::new (S.Context) |
| 1809 | TLSModelAttr(Attr.getRange(), S.Context, Model, |
| 1810 | Attr.getAttributeSpellingListIndex())); |
Hans Wennborg | d3b01bc | 2012-06-23 11:51:46 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1813 | static void handleRestrictAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1814 | QualType ResultType = getFunctionOrMethodResultType(D); |
| 1815 | if (ResultType->isAnyPointerType() || ResultType->isBlockPointerType()) { |
| 1816 | D->addAttr(::new (S.Context) RestrictAttr( |
| 1817 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1818 | return; |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 1821 | S.Diag(Attr.getLoc(), diag::warn_attribute_return_pointers_only) |
| 1822 | << Attr.getName() << getFunctionOrMethodResultSourceRange(D); |
Ryan Flynn | 1f1fdc0 | 2009-08-09 20:07:29 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1825 | static void handleCommonAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1826 | if (S.LangOpts.CPlusPlus) { |
Aaron Ballman | 3db8966 | 2013-11-24 21:48:06 +0000 | [diff] [blame] | 1827 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1828 | << Attr.getName() << AttributeLangSupport::Cpp; |
Eli Friedman | 6fc7ad1 | 2013-06-20 22:55:04 +0000 | [diff] [blame] | 1829 | return; |
| 1830 | } |
| 1831 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1832 | if (CommonAttr *CA = S.mergeCommonAttr(D, Attr.getRange(), Attr.getName(), |
| 1833 | Attr.getAttributeSpellingListIndex())) |
| 1834 | D->addAttr(CA); |
Eric Christopher | 8a2ee39 | 2010-12-02 02:45:55 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 1837 | static void handleNakedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 1838 | if (checkAttrMutualExclusion<DisableTailCallsAttr>(S, D, Attr.getRange(), |
| 1839 | Attr.getName())) |
| 1840 | return; |
| 1841 | |
| 1842 | D->addAttr(::new (S.Context) NakedAttr(Attr.getRange(), S.Context, |
| 1843 | Attr.getAttributeSpellingListIndex())); |
| 1844 | } |
| 1845 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1846 | static void handleNoReturnAttr(Sema &S, Decl *D, const AttributeList &attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1847 | if (hasDeclarator(D)) return; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1848 | |
| 1849 | if (S.CheckNoReturnAttr(attr)) return; |
| 1850 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1851 | if (!isa<ObjCMethodDecl>(D)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1852 | S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1853 | << attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1854 | return; |
| 1855 | } |
| 1856 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1857 | D->addAttr(::new (S.Context) |
| 1858 | NoReturnAttr(attr.getRange(), S.Context, |
| 1859 | attr.getAttributeSpellingListIndex())); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | bool Sema::CheckNoReturnAttr(const AttributeList &attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1863 | if (!checkAttributeNumArgs(*this, attr, 0)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 1864 | attr.setInvalid(); |
| 1865 | return true; |
| 1866 | } |
| 1867 | |
| 1868 | return false; |
Ted Kremenek | 40f4ee7 | 2009-04-10 00:01:14 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1871 | static void handleAnalyzerNoReturnAttr(Sema &S, Decl *D, |
| 1872 | const AttributeList &Attr) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1873 | |
| 1874 | // The checking path for 'noreturn' and 'analyzer_noreturn' are different |
| 1875 | // because 'analyzer_noreturn' does not impact the type. |
David Majnemer | 0686481 | 2015-04-07 06:01:53 +0000 | [diff] [blame] | 1876 | if (!isFunctionOrMethodOrBlock(D)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1877 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 1878 | if (!VD || (!VD->getType()->isBlockPointerType() && |
| 1879 | !VD->getType()->isFunctionPointerType())) { |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1880 | S.Diag(Attr.getLoc(), |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1881 | Attr.isCXX11Attribute() ? diag::err_attribute_wrong_decl_type |
Craig Topper | 8f7f3ea | 2015-11-17 05:40:05 +0000 | [diff] [blame] | 1882 | : diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1883 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Ted Kremenek | 5295ce8 | 2010-08-19 00:51:58 +0000 | [diff] [blame] | 1884 | return; |
| 1885 | } |
| 1886 | } |
| 1887 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1888 | D->addAttr(::new (S.Context) |
| 1889 | AnalyzerNoReturnAttr(Attr.getRange(), S.Context, |
| 1890 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1893 | // PS3 PPU-specific. |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1894 | static void handleVecReturnAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1895 | /* |
| 1896 | Returning a Vector Class in Registers |
| 1897 | |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 1898 | According to the PPU ABI specifications, a class with a single member of |
| 1899 | vector type is returned in memory when used as the return value of a function. |
| 1900 | This results in inefficient code when implementing vector classes. To return |
| 1901 | the value in a single vector register, add the vecreturn attribute to the |
| 1902 | class definition. This attribute is also applicable to struct types. |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1903 | |
| 1904 | Example: |
| 1905 | |
| 1906 | struct Vector |
| 1907 | { |
| 1908 | __vector float xyzw; |
| 1909 | } __attribute__((vecreturn)); |
| 1910 | |
| 1911 | Vector Add(Vector lhs, Vector rhs) |
| 1912 | { |
| 1913 | Vector result; |
| 1914 | result.xyzw = vec_add(lhs.xyzw, rhs.xyzw); |
| 1915 | return result; // This will be returned in a register |
| 1916 | } |
| 1917 | */ |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 1918 | if (VecReturnAttr *A = D->getAttr<VecReturnAttr>()) { |
| 1919 | S.Diag(Attr.getLoc(), diag::err_repeat_attribute) << A; |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1920 | return; |
| 1921 | } |
| 1922 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1923 | RecordDecl *record = cast<RecordDecl>(D); |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1924 | int count = 0; |
| 1925 | |
| 1926 | if (!isa<CXXRecordDecl>(record)) { |
| 1927 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1928 | return; |
| 1929 | } |
| 1930 | |
| 1931 | if (!cast<CXXRecordDecl>(record)->isPOD()) { |
| 1932 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_pod_record); |
| 1933 | return; |
| 1934 | } |
| 1935 | |
Aaron Ballman | e8a8bae | 2014-03-08 20:12:42 +0000 | [diff] [blame] | 1936 | for (const auto *I : record->fields()) { |
| 1937 | if ((count == 1) || !I->getType()->isVectorType()) { |
John Thompson | 9a587aaa | 2010-09-18 01:12:07 +0000 | [diff] [blame] | 1938 | S.Diag(Attr.getLoc(), diag::err_attribute_vecreturn_only_vector_member); |
| 1939 | return; |
| 1940 | } |
| 1941 | count++; |
| 1942 | } |
| 1943 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1944 | D->addAttr(::new (S.Context) |
| 1945 | VecReturnAttr(Attr.getRange(), S.Context, |
| 1946 | Attr.getAttributeSpellingListIndex())); |
John Thompson | cdb847ba | 2010-08-09 21:53:52 +0000 | [diff] [blame] | 1947 | } |
| 1948 | |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1949 | static void handleDependencyAttr(Sema &S, Scope *Scope, Decl *D, |
| 1950 | const AttributeList &Attr) { |
| 1951 | if (isa<ParmVarDecl>(D)) { |
| 1952 | // [[carries_dependency]] can only be applied to a parameter if it is a |
| 1953 | // parameter of a function declaration or lambda. |
| 1954 | if (!(Scope->getFlags() & clang::Scope::FunctionDeclarationScope)) { |
| 1955 | S.Diag(Attr.getLoc(), |
| 1956 | diag::err_carries_dependency_param_not_function_decl); |
| 1957 | return; |
| 1958 | } |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1959 | } |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 1960 | |
| 1961 | D->addAttr(::new (S.Context) CarriesDependencyAttr( |
| 1962 | Attr.getRange(), S.Context, |
| 1963 | Attr.getAttributeSpellingListIndex())); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 1966 | static void handleNotTailCalledAttr(Sema &S, Decl *D, |
| 1967 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 1968 | if (checkAttrMutualExclusion<AlwaysInlineAttr>(S, D, Attr.getRange(), |
| 1969 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 1970 | return; |
| 1971 | |
| 1972 | D->addAttr(::new (S.Context) NotTailCalledAttr( |
| 1973 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1974 | } |
| 1975 | |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 1976 | static void handleDisableTailCallsAttr(Sema &S, Decl *D, |
| 1977 | const AttributeList &Attr) { |
| 1978 | if (checkAttrMutualExclusion<NakedAttr>(S, D, Attr.getRange(), |
| 1979 | Attr.getName())) |
| 1980 | return; |
| 1981 | |
| 1982 | D->addAttr(::new (S.Context) DisableTailCallsAttr( |
| 1983 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 1984 | } |
| 1985 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 1986 | static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1987 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
Rafael Espindola | 87198cd | 2013-08-16 23:18:50 +0000 | [diff] [blame] | 1988 | if (VD->hasLocalStorage()) { |
Aaron Ballman | 88fe322 | 2013-12-26 17:30:44 +0000 | [diff] [blame] | 1989 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1990 | return; |
| 1991 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 1992 | } else if (!isFunctionOrMethod(D)) { |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1993 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 1994 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 1995 | return; |
| 1996 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 1997 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 1998 | D->addAttr(::new (S.Context) |
| 1999 | UsedAttr(Attr.getRange(), S.Context, |
| 2000 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | fee07a0 | 2009-02-13 19:23:53 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 2003 | static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2004 | bool IsCXX1zAttr = Attr.isCXX11Attribute() && !Attr.getScopeName(); |
| 2005 | |
| 2006 | if (IsCXX1zAttr && isa<VarDecl>(D)) { |
| 2007 | // The C++1z spelling of this attribute cannot be applied to a static data |
| 2008 | // member per [dcl.attr.unused]p2. |
| 2009 | if (cast<VarDecl>(D)->isStaticDataMember()) { |
| 2010 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 2011 | << Attr.getName() << ExpectedForMaybeUnused; |
| 2012 | return; |
| 2013 | } |
| 2014 | } |
| 2015 | |
| 2016 | // If this is spelled as the standard C++1z attribute, but not in C++1z, warn |
| 2017 | // about using it as an extension. |
| 2018 | if (!S.getLangOpts().CPlusPlus1z && IsCXX1zAttr) |
| 2019 | S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName(); |
| 2020 | |
| 2021 | D->addAttr(::new (S.Context) UnusedAttr( |
| 2022 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 2023 | } |
| 2024 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2025 | static void handleConstructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2026 | uint32_t priority = ConstructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 2027 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2028 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 2029 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2030 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2031 | D->addAttr(::new (S.Context) |
| 2032 | ConstructorAttr(Attr.getRange(), S.Context, priority, |
| 2033 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2034 | } |
| 2035 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2036 | static void handleDestructorAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | f28e499 | 2014-01-20 15:22:57 +0000 | [diff] [blame] | 2037 | uint32_t priority = DestructorAttr::DefaultPriority; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 2038 | if (Attr.getNumArgs() && |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2039 | !checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), priority)) |
| 2040 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2041 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2042 | D->addAttr(::new (S.Context) |
| 2043 | DestructorAttr(Attr.getRange(), S.Context, priority, |
| 2044 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 032db47 | 2008-07-31 22:40:48 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2047 | template <typename AttrTy> |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 2048 | static void handleAttrWithMessage(Sema &S, Decl *D, |
| 2049 | const AttributeList &Attr) { |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 2050 | // Handle the case where the attribute has a text message. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2051 | StringRef Str; |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 2052 | if (Attr.getNumArgs() == 1 && !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2053 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2054 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2055 | D->addAttr(::new (S.Context) AttrTy(Attr.getRange(), S.Context, Str, |
| 2056 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 1470e93 | 2008-12-17 01:07:27 +0000 | [diff] [blame] | 2057 | } |
| 2058 | |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2059 | static void handleObjCSuppresProtocolAttr(Sema &S, Decl *D, |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2060 | const AttributeList &Attr) { |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 2061 | if (!cast<ObjCProtocolDecl>(D)->isThisDeclarationADefinition()) { |
Ted Kremenek | 27cfe10 | 2014-02-21 22:49:04 +0000 | [diff] [blame] | 2062 | S.Diag(Attr.getLoc(), diag::err_objc_attr_protocol_requires_definition) |
| 2063 | << Attr.getName() << Attr.getRange(); |
| 2064 | return; |
| 2065 | } |
| 2066 | |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2067 | D->addAttr(::new (S.Context) |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 2068 | ObjCExplicitProtocolImplAttr(Attr.getRange(), S.Context, |
| 2069 | Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
Jordy Rose | 740b0c2 | 2012-05-08 03:27:22 +0000 | [diff] [blame] | 2072 | static bool checkAvailabilityAttr(Sema &S, SourceRange Range, |
| 2073 | IdentifierInfo *Platform, |
| 2074 | VersionTuple Introduced, |
| 2075 | VersionTuple Deprecated, |
| 2076 | VersionTuple Obsoleted) { |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2077 | StringRef PlatformName |
| 2078 | = AvailabilityAttr::getPrettyPlatformName(Platform->getName()); |
| 2079 | if (PlatformName.empty()) |
| 2080 | PlatformName = Platform->getName(); |
| 2081 | |
| 2082 | // Ensure that Introduced <= Deprecated <= Obsoleted (although not all |
| 2083 | // of these steps are needed). |
| 2084 | if (!Introduced.empty() && !Deprecated.empty() && |
| 2085 | !(Introduced <= Deprecated)) { |
| 2086 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2087 | << 1 << PlatformName << Deprecated.getAsString() |
| 2088 | << 0 << Introduced.getAsString(); |
| 2089 | return true; |
| 2090 | } |
| 2091 | |
| 2092 | if (!Introduced.empty() && !Obsoleted.empty() && |
| 2093 | !(Introduced <= Obsoleted)) { |
| 2094 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2095 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2096 | << 0 << Introduced.getAsString(); |
| 2097 | return true; |
| 2098 | } |
| 2099 | |
| 2100 | if (!Deprecated.empty() && !Obsoleted.empty() && |
| 2101 | !(Deprecated <= Obsoleted)) { |
| 2102 | S.Diag(Range.getBegin(), diag::warn_availability_version_ordering) |
| 2103 | << 2 << PlatformName << Obsoleted.getAsString() |
| 2104 | << 1 << Deprecated.getAsString(); |
| 2105 | return true; |
| 2106 | } |
| 2107 | |
| 2108 | return false; |
| 2109 | } |
| 2110 | |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2111 | /// \brief Check whether the two versions match. |
| 2112 | /// |
| 2113 | /// If either version tuple is empty, then they are assumed to match. If |
| 2114 | /// \p BeforeIsOkay is true, then \p X can be less than or equal to \p Y. |
| 2115 | static bool versionsMatch(const VersionTuple &X, const VersionTuple &Y, |
| 2116 | bool BeforeIsOkay) { |
| 2117 | if (X.empty() || Y.empty()) |
| 2118 | return true; |
| 2119 | |
| 2120 | if (X == Y) |
| 2121 | return true; |
| 2122 | |
| 2123 | if (BeforeIsOkay && X < Y) |
| 2124 | return true; |
| 2125 | |
| 2126 | return false; |
| 2127 | } |
| 2128 | |
Rafael Espindola | a3aea43 | 2013-01-08 22:04:34 +0000 | [diff] [blame] | 2129 | AvailabilityAttr *Sema::mergeAvailabilityAttr(NamedDecl *D, SourceRange Range, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2130 | IdentifierInfo *Platform, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2131 | bool Implicit, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2132 | VersionTuple Introduced, |
| 2133 | VersionTuple Deprecated, |
| 2134 | VersionTuple Obsoleted, |
| 2135 | bool IsUnavailable, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2136 | StringRef Message, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2137 | bool IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2138 | StringRef Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2139 | AvailabilityMergeKind AMK, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2140 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2141 | VersionTuple MergedIntroduced = Introduced; |
| 2142 | VersionTuple MergedDeprecated = Deprecated; |
| 2143 | VersionTuple MergedObsoleted = Obsoleted; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2144 | bool FoundAny = false; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2145 | bool OverrideOrImpl = false; |
| 2146 | switch (AMK) { |
| 2147 | case AMK_None: |
| 2148 | case AMK_Redeclaration: |
| 2149 | OverrideOrImpl = false; |
| 2150 | break; |
| 2151 | |
| 2152 | case AMK_Override: |
| 2153 | case AMK_ProtocolImplementation: |
| 2154 | OverrideOrImpl = true; |
| 2155 | break; |
| 2156 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2157 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2158 | if (D->hasAttrs()) { |
| 2159 | AttrVec &Attrs = D->getAttrs(); |
| 2160 | for (unsigned i = 0, e = Attrs.size(); i != e;) { |
| 2161 | const AvailabilityAttr *OldAA = dyn_cast<AvailabilityAttr>(Attrs[i]); |
| 2162 | if (!OldAA) { |
| 2163 | ++i; |
| 2164 | continue; |
| 2165 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2166 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2167 | IdentifierInfo *OldPlatform = OldAA->getPlatform(); |
| 2168 | if (OldPlatform != Platform) { |
| 2169 | ++i; |
| 2170 | continue; |
| 2171 | } |
| 2172 | |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2173 | // If there is an existing availability attribute for this platform that |
| 2174 | // is explicit and the new one is implicit use the explicit one and |
| 2175 | // discard the new implicit attribute. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2176 | if (!OldAA->isImplicit() && Implicit) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2177 | return nullptr; |
| 2178 | } |
| 2179 | |
| 2180 | // If there is an existing attribute for this platform that is implicit |
| 2181 | // and the new attribute is explicit then erase the old one and |
| 2182 | // continue processing the attributes. |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2183 | if (!Implicit && OldAA->isImplicit()) { |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2184 | Attrs.erase(Attrs.begin() + i); |
| 2185 | --e; |
| 2186 | continue; |
| 2187 | } |
| 2188 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2189 | FoundAny = true; |
| 2190 | VersionTuple OldIntroduced = OldAA->getIntroduced(); |
| 2191 | VersionTuple OldDeprecated = OldAA->getDeprecated(); |
| 2192 | VersionTuple OldObsoleted = OldAA->getObsoleted(); |
| 2193 | bool OldIsUnavailable = OldAA->getUnavailable(); |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2194 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2195 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl) || |
| 2196 | !versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl) || |
| 2197 | !versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl) || |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2198 | !(OldIsUnavailable == IsUnavailable || |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2199 | (OverrideOrImpl && !OldIsUnavailable && IsUnavailable))) { |
| 2200 | if (OverrideOrImpl) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2201 | int Which = -1; |
| 2202 | VersionTuple FirstVersion; |
| 2203 | VersionTuple SecondVersion; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2204 | if (!versionsMatch(OldIntroduced, Introduced, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2205 | Which = 0; |
| 2206 | FirstVersion = OldIntroduced; |
| 2207 | SecondVersion = Introduced; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2208 | } else if (!versionsMatch(Deprecated, OldDeprecated, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2209 | Which = 1; |
| 2210 | FirstVersion = Deprecated; |
| 2211 | SecondVersion = OldDeprecated; |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2212 | } else if (!versionsMatch(Obsoleted, OldObsoleted, OverrideOrImpl)) { |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2213 | Which = 2; |
| 2214 | FirstVersion = Obsoleted; |
| 2215 | SecondVersion = OldObsoleted; |
| 2216 | } |
| 2217 | |
| 2218 | if (Which == -1) { |
| 2219 | Diag(OldAA->getLocation(), |
| 2220 | diag::warn_mismatched_availability_override_unavail) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2221 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
| 2222 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2223 | } else { |
| 2224 | Diag(OldAA->getLocation(), |
| 2225 | diag::warn_mismatched_availability_override) |
| 2226 | << Which |
| 2227 | << AvailabilityAttr::getPrettyPlatformName(Platform->getName()) |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2228 | << FirstVersion.getAsString() << SecondVersion.getAsString() |
| 2229 | << (AMK == AMK_Override); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2230 | } |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2231 | if (AMK == AMK_Override) |
| 2232 | Diag(Range.getBegin(), diag::note_overridden_method); |
| 2233 | else |
| 2234 | Diag(Range.getBegin(), diag::note_protocol_method); |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2235 | } else { |
| 2236 | Diag(OldAA->getLocation(), diag::warn_mismatched_availability); |
| 2237 | Diag(Range.getBegin(), diag::note_previous_attribute); |
| 2238 | } |
| 2239 | |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2240 | Attrs.erase(Attrs.begin() + i); |
| 2241 | --e; |
| 2242 | continue; |
| 2243 | } |
| 2244 | |
| 2245 | VersionTuple MergedIntroduced2 = MergedIntroduced; |
| 2246 | VersionTuple MergedDeprecated2 = MergedDeprecated; |
| 2247 | VersionTuple MergedObsoleted2 = MergedObsoleted; |
| 2248 | |
| 2249 | if (MergedIntroduced2.empty()) |
| 2250 | MergedIntroduced2 = OldIntroduced; |
| 2251 | if (MergedDeprecated2.empty()) |
| 2252 | MergedDeprecated2 = OldDeprecated; |
| 2253 | if (MergedObsoleted2.empty()) |
| 2254 | MergedObsoleted2 = OldObsoleted; |
| 2255 | |
| 2256 | if (checkAvailabilityAttr(*this, OldAA->getRange(), Platform, |
| 2257 | MergedIntroduced2, MergedDeprecated2, |
| 2258 | MergedObsoleted2)) { |
| 2259 | Attrs.erase(Attrs.begin() + i); |
| 2260 | --e; |
| 2261 | continue; |
| 2262 | } |
| 2263 | |
| 2264 | MergedIntroduced = MergedIntroduced2; |
| 2265 | MergedDeprecated = MergedDeprecated2; |
| 2266 | MergedObsoleted = MergedObsoleted2; |
| 2267 | ++i; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2268 | } |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
| 2271 | if (FoundAny && |
| 2272 | MergedIntroduced == Introduced && |
| 2273 | MergedDeprecated == Deprecated && |
| 2274 | MergedObsoleted == Obsoleted) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2275 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2276 | |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2277 | // Only create a new attribute if !OverrideOrImpl, but we want to do |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2278 | // the checking. |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2279 | if (!checkAvailabilityAttr(*this, Range, Platform, MergedIntroduced, |
Ted Kremenek | b544572 | 2013-04-06 00:34:27 +0000 | [diff] [blame] | 2280 | MergedDeprecated, MergedObsoleted) && |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2281 | !OverrideOrImpl) { |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2282 | auto *Avail = ::new (Context) AvailabilityAttr(Range, Context, Platform, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2283 | Introduced, Deprecated, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2284 | Obsoleted, IsUnavailable, Message, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2285 | IsStrict, Replacement, |
| 2286 | AttrSpellingListIndex); |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2287 | Avail->setImplicit(Implicit); |
| 2288 | return Avail; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2289 | } |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2290 | return nullptr; |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2293 | static void handleAvailabilityAttr(Sema &S, Decl *D, |
| 2294 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2295 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 2296 | return; |
| 2297 | IdentifierLoc *Platform = Attr.getArgAsIdent(0); |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2298 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2299 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2300 | IdentifierInfo *II = Platform->Ident; |
| 2301 | if (AvailabilityAttr::getPrettyPlatformName(II->getName()).empty()) |
| 2302 | S.Diag(Platform->Loc, diag::warn_availability_unknown_platform) |
| 2303 | << Platform->Ident; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2304 | |
Rafael Espindola | c231fab | 2013-01-08 21:30:32 +0000 | [diff] [blame] | 2305 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 2306 | if (!ND) { |
| 2307 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2308 | return; |
| 2309 | } |
| 2310 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2311 | AvailabilityChange Introduced = Attr.getAvailabilityIntroduced(); |
| 2312 | AvailabilityChange Deprecated = Attr.getAvailabilityDeprecated(); |
| 2313 | AvailabilityChange Obsoleted = Attr.getAvailabilityObsoleted(); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 2314 | bool IsUnavailable = Attr.getUnavailableLoc().isValid(); |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2315 | bool IsStrict = Attr.getStrictLoc().isValid(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2316 | StringRef Str; |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 2317 | if (const StringLiteral *SE = |
| 2318 | dyn_cast_or_null<StringLiteral>(Attr.getMessageExpr())) |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 2319 | Str = SE->getString(); |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2320 | StringRef Replacement; |
| 2321 | if (const StringLiteral *SE = |
| 2322 | dyn_cast_or_null<StringLiteral>(Attr.getReplacementExpr())) |
| 2323 | Replacement = SE->getString(); |
Rafael Espindola | 2d243bf | 2012-05-06 19:56:25 +0000 | [diff] [blame] | 2324 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2325 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, Attr.getRange(), II, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2326 | false/*Implicit*/, |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2327 | Introduced.Version, |
| 2328 | Deprecated.Version, |
| 2329 | Obsoleted.Version, |
Douglas Gregor | 66a8ca0 | 2013-01-15 22:43:08 +0000 | [diff] [blame] | 2330 | IsUnavailable, Str, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2331 | IsStrict, Replacement, |
Douglas Gregor | d2a713e | 2015-09-30 21:27:42 +0000 | [diff] [blame] | 2332 | Sema::AMK_None, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2333 | Index); |
Rafael Espindola | 19de561 | 2013-01-12 06:42:30 +0000 | [diff] [blame] | 2334 | if (NewAttr) |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2335 | D->addAttr(NewAttr); |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2336 | |
| 2337 | // Transcribe "ios" to "watchos" (and add a new attribute) if the versioning |
| 2338 | // matches before the start of the watchOS platform. |
| 2339 | if (S.Context.getTargetInfo().getTriple().isWatchOS()) { |
| 2340 | IdentifierInfo *NewII = nullptr; |
| 2341 | if (II->getName() == "ios") |
| 2342 | NewII = &S.Context.Idents.get("watchos"); |
| 2343 | else if (II->getName() == "ios_app_extension") |
| 2344 | NewII = &S.Context.Idents.get("watchos_app_extension"); |
| 2345 | |
| 2346 | if (NewII) { |
| 2347 | auto adjustWatchOSVersion = [](VersionTuple Version) -> VersionTuple { |
| 2348 | if (Version.empty()) |
| 2349 | return Version; |
| 2350 | auto Major = Version.getMajor(); |
| 2351 | auto NewMajor = Major >= 9 ? Major - 7 : 0; |
| 2352 | if (NewMajor >= 2) { |
| 2353 | if (Version.getMinor().hasValue()) { |
| 2354 | if (Version.getSubminor().hasValue()) |
| 2355 | return VersionTuple(NewMajor, Version.getMinor().getValue(), |
| 2356 | Version.getSubminor().getValue()); |
| 2357 | else |
| 2358 | return VersionTuple(NewMajor, Version.getMinor().getValue()); |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | return VersionTuple(2, 0); |
| 2363 | }; |
| 2364 | |
| 2365 | auto NewIntroduced = adjustWatchOSVersion(Introduced.Version); |
| 2366 | auto NewDeprecated = adjustWatchOSVersion(Deprecated.Version); |
| 2367 | auto NewObsoleted = adjustWatchOSVersion(Obsoleted.Version); |
| 2368 | |
| 2369 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2370 | Attr.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2371 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2372 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2373 | NewIntroduced, |
| 2374 | NewDeprecated, |
| 2375 | NewObsoleted, |
| 2376 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2377 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2378 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2379 | Sema::AMK_None, |
| 2380 | Index); |
| 2381 | if (NewAttr) |
| 2382 | D->addAttr(NewAttr); |
| 2383 | } |
| 2384 | } else if (S.Context.getTargetInfo().getTriple().isTvOS()) { |
| 2385 | // Transcribe "ios" to "tvos" (and add a new attribute) if the versioning |
| 2386 | // matches before the start of the tvOS platform. |
| 2387 | IdentifierInfo *NewII = nullptr; |
| 2388 | if (II->getName() == "ios") |
| 2389 | NewII = &S.Context.Idents.get("tvos"); |
| 2390 | else if (II->getName() == "ios_app_extension") |
| 2391 | NewII = &S.Context.Idents.get("tvos_app_extension"); |
| 2392 | |
| 2393 | if (NewII) { |
| 2394 | AvailabilityAttr *NewAttr = S.mergeAvailabilityAttr(ND, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2395 | Attr.getRange(), |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2396 | NewII, |
Manman Ren | 719a864 | 2016-05-06 21:04:01 +0000 | [diff] [blame] | 2397 | true/*Implicit*/, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2398 | Introduced.Version, |
| 2399 | Deprecated.Version, |
| 2400 | Obsoleted.Version, |
| 2401 | IsUnavailable, Str, |
Manman Ren | d8039df | 2016-02-22 04:47:24 +0000 | [diff] [blame] | 2402 | IsStrict, |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 2403 | Replacement, |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 2404 | Sema::AMK_None, |
| 2405 | Index); |
| 2406 | if (NewAttr) |
| 2407 | D->addAttr(NewAttr); |
| 2408 | } |
| 2409 | } |
Rafael Espindola | c67f223 | 2012-05-10 02:50:16 +0000 | [diff] [blame] | 2410 | } |
| 2411 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2412 | template <class T> |
| 2413 | static T *mergeVisibilityAttr(Sema &S, Decl *D, SourceRange range, |
| 2414 | typename T::VisibilityType value, |
| 2415 | unsigned attrSpellingListIndex) { |
| 2416 | T *existingAttr = D->getAttr<T>(); |
| 2417 | if (existingAttr) { |
| 2418 | typename T::VisibilityType existingValue = existingAttr->getVisibility(); |
| 2419 | if (existingValue == value) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2420 | return nullptr; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2421 | S.Diag(existingAttr->getLocation(), diag::err_mismatched_visibility); |
| 2422 | S.Diag(range.getBegin(), diag::note_previous_attribute); |
| 2423 | D->dropAttr<T>(); |
| 2424 | } |
| 2425 | return ::new (S.Context) T(range, S.Context, value, attrSpellingListIndex); |
| 2426 | } |
| 2427 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2428 | VisibilityAttr *Sema::mergeVisibilityAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2429 | VisibilityAttr::VisibilityType Vis, |
| 2430 | unsigned AttrSpellingListIndex) { |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2431 | return ::mergeVisibilityAttr<VisibilityAttr>(*this, D, Range, Vis, |
| 2432 | AttrSpellingListIndex); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2435 | TypeVisibilityAttr *Sema::mergeTypeVisibilityAttr(Decl *D, SourceRange Range, |
| 2436 | TypeVisibilityAttr::VisibilityType Vis, |
| 2437 | unsigned AttrSpellingListIndex) { |
| 2438 | return ::mergeVisibilityAttr<TypeVisibilityAttr>(*this, D, Range, Vis, |
| 2439 | AttrSpellingListIndex); |
| 2440 | } |
| 2441 | |
| 2442 | static void handleVisibilityAttr(Sema &S, Decl *D, const AttributeList &Attr, |
| 2443 | bool isTypeVisibility) { |
| 2444 | // Visibility attributes don't mean anything on a typedef. |
| 2445 | if (isa<TypedefNameDecl>(D)) { |
| 2446 | S.Diag(Attr.getRange().getBegin(), diag::warn_attribute_ignored) |
| 2447 | << Attr.getName(); |
| 2448 | return; |
| 2449 | } |
| 2450 | |
| 2451 | // 'type_visibility' can only go on a type or namespace. |
| 2452 | if (isTypeVisibility && |
| 2453 | !(isa<TagDecl>(D) || |
| 2454 | isa<ObjCInterfaceDecl>(D) || |
| 2455 | isa<NamespaceDecl>(D))) { |
| 2456 | S.Diag(Attr.getRange().getBegin(), diag::err_attribute_wrong_decl_type) |
| 2457 | << Attr.getName() << ExpectedTypeOrNamespace; |
| 2458 | return; |
| 2459 | } |
| 2460 | |
Benjamin Kramer | 7037021 | 2013-09-09 15:08:57 +0000 | [diff] [blame] | 2461 | // Check that the argument is a string literal. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2462 | StringRef TypeStr; |
| 2463 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2464 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, TypeStr, &LiteralLoc)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2465 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2466 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2467 | VisibilityAttr::VisibilityType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2468 | if (!VisibilityAttr::ConvertStrToVisibilityType(TypeStr, type)) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2469 | S.Diag(LiteralLoc, diag::warn_attribute_type_not_supported) |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2470 | << Attr.getName() << TypeStr; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2471 | return; |
| 2472 | } |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2473 | |
| 2474 | // Complain about attempts to use protected visibility on targets |
| 2475 | // (like Darwin) that don't support it. |
| 2476 | if (type == VisibilityAttr::Protected && |
| 2477 | !S.Context.getTargetInfo().hasProtectedVisibility()) { |
| 2478 | S.Diag(Attr.getLoc(), diag::warn_attribute_protected_visibility); |
| 2479 | type = VisibilityAttr::Default; |
| 2480 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2481 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2482 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 2483 | clang::Attr *newAttr; |
| 2484 | if (isTypeVisibility) { |
| 2485 | newAttr = S.mergeTypeVisibilityAttr(D, Attr.getRange(), |
| 2486 | (TypeVisibilityAttr::VisibilityType) type, |
| 2487 | Index); |
| 2488 | } else { |
| 2489 | newAttr = S.mergeVisibilityAttr(D, Attr.getRange(), type, Index); |
| 2490 | } |
| 2491 | if (newAttr) |
| 2492 | D->addAttr(newAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2495 | static void handleObjCMethodFamilyAttr(Sema &S, Decl *decl, |
| 2496 | const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2497 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(decl); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2498 | if (!Attr.isArgIdent(0)) { |
| 2499 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
| 2500 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2501 | return; |
| 2502 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2503 | |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2504 | IdentifierLoc *IL = Attr.getArgAsIdent(0); |
| 2505 | ObjCMethodFamilyAttr::FamilyKind F; |
| 2506 | if (!ObjCMethodFamilyAttr::ConvertStrToFamilyKind(IL->Ident->getName(), F)) { |
| 2507 | S.Diag(IL->Loc, diag::warn_attribute_type_not_supported) << Attr.getName() |
| 2508 | << IL->Ident; |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2509 | return; |
| 2510 | } |
| 2511 | |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2512 | if (F == ObjCMethodFamilyAttr::OMF_init && |
| 2513 | !method->getReturnType()->isObjCObjectPointerType()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2514 | S.Diag(method->getLocation(), diag::err_init_method_bad_return_type) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2515 | << method->getReturnType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2516 | // Ignore the attribute. |
| 2517 | return; |
| 2518 | } |
| 2519 | |
Argyrios Kyrtzidis | 309b4c4 | 2011-09-13 16:05:58 +0000 | [diff] [blame] | 2520 | method->addAttr(new (S.Context) ObjCMethodFamilyAttr(Attr.getRange(), |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2521 | S.Context, F, |
| 2522 | Attr.getAttributeSpellingListIndex())); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2525 | static void handleObjCNSObject(Sema &S, Decl *D, const AttributeList &Attr) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2526 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2527 | QualType T = TD->getUnderlyingType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2528 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2529 | S.Diag(TD->getLocation(), diag::err_nsobject_attribute); |
| 2530 | return; |
| 2531 | } |
| 2532 | } |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2533 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) { |
| 2534 | QualType T = PD->getType(); |
Ted Kremenek | 7712eef | 2012-08-29 22:54:47 +0000 | [diff] [blame] | 2535 | if (!T->isCARCBridgableType()) { |
Fariborz Jahanian | bebd0ba | 2012-05-31 23:18:32 +0000 | [diff] [blame] | 2536 | S.Diag(PD->getLocation(), diag::err_nsobject_attribute); |
| 2537 | return; |
| 2538 | } |
| 2539 | } |
| 2540 | else { |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2541 | // It is okay to include this attribute on properties, e.g.: |
| 2542 | // |
| 2543 | // @property (retain, nonatomic) struct Bork *Q __attribute__((NSObject)); |
| 2544 | // |
| 2545 | // In this case it follows tradition and suppresses an error in the above |
| 2546 | // case. |
Fariborz Jahanian | a45495a | 2011-11-29 01:48:40 +0000 | [diff] [blame] | 2547 | S.Diag(D->getLocation(), diag::warn_nsobject_attribute); |
Ted Kremenek | 05e916b | 2012-03-01 01:40:32 +0000 | [diff] [blame] | 2548 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2549 | D->addAttr(::new (S.Context) |
| 2550 | ObjCNSObjectAttr(Attr.getRange(), S.Context, |
| 2551 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 2552 | } |
| 2553 | |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 2554 | static void handleObjCIndependentClass(Sema &S, Decl *D, const AttributeList &Attr) { |
| 2555 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) { |
| 2556 | QualType T = TD->getUnderlyingType(); |
| 2557 | if (!T->isObjCObjectPointerType()) { |
| 2558 | S.Diag(TD->getLocation(), diag::warn_ptr_independentclass_attribute); |
| 2559 | return; |
| 2560 | } |
| 2561 | } else { |
| 2562 | S.Diag(D->getLocation(), diag::warn_independentclass_attribute); |
| 2563 | return; |
| 2564 | } |
| 2565 | D->addAttr(::new (S.Context) |
| 2566 | ObjCIndependentClassAttr(Attr.getRange(), S.Context, |
| 2567 | Attr.getAttributeSpellingListIndex())); |
| 2568 | } |
| 2569 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2570 | static void handleBlocksAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2571 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2572 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2573 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2574 | return; |
| 2575 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2576 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2577 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 2578 | BlocksAttr::BlockType type; |
Aaron Ballman | 682ee42 | 2013-09-11 19:47:58 +0000 | [diff] [blame] | 2579 | if (!BlocksAttr::ConvertStrToBlockType(II->getName(), type)) { |
| 2580 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 2581 | << Attr.getName() << II; |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2582 | return; |
| 2583 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2584 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2585 | D->addAttr(::new (S.Context) |
| 2586 | BlocksAttr(Attr.getRange(), S.Context, type, |
| 2587 | Attr.getAttributeSpellingListIndex())); |
Steve Naroff | 3405a73 | 2008-09-18 16:44:58 +0000 | [diff] [blame] | 2588 | } |
| 2589 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2590 | static void handleSentinelAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2591 | unsigned sentinel = (unsigned)SentinelAttr::DefaultSentinel; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2592 | if (Attr.getNumArgs() > 0) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2593 | Expr *E = Attr.getArgAsExpr(0); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2594 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2595 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2596 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2597 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2598 | << Attr.getName() << 1 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2599 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2600 | return; |
| 2601 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2602 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2603 | if (Idx.isSigned() && Idx.isNegative()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2604 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_less_than_zero) |
| 2605 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2606 | return; |
| 2607 | } |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2608 | |
| 2609 | sentinel = Idx.getZExtValue(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2610 | } |
| 2611 | |
Aaron Ballman | 18a7838 | 2013-11-21 00:28:23 +0000 | [diff] [blame] | 2612 | unsigned nullPos = (unsigned)SentinelAttr::DefaultNullPos; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2613 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2614 | Expr *E = Attr.getArgAsExpr(1); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2615 | llvm::APSInt Idx(32); |
Douglas Gregor | bdb604a | 2010-05-18 23:01:22 +0000 | [diff] [blame] | 2616 | if (E->isTypeDependent() || E->isValueDependent() || |
| 2617 | !E->isIntegerConstantExpr(Idx, S.Context)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2618 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 2619 | << Attr.getName() << 2 << AANT_ArgumentIntegerConstant |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 2620 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2621 | return; |
| 2622 | } |
| 2623 | nullPos = Idx.getZExtValue(); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2624 | |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2625 | if ((Idx.isSigned() && Idx.isNegative()) || nullPos > 1) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2626 | // FIXME: This error message could be improved, it would be nice |
| 2627 | // to say what the bounds actually are. |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2628 | S.Diag(Attr.getLoc(), diag::err_attribute_sentinel_not_zero_or_one) |
| 2629 | << E->getSourceRange(); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2630 | return; |
| 2631 | } |
| 2632 | } |
| 2633 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2634 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | b46f287 | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 2635 | const FunctionType *FT = FD->getType()->castAs<FunctionType>(); |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2636 | if (isa<FunctionNoProtoType>(FT)) { |
| 2637 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_named_arguments); |
| 2638 | return; |
| 2639 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2640 | |
Chris Lattner | 9363e31 | 2009-03-17 23:03:47 +0000 | [diff] [blame] | 2641 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2642 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2643 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2644 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2645 | } else if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2646 | if (!MD->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2647 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 0; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2648 | return; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2649 | } |
Eli Friedman | 5c5e3b7 | 2012-01-06 01:23:10 +0000 | [diff] [blame] | 2650 | } else if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 2651 | if (!BD->isVariadic()) { |
| 2652 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << 1; |
| 2653 | return; |
| 2654 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2655 | } else if (const VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2656 | QualType Ty = V->getType(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 2657 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 2658 | const FunctionType *FT = Ty->isFunctionPointerType() |
| 2659 | ? D->getFunctionType() |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 2660 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2661 | if (!cast<FunctionProtoType>(FT)->isVariadic()) { |
Fariborz Jahanian | 6802ed9 | 2009-05-15 21:18:04 +0000 | [diff] [blame] | 2662 | int m = Ty->isFunctionPointerType() ? 0 : 1; |
| 2663 | S.Diag(Attr.getLoc(), diag::warn_attribute_sentinel_not_variadic) << m; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2664 | return; |
| 2665 | } |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 2666 | } else { |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2667 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2668 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Fariborz Jahanian | 6607b21 | 2009-05-14 20:53:39 +0000 | [diff] [blame] | 2669 | return; |
| 2670 | } |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2671 | } else { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 2672 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2673 | << Attr.getName() << ExpectedFunctionMethodOrBlock; |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2674 | return; |
| 2675 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2676 | D->addAttr(::new (S.Context) |
| 2677 | SentinelAttr(Attr.getRange(), S.Context, sentinel, nullPos, |
| 2678 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | c181b01 | 2008-10-05 18:05:59 +0000 | [diff] [blame] | 2679 | } |
| 2680 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2681 | static void handleWarnUnusedResult(Sema &S, Decl *D, const AttributeList &Attr) { |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2682 | if (D->getFunctionType() && |
| 2683 | D->getFunctionType()->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2684 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2685 | << Attr.getName() << 0; |
Nuno Lopes | 56abcbd | 2009-12-22 23:59:52 +0000 | [diff] [blame] | 2686 | return; |
| 2687 | } |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2688 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 2689 | if (MD->getReturnType()->isVoidType()) { |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2690 | S.Diag(Attr.getLoc(), diag::warn_attribute_void_function_method) |
| 2691 | << Attr.getName() << 1; |
| 2692 | return; |
| 2693 | } |
| 2694 | |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2695 | // If this is spelled as the standard C++1z attribute, but not in C++1z, warn |
| 2696 | // about using it as an extension. |
| 2697 | if (!S.getLangOpts().CPlusPlus1z && Attr.isCXX11Attribute() && |
| 2698 | !Attr.getScopeName()) |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 2699 | S.Diag(Attr.getLoc(), diag::ext_cxx1z_attr) << Attr.getName(); |
Aaron Ballman | e796478 | 2016-03-07 22:44:55 +0000 | [diff] [blame] | 2700 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2701 | D->addAttr(::new (S.Context) |
| 2702 | WarnUnusedResultAttr(Attr.getRange(), S.Context, |
| 2703 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2706 | static void handleWeakImportAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2707 | // weak_import only applies to variable & function declarations. |
| 2708 | bool isDef = false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 2709 | if (!D->canBeWeakImported(isDef)) { |
| 2710 | if (isDef) |
Reid Kleckner | 52d598e | 2013-05-20 21:53:29 +0000 | [diff] [blame] | 2711 | S.Diag(Attr.getLoc(), diag::warn_attribute_invalid_on_definition) |
| 2712 | << "weak_import"; |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2713 | else if (isa<ObjCPropertyDecl>(D) || isa<ObjCMethodDecl>(D) || |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2714 | (S.Context.getTargetInfo().getTriple().isOSDarwin() && |
Fariborz Jahanian | 3249a1e | 2011-10-26 23:59:12 +0000 | [diff] [blame] | 2715 | (isa<ObjCInterfaceDecl>(D) || isa<EnumDecl>(D)))) { |
Douglas Gregor | d71149a | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 2716 | // Nothing to warn about here. |
| 2717 | } else |
Fariborz Jahanian | ea70a17 | 2010-04-13 20:22:35 +0000 | [diff] [blame] | 2718 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 2719 | << Attr.getName() << ExpectedVariableOrFunction; |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2720 | |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2721 | return; |
| 2722 | } |
| 2723 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2724 | D->addAttr(::new (S.Context) |
| 2725 | WeakImportAttr(Attr.getRange(), S.Context, |
| 2726 | Attr.getAttributeSpellingListIndex())); |
Daniel Dunbar | 5cb85eb | 2009-03-06 06:39:57 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2729 | // Handles reqd_work_group_size and work_group_size_hint. |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2730 | template <typename WorkGroupAttr> |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2731 | static void handleWorkGroupSize(Sema &S, Decl *D, |
Nick Lewycky | b9e4a3a | 2012-07-24 01:31:55 +0000 | [diff] [blame] | 2732 | const AttributeList &Attr) { |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 2733 | uint32_t WGSize[3]; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2734 | for (unsigned i = 0; i < 3; ++i) { |
| 2735 | const Expr *E = Attr.getArgAsExpr(i); |
| 2736 | if (!checkUInt32Argument(S, Attr, E, WGSize[i], i)) |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2737 | return; |
Joey Gouly | b1d23a8 | 2014-05-19 14:41:38 +0000 | [diff] [blame] | 2738 | if (WGSize[i] == 0) { |
| 2739 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_is_zero) |
| 2740 | << Attr.getName() << E->getSourceRange(); |
| 2741 | return; |
| 2742 | } |
| 2743 | } |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2744 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2745 | WorkGroupAttr *Existing = D->getAttr<WorkGroupAttr>(); |
| 2746 | if (Existing && !(Existing->getXDim() == WGSize[0] && |
| 2747 | Existing->getYDim() == WGSize[1] && |
| 2748 | Existing->getZDim() == WGSize[2])) |
| 2749 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 2750 | |
Aaron Ballman | 1d0d2a4 | 2013-12-02 22:38:33 +0000 | [diff] [blame] | 2751 | D->addAttr(::new (S.Context) WorkGroupAttr(Attr.getRange(), S.Context, |
| 2752 | WGSize[0], WGSize[1], WGSize[2], |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2753 | Attr.getAttributeSpellingListIndex())); |
Nate Begeman | f275870 | 2009-06-26 06:32:41 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2756 | static void handleVecTypeHint(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2757 | if (!Attr.hasParsedType()) { |
| 2758 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 2759 | << Attr.getName() << 1; |
| 2760 | return; |
| 2761 | } |
| 2762 | |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2763 | TypeSourceInfo *ParmTSI = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2764 | QualType ParmType = S.GetTypeFromParser(Attr.getTypeArg(), &ParmTSI); |
| 2765 | assert(ParmTSI && "no type source info for attribute argument"); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2766 | |
| 2767 | if (!ParmType->isExtVectorType() && !ParmType->isFloatingType() && |
| 2768 | (ParmType->isBooleanType() || |
| 2769 | !ParmType->isIntegralType(S.getASTContext()))) { |
| 2770 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_vec_type_hint) |
| 2771 | << ParmType; |
| 2772 | return; |
| 2773 | } |
| 2774 | |
Aaron Ballman | a9e0540 | 2013-12-02 22:16:55 +0000 | [diff] [blame] | 2775 | if (VecTypeHintAttr *A = D->getAttr<VecTypeHintAttr>()) { |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 2776 | if (!S.Context.hasSameType(A->getTypeHint(), ParmType)) { |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2777 | S.Diag(Attr.getLoc(), diag::warn_duplicate_attribute) << Attr.getName(); |
| 2778 | return; |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | D->addAttr(::new (S.Context) VecTypeHintAttr(Attr.getLoc(), S.Context, |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 2783 | ParmTSI, |
| 2784 | Attr.getAttributeSpellingListIndex())); |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2787 | SectionAttr *Sema::mergeSectionAttr(Decl *D, SourceRange Range, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2788 | StringRef Name, |
| 2789 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2790 | if (SectionAttr *ExistingAttr = D->getAttr<SectionAttr>()) { |
| 2791 | if (ExistingAttr->getName() == Name) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2792 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2793 | Diag(ExistingAttr->getLocation(), diag::warn_mismatched_section); |
| 2794 | Diag(Range.getBegin(), diag::note_previous_attribute); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2795 | return nullptr; |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2796 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2797 | return ::new (Context) SectionAttr(Range, Context, Name, |
| 2798 | AttrSpellingListIndex); |
Rafael Espindola | 9869c3a | 2012-05-13 02:42:42 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2801 | bool Sema::checkSectionName(SourceLocation LiteralLoc, StringRef SecName) { |
| 2802 | std::string Error = Context.getTargetInfo().isValidSectionSpecifier(SecName); |
| 2803 | if (!Error.empty()) { |
| 2804 | Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) << Error; |
| 2805 | return false; |
| 2806 | } |
| 2807 | return true; |
| 2808 | } |
| 2809 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2810 | static void handleSectionAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2811 | // Make sure that there is a string literal as the sections's single |
| 2812 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2813 | StringRef Str; |
| 2814 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 2815 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2816 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2817 | |
Reid Kleckner | 2a13322 | 2015-03-04 23:39:17 +0000 | [diff] [blame] | 2818 | if (!S.checkSectionName(LiteralLoc, Str)) |
| 2819 | return; |
| 2820 | |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2821 | // If the target wants to validate the section specifier, make it happen. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2822 | std::string Error = S.Context.getTargetInfo().isValidSectionSpecifier(Str); |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2823 | if (!Error.empty()) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2824 | S.Diag(LiteralLoc, diag::err_attribute_section_invalid_for_target) |
Chris Lattner | 20aee9b | 2010-01-12 20:58:53 +0000 | [diff] [blame] | 2825 | << Error; |
Chris Lattner | 30ba674 | 2009-08-10 19:03:04 +0000 | [diff] [blame] | 2826 | return; |
| 2827 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2828 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2829 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 2830 | SectionAttr *NewAttr = S.mergeSectionAttr(D, Attr.getRange(), Str, Index); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 2831 | if (NewAttr) |
| 2832 | D->addAttr(NewAttr); |
Daniel Dunbar | 648bf78 | 2009-02-12 17:28:23 +0000 | [diff] [blame] | 2833 | } |
| 2834 | |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2835 | // Check for things we'd like to warn about, no errors or validation for now. |
| 2836 | // TODO: Validation should use a backend target library that specifies |
| 2837 | // the allowable subtarget features and cpus. We could use something like a |
| 2838 | // TargetCodeGenInfo hook here to do validation. |
| 2839 | void Sema::checkTargetAttr(SourceLocation LiteralLoc, StringRef AttrStr) { |
| 2840 | for (auto Str : {"tune=", "fpmath="}) |
| 2841 | if (AttrStr.find(Str) != StringRef::npos) |
| 2842 | Diag(LiteralLoc, diag::warn_unsupported_target_attribute) << Str; |
| 2843 | } |
| 2844 | |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2845 | static void handleTargetAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2846 | StringRef Str; |
| 2847 | SourceLocation LiteralLoc; |
| 2848 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &LiteralLoc)) |
| 2849 | return; |
Eric Christopher | 789a7ad | 2015-06-12 01:36:05 +0000 | [diff] [blame] | 2850 | S.checkTargetAttr(LiteralLoc, Str); |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 2851 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 2852 | TargetAttr *NewAttr = |
| 2853 | ::new (S.Context) TargetAttr(Attr.getRange(), S.Context, Str, Index); |
| 2854 | D->addAttr(NewAttr); |
| 2855 | } |
| 2856 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2857 | static void handleCleanupAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 2858 | VarDecl *VD = cast<VarDecl>(D); |
| 2859 | if (!VD->hasLocalStorage()) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2860 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2861 | return; |
| 2862 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2863 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2864 | Expr *E = Attr.getArgAsExpr(0); |
| 2865 | SourceLocation Loc = E->getExprLoc(); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 2866 | FunctionDecl *FD = nullptr; |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2867 | DeclarationNameInfo NI; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2868 | |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2869 | // gcc only allows for simple identifiers. Since we support more than gcc, we |
| 2870 | // will warn the user. |
| 2871 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 2872 | if (DRE->hasQualifier()) |
| 2873 | S.Diag(Loc, diag::warn_cleanup_ext); |
| 2874 | FD = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 2875 | NI = DRE->getNameInfo(); |
| 2876 | if (!FD) { |
| 2877 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 1 |
| 2878 | << NI.getName(); |
| 2879 | return; |
| 2880 | } |
| 2881 | } else if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 2882 | if (ULE->hasExplicitTemplateArgs()) |
| 2883 | S.Diag(Loc, diag::warn_cleanup_ext); |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2884 | FD = S.ResolveSingleFunctionTemplateSpecialization(ULE, true); |
| 2885 | NI = ULE->getNameInfo(); |
Alp Toker | 67b47ac | 2013-10-20 18:48:56 +0000 | [diff] [blame] | 2886 | if (!FD) { |
| 2887 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 2 |
| 2888 | << NI.getName(); |
| 2889 | if (ULE->getType() == S.Context.OverloadTy) |
| 2890 | S.NoteAllOverloadCandidates(ULE); |
| 2891 | return; |
| 2892 | } |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2893 | } else { |
| 2894 | S.Diag(Loc, diag::err_attribute_cleanup_arg_not_function) << 0; |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2895 | return; |
| 2896 | } |
| 2897 | |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2898 | if (FD->getNumParams() != 1) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2899 | S.Diag(Loc, diag::err_attribute_cleanup_func_must_take_one_arg) |
| 2900 | << NI.getName(); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2901 | return; |
| 2902 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2903 | |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2904 | // We're currently more strict than GCC about what function types we accept. |
| 2905 | // If this ever proves to be a problem it should be easy to fix. |
| 2906 | QualType Ty = S.Context.getPointerType(VD->getType()); |
| 2907 | QualType ParamTy = FD->getParamDecl(0)->getType(); |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 2908 | if (S.CheckAssignmentConstraints(FD->getParamDecl(0)->getLocation(), |
| 2909 | ParamTy, Ty) != Sema::Compatible) { |
Aaron Ballman | c12aaff | 2013-09-11 01:37:41 +0000 | [diff] [blame] | 2910 | S.Diag(Loc, diag::err_attribute_cleanup_func_arg_incompatible_type) |
| 2911 | << NI.getName() << ParamTy << Ty; |
Anders Carlsson | 723f55d | 2009-02-07 23:16:50 +0000 | [diff] [blame] | 2912 | return; |
| 2913 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2914 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2915 | D->addAttr(::new (S.Context) |
| 2916 | CleanupAttr(Attr.getRange(), S.Context, FD, |
| 2917 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | d277d79 | 2009-01-31 01:16:18 +0000 | [diff] [blame] | 2918 | } |
| 2919 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2920 | /// Handle __attribute__((format_arg((idx)))) attribute based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2921 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2922 | static void handleFormatArgAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2923 | Expr *IdxExpr = Attr.getArgAsExpr(0); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2924 | uint64_t Idx; |
| 2925 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 1, IdxExpr, Idx)) |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2926 | return; |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 2927 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2928 | // Make sure the format string is really a string. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2929 | QualType Ty = getFunctionOrMethodParamType(D, Idx); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2930 | |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2931 | bool NotNSStringTy = !isNSStringType(Ty, S.Context); |
| 2932 | if (NotNSStringTy && |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2933 | !isCFStringType(Ty, S.Context) && |
| 2934 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2935 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2936 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2937 | << "a string type" << IdxExpr->getSourceRange() |
| 2938 | << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2939 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2940 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 2941 | Ty = getFunctionOrMethodResultType(D); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2942 | if (!isNSStringType(Ty, S.Context) && |
| 2943 | !isCFStringType(Ty, S.Context) && |
| 2944 | (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2945 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType())) { |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2946 | S.Diag(Attr.getLoc(), diag::err_format_attribute_result_not) |
Eric Christopher | b64963e | 2015-08-13 21:34:35 +0000 | [diff] [blame] | 2947 | << (NotNSStringTy ? "string type" : "NSString") |
Aaron Ballman | 2f9e88b | 2014-08-04 15:17:29 +0000 | [diff] [blame] | 2948 | << IdxExpr->getSourceRange() << getFunctionOrMethodParamRange(D, 0); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2949 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 2950 | } |
| 2951 | |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 2952 | // We cannot use the Idx returned from checkFunctionOrMethodParameterIndex |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2953 | // because that has corrected for the implicit this parameter, and is zero- |
| 2954 | // based. The attribute expects what the user wrote explicitly. |
| 2955 | llvm::APSInt Val; |
| 2956 | IdxExpr->EvaluateAsInt(Val, S.Context); |
| 2957 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2958 | D->addAttr(::new (S.Context) |
Aaron Ballman | be50eb8 | 2013-07-30 00:48:57 +0000 | [diff] [blame] | 2959 | FormatArgAttr(Attr.getRange(), S.Context, Val.getZExtValue(), |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 2960 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | f1c2502 | 2009-05-20 17:41:43 +0000 | [diff] [blame] | 2961 | } |
| 2962 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2963 | enum FormatAttrKind { |
| 2964 | CFStringFormat, |
| 2965 | NSStringFormat, |
| 2966 | StrftimeFormat, |
| 2967 | SupportedFormat, |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 2968 | IgnoredFormat, |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2969 | InvalidFormat |
| 2970 | }; |
| 2971 | |
| 2972 | /// getFormatAttrKind - Map from format attribute names to supported format |
| 2973 | /// types. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2974 | static FormatAttrKind getFormatAttrKind(StringRef Format) { |
Benjamin Kramer | 96a44b6 | 2012-05-16 12:44:25 +0000 | [diff] [blame] | 2975 | return llvm::StringSwitch<FormatAttrKind>(Format) |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 2976 | // Check for formats that get handled specially. |
| 2977 | .Case("NSString", NSStringFormat) |
| 2978 | .Case("CFString", CFStringFormat) |
| 2979 | .Case("strftime", StrftimeFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2980 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 2981 | // Otherwise, check for supported formats. |
| 2982 | .Cases("scanf", "printf", "printf0", "strfmon", SupportedFormat) |
| 2983 | .Cases("cmn_err", "vcmn_err", "zcmn_err", SupportedFormat) |
| 2984 | .Case("kprintf", SupportedFormat) // OpenBSD. |
| 2985 | .Case("freebsd_kprintf", SupportedFormat) // FreeBSD. |
| 2986 | .Case("os_trace", SupportedFormat) |
| 2987 | .Case("os_log", SupportedFormat) |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2988 | |
Mehdi Amini | 06d367c | 2016-10-24 20:39:34 +0000 | [diff] [blame] | 2989 | .Cases("gcc_diag", "gcc_cdiag", "gcc_cxxdiag", "gcc_tdiag", IgnoredFormat) |
| 2990 | .Default(InvalidFormat); |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 2991 | } |
| 2992 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2993 | /// Handle __attribute__((init_priority(priority))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2994 | /// http://gcc.gnu.org/onlinedocs/gcc/C_002b_002b-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 2995 | static void handleInitPriorityAttr(Sema &S, Decl *D, |
| 2996 | const AttributeList &Attr) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2997 | if (!S.getLangOpts().CPlusPlus) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 2998 | S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << Attr.getName(); |
| 2999 | return; |
| 3000 | } |
| 3001 | |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3002 | if (S.getCurFunctionOrMethodDecl()) { |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3003 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3004 | Attr.setInvalid(); |
| 3005 | return; |
| 3006 | } |
Aaron Ballman | 4a61115 | 2013-11-27 16:34:09 +0000 | [diff] [blame] | 3007 | QualType T = cast<VarDecl>(D)->getType(); |
Fariborz Jahanian | 0bf5ee7 | 2010-06-18 23:14:53 +0000 | [diff] [blame] | 3008 | if (S.Context.getAsArrayType(T)) |
| 3009 | T = S.Context.getBaseElementType(T); |
| 3010 | if (!T->getAs<RecordType>()) { |
| 3011 | S.Diag(Attr.getLoc(), diag::err_init_priority_object_attr); |
| 3012 | Attr.setInvalid(); |
| 3013 | return; |
| 3014 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3015 | |
| 3016 | Expr *E = Attr.getArgAsExpr(0); |
| 3017 | uint32_t prioritynum; |
| 3018 | if (!checkUInt32Argument(S, Attr, E, prioritynum)) { |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3019 | Attr.setInvalid(); |
| 3020 | return; |
| 3021 | } |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3022 | |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3023 | if (prioritynum < 101 || prioritynum > 65535) { |
| 3024 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_outof_range) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 3025 | << E->getSourceRange() << Attr.getName() << 101 << 65535; |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3026 | Attr.setInvalid(); |
| 3027 | return; |
| 3028 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3029 | D->addAttr(::new (S.Context) |
| 3030 | InitPriorityAttr(Attr.getRange(), S.Context, prioritynum, |
| 3031 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | ef5f621 | 2010-06-18 21:44:06 +0000 | [diff] [blame] | 3032 | } |
| 3033 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3034 | FormatAttr *Sema::mergeFormatAttr(Decl *D, SourceRange Range, |
| 3035 | IdentifierInfo *Format, int FormatIdx, |
| 3036 | int FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3037 | unsigned AttrSpellingListIndex) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3038 | // Check whether we already have an equivalent format attribute. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3039 | for (auto *F : D->specific_attrs<FormatAttr>()) { |
| 3040 | if (F->getType() == Format && |
| 3041 | F->getFormatIdx() == FormatIdx && |
| 3042 | F->getFirstArg() == FirstArg) { |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3043 | // If we don't have a valid location for this attribute, adopt the |
| 3044 | // location. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3045 | if (F->getLocation().isInvalid()) |
| 3046 | F->setRange(Range); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3047 | return nullptr; |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3048 | } |
| 3049 | } |
| 3050 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3051 | return ::new (Context) FormatAttr(Range, Context, Format, FormatIdx, |
| 3052 | FirstArg, AttrSpellingListIndex); |
Rafael Espindola | 92d4945 | 2012-05-11 00:36:07 +0000 | [diff] [blame] | 3053 | } |
| 3054 | |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3055 | /// Handle __attribute__((format(type,idx,firstarg))) attributes based on |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3056 | /// http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3057 | static void handleFormatAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3058 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 3059 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3060 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3061 | return; |
| 3062 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3063 | |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3064 | // In C++ the implicit 'this' function parameter also counts, and they are |
| 3065 | // counted from one. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3066 | bool HasImplicitThisParam = isInstanceMethod(D); |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3067 | unsigned NumArgs = getFunctionOrMethodNumParams(D) + HasImplicitThisParam; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3068 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3069 | IdentifierInfo *II = Attr.getArgAsIdent(0)->Ident; |
| 3070 | StringRef Format = II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3071 | |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 3072 | if (normalizeName(Format)) { |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3073 | // If we've modified the string name, we need a new identifier for it. |
| 3074 | II = &S.Context.Idents.get(Format); |
| 3075 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3076 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3077 | // Check for supported formats. |
| 3078 | FormatAttrKind Kind = getFormatAttrKind(Format); |
Chris Lattner | 12161d3 | 2010-03-22 21:08:50 +0000 | [diff] [blame] | 3079 | |
| 3080 | if (Kind == IgnoredFormat) |
| 3081 | return; |
| 3082 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3083 | if (Kind == InvalidFormat) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3084 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
Aaron Ballman | 190bad4 | 2013-12-26 16:13:50 +0000 | [diff] [blame] | 3085 | << Attr.getName() << II->getName(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3086 | return; |
| 3087 | } |
| 3088 | |
| 3089 | // checks for the 2nd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3090 | Expr *IdxExpr = Attr.getArgAsExpr(1); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3091 | uint32_t Idx; |
| 3092 | if (!checkUInt32Argument(S, Attr, IdxExpr, Idx, 2)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3093 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3094 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3095 | if (Idx < 1 || Idx > NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3096 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3097 | << Attr.getName() << 2 << IdxExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3098 | return; |
| 3099 | } |
| 3100 | |
| 3101 | // FIXME: Do we need to bounds check? |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3102 | unsigned ArgIdx = Idx - 1; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3103 | |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3104 | if (HasImplicitThisParam) { |
| 3105 | if (ArgIdx == 0) { |
Chandler Carruth | 743682b | 2010-11-16 08:35:43 +0000 | [diff] [blame] | 3106 | S.Diag(Attr.getLoc(), |
| 3107 | diag::err_format_attribute_implicit_this_format_string) |
| 3108 | << IdxExpr->getSourceRange(); |
Sebastian Redl | 6eedcc1 | 2009-11-17 18:02:24 +0000 | [diff] [blame] | 3109 | return; |
| 3110 | } |
| 3111 | ArgIdx--; |
| 3112 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3113 | |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3114 | // make sure the format string is really a string |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 3115 | QualType Ty = getFunctionOrMethodParamType(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3116 | |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3117 | if (Kind == CFStringFormat) { |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3118 | if (!isCFStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3119 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3120 | << "a CFString" << IdxExpr->getSourceRange() |
| 3121 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Daniel Dunbar | 980c669 | 2008-09-26 03:32:58 +0000 | [diff] [blame] | 3122 | return; |
| 3123 | } |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3124 | } else if (Kind == NSStringFormat) { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3125 | // FIXME: do we need to check if the type is NSString*? What are the |
| 3126 | // semantics? |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 3127 | if (!isNSStringType(Ty, S.Context)) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3128 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3129 | << "an NSString" << IdxExpr->getSourceRange() |
| 3130 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3131 | return; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3132 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3133 | } else if (!Ty->isPointerType() || |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3134 | !Ty->getAs<PointerType>()->getPointeeType()->isCharType()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3135 | S.Diag(Attr.getLoc(), diag::err_format_attribute_not) |
Aaron Ballman | dfe8cc5 | 2014-08-04 15:26:33 +0000 | [diff] [blame] | 3136 | << "a string type" << IdxExpr->getSourceRange() |
| 3137 | << getFunctionOrMethodParamRange(D, ArgIdx); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3138 | return; |
| 3139 | } |
| 3140 | |
| 3141 | // check the 3rd argument |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3142 | Expr *FirstArgExpr = Attr.getArgAsExpr(2); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3143 | uint32_t FirstArg; |
| 3144 | if (!checkUInt32Argument(S, Attr, FirstArgExpr, FirstArg, 3)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3145 | return; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3146 | |
| 3147 | // check if the function is variadic if the 3rd argument non-zero |
| 3148 | if (FirstArg != 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3149 | if (isFunctionOrMethodVariadic(D)) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3150 | ++NumArgs; // +1 for ... |
| 3151 | } else { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3152 | S.Diag(D->getLocation(), diag::err_format_attribute_requires_variadic); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3153 | return; |
| 3154 | } |
| 3155 | } |
| 3156 | |
Chris Lattner | 4bd8dd8 | 2008-11-19 08:23:25 +0000 | [diff] [blame] | 3157 | // strftime requires FirstArg to be 0 because it doesn't read from any |
| 3158 | // variable the input is just the current time + the format string. |
Daniel Dunbar | ccbd9a4 | 2009-10-18 02:09:17 +0000 | [diff] [blame] | 3159 | if (Kind == StrftimeFormat) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3160 | if (FirstArg != 0) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3161 | S.Diag(Attr.getLoc(), diag::err_format_strftime_third_parameter) |
| 3162 | << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3163 | return; |
| 3164 | } |
| 3165 | // if 0 it disables parameter checking (to use with e.g. va_list) |
| 3166 | } else if (FirstArg != 0 && FirstArg != NumArgs) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3167 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 3168 | << Attr.getName() << 3 << FirstArgExpr->getSourceRange(); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3169 | return; |
| 3170 | } |
| 3171 | |
Aaron Ballman | f58070b | 2013-09-03 21:02:22 +0000 | [diff] [blame] | 3172 | FormatAttr *NewAttr = S.mergeFormatAttr(D, Attr.getRange(), II, |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 3173 | Idx, FirstArg, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3174 | Attr.getAttributeSpellingListIndex()); |
Rafael Espindola | e200f1c | 2012-05-13 03:25:18 +0000 | [diff] [blame] | 3175 | if (NewAttr) |
| 3176 | D->addAttr(NewAttr); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3177 | } |
| 3178 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3179 | static void handleTransparentUnionAttr(Sema &S, Decl *D, |
| 3180 | const AttributeList &Attr) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3181 | // Try to find the underlying union declaration. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3182 | RecordDecl *RD = nullptr; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3183 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3184 | if (TD && TD->getUnderlyingType()->isUnionType()) |
| 3185 | RD = TD->getUnderlyingType()->getAsUnionType()->getDecl(); |
| 3186 | else |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3187 | RD = dyn_cast<RecordDecl>(D); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3188 | |
| 3189 | if (!RD || !RD->isUnion()) { |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 3190 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
John McCall | 5fca7ea | 2011-03-02 12:29:23 +0000 | [diff] [blame] | 3191 | << Attr.getName() << ExpectedUnion; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3192 | return; |
| 3193 | } |
| 3194 | |
John McCall | f937c02 | 2011-10-07 06:10:15 +0000 | [diff] [blame] | 3195 | if (!RD->isCompleteDefinition()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3196 | S.Diag(Attr.getLoc(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3197 | diag::warn_transparent_union_attribute_not_definition); |
| 3198 | return; |
| 3199 | } |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3200 | |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3201 | RecordDecl::field_iterator Field = RD->field_begin(), |
| 3202 | FieldEnd = RD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3203 | if (Field == FieldEnd) { |
| 3204 | S.Diag(Attr.getLoc(), diag::warn_transparent_union_attribute_zero_fields); |
| 3205 | return; |
| 3206 | } |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3207 | |
David Blaikie | 40ed297 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 3208 | FieldDecl *FirstField = *Field; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3209 | QualType FirstType = FirstField->getType(); |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3210 | if (FirstType->hasFloatingRepresentation() || FirstType->isVectorType()) { |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3211 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 2187266 | 2010-06-30 17:24:13 +0000 | [diff] [blame] | 3212 | diag::warn_transparent_union_attribute_floating) |
| 3213 | << FirstType->isVectorType() << FirstType; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3214 | return; |
| 3215 | } |
| 3216 | |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3217 | if (FirstType->isIncompleteType()) |
| 3218 | return; |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3219 | uint64_t FirstSize = S.Context.getTypeSize(FirstType); |
| 3220 | uint64_t FirstAlign = S.Context.getTypeAlign(FirstType); |
| 3221 | for (; Field != FieldEnd; ++Field) { |
| 3222 | QualType FieldType = Field->getType(); |
Alex Lorenz | 6f4bc4f | 2016-10-06 09:47:29 +0000 | [diff] [blame] | 3223 | if (FieldType->isIncompleteType()) |
| 3224 | return; |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3225 | // FIXME: this isn't fully correct; we also need to test whether the |
| 3226 | // members of the union would all have the same calling convention as the |
| 3227 | // first member of the union. Checking just the size and alignment isn't |
| 3228 | // sufficient (consider structs passed on the stack instead of in registers |
| 3229 | // as an example). |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3230 | if (S.Context.getTypeSize(FieldType) != FirstSize || |
Aaron Ballman | 54fe5eb | 2014-01-28 01:47:34 +0000 | [diff] [blame] | 3231 | S.Context.getTypeAlign(FieldType) > FirstAlign) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3232 | // Warn if we drop the attribute. |
| 3233 | bool isSize = S.Context.getTypeSize(FieldType) != FirstSize; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3234 | unsigned FieldBits = isSize? S.Context.getTypeSize(FieldType) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3235 | : S.Context.getTypeAlign(FieldType); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3236 | S.Diag(Field->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3237 | diag::warn_transparent_union_attribute_field_size_align) |
| 3238 | << isSize << Field->getDeclName() << FieldBits; |
| 3239 | unsigned FirstBits = isSize? FirstSize : FirstAlign; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3240 | S.Diag(FirstField->getLocation(), |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 3241 | diag::note_transparent_union_first_field_size_align) |
| 3242 | << isSize << FirstBits; |
Eli Friedman | 7c9ba6a | 2008-09-02 05:19:23 +0000 | [diff] [blame] | 3243 | return; |
| 3244 | } |
| 3245 | } |
| 3246 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3247 | RD->addAttr(::new (S.Context) |
| 3248 | TransparentUnionAttr(Attr.getRange(), S.Context, |
| 3249 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3252 | static void handleAnnotateAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3253 | // Make sure that there is a string literal as the annotation's single |
| 3254 | // argument. |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3255 | StringRef Str; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 3256 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3257 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3258 | |
| 3259 | // Don't duplicate annotations that are already set. |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3260 | for (const auto *I : D->specific_attrs<AnnotateAttr>()) { |
| 3261 | if (I->getAnnotation() == Str) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3262 | return; |
Julien Lerouge | 5a6b698 | 2011-09-09 22:41:49 +0000 | [diff] [blame] | 3263 | } |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3264 | |
| 3265 | D->addAttr(::new (S.Context) |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 3266 | AnnotateAttr(Attr.getRange(), S.Context, Str, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3267 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3270 | static void handleAlignValueAttr(Sema &S, Decl *D, |
| 3271 | const AttributeList &Attr) { |
| 3272 | S.AddAlignValueAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 3273 | Attr.getAttributeSpellingListIndex()); |
| 3274 | } |
| 3275 | |
| 3276 | void Sema::AddAlignValueAttr(SourceRange AttrRange, Decl *D, Expr *E, |
| 3277 | unsigned SpellingListIndex) { |
| 3278 | AlignValueAttr TmpAttr(AttrRange, Context, E, SpellingListIndex); |
| 3279 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3280 | |
| 3281 | QualType T; |
| 3282 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3283 | T = TD->getUnderlyingType(); |
| 3284 | else if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) |
| 3285 | T = VD->getType(); |
| 3286 | else |
| 3287 | llvm_unreachable("Unknown decl type for align_value"); |
| 3288 | |
| 3289 | if (!T->isDependentType() && !T->isAnyPointerType() && |
| 3290 | !T->isReferenceType() && !T->isMemberPointerType()) { |
| 3291 | Diag(AttrLoc, diag::warn_attribute_pointer_or_reference_only) |
| 3292 | << &TmpAttr /*TmpAttr.getName()*/ << T << D->getSourceRange(); |
| 3293 | return; |
| 3294 | } |
| 3295 | |
| 3296 | if (!E->isValueDependent()) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3297 | llvm::APSInt Alignment; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3298 | ExprResult ICE |
| 3299 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3300 | diag::err_align_value_attribute_argument_not_int, |
| 3301 | /*AllowFold*/ false); |
| 3302 | if (ICE.isInvalid()) |
| 3303 | return; |
| 3304 | |
| 3305 | if (!Alignment.isPowerOf2()) { |
| 3306 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3307 | << E->getSourceRange(); |
| 3308 | return; |
| 3309 | } |
| 3310 | |
| 3311 | D->addAttr(::new (Context) |
| 3312 | AlignValueAttr(AttrRange, Context, ICE.get(), |
| 3313 | SpellingListIndex)); |
| 3314 | return; |
| 3315 | } |
| 3316 | |
| 3317 | // Save dependent expressions in the AST to be instantiated. |
| 3318 | D->addAttr(::new (Context) AlignValueAttr(TmpAttr)); |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3321 | static void handleAlignedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3322 | // check the attribute arguments. |
Chris Lattner | 4a927cb | 2008-06-28 23:36:30 +0000 | [diff] [blame] | 3323 | if (Attr.getNumArgs() > 1) { |
Aaron Ballman | b724338 | 2013-07-23 19:30:11 +0000 | [diff] [blame] | 3324 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) |
| 3325 | << Attr.getName() << 1; |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3326 | return; |
| 3327 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3328 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3329 | if (Attr.getNumArgs() == 0) { |
| 3330 | D->addAttr(::new (S.Context) AlignedAttr(Attr.getRange(), S.Context, |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3331 | true, nullptr, Attr.getAttributeSpellingListIndex())); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3332 | return; |
| 3333 | } |
| 3334 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 3335 | Expr *E = Attr.getArgAsExpr(0); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3336 | if (Attr.isPackExpansion() && !E->containsUnexpandedParameterPack()) { |
| 3337 | S.Diag(Attr.getEllipsisLoc(), |
| 3338 | diag::err_pack_expansion_without_parameter_packs); |
| 3339 | return; |
| 3340 | } |
| 3341 | |
| 3342 | if (!Attr.isPackExpansion() && S.DiagnoseUnexpandedParameterPack(E)) |
| 3343 | return; |
| 3344 | |
David Majnemer | 26a1e0e | 2015-04-07 02:37:09 +0000 | [diff] [blame] | 3345 | if (E->isValueDependent()) { |
| 3346 | if (const auto *TND = dyn_cast<TypedefNameDecl>(D)) { |
| 3347 | if (!TND->getUnderlyingType()->isDependentType()) { |
| 3348 | S.Diag(Attr.getLoc(), diag::err_alignment_dependent_typedef_name) |
| 3349 | << E->getSourceRange(); |
| 3350 | return; |
| 3351 | } |
| 3352 | } |
| 3353 | } |
| 3354 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3355 | S.AddAlignedAttr(Attr.getRange(), D, E, Attr.getAttributeSpellingListIndex(), |
| 3356 | Attr.isPackExpansion()); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, Expr *E, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3360 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3361 | AlignedAttr TmpAttr(AttrRange, Context, true, E, SpellingListIndex); |
| 3362 | SourceLocation AttrLoc = AttrRange.getBegin(); |
| 3363 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3364 | // C++11 alignas(...) and C11 _Alignas(...) have additional requirements. |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3365 | if (TmpAttr.isAlignas()) { |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3366 | // C++11 [dcl.align]p1: |
| 3367 | // An alignment-specifier may be applied to a variable or to a class |
| 3368 | // data member, but it shall not be applied to a bit-field, a function |
| 3369 | // parameter, the formal parameter of a catch clause, or a variable |
| 3370 | // declared with the register storage class specifier. An |
| 3371 | // alignment-specifier may also be applied to the declaration of a class |
| 3372 | // or enumeration type. |
| 3373 | // C11 6.7.5/2: |
| 3374 | // An alignment attribute shall not be specified in a declaration of |
| 3375 | // a typedef, or a bit-field, or a function, or a parameter, or an |
| 3376 | // object declared with the register storage-class specifier. |
| 3377 | int DiagKind = -1; |
| 3378 | if (isa<ParmVarDecl>(D)) { |
| 3379 | DiagKind = 0; |
| 3380 | } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 3381 | if (VD->getStorageClass() == SC_Register) |
| 3382 | DiagKind = 1; |
| 3383 | if (VD->isExceptionVariable()) |
| 3384 | DiagKind = 2; |
| 3385 | } else if (FieldDecl *FD = dyn_cast<FieldDecl>(D)) { |
| 3386 | if (FD->isBitField()) |
| 3387 | DiagKind = 3; |
| 3388 | } else if (!isa<TagDecl>(D)) { |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3389 | Diag(AttrLoc, diag::err_attribute_wrong_decl_type) << &TmpAttr |
Richard Smith | 9eaab4b | 2013-02-01 08:25:07 +0000 | [diff] [blame] | 3390 | << (TmpAttr.isC11() ? ExpectedVariableOrField |
| 3391 | : ExpectedVariableFieldOrTag); |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3392 | return; |
| 3393 | } |
| 3394 | if (DiagKind != -1) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3395 | Diag(AttrLoc, diag::err_alignas_attribute_wrong_decl_type) |
Aaron Ballman | 3d216a5 | 2014-01-02 23:39:11 +0000 | [diff] [blame] | 3396 | << &TmpAttr << DiagKind; |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3397 | return; |
| 3398 | } |
| 3399 | } |
| 3400 | |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3401 | if (E->isTypeDependent() || E->isValueDependent()) { |
| 3402 | // Save dependent expressions in the AST to be instantiated. |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3403 | AlignedAttr *AA = ::new (Context) AlignedAttr(TmpAttr); |
| 3404 | AA->setPackExpansion(IsPackExpansion); |
| 3405 | D->addAttr(AA); |
Chandler Carruth | f40c42f | 2010-06-25 03:22:07 +0000 | [diff] [blame] | 3406 | return; |
| 3407 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3408 | |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3409 | // FIXME: Cache the number on the Attr object? |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3410 | llvm::APSInt Alignment; |
Douglas Gregor | e2b3744 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 3411 | ExprResult ICE |
| 3412 | = VerifyIntegerConstantExpression(E, &Alignment, |
| 3413 | diag::err_aligned_attribute_argument_not_int, |
| 3414 | /*AllowFold*/ false); |
Richard Smith | f4c51d9 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 3415 | if (ICE.isInvalid()) |
Chris Lattner | 4627b74 | 2008-06-28 23:50:44 +0000 | [diff] [blame] | 3416 | return; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3417 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3418 | uint64_t AlignVal = Alignment.getZExtValue(); |
| 3419 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3420 | // C++11 [dcl.align]p2: |
| 3421 | // -- if the constant expression evaluates to zero, the alignment |
| 3422 | // specifier shall have no effect |
| 3423 | // C11 6.7.5p6: |
| 3424 | // An alignment specification of zero has no effect. |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3425 | if (!(TmpAttr.isAlignas() && !Alignment)) { |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3426 | if (!llvm::isPowerOf2_64(AlignVal)) { |
Paul Robinson | d30e2ee | 2015-07-14 20:52:32 +0000 | [diff] [blame] | 3427 | Diag(AttrLoc, diag::err_alignment_not_power_of_two) |
| 3428 | << E->getSourceRange(); |
| 3429 | return; |
| 3430 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3431 | } |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3432 | |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3433 | // Alignment calculations can wrap around if it's greater than 2**28. |
David Majnemer | 29c69db | 2015-07-26 01:48:59 +0000 | [diff] [blame] | 3434 | unsigned MaxValidAlignment = |
| 3435 | Context.getTargetInfo().getTriple().isOSBinFormatCOFF() ? 8192 |
| 3436 | : 268435456; |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3437 | if (AlignVal > MaxValidAlignment) { |
David Majnemer | abecae7 | 2014-02-12 20:36:10 +0000 | [diff] [blame] | 3438 | Diag(AttrLoc, diag::err_attribute_aligned_too_great) << MaxValidAlignment |
| 3439 | << E->getSourceRange(); |
| 3440 | return; |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 3441 | } |
Daniel Dunbar | 6e8c07d | 2009-02-16 23:37:57 +0000 | [diff] [blame] | 3442 | |
David Majnemer | 0be6bd0 | 2015-07-26 09:02:21 +0000 | [diff] [blame] | 3443 | if (Context.getTargetInfo().isTLSSupported()) { |
| 3444 | unsigned MaxTLSAlign = |
| 3445 | Context.toCharUnitsFromBits(Context.getTargetInfo().getMaxTLSAlign()) |
| 3446 | .getQuantity(); |
| 3447 | auto *VD = dyn_cast<VarDecl>(D); |
| 3448 | if (MaxTLSAlign && AlignVal > MaxTLSAlign && VD && |
| 3449 | VD->getTLSKind() != VarDecl::TLS_None) { |
| 3450 | Diag(VD->getLocation(), diag::err_tls_var_aligned_over_maximum) |
| 3451 | << (unsigned)AlignVal << VD << MaxTLSAlign; |
| 3452 | return; |
| 3453 | } |
| 3454 | } |
| 3455 | |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3456 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, true, |
Nikola Smiljanic | 01a7598 | 2014-05-29 10:55:11 +0000 | [diff] [blame] | 3457 | ICE.get(), SpellingListIndex); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3458 | AA->setPackExpansion(IsPackExpansion); |
| 3459 | D->addAttr(AA); |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3460 | } |
| 3461 | |
Michael Han | af02bbe | 2013-02-01 01:19:17 +0000 | [diff] [blame] | 3462 | void Sema::AddAlignedAttr(SourceRange AttrRange, Decl *D, TypeSourceInfo *TS, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3463 | unsigned SpellingListIndex, bool IsPackExpansion) { |
Alexis Hunt | dcfba7b | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 3464 | // FIXME: Cache the number on the Attr object if non-dependent? |
| 3465 | // FIXME: Perform checking of type validity |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 3466 | AlignedAttr *AA = ::new (Context) AlignedAttr(AttrRange, Context, false, TS, |
| 3467 | SpellingListIndex); |
| 3468 | AA->setPackExpansion(IsPackExpansion); |
| 3469 | D->addAttr(AA); |
Chris Lattner | 2c6fcf5 | 2008-06-26 18:38:35 +0000 | [diff] [blame] | 3470 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3471 | |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3472 | void Sema::CheckAlignasUnderalignment(Decl *D) { |
| 3473 | assert(D->hasAttrs() && "no attributes on decl"); |
| 3474 | |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3475 | QualType UnderlyingTy, DiagTy; |
| 3476 | if (ValueDecl *VD = dyn_cast<ValueDecl>(D)) { |
| 3477 | UnderlyingTy = DiagTy = VD->getType(); |
| 3478 | } else { |
| 3479 | UnderlyingTy = DiagTy = Context.getTagDeclType(cast<TagDecl>(D)); |
| 3480 | if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3481 | UnderlyingTy = ED->getIntegerType(); |
| 3482 | } |
| 3483 | if (DiagTy->isDependentType() || DiagTy->isIncompleteType()) |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3484 | return; |
| 3485 | |
| 3486 | // C++11 [dcl.align]p5, C11 6.7.5/4: |
| 3487 | // The combined effect of all alignment attributes in a declaration shall |
| 3488 | // not specify an alignment that is less strict than the alignment that |
| 3489 | // would otherwise be required for the entity being declared. |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 3490 | AlignedAttr *AlignasAttr = nullptr; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3491 | unsigned Align = 0; |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3492 | for (auto *I : D->specific_attrs<AlignedAttr>()) { |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3493 | if (I->isAlignmentDependent()) |
| 3494 | return; |
| 3495 | if (I->isAlignas()) |
Aaron Ballman | be22bcb | 2014-03-10 17:08:28 +0000 | [diff] [blame] | 3496 | AlignasAttr = I; |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3497 | Align = std::max(Align, I->getAlignment(Context)); |
| 3498 | } |
| 3499 | |
| 3500 | if (AlignasAttr && Align) { |
| 3501 | CharUnits RequestedAlign = Context.toCharUnitsFromBits(Align); |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3502 | CharUnits NaturalAlign = Context.getTypeAlignInChars(UnderlyingTy); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3503 | if (NaturalAlign > RequestedAlign) |
| 3504 | Diag(AlignasAttr->getLocation(), diag::err_alignas_underaligned) |
David Majnemer | 475b25e | 2015-01-21 10:54:38 +0000 | [diff] [blame] | 3505 | << DiagTy << (unsigned)NaturalAlign.getQuantity(); |
Richard Smith | 848e1f1 | 2013-02-01 08:12:08 +0000 | [diff] [blame] | 3506 | } |
| 3507 | } |
| 3508 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3509 | bool Sema::checkMSInheritanceAttrOnDefinition( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3510 | CXXRecordDecl *RD, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3511 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 3512 | assert(RD->hasDefinition() && "RD has no definition!"); |
| 3513 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3514 | // We may not have seen base specifiers or any virtual methods yet. We will |
| 3515 | // have to wait until the record is defined to catch any mismatches. |
| 3516 | if (!RD->getDefinition()->isCompleteDefinition()) |
| 3517 | return false; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3518 | |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3519 | // The unspecified model never matches what a definition could need. |
| 3520 | if (SemanticSpelling == MSInheritanceAttr::Keyword_unspecified_inheritance) |
| 3521 | return false; |
| 3522 | |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 3523 | if (BestCase) { |
| 3524 | if (RD->calculateInheritanceModel() == SemanticSpelling) |
| 3525 | return false; |
| 3526 | } else { |
| 3527 | if (RD->calculateInheritanceModel() <= SemanticSpelling) |
| 3528 | return false; |
| 3529 | } |
David Majnemer | 98c9ee2 | 2014-02-07 00:43:07 +0000 | [diff] [blame] | 3530 | |
| 3531 | Diag(Range.getBegin(), diag::err_mismatched_ms_inheritance) |
| 3532 | << 0 /*definition*/; |
| 3533 | Diag(RD->getDefinition()->getLocation(), diag::note_defined_here) |
| 3534 | << RD->getNameAsString(); |
| 3535 | return true; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3538 | /// parseModeAttrArg - Parses attribute mode string and returns parsed type |
| 3539 | /// attribute. |
| 3540 | static void parseModeAttrArg(Sema &S, StringRef Str, unsigned &DestWidth, |
| 3541 | bool &IntegerMode, bool &ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3542 | IntegerMode = true; |
| 3543 | ComplexMode = false; |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3544 | switch (Str.size()) { |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3545 | case 2: |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3546 | switch (Str[0]) { |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3547 | case 'Q': |
| 3548 | DestWidth = 8; |
| 3549 | break; |
| 3550 | case 'H': |
| 3551 | DestWidth = 16; |
| 3552 | break; |
| 3553 | case 'S': |
| 3554 | DestWidth = 32; |
| 3555 | break; |
| 3556 | case 'D': |
| 3557 | DestWidth = 64; |
| 3558 | break; |
| 3559 | case 'X': |
| 3560 | DestWidth = 96; |
| 3561 | break; |
| 3562 | case 'T': |
| 3563 | DestWidth = 128; |
| 3564 | break; |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3565 | } |
| 3566 | if (Str[1] == 'F') { |
| 3567 | IntegerMode = false; |
| 3568 | } else if (Str[1] == 'C') { |
| 3569 | IntegerMode = false; |
| 3570 | ComplexMode = true; |
| 3571 | } else if (Str[1] != 'I') { |
| 3572 | DestWidth = 0; |
| 3573 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3574 | break; |
| 3575 | case 4: |
| 3576 | // FIXME: glibc uses 'word' to define register_t; this is narrower than a |
| 3577 | // pointer on PIC16 and other embedded platforms. |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3578 | if (Str == "word") |
Reid Kleckner | f27e752 | 2016-02-01 18:58:24 +0000 | [diff] [blame] | 3579 | DestWidth = S.Context.getTargetInfo().getRegisterWidth(); |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3580 | else if (Str == "byte") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3581 | DestWidth = S.Context.getTargetInfo().getCharWidth(); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3582 | break; |
| 3583 | case 7: |
Daniel Dunbar | afff434 | 2009-10-18 02:09:24 +0000 | [diff] [blame] | 3584 | if (Str == "pointer") |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 3585 | DestWidth = S.Context.getTargetInfo().getPointerWidth(0); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3586 | break; |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3587 | case 11: |
| 3588 | if (Str == "unwind_word") |
Rafael Espindola | 0370597 | 2013-01-07 20:01:57 +0000 | [diff] [blame] | 3589 | DestWidth = S.Context.getTargetInfo().getUnwindWordWidth(); |
Rafael Espindola | f0dafd3 | 2013-01-07 19:58:54 +0000 | [diff] [blame] | 3590 | break; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3591 | } |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3592 | } |
| 3593 | |
| 3594 | /// handleModeAttr - This attribute modifies the width of a decl with primitive |
| 3595 | /// type. |
| 3596 | /// |
| 3597 | /// Despite what would be logical, the mode attribute is a decl attribute, not a |
| 3598 | /// type attribute: 'int ** __attribute((mode(HI))) *G;' tries to make 'G' be |
| 3599 | /// HImode, not an intermediate pointer. |
| 3600 | static void handleModeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3601 | // This attribute isn't documented, but glibc uses it. It changes |
| 3602 | // the width of an int or unsigned int to the specified size. |
| 3603 | if (!Attr.isArgIdent(0)) { |
| 3604 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 3605 | << AANT_ArgumentIdentifier; |
| 3606 | return; |
| 3607 | } |
| 3608 | |
| 3609 | IdentifierInfo *Name = Attr.getArgAsIdent(0)->Ident; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3610 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3611 | S.AddModeAttr(Attr.getRange(), D, Name, Attr.getAttributeSpellingListIndex()); |
| 3612 | } |
| 3613 | |
| 3614 | void Sema::AddModeAttr(SourceRange AttrRange, Decl *D, IdentifierInfo *Name, |
| 3615 | unsigned SpellingListIndex, bool InInstantiation) { |
| 3616 | StringRef Str = Name->getName(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3617 | normalizeName(Str); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3618 | SourceLocation AttrLoc = AttrRange.getBegin(); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3619 | |
| 3620 | unsigned DestWidth = 0; |
| 3621 | bool IntegerMode = true; |
| 3622 | bool ComplexMode = false; |
| 3623 | llvm::APInt VectorSize(64, 0); |
| 3624 | if (Str.size() >= 4 && Str[0] == 'V') { |
| 3625 | // Minimal length of vector mode is 4: 'V' + NUMBER(>=1) + TYPE(>=2). |
| 3626 | size_t StrSize = Str.size(); |
| 3627 | size_t VectorStringLength = 0; |
| 3628 | while ((VectorStringLength + 1) < StrSize && |
| 3629 | isdigit(Str[VectorStringLength + 1])) |
| 3630 | ++VectorStringLength; |
| 3631 | if (VectorStringLength && |
| 3632 | !Str.substr(1, VectorStringLength).getAsInteger(10, VectorSize) && |
| 3633 | VectorSize.isPowerOf2()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3634 | parseModeAttrArg(*this, Str.substr(VectorStringLength + 1), DestWidth, |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3635 | IntegerMode, ComplexMode); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3636 | // Avoid duplicate warning from template instantiation. |
| 3637 | if (!InInstantiation) |
| 3638 | Diag(AttrLoc, diag::warn_vector_mode_deprecated); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3639 | } else { |
| 3640 | VectorSize = 0; |
| 3641 | } |
| 3642 | } |
| 3643 | |
| 3644 | if (!VectorSize) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3645 | parseModeAttrArg(*this, Str, DestWidth, IntegerMode, ComplexMode); |
| 3646 | |
| 3647 | // FIXME: Sync this with InitializePredefinedMacros; we need to match int8_t |
| 3648 | // and friends, at least with glibc. |
| 3649 | // FIXME: Make sure floating-point mappings are accurate |
| 3650 | // FIXME: Support XF and TF types |
| 3651 | if (!DestWidth) { |
| 3652 | Diag(AttrLoc, diag::err_machine_mode) << 0 /*Unknown*/ << Name; |
| 3653 | return; |
| 3654 | } |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3655 | |
| 3656 | QualType OldTy; |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 3657 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3658 | OldTy = TD->getUnderlyingType(); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3659 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) { |
| 3660 | // Something like 'typedef enum { X } __attribute__((mode(XX))) T;'. |
| 3661 | // Try to get type from enum declaration, default to int. |
| 3662 | OldTy = ED->getIntegerType(); |
| 3663 | if (OldTy.isNull()) |
| 3664 | OldTy = Context.IntTy; |
| 3665 | } else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3666 | OldTy = cast<ValueDecl>(D)->getType(); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3667 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3668 | if (OldTy->isDependentType()) { |
| 3669 | D->addAttr(::new (Context) |
| 3670 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
| 3671 | return; |
| 3672 | } |
| 3673 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3674 | // Base type can also be a vector type (see PR17453). |
| 3675 | // Distinguish between base type and base element type. |
| 3676 | QualType OldElemTy = OldTy; |
| 3677 | if (const VectorType *VT = OldTy->getAs<VectorType>()) |
| 3678 | OldElemTy = VT->getElementType(); |
| 3679 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3680 | // GCC allows 'mode' attribute on enumeration types (even incomplete), except |
| 3681 | // for vector modes. So, 'enum X __attribute__((mode(QI)));' forms a complete |
| 3682 | // type, 'enum { A } __attribute__((mode(V4SI)))' is rejected. |
| 3683 | if ((isa<EnumDecl>(D) || OldElemTy->getAs<EnumType>()) && |
| 3684 | VectorSize.getBoolValue()) { |
| 3685 | Diag(AttrLoc, diag::err_enum_mode_vector_type) << Name << AttrRange; |
| 3686 | return; |
| 3687 | } |
| 3688 | bool IntegralOrAnyEnumType = |
| 3689 | OldElemTy->isIntegralOrEnumerationType() || OldElemTy->getAs<EnumType>(); |
| 3690 | |
| 3691 | if (!OldElemTy->getAs<BuiltinType>() && !OldElemTy->isComplexType() && |
| 3692 | !IntegralOrAnyEnumType) |
| 3693 | Diag(AttrLoc, diag::err_mode_not_primitive); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3694 | else if (IntegerMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3695 | if (!IntegralOrAnyEnumType) |
| 3696 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3697 | } else if (ComplexMode) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3698 | if (!OldElemTy->isComplexType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3699 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3700 | } else { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3701 | if (!OldElemTy->isFloatingType()) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3702 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3705 | QualType NewElemTy; |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3706 | |
| 3707 | if (IntegerMode) |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3708 | NewElemTy = Context.getIntTypeForBitwidth(DestWidth, |
| 3709 | OldElemTy->isSignedIntegerType()); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3710 | else |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3711 | NewElemTy = Context.getRealTypeForBitwidth(DestWidth); |
Stepan Dyatkovskiy | b88c30f | 2013-09-18 09:08:52 +0000 | [diff] [blame] | 3712 | |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3713 | if (NewElemTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3714 | Diag(AttrLoc, diag::err_machine_mode) << 1 /*Unsupported*/ << Name; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3715 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3716 | } |
| 3717 | |
Eli Friedman | 4735374e | 2009-03-03 06:41:03 +0000 | [diff] [blame] | 3718 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3719 | NewElemTy = Context.getComplexType(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | QualType NewTy = NewElemTy; |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3723 | if (VectorSize.getBoolValue()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3724 | NewTy = Context.getVectorType(NewTy, VectorSize.getZExtValue(), |
| 3725 | VectorType::GenericVector); |
Alexey Bataev | f278eb1 | 2015-11-19 10:13:11 +0000 | [diff] [blame] | 3726 | } else if (const VectorType *OldVT = OldTy->getAs<VectorType>()) { |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3727 | // Complex machine mode does not support base vector types. |
| 3728 | if (ComplexMode) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3729 | Diag(AttrLoc, diag::err_complex_mode_vector_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3730 | return; |
| 3731 | } |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3732 | unsigned NumElements = Context.getTypeSize(OldElemTy) * |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3733 | OldVT->getNumElements() / |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3734 | Context.getTypeSize(NewElemTy); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3735 | NewTy = |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3736 | Context.getVectorType(NewElemTy, NumElements, OldVT->getVectorKind()); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | if (NewTy.isNull()) { |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3740 | Diag(AttrLoc, diag::err_mode_wrong_type); |
Alexey Bataev | 326057d | 2015-06-19 07:46:21 +0000 | [diff] [blame] | 3741 | return; |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | // Install the new type. |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3745 | if (TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D)) |
| 3746 | TD->setModedTypeSourceInfo(TD->getTypeSourceInfo(), NewTy); |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3747 | else if (EnumDecl *ED = dyn_cast<EnumDecl>(D)) |
| 3748 | ED->setIntegerType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3749 | else |
Aaron Ballman | 6c8848a | 2016-01-19 22:54:26 +0000 | [diff] [blame] | 3750 | cast<ValueDecl>(D)->setType(NewTy); |
Enea Zaffanella | a86d88c | 2013-06-20 12:46:19 +0000 | [diff] [blame] | 3751 | |
Denis Zobnin | d9e2dcd | 2016-02-02 13:50:39 +0000 | [diff] [blame] | 3752 | D->addAttr(::new (Context) |
| 3753 | ModeAttr(AttrRange, Context, Name, SpellingListIndex)); |
Chris Lattner | acbc2d2 | 2008-06-27 22:18:37 +0000 | [diff] [blame] | 3754 | } |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 3755 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3756 | static void handleNoDebugAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3757 | D->addAttr(::new (S.Context) |
| 3758 | NoDebugAttr(Attr.getRange(), S.Context, |
| 3759 | Attr.getAttributeSpellingListIndex())); |
Anders Carlsson | 76187b4 | 2009-02-13 06:46:13 +0000 | [diff] [blame] | 3760 | } |
| 3761 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3762 | AlwaysInlineAttr *Sema::mergeAlwaysInlineAttr(Decl *D, SourceRange Range, |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3763 | IdentifierInfo *Ident, |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3764 | unsigned AttrSpellingListIndex) { |
| 3765 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3766 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << Ident; |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3767 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3768 | return nullptr; |
| 3769 | } |
| 3770 | |
| 3771 | if (D->hasAttr<AlwaysInlineAttr>()) |
| 3772 | return nullptr; |
| 3773 | |
| 3774 | return ::new (Context) AlwaysInlineAttr(Range, Context, |
| 3775 | AttrSpellingListIndex); |
| 3776 | } |
| 3777 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3778 | CommonAttr *Sema::mergeCommonAttr(Decl *D, SourceRange Range, |
| 3779 | IdentifierInfo *Ident, |
| 3780 | unsigned AttrSpellingListIndex) { |
| 3781 | if (checkAttrMutualExclusion<InternalLinkageAttr>(*this, D, Range, Ident)) |
| 3782 | return nullptr; |
| 3783 | |
| 3784 | return ::new (Context) CommonAttr(Range, Context, AttrSpellingListIndex); |
| 3785 | } |
| 3786 | |
| 3787 | InternalLinkageAttr * |
| 3788 | Sema::mergeInternalLinkageAttr(Decl *D, SourceRange Range, |
| 3789 | IdentifierInfo *Ident, |
| 3790 | unsigned AttrSpellingListIndex) { |
| 3791 | if (auto VD = dyn_cast<VarDecl>(D)) { |
| 3792 | // Attribute applies to Var but not any subclass of it (like ParmVar, |
| 3793 | // ImplicitParm or VarTemplateSpecialization). |
| 3794 | if (VD->getKind() != Decl::Var) { |
| 3795 | Diag(Range.getBegin(), diag::warn_attribute_wrong_decl_type) |
| 3796 | << Ident << (getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass |
| 3797 | : ExpectedVariableOrFunction); |
| 3798 | return nullptr; |
| 3799 | } |
| 3800 | // Attribute does not apply to non-static local variables. |
| 3801 | if (VD->hasLocalStorage()) { |
| 3802 | Diag(VD->getLocation(), diag::warn_internal_linkage_local_storage); |
| 3803 | return nullptr; |
| 3804 | } |
| 3805 | } |
| 3806 | |
| 3807 | if (checkAttrMutualExclusion<CommonAttr>(*this, D, Range, Ident)) |
| 3808 | return nullptr; |
| 3809 | |
| 3810 | return ::new (Context) |
| 3811 | InternalLinkageAttr(Range, Context, AttrSpellingListIndex); |
| 3812 | } |
| 3813 | |
Paul Robinson | 30e41fb | 2014-12-15 18:57:28 +0000 | [diff] [blame] | 3814 | MinSizeAttr *Sema::mergeMinSizeAttr(Decl *D, SourceRange Range, |
| 3815 | unsigned AttrSpellingListIndex) { |
| 3816 | if (OptimizeNoneAttr *Optnone = D->getAttr<OptimizeNoneAttr>()) { |
| 3817 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'minsize'"; |
| 3818 | Diag(Optnone->getLocation(), diag::note_conflicting_attribute); |
| 3819 | return nullptr; |
| 3820 | } |
| 3821 | |
| 3822 | if (D->hasAttr<MinSizeAttr>()) |
| 3823 | return nullptr; |
| 3824 | |
| 3825 | return ::new (Context) MinSizeAttr(Range, Context, AttrSpellingListIndex); |
| 3826 | } |
| 3827 | |
| 3828 | OptimizeNoneAttr *Sema::mergeOptimizeNoneAttr(Decl *D, SourceRange Range, |
| 3829 | unsigned AttrSpellingListIndex) { |
| 3830 | if (AlwaysInlineAttr *Inline = D->getAttr<AlwaysInlineAttr>()) { |
| 3831 | Diag(Inline->getLocation(), diag::warn_attribute_ignored) << Inline; |
| 3832 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3833 | D->dropAttr<AlwaysInlineAttr>(); |
| 3834 | } |
| 3835 | if (MinSizeAttr *MinSize = D->getAttr<MinSizeAttr>()) { |
| 3836 | Diag(MinSize->getLocation(), diag::warn_attribute_ignored) << MinSize; |
| 3837 | Diag(Range.getBegin(), diag::note_conflicting_attribute); |
| 3838 | D->dropAttr<MinSizeAttr>(); |
| 3839 | } |
| 3840 | |
| 3841 | if (D->hasAttr<OptimizeNoneAttr>()) |
| 3842 | return nullptr; |
| 3843 | |
| 3844 | return ::new (Context) OptimizeNoneAttr(Range, Context, |
| 3845 | AttrSpellingListIndex); |
| 3846 | } |
| 3847 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3848 | static void handleAlwaysInlineAttr(Sema &S, Decl *D, |
| 3849 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 3850 | if (checkAttrMutualExclusion<NotTailCalledAttr>(S, D, Attr.getRange(), |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 3851 | Attr.getName())) |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 3852 | return; |
| 3853 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3854 | if (AlwaysInlineAttr *Inline = S.mergeAlwaysInlineAttr( |
| 3855 | D, Attr.getRange(), Attr.getName(), |
| 3856 | Attr.getAttributeSpellingListIndex())) |
| 3857 | D->addAttr(Inline); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3858 | } |
| 3859 | |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3860 | static void handleMinSizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3861 | if (MinSizeAttr *MinSize = S.mergeMinSizeAttr( |
| 3862 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3863 | D->addAttr(MinSize); |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 3864 | } |
| 3865 | |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3866 | static void handleOptimizeNoneAttr(Sema &S, Decl *D, |
| 3867 | const AttributeList &Attr) { |
Paul Robinson | 080b1f3 | 2015-01-13 18:34:56 +0000 | [diff] [blame] | 3868 | if (OptimizeNoneAttr *Optnone = S.mergeOptimizeNoneAttr( |
| 3869 | D, Attr.getRange(), Attr.getAttributeSpellingListIndex())) |
| 3870 | D->addAttr(Optnone); |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 3871 | } |
| 3872 | |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 3873 | static void handleConstantAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3874 | if (checkAttrMutualExclusion<CUDASharedAttr>(S, D, Attr.getRange(), |
| 3875 | Attr.getName())) |
| 3876 | return; |
| 3877 | auto *VD = cast<VarDecl>(D); |
| 3878 | if (!VD->hasGlobalStorage()) { |
| 3879 | S.Diag(Attr.getLoc(), diag::err_cuda_nonglobal_constant); |
| 3880 | return; |
| 3881 | } |
| 3882 | D->addAttr(::new (S.Context) CUDAConstantAttr( |
| 3883 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 3884 | } |
| 3885 | |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 3886 | static void handleSharedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 3887 | if (checkAttrMutualExclusion<CUDAConstantAttr>(S, D, Attr.getRange(), |
| 3888 | Attr.getName())) |
| 3889 | return; |
| 3890 | auto *VD = cast<VarDecl>(D); |
Justin Lebar | 281ce2a | 2016-10-02 15:24:50 +0000 | [diff] [blame] | 3891 | // extern __shared__ is only allowed on arrays with no length (e.g. |
| 3892 | // "int x[]"). |
| 3893 | if (VD->hasExternalStorage() && !isa<IncompleteArrayType>(VD->getType())) { |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 3894 | S.Diag(Attr.getLoc(), diag::err_cuda_extern_shared) << VD; |
| 3895 | return; |
| 3896 | } |
Justin Lebar | aa370bd | 2016-10-13 18:45:13 +0000 | [diff] [blame] | 3897 | if (S.getLangOpts().CUDA && VD->hasLocalStorage() && |
| 3898 | S.CUDADiagIfHostCode(Attr.getLoc(), diag::err_cuda_host_shared) |
| 3899 | << S.CurrentCUDATarget()) |
| 3900 | return; |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 3901 | D->addAttr(::new (S.Context) CUDASharedAttr( |
| 3902 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 3903 | } |
| 3904 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3905 | static void handleGlobalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 3906 | if (checkAttrMutualExclusion<CUDADeviceAttr>(S, D, Attr.getRange(), |
| 3907 | Attr.getName()) || |
| 3908 | checkAttrMutualExclusion<CUDAHostAttr>(S, D, Attr.getRange(), |
| 3909 | Attr.getName())) { |
| 3910 | return; |
| 3911 | } |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3912 | FunctionDecl *FD = cast<FunctionDecl>(D); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 3913 | if (!FD->getReturnType()->isVoidType()) { |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3914 | SourceRange RTRange = FD->getReturnTypeSourceRange(); |
| 3915 | S.Diag(FD->getTypeSpecStartLoc(), diag::err_kern_type_not_void_return) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3916 | << FD->getType() |
Alp Toker | f5b1079 | 2014-07-02 12:55:58 +0000 | [diff] [blame] | 3917 | << (RTRange.isValid() ? FixItHint::CreateReplacement(RTRange, "void") |
| 3918 | : FixItHint()); |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3919 | return; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3920 | } |
Justin Lebar | c66a106 | 2016-01-20 00:26:57 +0000 | [diff] [blame] | 3921 | if (const auto *Method = dyn_cast<CXXMethodDecl>(FD)) { |
| 3922 | if (Method->isInstance()) { |
| 3923 | S.Diag(Method->getLocStart(), diag::err_kern_is_nonstatic_method) |
| 3924 | << Method; |
| 3925 | return; |
| 3926 | } |
| 3927 | S.Diag(Method->getLocStart(), diag::warn_kern_is_method) << Method; |
| 3928 | } |
| 3929 | // Only warn for "inline" when compiling for host, to cut down on noise. |
| 3930 | if (FD->isInlineSpecified() && !S.getLangOpts().CUDAIsDevice) |
| 3931 | S.Diag(FD->getLocStart(), diag::warn_kern_is_inline) << FD; |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3932 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 3933 | D->addAttr(::new (S.Context) |
| 3934 | CUDAGlobalAttr(Attr.getRange(), S.Context, |
Eli Bendersky | 8386004 | 2014-09-02 22:00:06 +0000 | [diff] [blame] | 3935 | Attr.getAttributeSpellingListIndex())); |
Peter Collingbourne | 6ab610c | 2010-12-01 03:15:31 +0000 | [diff] [blame] | 3936 | } |
| 3937 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3938 | static void handleGNUInlineAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 3939 | FunctionDecl *Fn = cast<FunctionDecl>(D); |
Douglas Gregor | 35b5753 | 2009-10-27 21:01:01 +0000 | [diff] [blame] | 3940 | if (!Fn->isInlineSpecified()) { |
Chris Lattner | ddf6ca0 | 2009-04-20 19:12:28 +0000 | [diff] [blame] | 3941 | S.Diag(Attr.getLoc(), diag::warn_gnu_inline_attribute_requires_inline); |
Chris Lattner | 4225e23 | 2009-04-14 17:02:11 +0000 | [diff] [blame] | 3942 | return; |
| 3943 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 3944 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3945 | D->addAttr(::new (S.Context) |
| 3946 | GNUInlineAttr(Attr.getRange(), S.Context, |
| 3947 | Attr.getAttributeSpellingListIndex())); |
Chris Lattner | eaad6b7 | 2009-04-14 16:30:50 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 3950 | static void handleCallConvAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3951 | if (hasDeclarator(D)) return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3952 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3953 | // Diagnostic is emitted elsewhere: here we store the (valid) Attr |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3954 | // in the Decl node for syntactic reasoning, e.g., pretty-printing. |
| 3955 | CallingConv CC; |
Richard Smith | eec7cb1 | 2015-05-28 23:38:53 +0000 | [diff] [blame] | 3956 | if (S.CheckCallingConvAttr(Attr, CC, /*FD*/nullptr)) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3957 | return; |
| 3958 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3959 | if (!isa<ObjCMethodDecl>(D)) { |
| 3960 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 3961 | << Attr.getName() << ExpectedFunctionOrMethod; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 3962 | return; |
| 3963 | } |
| 3964 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 3965 | switch (Attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3966 | case AttributeList::AT_FastCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3967 | D->addAttr(::new (S.Context) |
| 3968 | FastCallAttr(Attr.getRange(), S.Context, |
| 3969 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3970 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3971 | case AttributeList::AT_StdCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3972 | D->addAttr(::new (S.Context) |
| 3973 | StdCallAttr(Attr.getRange(), S.Context, |
| 3974 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3975 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3976 | case AttributeList::AT_ThisCall: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3977 | D->addAttr(::new (S.Context) |
| 3978 | ThisCallAttr(Attr.getRange(), S.Context, |
| 3979 | Attr.getAttributeSpellingListIndex())); |
Douglas Gregor | 4d13d10 | 2010-08-30 23:30:49 +0000 | [diff] [blame] | 3980 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3981 | case AttributeList::AT_CDecl: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3982 | D->addAttr(::new (S.Context) |
| 3983 | CDeclAttr(Attr.getRange(), S.Context, |
| 3984 | Attr.getAttributeSpellingListIndex())); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 3985 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 3986 | case AttributeList::AT_Pascal: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 3987 | D->addAttr(::new (S.Context) |
| 3988 | PascalAttr(Attr.getRange(), S.Context, |
| 3989 | Attr.getAttributeSpellingListIndex())); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3990 | return; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 3991 | case AttributeList::AT_SwiftCall: |
| 3992 | D->addAttr(::new (S.Context) |
| 3993 | SwiftCallAttr(Attr.getRange(), S.Context, |
| 3994 | Attr.getAttributeSpellingListIndex())); |
| 3995 | return; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 3996 | case AttributeList::AT_VectorCall: |
| 3997 | D->addAttr(::new (S.Context) |
| 3998 | VectorCallAttr(Attr.getRange(), S.Context, |
| 3999 | Attr.getAttributeSpellingListIndex())); |
| 4000 | return; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4001 | case AttributeList::AT_MSABI: |
| 4002 | D->addAttr(::new (S.Context) |
| 4003 | MSABIAttr(Attr.getRange(), S.Context, |
| 4004 | Attr.getAttributeSpellingListIndex())); |
| 4005 | return; |
| 4006 | case AttributeList::AT_SysVABI: |
| 4007 | D->addAttr(::new (S.Context) |
| 4008 | SysVABIAttr(Attr.getRange(), S.Context, |
| 4009 | Attr.getAttributeSpellingListIndex())); |
| 4010 | return; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4011 | case AttributeList::AT_RegCall: |
| 4012 | D->addAttr(::new (S.Context) RegCallAttr( |
| 4013 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4014 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4015 | case AttributeList::AT_Pcs: { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4016 | PcsAttr::PCSType PCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4017 | switch (CC) { |
| 4018 | case CC_AAPCS: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4019 | PCS = PcsAttr::AAPCS; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4020 | break; |
| 4021 | case CC_AAPCS_VFP: |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4022 | PCS = PcsAttr::AAPCS_VFP; |
Benjamin Kramer | 25885f4 | 2012-08-14 13:24:39 +0000 | [diff] [blame] | 4023 | break; |
| 4024 | default: |
| 4025 | llvm_unreachable("unexpected calling convention in pcs attribute"); |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4026 | } |
| 4027 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4028 | D->addAttr(::new (S.Context) |
| 4029 | PcsAttr(Attr.getRange(), S.Context, PCS, |
| 4030 | Attr.getAttributeSpellingListIndex())); |
Derek Schuff | a202096 | 2012-10-16 22:30:41 +0000 | [diff] [blame] | 4031 | return; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4032 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4033 | case AttributeList::AT_IntelOclBicc: |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4034 | D->addAttr(::new (S.Context) |
| 4035 | IntelOclBiccAttr(Attr.getRange(), S.Context, |
| 4036 | Attr.getAttributeSpellingListIndex())); |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4037 | return; |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4038 | case AttributeList::AT_PreserveMost: |
| 4039 | D->addAttr(::new (S.Context) PreserveMostAttr( |
| 4040 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4041 | return; |
| 4042 | case AttributeList::AT_PreserveAll: |
| 4043 | D->addAttr(::new (S.Context) PreserveAllAttr( |
| 4044 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4045 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4046 | default: |
| 4047 | llvm_unreachable("unexpected attribute kind"); |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 4048 | } |
| 4049 | } |
| 4050 | |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4051 | bool Sema::CheckCallingConvAttr(const AttributeList &attr, CallingConv &CC, |
| 4052 | const FunctionDecl *FD) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4053 | if (attr.isInvalid()) |
| 4054 | return true; |
| 4055 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4056 | if (attr.hasProcessingCache()) { |
| 4057 | CC = (CallingConv) attr.getProcessingCache(); |
| 4058 | return false; |
| 4059 | } |
| 4060 | |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4061 | unsigned ReqArgs = attr.getKind() == AttributeList::AT_Pcs ? 1 : 0; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4062 | if (!checkAttributeNumArgs(*this, attr, ReqArgs)) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4063 | attr.setInvalid(); |
| 4064 | return true; |
| 4065 | } |
| 4066 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4067 | // TODO: diagnose uses of these conventions on the wrong target. |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4068 | switch (attr.getKind()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4069 | case AttributeList::AT_CDecl: CC = CC_C; break; |
| 4070 | case AttributeList::AT_FastCall: CC = CC_X86FastCall; break; |
| 4071 | case AttributeList::AT_StdCall: CC = CC_X86StdCall; break; |
| 4072 | case AttributeList::AT_ThisCall: CC = CC_X86ThisCall; break; |
| 4073 | case AttributeList::AT_Pascal: CC = CC_X86Pascal; break; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4074 | case AttributeList::AT_SwiftCall: CC = CC_Swift; break; |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 4075 | case AttributeList::AT_VectorCall: CC = CC_X86VectorCall; break; |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 4076 | case AttributeList::AT_RegCall: CC = CC_X86RegCall; break; |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 4077 | case AttributeList::AT_MSABI: |
| 4078 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_C : |
| 4079 | CC_X86_64Win64; |
| 4080 | break; |
| 4081 | case AttributeList::AT_SysVABI: |
| 4082 | CC = Context.getTargetInfo().getTriple().isOSWindows() ? CC_X86_64SysV : |
| 4083 | CC_C; |
| 4084 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4085 | case AttributeList::AT_Pcs: { |
Aaron Ballman | d6600a5 | 2013-09-13 17:48:25 +0000 | [diff] [blame] | 4086 | StringRef StrRef; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4087 | if (!checkStringLiteralArgumentAttr(attr, 0, StrRef)) { |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4088 | attr.setInvalid(); |
| 4089 | return true; |
| 4090 | } |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4091 | if (StrRef == "aapcs") { |
| 4092 | CC = CC_AAPCS; |
| 4093 | break; |
| 4094 | } else if (StrRef == "aapcs-vfp") { |
| 4095 | CC = CC_AAPCS_VFP; |
| 4096 | break; |
| 4097 | } |
Benjamin Kramer | 833fb9f | 2012-08-14 13:13:47 +0000 | [diff] [blame] | 4098 | |
| 4099 | attr.setInvalid(); |
| 4100 | Diag(attr.getLoc(), diag::err_invalid_pcs); |
| 4101 | return true; |
Anton Korobeynikov | 231e875 | 2011-04-14 20:06:49 +0000 | [diff] [blame] | 4102 | } |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 4103 | case AttributeList::AT_IntelOclBicc: CC = CC_IntelOclBicc; break; |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 4104 | case AttributeList::AT_PreserveMost: CC = CC_PreserveMost; break; |
| 4105 | case AttributeList::AT_PreserveAll: CC = CC_PreserveAll; break; |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4106 | default: llvm_unreachable("unexpected attribute kind"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4107 | } |
| 4108 | |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4109 | const TargetInfo &TI = Context.getTargetInfo(); |
| 4110 | TargetInfo::CallingConvCheckResult A = TI.checkCallingConvention(CC); |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4111 | if (A != TargetInfo::CCCR_OK) { |
| 4112 | if (A == TargetInfo::CCCR_Warning) |
| 4113 | Diag(attr.getLoc(), diag::warn_cconv_ignored) << attr.getName(); |
Aaron Ballman | 02df2e0 | 2012-12-09 17:45:41 +0000 | [diff] [blame] | 4114 | |
Reid Kleckner | 9fde2e0 | 2015-02-26 19:43:46 +0000 | [diff] [blame] | 4115 | // This convention is not valid for the target. Use the default function or |
| 4116 | // method calling convention. |
Alexey Bataev | a754718 | 2016-05-18 09:06:38 +0000 | [diff] [blame] | 4117 | bool IsCXXMethod = false, IsVariadic = false; |
| 4118 | if (FD) { |
| 4119 | IsCXXMethod = FD->isCXXInstanceMember(); |
| 4120 | IsVariadic = FD->isVariadic(); |
| 4121 | } |
| 4122 | CC = Context.getDefaultCallingConvention(IsVariadic, IsCXXMethod); |
Aaron Ballman | e91c6be | 2012-10-02 14:26:08 +0000 | [diff] [blame] | 4123 | } |
| 4124 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4125 | attr.setProcessingCache((unsigned) CC); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4126 | return false; |
| 4127 | } |
| 4128 | |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 4129 | /// Pointer-like types in the default address space. |
| 4130 | static bool isValidSwiftContextType(QualType type) { |
| 4131 | if (!type->hasPointerRepresentation()) |
| 4132 | return type->isDependentType(); |
| 4133 | return type->getPointeeType().getAddressSpace() == 0; |
| 4134 | } |
| 4135 | |
| 4136 | /// Pointers and references in the default address space. |
| 4137 | static bool isValidSwiftIndirectResultType(QualType type) { |
| 4138 | if (auto ptrType = type->getAs<PointerType>()) { |
| 4139 | type = ptrType->getPointeeType(); |
| 4140 | } else if (auto refType = type->getAs<ReferenceType>()) { |
| 4141 | type = refType->getPointeeType(); |
| 4142 | } else { |
| 4143 | return type->isDependentType(); |
| 4144 | } |
| 4145 | return type.getAddressSpace() == 0; |
| 4146 | } |
| 4147 | |
| 4148 | /// Pointers and references to pointers in the default address space. |
| 4149 | static bool isValidSwiftErrorResultType(QualType type) { |
| 4150 | if (auto ptrType = type->getAs<PointerType>()) { |
| 4151 | type = ptrType->getPointeeType(); |
| 4152 | } else if (auto refType = type->getAs<ReferenceType>()) { |
| 4153 | type = refType->getPointeeType(); |
| 4154 | } else { |
| 4155 | return type->isDependentType(); |
| 4156 | } |
| 4157 | if (!type.getQualifiers().empty()) |
| 4158 | return false; |
| 4159 | return isValidSwiftContextType(type); |
| 4160 | } |
| 4161 | |
| 4162 | static void handleParameterABIAttr(Sema &S, Decl *D, const AttributeList &attr, |
| 4163 | ParameterABI abi) { |
| 4164 | S.AddParameterABIAttr(attr.getRange(), D, abi, |
| 4165 | attr.getAttributeSpellingListIndex()); |
| 4166 | } |
| 4167 | |
| 4168 | void Sema::AddParameterABIAttr(SourceRange range, Decl *D, ParameterABI abi, |
| 4169 | unsigned spellingIndex) { |
| 4170 | |
| 4171 | QualType type = cast<ParmVarDecl>(D)->getType(); |
| 4172 | |
| 4173 | if (auto existingAttr = D->getAttr<ParameterABIAttr>()) { |
| 4174 | if (existingAttr->getABI() != abi) { |
| 4175 | Diag(range.getBegin(), diag::err_attributes_are_not_compatible) |
| 4176 | << getParameterABISpelling(abi) << existingAttr; |
| 4177 | Diag(existingAttr->getLocation(), diag::note_conflicting_attribute); |
| 4178 | return; |
| 4179 | } |
| 4180 | } |
| 4181 | |
| 4182 | switch (abi) { |
| 4183 | case ParameterABI::Ordinary: |
| 4184 | llvm_unreachable("explicit attribute for ordinary parameter ABI?"); |
| 4185 | |
| 4186 | case ParameterABI::SwiftContext: |
| 4187 | if (!isValidSwiftContextType(type)) { |
| 4188 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4189 | << getParameterABISpelling(abi) |
| 4190 | << /*pointer to pointer */ 0 << type; |
| 4191 | } |
| 4192 | D->addAttr(::new (Context) |
| 4193 | SwiftContextAttr(range, Context, spellingIndex)); |
| 4194 | return; |
| 4195 | |
| 4196 | case ParameterABI::SwiftErrorResult: |
| 4197 | if (!isValidSwiftErrorResultType(type)) { |
| 4198 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4199 | << getParameterABISpelling(abi) |
| 4200 | << /*pointer to pointer */ 1 << type; |
| 4201 | } |
| 4202 | D->addAttr(::new (Context) |
| 4203 | SwiftErrorResultAttr(range, Context, spellingIndex)); |
| 4204 | return; |
| 4205 | |
| 4206 | case ParameterABI::SwiftIndirectResult: |
| 4207 | if (!isValidSwiftIndirectResultType(type)) { |
| 4208 | Diag(range.getBegin(), diag::err_swift_abi_parameter_wrong_type) |
| 4209 | << getParameterABISpelling(abi) |
| 4210 | << /*pointer*/ 0 << type; |
| 4211 | } |
| 4212 | D->addAttr(::new (Context) |
| 4213 | SwiftIndirectResultAttr(range, Context, spellingIndex)); |
| 4214 | return; |
| 4215 | } |
| 4216 | llvm_unreachable("bad parameter ABI attribute"); |
| 4217 | } |
| 4218 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4219 | /// Checks a regparm attribute, returning true if it is ill-formed and |
| 4220 | /// otherwise setting numParams to the appropriate value. |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4221 | bool Sema::CheckRegparmAttr(const AttributeList &Attr, unsigned &numParams) { |
| 4222 | if (Attr.isInvalid()) |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4223 | return true; |
| 4224 | |
Aaron Ballman | c2cbc66 | 2013-07-18 18:01:48 +0000 | [diff] [blame] | 4225 | if (!checkAttributeNumArgs(*this, Attr, 1)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4226 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4227 | return true; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4228 | } |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4229 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4230 | uint32_t NP; |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4231 | Expr *NumParamsExpr = Attr.getArgAsExpr(0); |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4232 | if (!checkUInt32Argument(*this, Attr, NumParamsExpr, NP)) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4233 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4234 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4237 | if (Context.getTargetInfo().getRegParmMax() == 0) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4238 | Diag(Attr.getLoc(), diag::err_attribute_regparm_wrong_platform) |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4239 | << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4240 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4241 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4242 | } |
| 4243 | |
Aaron Ballman | f22ef5a | 2013-11-21 01:50:40 +0000 | [diff] [blame] | 4244 | numParams = NP; |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4245 | if (numParams > Context.getTargetInfo().getRegParmMax()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4246 | Diag(Attr.getLoc(), diag::err_attribute_regparm_invalid_number) |
Douglas Gregor | e8bbc12 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 4247 | << Context.getTargetInfo().getRegParmMax() << NumParamsExpr->getSourceRange(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4248 | Attr.setInvalid(); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4249 | return true; |
Eli Friedman | 7044b76 | 2009-03-27 21:06:47 +0000 | [diff] [blame] | 4250 | } |
| 4251 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 4252 | return false; |
Fariborz Jahanian | a2d609e | 2009-03-27 18:38:55 +0000 | [diff] [blame] | 4253 | } |
| 4254 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4255 | // Checks whether an argument of launch_bounds attribute is |
| 4256 | // acceptable, performs implicit conversion to Rvalue, and returns |
| 4257 | // non-nullptr Expr result on success. Otherwise, it returns nullptr |
| 4258 | // and may output an error. |
| 4259 | static Expr *makeLaunchBoundsArgExpr(Sema &S, Expr *E, |
| 4260 | const CUDALaunchBoundsAttr &Attr, |
| 4261 | const unsigned Idx) { |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4262 | if (S.DiagnoseUnexpandedParameterPack(E)) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4263 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4264 | |
| 4265 | // Accept template arguments for now as they depend on something else. |
| 4266 | // We'll get to check them when they eventually get instantiated. |
| 4267 | if (E->isValueDependent()) |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4268 | return E; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4269 | |
| 4270 | llvm::APSInt I(64); |
| 4271 | if (!E->isIntegerConstantExpr(I, S.Context)) { |
| 4272 | S.Diag(E->getExprLoc(), diag::err_attribute_argument_n_type) |
| 4273 | << &Attr << Idx << AANT_ArgumentIntegerConstant << E->getSourceRange(); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4274 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4275 | } |
| 4276 | // Make sure we can fit it in 32 bits. |
| 4277 | if (!I.isIntN(32)) { |
| 4278 | S.Diag(E->getExprLoc(), diag::err_ice_too_large) << I.toString(10, false) |
| 4279 | << 32 << /* Unsigned */ 1; |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4280 | return nullptr; |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4281 | } |
| 4282 | if (I < 0) |
| 4283 | S.Diag(E->getExprLoc(), diag::warn_attribute_argument_n_negative) |
| 4284 | << &Attr << Idx << E->getSourceRange(); |
| 4285 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4286 | // We may need to perform implicit conversion of the argument. |
| 4287 | InitializedEntity Entity = InitializedEntity::InitializeParameter( |
| 4288 | S.Context, S.Context.getConstType(S.Context.IntTy), /*consume*/ false); |
| 4289 | ExprResult ValArg = S.PerformCopyInitialization(Entity, SourceLocation(), E); |
| 4290 | assert(!ValArg.isInvalid() && |
| 4291 | "Unexpected PerformCopyInitialization() failure."); |
| 4292 | |
| 4293 | return ValArg.getAs<Expr>(); |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4294 | } |
| 4295 | |
| 4296 | void Sema::AddLaunchBoundsAttr(SourceRange AttrRange, Decl *D, Expr *MaxThreads, |
| 4297 | Expr *MinBlocks, unsigned SpellingListIndex) { |
| 4298 | CUDALaunchBoundsAttr TmpAttr(AttrRange, Context, MaxThreads, MinBlocks, |
| 4299 | SpellingListIndex); |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4300 | MaxThreads = makeLaunchBoundsArgExpr(*this, MaxThreads, TmpAttr, 0); |
| 4301 | if (MaxThreads == nullptr) |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 4302 | return; |
| 4303 | |
Artem Belevich | bcec9da | 2016-06-06 22:54:57 +0000 | [diff] [blame] | 4304 | if (MinBlocks) { |
| 4305 | MinBlocks = makeLaunchBoundsArgExpr(*this, MinBlocks, TmpAttr, 1); |
| 4306 | if (MinBlocks == nullptr) |
| 4307 | return; |
| 4308 | } |
Artem Belevich | 7093e40 | 2015-04-21 22:55:54 +0000 | [diff] [blame] | 4309 | |
| 4310 | D->addAttr(::new (Context) CUDALaunchBoundsAttr( |
| 4311 | AttrRange, Context, MaxThreads, MinBlocks, SpellingListIndex)); |
| 4312 | } |
| 4313 | |
| 4314 | static void handleLaunchBoundsAttr(Sema &S, Decl *D, |
| 4315 | const AttributeList &Attr) { |
| 4316 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1) || |
| 4317 | !checkAttributeAtMostNumArgs(S, Attr, 2)) |
| 4318 | return; |
| 4319 | |
| 4320 | S.AddLaunchBoundsAttr(Attr.getRange(), D, Attr.getArgAsExpr(0), |
| 4321 | Attr.getNumArgs() > 1 ? Attr.getArgAsExpr(1) : nullptr, |
| 4322 | Attr.getAttributeSpellingListIndex()); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4325 | static void handleArgumentWithTypeTagAttr(Sema &S, Decl *D, |
| 4326 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4327 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4328 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4329 | << Attr.getName() << /* arg num = */ 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4330 | return; |
| 4331 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4332 | |
| 4333 | if (!checkAttributeNumArgs(S, Attr, 3)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4334 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4335 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4336 | IdentifierInfo *ArgumentKind = Attr.getArgAsIdent(0)->Ident; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4337 | |
| 4338 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D)) { |
| 4339 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4340 | << Attr.getName() << ExpectedFunctionOrMethod; |
| 4341 | return; |
| 4342 | } |
| 4343 | |
| 4344 | uint64_t ArgumentIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4345 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 2, Attr.getArgAsExpr(1), |
| 4346 | ArgumentIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4347 | return; |
| 4348 | |
| 4349 | uint64_t TypeTagIdx; |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4350 | if (!checkFunctionOrMethodParameterIndex(S, D, Attr, 3, Attr.getArgAsExpr(2), |
| 4351 | TypeTagIdx)) |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4352 | return; |
| 4353 | |
Aaron Ballman | faed0fa | 2013-12-26 16:30:30 +0000 | [diff] [blame] | 4354 | bool IsPointer = (Attr.getName()->getName() == "pointer_with_type_tag"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4355 | if (IsPointer) { |
| 4356 | // Ensure that buffer has a pointer type. |
Alp Toker | 601b22c | 2014-01-21 23:35:24 +0000 | [diff] [blame] | 4357 | QualType BufferTy = getFunctionOrMethodParamType(D, ArgumentIdx); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4358 | if (!BufferTy->isPointerType()) { |
| 4359 | S.Diag(Attr.getLoc(), diag::err_attribute_pointers_only) |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 4360 | << Attr.getName() << 0; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4361 | } |
| 4362 | } |
| 4363 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4364 | D->addAttr(::new (S.Context) |
| 4365 | ArgumentWithTypeTagAttr(Attr.getRange(), S.Context, ArgumentKind, |
| 4366 | ArgumentIdx, TypeTagIdx, IsPointer, |
| 4367 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4368 | } |
| 4369 | |
| 4370 | static void handleTypeTagForDatatypeAttr(Sema &S, Decl *D, |
| 4371 | const AttributeList &Attr) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4372 | if (!Attr.isArgIdent(0)) { |
Aaron Ballman | 2998227 | 2013-07-23 14:03:57 +0000 | [diff] [blame] | 4373 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_n_type) |
Aaron Ballman | 3bf758c | 2013-07-30 01:31:03 +0000 | [diff] [blame] | 4374 | << Attr.getName() << 1 << AANT_ArgumentIdentifier; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4375 | return; |
| 4376 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4377 | |
| 4378 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4379 | return; |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4380 | |
Aaron Ballman | 90f8c6f | 2013-11-25 18:50:49 +0000 | [diff] [blame] | 4381 | if (!isa<VarDecl>(D)) { |
| 4382 | S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type) |
| 4383 | << Attr.getName() << ExpectedVariable; |
| 4384 | return; |
| 4385 | } |
| 4386 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 4387 | IdentifierInfo *PointerKind = Attr.getArgAsIdent(0)->Ident; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4388 | TypeSourceInfo *MatchingCTypeLoc = nullptr; |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4389 | S.GetTypeFromParser(Attr.getMatchingCType(), &MatchingCTypeLoc); |
| 4390 | assert(MatchingCTypeLoc && "no type source info for attribute argument"); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4391 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4392 | D->addAttr(::new (S.Context) |
| 4393 | TypeTagForDatatypeAttr(Attr.getRange(), S.Context, PointerKind, |
Richard Smith | b87c465 | 2013-10-31 21:23:20 +0000 | [diff] [blame] | 4394 | MatchingCTypeLoc, |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4395 | Attr.getLayoutCompatible(), |
| 4396 | Attr.getMustBeNull(), |
| 4397 | Attr.getAttributeSpellingListIndex())); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 4398 | } |
| 4399 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 4400 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4401 | // Checker-specific attribute handlers. |
| 4402 | //===----------------------------------------------------------------------===// |
| 4403 | |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4404 | static bool isValidSubjectOfNSReturnsRetainedAttribute(QualType type) { |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4405 | return type->isDependentType() || |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4406 | type->isObjCRetainableType(); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4407 | } |
| 4408 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4409 | static bool isValidSubjectOfNSAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4410 | return type->isDependentType() || |
| 4411 | type->isObjCObjectPointerType() || |
| 4412 | S.Context.isObjCNSObjectType(type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4413 | } |
Eugene Zelenko | 1ced509 | 2016-02-12 22:53:10 +0000 | [diff] [blame] | 4414 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4415 | static bool isValidSubjectOfCFAttribute(Sema &S, QualType type) { |
Douglas Gregor | f892c7f | 2011-10-09 22:26:49 +0000 | [diff] [blame] | 4416 | return type->isDependentType() || |
| 4417 | type->isPointerType() || |
| 4418 | isValidSubjectOfNSAttribute(S, type); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4419 | } |
| 4420 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4421 | static void handleNSConsumedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4422 | S.AddNSConsumedAttr(Attr.getRange(), D, Attr.getAttributeSpellingListIndex(), |
| 4423 | Attr.getKind() == AttributeList::AT_NSConsumed, |
| 4424 | /*template instantiation*/ false); |
| 4425 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4426 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4427 | void Sema::AddNSConsumedAttr(SourceRange attrRange, Decl *D, |
| 4428 | unsigned spellingIndex, bool isNSConsumed, |
| 4429 | bool isTemplateInstantiation) { |
| 4430 | ParmVarDecl *param = cast<ParmVarDecl>(D); |
| 4431 | bool typeOK; |
| 4432 | |
| 4433 | if (isNSConsumed) { |
| 4434 | typeOK = isValidSubjectOfNSAttribute(*this, param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4435 | } else { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4436 | typeOK = isValidSubjectOfCFAttribute(*this, param->getType()); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
| 4439 | if (!typeOK) { |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4440 | // These attributes are normally just advisory, but in ARC, ns_consumed |
| 4441 | // is significant. Allow non-dependent code to contain inappropriate |
| 4442 | // attributes even in ARC, but require template instantiations to be |
| 4443 | // set up correctly. |
| 4444 | Diag(D->getLocStart(), |
| 4445 | (isTemplateInstantiation && isNSConsumed && |
| 4446 | getLangOpts().ObjCAutoRefCount |
| 4447 | ? diag::err_ns_attribute_wrong_parameter_type |
| 4448 | : diag::warn_ns_attribute_wrong_parameter_type)) |
| 4449 | << attrRange |
| 4450 | << (isNSConsumed ? "ns_consumed" : "cf_consumed") |
| 4451 | << (isNSConsumed ? /*objc pointers*/ 0 : /*cf pointers*/ 1); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4452 | return; |
| 4453 | } |
| 4454 | |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4455 | if (isNSConsumed) |
| 4456 | param->addAttr(::new (Context) |
| 4457 | NSConsumedAttr(attrRange, Context, spellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4458 | else |
John McCall | 3b5a8f5 | 2016-03-03 00:10:03 +0000 | [diff] [blame] | 4459 | param->addAttr(::new (Context) |
| 4460 | CFConsumedAttr(attrRange, Context, spellingIndex)); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4461 | } |
| 4462 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4463 | static void handleNSReturnsRetainedAttr(Sema &S, Decl *D, |
| 4464 | const AttributeList &Attr) { |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4465 | QualType returnType; |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4466 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4467 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4468 | returnType = MD->getReturnType(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4469 | else if (S.getLangOpts().ObjCAutoRefCount && hasDeclarator(D) && |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4470 | (Attr.getKind() == AttributeList::AT_NSReturnsRetained)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4471 | return; // ignore: was handled as a type attribute |
Fariborz Jahanian | 272b7dc | 2012-08-28 22:26:21 +0000 | [diff] [blame] | 4472 | else if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(D)) |
| 4473 | returnType = PD->getType(); |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4474 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4475 | returnType = FD->getReturnType(); |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4476 | else if (auto *Param = dyn_cast<ParmVarDecl>(D)) { |
| 4477 | returnType = Param->getType()->getPointeeType(); |
| 4478 | if (returnType.isNull()) { |
| 4479 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4480 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4481 | << Attr.getRange(); |
| 4482 | return; |
| 4483 | } |
| 4484 | } else { |
| 4485 | AttributeDeclKind ExpectedDeclKind; |
| 4486 | switch (Attr.getKind()) { |
| 4487 | default: llvm_unreachable("invalid ownership attribute"); |
| 4488 | case AttributeList::AT_NSReturnsRetained: |
| 4489 | case AttributeList::AT_NSReturnsAutoreleased: |
| 4490 | case AttributeList::AT_NSReturnsNotRetained: |
| 4491 | ExpectedDeclKind = ExpectedFunctionOrMethod; |
| 4492 | break; |
| 4493 | |
| 4494 | case AttributeList::AT_CFReturnsRetained: |
| 4495 | case AttributeList::AT_CFReturnsNotRetained: |
| 4496 | ExpectedDeclKind = ExpectedFunctionMethodOrParameter; |
| 4497 | break; |
| 4498 | } |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4499 | S.Diag(D->getLocStart(), diag::warn_attribute_wrong_decl_type) |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4500 | << Attr.getRange() << Attr.getName() << ExpectedDeclKind; |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4501 | return; |
| 4502 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4503 | |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4504 | bool typeOK; |
| 4505 | bool cf; |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4506 | switch (Attr.getKind()) { |
David Blaikie | 8a40f70 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4507 | default: llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4508 | case AttributeList::AT_NSReturnsRetained: |
Fariborz Jahanian | 4eba3dc | 2014-06-12 16:12:30 +0000 | [diff] [blame] | 4509 | typeOK = isValidSubjectOfNSReturnsRetainedAttribute(returnType); |
Fariborz Jahanian | 9c10032 | 2014-06-11 21:22:53 +0000 | [diff] [blame] | 4510 | cf = false; |
| 4511 | break; |
| 4512 | |
| 4513 | case AttributeList::AT_NSReturnsAutoreleased: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4514 | case AttributeList::AT_NSReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4515 | typeOK = isValidSubjectOfNSAttribute(S, returnType); |
| 4516 | cf = false; |
| 4517 | break; |
| 4518 | |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4519 | case AttributeList::AT_CFReturnsRetained: |
| 4520 | case AttributeList::AT_CFReturnsNotRetained: |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4521 | typeOK = isValidSubjectOfCFAttribute(S, returnType); |
| 4522 | cf = true; |
| 4523 | break; |
| 4524 | } |
| 4525 | |
| 4526 | if (!typeOK) { |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 4527 | if (isa<ParmVarDecl>(D)) { |
| 4528 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_parameter_type) |
| 4529 | << Attr.getName() << /*pointer-to-CF*/2 |
| 4530 | << Attr.getRange(); |
| 4531 | } else { |
| 4532 | // Needs to be kept in sync with warn_ns_attribute_wrong_return_type. |
| 4533 | enum : unsigned { |
| 4534 | Function, |
| 4535 | Method, |
| 4536 | Property |
| 4537 | } SubjectKind = Function; |
| 4538 | if (isa<ObjCMethodDecl>(D)) |
| 4539 | SubjectKind = Method; |
| 4540 | else if (isa<ObjCPropertyDecl>(D)) |
| 4541 | SubjectKind = Property; |
| 4542 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
| 4543 | << Attr.getName() << SubjectKind << cf |
| 4544 | << Attr.getRange(); |
| 4545 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4546 | return; |
Ted Kremenek | 3b204e4 | 2009-05-13 21:07:32 +0000 | [diff] [blame] | 4547 | } |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 4548 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4549 | switch (Attr.getKind()) { |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4550 | default: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 4551 | llvm_unreachable("invalid ownership attribute"); |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4552 | case AttributeList::AT_NSReturnsAutoreleased: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4553 | D->addAttr(::new (S.Context) NSReturnsAutoreleasedAttr( |
| 4554 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
John McCall | ed43393 | 2011-01-25 03:31:58 +0000 | [diff] [blame] | 4555 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4556 | case AttributeList::AT_CFReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4557 | D->addAttr(::new (S.Context) CFReturnsNotRetainedAttr( |
| 4558 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4559 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4560 | case AttributeList::AT_NSReturnsNotRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4561 | D->addAttr(::new (S.Context) NSReturnsNotRetainedAttr( |
| 4562 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | d9c6663 | 2010-02-18 00:05:45 +0000 | [diff] [blame] | 4563 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4564 | case AttributeList::AT_CFReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4565 | D->addAttr(::new (S.Context) CFReturnsRetainedAttr( |
| 4566 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4567 | return; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 4568 | case AttributeList::AT_NSReturnsRetained: |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4569 | D->addAttr(::new (S.Context) NSReturnsRetainedAttr( |
| 4570 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 4571 | return; |
| 4572 | }; |
| 4573 | } |
| 4574 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4575 | static void handleObjCReturnsInnerPointerAttr(Sema &S, Decl *D, |
| 4576 | const AttributeList &attr) { |
Fariborz Jahanian | 8bf0556 | 2013-09-19 17:52:50 +0000 | [diff] [blame] | 4577 | const int EP_ObjCMethod = 1; |
| 4578 | const int EP_ObjCProperty = 2; |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4579 | |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4580 | SourceLocation loc = attr.getLoc(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4581 | QualType resultType; |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4582 | if (isa<ObjCMethodDecl>(D)) |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 4583 | resultType = cast<ObjCMethodDecl>(D)->getReturnType(); |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4584 | else |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4585 | resultType = cast<ObjCPropertyDecl>(D)->getType(); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4586 | |
Fariborz Jahanian | 044a5be | 2011-09-30 20:50:23 +0000 | [diff] [blame] | 4587 | if (!resultType->isReferenceType() && |
| 4588 | (!resultType->isPointerType() || resultType->isObjCRetainableType())) { |
Fariborz Jahanian | 8a5e947 | 2013-09-19 16:37:20 +0000 | [diff] [blame] | 4589 | S.Diag(D->getLocStart(), diag::warn_ns_attribute_wrong_return_type) |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4590 | << SourceRange(loc) |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4591 | << attr.getName() |
| 4592 | << (isa<ObjCMethodDecl>(D) ? EP_ObjCMethod : EP_ObjCProperty) |
Fariborz Jahanian | 5c00583 | 2013-09-19 17:18:55 +0000 | [diff] [blame] | 4593 | << /*non-retainable pointer*/ 2; |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4594 | |
| 4595 | // Drop the attribute. |
| 4596 | return; |
| 4597 | } |
| 4598 | |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4599 | D->addAttr(::new (S.Context) ObjCReturnsInnerPointerAttr( |
| 4600 | attr.getRange(), S.Context, attr.getAttributeSpellingListIndex())); |
John McCall | cf16670 | 2011-07-22 08:53:00 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4603 | static void handleObjCRequiresSuperAttr(Sema &S, Decl *D, |
| 4604 | const AttributeList &attr) { |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 4605 | ObjCMethodDecl *method = cast<ObjCMethodDecl>(D); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4606 | |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4607 | DeclContext *DC = method->getDeclContext(); |
| 4608 | if (const ObjCProtocolDecl *PDecl = dyn_cast_or_null<ObjCProtocolDecl>(DC)) { |
| 4609 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4610 | << attr.getName() << 0; |
| 4611 | S.Diag(PDecl->getLocation(), diag::note_protocol_decl); |
| 4612 | return; |
| 4613 | } |
| 4614 | if (method->getMethodFamily() == OMF_dealloc) { |
| 4615 | S.Diag(D->getLocStart(), diag::warn_objc_requires_super_protocol) |
| 4616 | << attr.getName() << 1; |
| 4617 | return; |
| 4618 | } |
| 4619 | |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4620 | method->addAttr(::new (S.Context) |
| 4621 | ObjCRequiresSuperAttr(attr.getRange(), S.Context, |
| 4622 | attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 4623 | } |
| 4624 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4625 | static void handleCFAuditedTransferAttr(Sema &S, Decl *D, |
| 4626 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4627 | if (checkAttrMutualExclusion<CFUnknownTransferAttr>(S, D, Attr.getRange(), |
| 4628 | Attr.getName())) |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4629 | return; |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4630 | |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4631 | D->addAttr(::new (S.Context) |
| 4632 | CFAuditedTransferAttr(Attr.getRange(), S.Context, |
| 4633 | Attr.getAttributeSpellingListIndex())); |
| 4634 | } |
| 4635 | |
| 4636 | static void handleCFUnknownTransferAttr(Sema &S, Decl *D, |
| 4637 | const AttributeList &Attr) { |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 4638 | if (checkAttrMutualExclusion<CFAuditedTransferAttr>(S, D, Attr.getRange(), |
| 4639 | Attr.getName())) |
Aaron Ballman | fb76304 | 2013-12-02 18:05:46 +0000 | [diff] [blame] | 4640 | return; |
| 4641 | |
| 4642 | D->addAttr(::new (S.Context) |
| 4643 | CFUnknownTransferAttr(Attr.getRange(), S.Context, |
| 4644 | Attr.getAttributeSpellingListIndex())); |
John McCall | 32f5fe1 | 2011-09-30 05:12:12 +0000 | [diff] [blame] | 4645 | } |
| 4646 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4647 | static void handleObjCBridgeAttr(Sema &S, Scope *Sc, Decl *D, |
| 4648 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4649 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4650 | |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4651 | if (!Parm) { |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4652 | 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] | 4653 | return; |
| 4654 | } |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 4655 | |
| 4656 | // Typedefs only allow objc_bridge(id) and have some additional checking. |
| 4657 | if (auto TD = dyn_cast<TypedefNameDecl>(D)) { |
| 4658 | if (!Parm->Ident->isStr("id")) { |
| 4659 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_id) |
| 4660 | << Attr.getName(); |
| 4661 | return; |
| 4662 | } |
| 4663 | |
| 4664 | // Only allow 'cv void *'. |
| 4665 | QualType T = TD->getUnderlyingType(); |
| 4666 | if (!T->isVoidPointerType()) { |
| 4667 | S.Diag(Attr.getLoc(), diag::err_objc_attr_typedef_not_void_pointer); |
| 4668 | return; |
| 4669 | } |
| 4670 | } |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4671 | |
| 4672 | D->addAttr(::new (S.Context) |
Ted Kremenek | 2d3379e | 2013-11-21 07:20:34 +0000 | [diff] [blame] | 4673 | ObjCBridgeAttr(Attr.getRange(), S.Context, Parm->Ident, |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 4674 | Attr.getAttributeSpellingListIndex())); |
| 4675 | } |
| 4676 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4677 | static void handleObjCBridgeMutableAttr(Sema &S, Scope *Sc, Decl *D, |
| 4678 | const AttributeList &Attr) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4679 | IdentifierLoc * Parm = Attr.isArgIdent(0) ? Attr.getArgAsIdent(0) : nullptr; |
| 4680 | |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 4681 | if (!Parm) { |
| 4682 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4683 | return; |
| 4684 | } |
| 4685 | |
| 4686 | D->addAttr(::new (S.Context) |
| 4687 | ObjCBridgeMutableAttr(Attr.getRange(), S.Context, Parm->Ident, |
| 4688 | Attr.getAttributeSpellingListIndex())); |
| 4689 | } |
| 4690 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4691 | static void handleObjCBridgeRelatedAttr(Sema &S, Scope *Sc, Decl *D, |
| 4692 | const AttributeList &Attr) { |
| 4693 | IdentifierInfo *RelatedClass = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4694 | Attr.isArgIdent(0) ? Attr.getArgAsIdent(0)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4695 | if (!RelatedClass) { |
| 4696 | S.Diag(D->getLocStart(), diag::err_objc_attr_not_id) << Attr.getName() << 0; |
| 4697 | return; |
| 4698 | } |
| 4699 | IdentifierInfo *ClassMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4700 | Attr.getArgAsIdent(1) ? Attr.getArgAsIdent(1)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4701 | IdentifierInfo *InstanceMethod = |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 4702 | Attr.getArgAsIdent(2) ? Attr.getArgAsIdent(2)->Ident : nullptr; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 4703 | D->addAttr(::new (S.Context) |
| 4704 | ObjCBridgeRelatedAttr(Attr.getRange(), S.Context, RelatedClass, |
| 4705 | ClassMethod, InstanceMethod, |
| 4706 | Attr.getAttributeSpellingListIndex())); |
| 4707 | } |
| 4708 | |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4709 | static void handleObjCDesignatedInitializer(Sema &S, Decl *D, |
| 4710 | const AttributeList &Attr) { |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4711 | ObjCInterfaceDecl *IFace; |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 4712 | if (ObjCCategoryDecl *CatDecl = |
| 4713 | dyn_cast<ObjCCategoryDecl>(D->getDeclContext())) |
Fariborz Jahanian | 6efab6e | 2014-03-14 18:19:46 +0000 | [diff] [blame] | 4714 | IFace = CatDecl->getClassInterface(); |
| 4715 | else |
| 4716 | IFace = cast<ObjCInterfaceDecl>(D->getDeclContext()); |
Ben Langmuir | c91ac9e | 2015-01-20 20:41:36 +0000 | [diff] [blame] | 4717 | |
| 4718 | if (!IFace) |
| 4719 | return; |
| 4720 | |
Argyrios Kyrtzidis | 9ed9e5f | 2013-12-03 21:11:30 +0000 | [diff] [blame] | 4721 | IFace->setHasDesignatedInitializers(); |
Argyrios Kyrtzidis | e818681 | 2013-12-07 06:08:04 +0000 | [diff] [blame] | 4722 | D->addAttr(::new (S.Context) |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 4723 | ObjCDesignatedInitializerAttr(Attr.getRange(), S.Context, |
| 4724 | Attr.getAttributeSpellingListIndex())); |
| 4725 | } |
| 4726 | |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4727 | static void handleObjCRuntimeName(Sema &S, Decl *D, |
| 4728 | const AttributeList &Attr) { |
Fariborz Jahanian | a2e5deb | 2014-07-16 19:44:34 +0000 | [diff] [blame] | 4729 | StringRef MetaDataName; |
| 4730 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, MetaDataName)) |
| 4731 | return; |
| 4732 | D->addAttr(::new (S.Context) |
| 4733 | ObjCRuntimeNameAttr(Attr.getRange(), S.Context, |
| 4734 | MetaDataName, |
| 4735 | Attr.getAttributeSpellingListIndex())); |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 4736 | } |
| 4737 | |
Nico Weber | a691689 | 2016-06-10 18:53:04 +0000 | [diff] [blame] | 4738 | // When a user wants to use objc_boxable with a union or struct |
| 4739 | // but they don't have access to the declaration (legacy/third-party code) |
| 4740 | // then they can 'enable' this feature with a typedef: |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 4741 | // typedef struct __attribute((objc_boxable)) legacy_struct legacy_struct; |
| 4742 | static void handleObjCBoxable(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4743 | bool notify = false; |
| 4744 | |
| 4745 | RecordDecl *RD = dyn_cast<RecordDecl>(D); |
| 4746 | if (RD && RD->getDefinition()) { |
| 4747 | RD = RD->getDefinition(); |
| 4748 | notify = true; |
| 4749 | } |
| 4750 | |
| 4751 | if (RD) { |
| 4752 | ObjCBoxableAttr *BoxableAttr = ::new (S.Context) |
| 4753 | ObjCBoxableAttr(Attr.getRange(), S.Context, |
| 4754 | Attr.getAttributeSpellingListIndex()); |
| 4755 | RD->addAttr(BoxableAttr); |
| 4756 | if (notify) { |
| 4757 | // we need to notify ASTReader/ASTWriter about |
| 4758 | // modification of existing declaration |
| 4759 | if (ASTMutationListener *L = S.getASTMutationListener()) |
| 4760 | L->AddedAttributeToRecord(BoxableAttr, RD); |
| 4761 | } |
| 4762 | } |
| 4763 | } |
| 4764 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4765 | static void handleObjCOwnershipAttr(Sema &S, Decl *D, |
| 4766 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4767 | if (hasDeclarator(D)) return; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4768 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4769 | S.Diag(D->getLocStart(), diag::err_attribute_wrong_decl_type) |
Douglas Gregor | 5c3cc42 | 2012-03-14 16:55:17 +0000 | [diff] [blame] | 4770 | << Attr.getRange() << Attr.getName() << ExpectedVariable; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4771 | } |
| 4772 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4773 | static void handleObjCPreciseLifetimeAttr(Sema &S, Decl *D, |
| 4774 | const AttributeList &Attr) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4775 | ValueDecl *vd = cast<ValueDecl>(D); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4776 | QualType type = vd->getType(); |
| 4777 | |
| 4778 | if (!type->isDependentType() && |
| 4779 | !type->isObjCLifetimeType()) { |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4780 | S.Diag(Attr.getLoc(), diag::err_objc_precise_lifetime_bad_type) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4781 | << type; |
| 4782 | return; |
| 4783 | } |
| 4784 | |
| 4785 | Qualifiers::ObjCLifetime lifetime = type.getObjCLifetime(); |
| 4786 | |
| 4787 | // If we have no lifetime yet, check the lifetime we're presumably |
| 4788 | // going to infer. |
| 4789 | if (lifetime == Qualifiers::OCL_None && !type->isDependentType()) |
| 4790 | lifetime = type->getObjCARCImplicitLifetime(); |
| 4791 | |
| 4792 | switch (lifetime) { |
| 4793 | case Qualifiers::OCL_None: |
| 4794 | assert(type->isDependentType() && |
| 4795 | "didn't infer lifetime for non-dependent type?"); |
| 4796 | break; |
| 4797 | |
| 4798 | case Qualifiers::OCL_Weak: // meaningful |
| 4799 | case Qualifiers::OCL_Strong: // meaningful |
| 4800 | break; |
| 4801 | |
| 4802 | case Qualifiers::OCL_ExplicitNone: |
| 4803 | case Qualifiers::OCL_Autoreleasing: |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4804 | S.Diag(Attr.getLoc(), diag::warn_objc_precise_lifetime_meaningless) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4805 | << (lifetime == Qualifiers::OCL_Autoreleasing); |
| 4806 | break; |
| 4807 | } |
| 4808 | |
Chandler Carruth | ff4c4f0 | 2011-07-01 23:49:12 +0000 | [diff] [blame] | 4809 | D->addAttr(::new (S.Context) |
Michael Han | 9931593 | 2013-01-24 16:46:58 +0000 | [diff] [blame] | 4810 | ObjCPreciseLifetimeAttr(Attr.getRange(), S.Context, |
| 4811 | Attr.getAttributeSpellingListIndex())); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4812 | } |
| 4813 | |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4814 | //===----------------------------------------------------------------------===// |
| 4815 | // Microsoft specific attribute handlers. |
| 4816 | //===----------------------------------------------------------------------===// |
| 4817 | |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 4818 | UuidAttr *Sema::mergeUuidAttr(Decl *D, SourceRange Range, |
| 4819 | unsigned AttrSpellingListIndex, StringRef Uuid) { |
| 4820 | if (const auto *UA = D->getAttr<UuidAttr>()) { |
Nico Weber | d58c260 | 2016-09-14 01:16:54 +0000 | [diff] [blame] | 4821 | if (UA->getGuid().equals_lower(Uuid)) |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 4822 | return nullptr; |
| 4823 | Diag(UA->getLocation(), diag::err_mismatched_uuid); |
| 4824 | Diag(Range.getBegin(), diag::note_previous_uuid); |
| 4825 | D->dropAttr<UuidAttr>(); |
| 4826 | } |
| 4827 | |
| 4828 | return ::new (Context) UuidAttr(Range, Context, Uuid, AttrSpellingListIndex); |
| 4829 | } |
| 4830 | |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 4831 | static void handleUuidAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
Aaron Ballman | df8fe4c | 2013-11-24 21:35:16 +0000 | [diff] [blame] | 4832 | if (!S.LangOpts.CPlusPlus) { |
| 4833 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4834 | << Attr.getName() << AttributeLangSupport::C; |
| 4835 | return; |
| 4836 | } |
| 4837 | |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4838 | StringRef StrRef; |
| 4839 | SourceLocation LiteralLoc; |
Tim Northover | 6a6b63b | 2013-10-01 14:34:18 +0000 | [diff] [blame] | 4840 | if (!S.checkStringLiteralArgumentAttr(Attr, 0, StrRef, &LiteralLoc)) |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4841 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4842 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4843 | // GUID format is "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" or |
| 4844 | // "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}", normalize to the former. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4845 | if (StrRef.size() == 38 && StrRef.front() == '{' && StrRef.back() == '}') |
| 4846 | StrRef = StrRef.drop_front().drop_back(); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4847 | |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4848 | // Validate GUID length. |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4849 | if (StrRef.size() != 36) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4850 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4851 | return; |
| 4852 | } |
Anders Carlsson | 19588aa | 2011-01-23 21:07:30 +0000 | [diff] [blame] | 4853 | |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4854 | for (unsigned i = 0; i < 36; ++i) { |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4855 | if (i == 8 || i == 13 || i == 18 || i == 23) { |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4856 | if (StrRef[i] != '-') { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4857 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4858 | return; |
| 4859 | } |
David Majnemer | 8908534 | 2013-08-09 08:56:20 +0000 | [diff] [blame] | 4860 | } else if (!isHexDigit(StrRef[i])) { |
Benjamin Kramer | 6ee1562 | 2013-09-13 15:35:43 +0000 | [diff] [blame] | 4861 | S.Diag(LiteralLoc, diag::err_attribute_uuid_malformed_guid); |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4862 | return; |
Francois Pichet | 7da1166 | 2010-12-20 01:41:49 +0000 | [diff] [blame] | 4863 | } |
Reid Kleckner | 140c4a7 | 2013-05-17 14:04:52 +0000 | [diff] [blame] | 4864 | } |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 4865 | |
Nico Weber | 88f5ed9 | 2016-09-13 18:55:26 +0000 | [diff] [blame] | 4866 | UuidAttr *UA = S.mergeUuidAttr(D, Attr.getRange(), |
| 4867 | Attr.getAttributeSpellingListIndex(), StrRef); |
| 4868 | if (UA) |
| 4869 | D->addAttr(UA); |
Charles Davis | 163855f | 2010-02-16 18:27:26 +0000 | [diff] [blame] | 4870 | } |
| 4871 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4872 | static void handleMSInheritanceAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4873 | if (!S.LangOpts.CPlusPlus) { |
| 4874 | S.Diag(Attr.getLoc(), diag::err_attribute_not_supported_in_lang) |
| 4875 | << Attr.getName() << AttributeLangSupport::C; |
| 4876 | return; |
| 4877 | } |
| 4878 | MSInheritanceAttr *IA = S.mergeMSInheritanceAttr( |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 4879 | D, Attr.getRange(), /*BestCase=*/true, |
| 4880 | Attr.getAttributeSpellingListIndex(), |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4881 | (MSInheritanceAttr::Spelling)Attr.getSemanticSpelling()); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 4882 | if (IA) { |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4883 | D->addAttr(IA); |
David Majnemer | 929025d | 2016-01-26 19:30:26 +0000 | [diff] [blame] | 4884 | S.Consumer.AssignInheritanceModel(cast<CXXRecordDecl>(D)); |
| 4885 | } |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 4886 | } |
| 4887 | |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 4888 | static void handleDeclspecThreadAttr(Sema &S, Decl *D, |
| 4889 | const AttributeList &Attr) { |
| 4890 | VarDecl *VD = cast<VarDecl>(D); |
| 4891 | if (!S.Context.getTargetInfo().isTLSSupported()) { |
| 4892 | S.Diag(Attr.getLoc(), diag::err_thread_unsupported); |
| 4893 | return; |
| 4894 | } |
| 4895 | if (VD->getTSCSpec() != TSCS_unspecified) { |
| 4896 | S.Diag(Attr.getLoc(), diag::err_declspec_thread_on_thread_variable); |
| 4897 | return; |
| 4898 | } |
| 4899 | if (VD->hasLocalStorage()) { |
| 4900 | S.Diag(Attr.getLoc(), diag::err_thread_non_global) << "__declspec(thread)"; |
| 4901 | return; |
| 4902 | } |
| 4903 | VD->addAttr(::new (S.Context) ThreadAttr( |
| 4904 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 4905 | } |
| 4906 | |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 4907 | static void handleAbiTagAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 4908 | SmallVector<StringRef, 4> Tags; |
| 4909 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 4910 | StringRef Tag; |
| 4911 | if (!S.checkStringLiteralArgumentAttr(Attr, I, Tag)) |
| 4912 | return; |
| 4913 | Tags.push_back(Tag); |
| 4914 | } |
| 4915 | |
| 4916 | if (const auto *NS = dyn_cast<NamespaceDecl>(D)) { |
| 4917 | if (!NS->isInline()) { |
| 4918 | S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 0; |
| 4919 | return; |
| 4920 | } |
| 4921 | if (NS->isAnonymousNamespace()) { |
| 4922 | S.Diag(Attr.getLoc(), diag::warn_attr_abi_tag_namespace) << 1; |
| 4923 | return; |
| 4924 | } |
| 4925 | if (Attr.getNumArgs() == 0) |
| 4926 | Tags.push_back(NS->getName()); |
| 4927 | } else if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 4928 | return; |
| 4929 | |
| 4930 | // Store tags sorted and without duplicates. |
| 4931 | std::sort(Tags.begin(), Tags.end()); |
| 4932 | Tags.erase(std::unique(Tags.begin(), Tags.end()), Tags.end()); |
| 4933 | |
| 4934 | D->addAttr(::new (S.Context) |
| 4935 | AbiTagAttr(Attr.getRange(), S.Context, Tags.data(), Tags.size(), |
| 4936 | Attr.getAttributeSpellingListIndex())); |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 4939 | static void handleARMInterruptAttr(Sema &S, Decl *D, |
| 4940 | const AttributeList &Attr) { |
| 4941 | // Check the attribute arguments. |
| 4942 | if (Attr.getNumArgs() > 1) { |
| 4943 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 4944 | << Attr.getName() << 1; |
| 4945 | return; |
| 4946 | } |
| 4947 | |
| 4948 | StringRef Str; |
| 4949 | SourceLocation ArgLoc; |
| 4950 | |
| 4951 | if (Attr.getNumArgs() == 0) |
| 4952 | Str = ""; |
| 4953 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 4954 | return; |
| 4955 | |
| 4956 | ARMInterruptAttr::InterruptType Kind; |
| 4957 | if (!ARMInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 4958 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 4959 | << Attr.getName() << Str << ArgLoc; |
| 4960 | return; |
| 4961 | } |
| 4962 | |
| 4963 | unsigned Index = Attr.getAttributeSpellingListIndex(); |
| 4964 | D->addAttr(::new (S.Context) |
| 4965 | ARMInterruptAttr(Attr.getLoc(), S.Context, Kind, Index)); |
| 4966 | } |
| 4967 | |
| 4968 | static void handleMSP430InterruptAttr(Sema &S, Decl *D, |
| 4969 | const AttributeList &Attr) { |
| 4970 | if (!checkAttributeNumArgs(S, Attr, 1)) |
| 4971 | return; |
| 4972 | |
| 4973 | if (!Attr.isArgExpr(0)) { |
| 4974 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) << Attr.getName() |
| 4975 | << AANT_ArgumentIntegerConstant; |
| 4976 | return; |
| 4977 | } |
| 4978 | |
| 4979 | // FIXME: Check for decl - it should be void ()(void). |
| 4980 | |
| 4981 | Expr *NumParamsExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 4982 | llvm::APSInt NumParams(32); |
| 4983 | if (!NumParamsExpr->isIntegerConstantExpr(NumParams, S.Context)) { |
| 4984 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_type) |
| 4985 | << Attr.getName() << AANT_ArgumentIntegerConstant |
| 4986 | << NumParamsExpr->getSourceRange(); |
| 4987 | return; |
| 4988 | } |
| 4989 | |
| 4990 | unsigned Num = NumParams.getLimitedValue(255); |
| 4991 | if ((Num & 1) || Num > 30) { |
| 4992 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 4993 | << Attr.getName() << (int)NumParams.getSExtValue() |
| 4994 | << NumParamsExpr->getSourceRange(); |
| 4995 | return; |
| 4996 | } |
| 4997 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 4998 | D->addAttr(::new (S.Context) |
| 4999 | MSP430InterruptAttr(Attr.getLoc(), S.Context, Num, |
| 5000 | Attr.getAttributeSpellingListIndex())); |
| 5001 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5002 | } |
| 5003 | |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5004 | static void handleMipsInterruptAttr(Sema &S, Decl *D, |
| 5005 | const AttributeList &Attr) { |
| 5006 | // Only one optional argument permitted. |
| 5007 | if (Attr.getNumArgs() > 1) { |
| 5008 | S.Diag(Attr.getLoc(), diag::err_attribute_too_many_arguments) |
| 5009 | << Attr.getName() << 1; |
| 5010 | return; |
| 5011 | } |
| 5012 | |
| 5013 | StringRef Str; |
| 5014 | SourceLocation ArgLoc; |
| 5015 | |
| 5016 | if (Attr.getNumArgs() == 0) |
| 5017 | Str = ""; |
| 5018 | else if (!S.checkStringLiteralArgumentAttr(Attr, 0, Str, &ArgLoc)) |
| 5019 | return; |
| 5020 | |
| 5021 | // Semantic checks for a function with the 'interrupt' attribute for MIPS: |
| 5022 | // a) Must be a function. |
| 5023 | // b) Must have no parameters. |
| 5024 | // c) Must have the 'void' return type. |
| 5025 | // d) Cannot have the 'mips16' attribute, as that instruction set |
| 5026 | // lacks the 'eret' instruction. |
| 5027 | // e) The attribute itself must either have no argument or one of the |
| 5028 | // valid interrupt types, see [MipsInterruptDocs]. |
| 5029 | |
| 5030 | if (!isFunctionOrMethod(D)) { |
| 5031 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5032 | << "'interrupt'" << ExpectedFunctionOrMethod; |
| 5033 | return; |
| 5034 | } |
| 5035 | |
| 5036 | if (hasFunctionProto(D) && getFunctionOrMethodNumParams(D) != 0) { |
| 5037 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 5038 | << 0; |
| 5039 | return; |
| 5040 | } |
| 5041 | |
| 5042 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5043 | S.Diag(D->getLocation(), diag::warn_mips_interrupt_attribute) |
| 5044 | << 1; |
| 5045 | return; |
| 5046 | } |
| 5047 | |
| 5048 | if (checkAttrMutualExclusion<Mips16Attr>(S, D, Attr.getRange(), |
| 5049 | Attr.getName())) |
| 5050 | return; |
| 5051 | |
| 5052 | MipsInterruptAttr::InterruptType Kind; |
| 5053 | if (!MipsInterruptAttr::ConvertStrToInterruptType(Str, Kind)) { |
| 5054 | S.Diag(Attr.getLoc(), diag::warn_attribute_type_not_supported) |
| 5055 | << Attr.getName() << "'" + std::string(Str) + "'"; |
| 5056 | return; |
| 5057 | } |
| 5058 | |
| 5059 | D->addAttr(::new (S.Context) MipsInterruptAttr( |
| 5060 | Attr.getLoc(), S.Context, Kind, Attr.getAttributeSpellingListIndex())); |
| 5061 | } |
| 5062 | |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5063 | static void handleAnyX86InterruptAttr(Sema &S, Decl *D, |
| 5064 | const AttributeList &Attr) { |
| 5065 | // Semantic checks for a function with the 'interrupt' attribute. |
| 5066 | // a) Must be a function. |
| 5067 | // b) Must have the 'void' return type. |
| 5068 | // c) Must take 1 or 2 arguments. |
| 5069 | // d) The 1st argument must be a pointer. |
| 5070 | // e) The 2nd argument (if any) must be an unsigned integer. |
| 5071 | if (!isFunctionOrMethod(D) || !hasFunctionProto(D) || isInstanceMethod(D) || |
| 5072 | CXXMethodDecl::isStaticOverloadedOperator( |
| 5073 | cast<NamedDecl>(D)->getDeclName().getCXXOverloadedOperator())) { |
| 5074 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 5075 | << Attr.getName() << ExpectedFunctionWithProtoType; |
| 5076 | return; |
| 5077 | } |
| 5078 | // Interrupt handler must have void return type. |
| 5079 | if (!getFunctionOrMethodResultType(D)->isVoidType()) { |
| 5080 | S.Diag(getFunctionOrMethodResultSourceRange(D).getBegin(), |
| 5081 | diag::err_anyx86_interrupt_attribute) |
| 5082 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5083 | ? 0 |
| 5084 | : 1) |
| 5085 | << 0; |
| 5086 | return; |
| 5087 | } |
| 5088 | // Interrupt handler must have 1 or 2 parameters. |
| 5089 | unsigned NumParams = getFunctionOrMethodNumParams(D); |
| 5090 | if (NumParams < 1 || NumParams > 2) { |
| 5091 | S.Diag(D->getLocStart(), diag::err_anyx86_interrupt_attribute) |
| 5092 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5093 | ? 0 |
| 5094 | : 1) |
| 5095 | << 1; |
| 5096 | return; |
| 5097 | } |
| 5098 | // The first argument must be a pointer. |
| 5099 | if (!getFunctionOrMethodParamType(D, 0)->isPointerType()) { |
| 5100 | S.Diag(getFunctionOrMethodParamRange(D, 0).getBegin(), |
| 5101 | diag::err_anyx86_interrupt_attribute) |
| 5102 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5103 | ? 0 |
| 5104 | : 1) |
| 5105 | << 2; |
| 5106 | return; |
| 5107 | } |
| 5108 | // The second argument, if present, must be an unsigned integer. |
| 5109 | unsigned TypeSize = |
| 5110 | S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86_64 |
| 5111 | ? 64 |
| 5112 | : 32; |
| 5113 | if (NumParams == 2 && |
| 5114 | (!getFunctionOrMethodParamType(D, 1)->isUnsignedIntegerType() || |
| 5115 | S.Context.getTypeSize(getFunctionOrMethodParamType(D, 1)) != TypeSize)) { |
| 5116 | S.Diag(getFunctionOrMethodParamRange(D, 1).getBegin(), |
| 5117 | diag::err_anyx86_interrupt_attribute) |
| 5118 | << (S.Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86 |
| 5119 | ? 0 |
| 5120 | : 1) |
| 5121 | << 3 << S.Context.getIntTypeForBitwidth(TypeSize, /*Signed=*/false); |
| 5122 | return; |
| 5123 | } |
| 5124 | D->addAttr(::new (S.Context) AnyX86InterruptAttr( |
| 5125 | Attr.getLoc(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 5126 | D->addAttr(UsedAttr::CreateImplicit(S.Context)); |
| 5127 | } |
| 5128 | |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5129 | static void handleAVRInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5130 | if (!isFunctionOrMethod(D)) { |
| 5131 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5132 | << "'interrupt'" << ExpectedFunction; |
| 5133 | return; |
| 5134 | } |
| 5135 | |
| 5136 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 5137 | return; |
| 5138 | |
| 5139 | handleSimpleAttribute<AVRInterruptAttr>(S, D, Attr); |
| 5140 | } |
| 5141 | |
| 5142 | static void handleAVRSignalAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5143 | if (!isFunctionOrMethod(D)) { |
| 5144 | S.Diag(D->getLocation(), diag::warn_attribute_wrong_decl_type) |
| 5145 | << "'signal'" << ExpectedFunction; |
| 5146 | return; |
| 5147 | } |
| 5148 | |
| 5149 | if (!checkAttributeNumArgs(S, Attr, 0)) |
| 5150 | return; |
| 5151 | |
| 5152 | handleSimpleAttribute<AVRSignalAttr>(S, D, Attr); |
| 5153 | } |
| 5154 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5155 | static void handleInterruptAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5156 | // Dispatch the interrupt attribute based on the current target. |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5157 | switch (S.Context.getTargetInfo().getTriple().getArch()) { |
| 5158 | case llvm::Triple::msp430: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5159 | handleMSP430InterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5160 | break; |
| 5161 | case llvm::Triple::mipsel: |
| 5162 | case llvm::Triple::mips: |
Daniel Sanders | bd3f47f | 2015-11-27 18:03:44 +0000 | [diff] [blame] | 5163 | handleMipsInterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5164 | break; |
| 5165 | case llvm::Triple::x86: |
| 5166 | case llvm::Triple::x86_64: |
| 5167 | handleAnyX86InterruptAttr(S, D, Attr); |
| 5168 | break; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5169 | case llvm::Triple::avr: |
| 5170 | handleAVRInterruptAttr(S, D, Attr); |
| 5171 | break; |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5172 | default: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5173 | handleARMInterruptAttr(S, D, Attr); |
Alexey Bataev | d51e993 | 2016-01-15 04:06:31 +0000 | [diff] [blame] | 5174 | break; |
| 5175 | } |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5178 | static void handleAMDGPUFlatWorkGroupSizeAttr(Sema &S, Decl *D, |
| 5179 | const AttributeList &Attr) { |
| 5180 | uint32_t Min = 0; |
| 5181 | Expr *MinExpr = Attr.getArgAsExpr(0); |
| 5182 | if (!checkUInt32Argument(S, Attr, MinExpr, Min)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5183 | return; |
| 5184 | |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5185 | uint32_t Max = 0; |
| 5186 | Expr *MaxExpr = Attr.getArgAsExpr(1); |
| 5187 | if (!checkUInt32Argument(S, Attr, MaxExpr, Max)) |
| 5188 | return; |
| 5189 | |
| 5190 | if (Min == 0 && Max != 0) { |
| 5191 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5192 | << Attr.getName() << 0; |
| 5193 | return; |
| 5194 | } |
| 5195 | if (Min > Max) { |
| 5196 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5197 | << Attr.getName() << 1; |
| 5198 | return; |
| 5199 | } |
| 5200 | |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5201 | D->addAttr(::new (S.Context) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5202 | AMDGPUFlatWorkGroupSizeAttr(Attr.getLoc(), S.Context, Min, Max, |
| 5203 | Attr.getAttributeSpellingListIndex())); |
| 5204 | } |
| 5205 | |
| 5206 | static void handleAMDGPUWavesPerEUAttr(Sema &S, Decl *D, |
| 5207 | const AttributeList &Attr) { |
| 5208 | uint32_t Min = 0; |
| 5209 | Expr *MinExpr = Attr.getArgAsExpr(0); |
| 5210 | if (!checkUInt32Argument(S, Attr, MinExpr, Min)) |
| 5211 | return; |
| 5212 | |
| 5213 | uint32_t Max = 0; |
| 5214 | if (Attr.getNumArgs() == 2) { |
| 5215 | Expr *MaxExpr = Attr.getArgAsExpr(1); |
| 5216 | if (!checkUInt32Argument(S, Attr, MaxExpr, Max)) |
| 5217 | return; |
| 5218 | } |
| 5219 | |
| 5220 | if (Min == 0 && Max != 0) { |
| 5221 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5222 | << Attr.getName() << 0; |
| 5223 | return; |
| 5224 | } |
| 5225 | if (Max != 0 && Min > Max) { |
| 5226 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_invalid) |
| 5227 | << Attr.getName() << 1; |
| 5228 | return; |
| 5229 | } |
| 5230 | |
| 5231 | D->addAttr(::new (S.Context) |
| 5232 | AMDGPUWavesPerEUAttr(Attr.getLoc(), S.Context, Min, Max, |
| 5233 | Attr.getAttributeSpellingListIndex())); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5234 | } |
| 5235 | |
| 5236 | static void handleAMDGPUNumSGPRAttr(Sema &S, Decl *D, |
| 5237 | const AttributeList &Attr) { |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5238 | uint32_t NumSGPR = 0; |
| 5239 | Expr *NumSGPRExpr = Attr.getArgAsExpr(0); |
| 5240 | if (!checkUInt32Argument(S, Attr, NumSGPRExpr, NumSGPR)) |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5241 | return; |
| 5242 | |
| 5243 | D->addAttr(::new (S.Context) |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5244 | AMDGPUNumSGPRAttr(Attr.getLoc(), S.Context, NumSGPR, |
| 5245 | Attr.getAttributeSpellingListIndex())); |
| 5246 | } |
| 5247 | |
| 5248 | static void handleAMDGPUNumVGPRAttr(Sema &S, Decl *D, |
| 5249 | const AttributeList &Attr) { |
| 5250 | uint32_t NumVGPR = 0; |
| 5251 | Expr *NumVGPRExpr = Attr.getArgAsExpr(0); |
| 5252 | if (!checkUInt32Argument(S, Attr, NumVGPRExpr, NumVGPR)) |
| 5253 | return; |
| 5254 | |
| 5255 | D->addAttr(::new (S.Context) |
| 5256 | AMDGPUNumVGPRAttr(Attr.getLoc(), S.Context, NumVGPR, |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5257 | Attr.getAttributeSpellingListIndex())); |
| 5258 | } |
| 5259 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5260 | static void handleX86ForceAlignArgPointerAttr(Sema &S, Decl *D, |
| 5261 | const AttributeList& Attr) { |
| 5262 | // If we try to apply it to a function pointer, don't warn, but don't |
| 5263 | // do anything, either. It doesn't matter anyway, because there's nothing |
| 5264 | // special about calling a force_align_arg_pointer function. |
| 5265 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 5266 | if (VD && VD->getType()->isFunctionPointerType()) |
| 5267 | return; |
| 5268 | // Also don't warn on function pointer typedefs. |
| 5269 | TypedefNameDecl *TD = dyn_cast<TypedefNameDecl>(D); |
| 5270 | if (TD && (TD->getUnderlyingType()->isFunctionPointerType() || |
| 5271 | TD->getUnderlyingType()->isFunctionType())) |
| 5272 | return; |
| 5273 | // Attribute can only be applied to function types. |
| 5274 | if (!isa<FunctionDecl>(D)) { |
| 5275 | S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type) |
| 5276 | << Attr.getName() << /* function */0; |
| 5277 | return; |
| 5278 | } |
| 5279 | |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 5280 | D->addAttr(::new (S.Context) |
| 5281 | X86ForceAlignArgPointerAttr(Attr.getRange(), S.Context, |
| 5282 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5283 | } |
| 5284 | |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 5285 | static void handleLayoutVersion(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5286 | uint32_t Version; |
| 5287 | Expr *VersionExpr = static_cast<Expr *>(Attr.getArgAsExpr(0)); |
| 5288 | if (!checkUInt32Argument(S, Attr, Attr.getArgAsExpr(0), Version)) |
| 5289 | return; |
| 5290 | |
| 5291 | // TODO: Investigate what happens with the next major version of MSVC. |
| 5292 | if (Version != LangOptions::MSVC2015) { |
| 5293 | S.Diag(Attr.getLoc(), diag::err_attribute_argument_out_of_bounds) |
| 5294 | << Attr.getName() << Version << VersionExpr->getSourceRange(); |
| 5295 | return; |
| 5296 | } |
| 5297 | |
| 5298 | D->addAttr(::new (S.Context) |
| 5299 | LayoutVersionAttr(Attr.getRange(), S.Context, Version, |
| 5300 | Attr.getAttributeSpellingListIndex())); |
| 5301 | } |
| 5302 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5303 | DLLImportAttr *Sema::mergeDLLImportAttr(Decl *D, SourceRange Range, |
| 5304 | unsigned AttrSpellingListIndex) { |
| 5305 | if (D->hasAttr<DLLExportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5306 | Diag(Range.getBegin(), diag::warn_attribute_ignored) << "'dllimport'"; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5307 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5308 | } |
| 5309 | |
| 5310 | if (D->hasAttr<DLLImportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5311 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5312 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5313 | return ::new (Context) DLLImportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5314 | } |
| 5315 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5316 | DLLExportAttr *Sema::mergeDLLExportAttr(Decl *D, SourceRange Range, |
| 5317 | unsigned AttrSpellingListIndex) { |
| 5318 | if (DLLImportAttr *Import = D->getAttr<DLLImportAttr>()) { |
Nico Rieck | 6047866 | 2014-02-22 19:47:30 +0000 | [diff] [blame] | 5319 | Diag(Import->getLocation(), diag::warn_attribute_ignored) << Import; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5320 | D->dropAttr<DLLImportAttr>(); |
| 5321 | } |
| 5322 | |
| 5323 | if (D->hasAttr<DLLExportAttr>()) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5324 | return nullptr; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5325 | |
Aaron Ballman | 3f5f3e7 | 2014-01-20 16:15:55 +0000 | [diff] [blame] | 5326 | return ::new (Context) DLLExportAttr(Range, Context, AttrSpellingListIndex); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5327 | } |
| 5328 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5329 | static void handleDLLAttr(Sema &S, Decl *D, const AttributeList &A) { |
Hans Wennborg | 5e64528 | 2014-06-24 23:57:13 +0000 | [diff] [blame] | 5330 | if (isa<ClassTemplatePartialSpecializationDecl>(D) && |
| 5331 | S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5332 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored) |
| 5333 | << A.getName(); |
| 5334 | return; |
| 5335 | } |
| 5336 | |
Hans Wennborg | 606bd6d | 2014-11-03 14:24:45 +0000 | [diff] [blame] | 5337 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 5338 | if (FD->isInlined() && A.getKind() == AttributeList::AT_DLLImport && |
| 5339 | !S.Context.getTargetInfo().getCXXABI().isMicrosoft()) { |
| 5340 | // MinGW doesn't allow dllimport on inline functions. |
| 5341 | S.Diag(A.getRange().getBegin(), diag::warn_attribute_ignored_on_inline) |
| 5342 | << A.getName(); |
| 5343 | return; |
| 5344 | } |
| 5345 | } |
| 5346 | |
Hans Wennborg | 5869ec4 | 2015-09-15 21:05:30 +0000 | [diff] [blame] | 5347 | if (auto *MD = dyn_cast<CXXMethodDecl>(D)) { |
| 5348 | if (S.Context.getTargetInfo().getCXXABI().isMicrosoft() && |
| 5349 | MD->getParent()->isLambda()) { |
| 5350 | S.Diag(A.getRange().getBegin(), diag::err_attribute_dll_lambda) << A.getName(); |
| 5351 | return; |
| 5352 | } |
| 5353 | } |
| 5354 | |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5355 | unsigned Index = A.getAttributeSpellingListIndex(); |
| 5356 | Attr *NewAttr = A.getKind() == AttributeList::AT_DLLExport |
| 5357 | ? (Attr *)S.mergeDLLExportAttr(D, A.getRange(), Index) |
| 5358 | : (Attr *)S.mergeDLLImportAttr(D, A.getRange(), Index); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5359 | if (NewAttr) |
| 5360 | D->addAttr(NewAttr); |
| 5361 | } |
| 5362 | |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5363 | MSInheritanceAttr * |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5364 | Sema::mergeMSInheritanceAttr(Decl *D, SourceRange Range, bool BestCase, |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5365 | unsigned AttrSpellingListIndex, |
| 5366 | MSInheritanceAttr::Spelling SemanticSpelling) { |
| 5367 | if (MSInheritanceAttr *IA = D->getAttr<MSInheritanceAttr>()) { |
| 5368 | if (IA->getSemanticSpelling() == SemanticSpelling) |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5369 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5370 | Diag(IA->getLocation(), diag::err_mismatched_ms_inheritance) |
| 5371 | << 1 /*previous declaration*/; |
| 5372 | Diag(Range.getBegin(), diag::note_previous_ms_inheritance); |
| 5373 | D->dropAttr<MSInheritanceAttr>(); |
| 5374 | } |
| 5375 | |
| 5376 | CXXRecordDecl *RD = cast<CXXRecordDecl>(D); |
| 5377 | if (RD->hasDefinition()) { |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5378 | if (checkMSInheritanceAttrOnDefinition(RD, Range, BestCase, |
| 5379 | SemanticSpelling)) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5380 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5381 | } |
| 5382 | } else { |
| 5383 | if (isa<ClassTemplatePartialSpecializationDecl>(RD)) { |
| 5384 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5385 | << 1 /*partial specialization*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5386 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5387 | } |
| 5388 | if (RD->getDescribedClassTemplate()) { |
| 5389 | Diag(Range.getBegin(), diag::warn_ignored_ms_inheritance) |
| 5390 | << 0 /*primary template*/; |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 5391 | return nullptr; |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5392 | } |
| 5393 | } |
| 5394 | |
| 5395 | return ::new (Context) |
David Majnemer | 4bb0980 | 2014-02-10 19:50:15 +0000 | [diff] [blame] | 5396 | MSInheritanceAttr(Range, Context, BestCase, AttrSpellingListIndex); |
David Majnemer | 2c4e00a | 2014-01-29 22:07:36 +0000 | [diff] [blame] | 5397 | } |
| 5398 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5399 | static void handleCapabilityAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5400 | // The capability attributes take a single string parameter for the name of |
| 5401 | // the capability they represent. The lockable attribute does not take any |
| 5402 | // parameters. However, semantically, both attributes represent the same |
| 5403 | // concept, and so they use the same semantic attribute. Eventually, the |
| 5404 | // lockable attribute will be removed. |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5405 | // |
Alp Toker | 958027b | 2014-07-14 19:42:55 +0000 | [diff] [blame] | 5406 | // For backward compatibility, any capability which has no specified string |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5407 | // literal will be considered a "mutex." |
| 5408 | StringRef N("mutex"); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5409 | SourceLocation LiteralLoc; |
| 5410 | if (Attr.getKind() == AttributeList::AT_Capability && |
| 5411 | !S.checkStringLiteralArgumentAttr(Attr, 0, N, &LiteralLoc)) |
| 5412 | return; |
| 5413 | |
Aaron Ballman | 6c81007 | 2014-03-05 21:47:13 +0000 | [diff] [blame] | 5414 | // Currently, there are only two names allowed for a capability: role and |
| 5415 | // mutex (case insensitive). Diagnose other capability names. |
| 5416 | if (!N.equals_lower("mutex") && !N.equals_lower("role")) |
| 5417 | S.Diag(LiteralLoc, diag::warn_invalid_capability_name) << N; |
| 5418 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5419 | D->addAttr(::new (S.Context) CapabilityAttr(Attr.getRange(), S.Context, N, |
| 5420 | Attr.getAttributeSpellingListIndex())); |
| 5421 | } |
| 5422 | |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5423 | static void handleAssertCapabilityAttr(Sema &S, Decl *D, |
| 5424 | const AttributeList &Attr) { |
| 5425 | D->addAttr(::new (S.Context) AssertCapabilityAttr(Attr.getRange(), S.Context, |
| 5426 | Attr.getArgAsExpr(0), |
| 5427 | Attr.getAttributeSpellingListIndex())); |
| 5428 | } |
| 5429 | |
| 5430 | static void handleAcquireCapabilityAttr(Sema &S, Decl *D, |
| 5431 | const AttributeList &Attr) { |
| 5432 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5433 | if (!checkLockFunAttrCommon(S, D, Attr, Args)) |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5434 | return; |
| 5435 | |
| 5436 | D->addAttr(::new (S.Context) AcquireCapabilityAttr(Attr.getRange(), |
| 5437 | S.Context, |
| 5438 | Args.data(), Args.size(), |
| 5439 | Attr.getAttributeSpellingListIndex())); |
| 5440 | } |
| 5441 | |
| 5442 | static void handleTryAcquireCapabilityAttr(Sema &S, Decl *D, |
| 5443 | const AttributeList &Attr) { |
| 5444 | SmallVector<Expr*, 2> Args; |
| 5445 | if (!checkTryLockFunAttrCommon(S, D, Attr, Args)) |
| 5446 | return; |
| 5447 | |
| 5448 | D->addAttr(::new (S.Context) TryAcquireCapabilityAttr(Attr.getRange(), |
| 5449 | S.Context, |
| 5450 | Attr.getArgAsExpr(0), |
| 5451 | Args.data(), |
| 5452 | Args.size(), |
| 5453 | Attr.getAttributeSpellingListIndex())); |
| 5454 | } |
| 5455 | |
| 5456 | static void handleReleaseCapabilityAttr(Sema &S, Decl *D, |
| 5457 | const AttributeList &Attr) { |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5458 | // Check that all arguments are lockable objects. |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5459 | SmallVector<Expr *, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 5460 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args, 0, true); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5461 | |
Aaron Ballman | 18d85ae | 2014-03-20 16:02:49 +0000 | [diff] [blame] | 5462 | D->addAttr(::new (S.Context) ReleaseCapabilityAttr( |
| 5463 | Attr.getRange(), S.Context, Args.data(), Args.size(), |
| 5464 | Attr.getAttributeSpellingListIndex())); |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 5465 | } |
| 5466 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5467 | static void handleRequiresCapabilityAttr(Sema &S, Decl *D, |
| 5468 | const AttributeList &Attr) { |
| 5469 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5470 | return; |
| 5471 | |
| 5472 | // check that all arguments are lockable objects |
| 5473 | SmallVector<Expr*, 1> Args; |
Aaron Ballman | 69e6e7c | 2014-03-24 19:29:19 +0000 | [diff] [blame] | 5474 | checkAttrArgsAreCapabilityObjs(S, D, Attr, Args); |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 5475 | if (Args.empty()) |
| 5476 | return; |
| 5477 | |
| 5478 | RequiresCapabilityAttr *RCA = ::new (S.Context) |
| 5479 | RequiresCapabilityAttr(Attr.getRange(), S.Context, Args.data(), |
| 5480 | Args.size(), Attr.getAttributeSpellingListIndex()); |
| 5481 | |
| 5482 | D->addAttr(RCA); |
| 5483 | } |
| 5484 | |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5485 | static void handleDeprecatedAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5486 | if (auto *NSD = dyn_cast<NamespaceDecl>(D)) { |
| 5487 | if (NSD->isAnonymousNamespace()) { |
| 5488 | S.Diag(Attr.getLoc(), diag::warn_deprecated_anonymous_namespace); |
| 5489 | // Do not want to attach the attribute to the namespace because that will |
| 5490 | // cause confusing diagnostic reports for uses of declarations within the |
| 5491 | // namespace. |
| 5492 | return; |
| 5493 | } |
| 5494 | } |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5495 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 5496 | // Handle the cases where the attribute has a text message. |
| 5497 | StringRef Str, Replacement; |
| 5498 | if (Attr.isArgExpr(0) && Attr.getArgAsExpr(0) && |
| 5499 | !S.checkStringLiteralArgumentAttr(Attr, 0, Str)) |
| 5500 | return; |
| 5501 | |
| 5502 | // Only support a single optional message for Declspec and CXX11. |
| 5503 | if (Attr.isDeclspecAttribute() || Attr.isCXX11Attribute()) |
| 5504 | checkAttributeAtMostNumArgs(S, Attr, 1); |
| 5505 | else if (Attr.isArgExpr(1) && Attr.getArgAsExpr(1) && |
| 5506 | !S.checkStringLiteralArgumentAttr(Attr, 1, Replacement)) |
| 5507 | return; |
| 5508 | |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5509 | if (!S.getLangOpts().CPlusPlus14) |
| 5510 | if (Attr.isCXX11Attribute() && |
| 5511 | !(Attr.hasScope() && Attr.getScopeName()->isStr("gnu"))) |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5512 | S.Diag(Attr.getLoc(), diag::ext_cxx14_attr) << Attr.getName(); |
Saleem Abdulrasool | f931a38 | 2015-02-16 22:27:01 +0000 | [diff] [blame] | 5513 | |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5514 | D->addAttr(::new (S.Context) |
| 5515 | DeprecatedAttr(Attr.getRange(), S.Context, Str, Replacement, |
| 5516 | Attr.getAttributeSpellingListIndex())); |
| 5517 | } |
| 5518 | |
| 5519 | static bool isGlobalVar(const Decl *D) { |
| 5520 | if (const auto *S = dyn_cast<VarDecl>(D)) |
| 5521 | return S->hasGlobalStorage(); |
| 5522 | return false; |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5523 | } |
| 5524 | |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5525 | static void handleNoSanitizeAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5526 | if (!checkAttributeAtLeastNumArgs(S, Attr, 1)) |
| 5527 | return; |
| 5528 | |
Benjamin Kramer | 1b58201 | 2016-02-13 18:11:49 +0000 | [diff] [blame] | 5529 | std::vector<StringRef> Sanitizers; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5530 | |
| 5531 | for (unsigned I = 0, E = Attr.getNumArgs(); I != E; ++I) { |
| 5532 | StringRef SanitizerName; |
| 5533 | SourceLocation LiteralLoc; |
| 5534 | |
| 5535 | if (!S.checkStringLiteralArgumentAttr(Attr, I, SanitizerName, &LiteralLoc)) |
| 5536 | return; |
| 5537 | |
| 5538 | if (parseSanitizerValue(SanitizerName, /*AllowGroups=*/true) == 0) |
| 5539 | S.Diag(LiteralLoc, diag::warn_unknown_sanitizer_ignored) << SanitizerName; |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5540 | else if (isGlobalVar(D) && SanitizerName != "address") |
| 5541 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5542 | << Attr.getName() << ExpectedFunctionOrMethod; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5543 | Sanitizers.push_back(SanitizerName); |
| 5544 | } |
| 5545 | |
| 5546 | D->addAttr(::new (S.Context) NoSanitizeAttr( |
| 5547 | Attr.getRange(), S.Context, Sanitizers.data(), Sanitizers.size(), |
| 5548 | Attr.getAttributeSpellingListIndex())); |
| 5549 | } |
| 5550 | |
| 5551 | static void handleNoSanitizeSpecificAttr(Sema &S, Decl *D, |
| 5552 | const AttributeList &Attr) { |
Aaron Ballman | 8b5e7ba | 2015-10-08 19:24:08 +0000 | [diff] [blame] | 5553 | StringRef AttrName = Attr.getName()->getName(); |
| 5554 | normalizeName(AttrName); |
Douglas Katzman | 3ed0f64 | 2016-10-14 19:55:09 +0000 | [diff] [blame] | 5555 | StringRef SanitizerName = llvm::StringSwitch<StringRef>(AttrName) |
| 5556 | .Case("no_address_safety_analysis", "address") |
| 5557 | .Case("no_sanitize_address", "address") |
| 5558 | .Case("no_sanitize_thread", "thread") |
| 5559 | .Case("no_sanitize_memory", "memory"); |
| 5560 | if (isGlobalVar(D) && SanitizerName != "address") |
| 5561 | S.Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 5562 | << Attr.getName() << ExpectedFunction; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 5563 | D->addAttr(::new (S.Context) |
| 5564 | NoSanitizeAttr(Attr.getRange(), S.Context, &SanitizerName, 1, |
| 5565 | Attr.getAttributeSpellingListIndex())); |
| 5566 | } |
| 5567 | |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 5568 | static void handleInternalLinkageAttr(Sema &S, Decl *D, |
| 5569 | const AttributeList &Attr) { |
| 5570 | if (InternalLinkageAttr *Internal = |
| 5571 | S.mergeInternalLinkageAttr(D, Attr.getRange(), Attr.getName(), |
| 5572 | Attr.getAttributeSpellingListIndex())) |
| 5573 | D->addAttr(Internal); |
| 5574 | } |
| 5575 | |
Anastasia Stulova | c4bb5df | 2016-03-31 11:07:22 +0000 | [diff] [blame] | 5576 | static void handleOpenCLNoSVMAttr(Sema &S, Decl *D, const AttributeList &Attr) { |
| 5577 | if (S.LangOpts.OpenCLVersion != 200) |
| 5578 | S.Diag(Attr.getLoc(), diag::err_attribute_requires_opencl_version) |
| 5579 | << Attr.getName() << "2.0" << 0; |
| 5580 | else |
| 5581 | S.Diag(Attr.getLoc(), diag::warn_opencl_attr_deprecated_ignored) |
| 5582 | << Attr.getName() << "2.0"; |
| 5583 | } |
| 5584 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5585 | /// Handles semantic checking for features that are common to all attributes, |
| 5586 | /// such as checking whether a parameter was properly specified, or the correct |
| 5587 | /// number of arguments were passed, etc. |
| 5588 | static bool handleCommonAttributeFeatures(Sema &S, Scope *scope, Decl *D, |
| 5589 | const AttributeList &Attr) { |
| 5590 | // Several attributes carry different semantics than the parsing requires, so |
| 5591 | // those are opted out of the common handling. |
| 5592 | // |
| 5593 | // We also bail on unknown and ignored attributes because those are handled |
| 5594 | // as part of the target-specific handling logic. |
| 5595 | if (Attr.hasCustomParsing() || |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5596 | Attr.getKind() == AttributeList::UnknownAttribute) |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5597 | return false; |
| 5598 | |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5599 | // Check whether the attribute requires specific language extensions to be |
| 5600 | // enabled. |
| 5601 | if (!Attr.diagnoseLangOpts(S)) |
| 5602 | return true; |
| 5603 | |
Aaron Ballman | 8ed8dbd | 2014-07-31 16:37:04 +0000 | [diff] [blame] | 5604 | if (Attr.getMinArgs() == Attr.getMaxArgs()) { |
| 5605 | // If there are no optional arguments, then checking for the argument count |
| 5606 | // is trivial. |
| 5607 | if (!checkAttributeNumArgs(S, Attr, Attr.getMinArgs())) |
| 5608 | return true; |
| 5609 | } else { |
| 5610 | // There are optional arguments, so checking is slightly more involved. |
| 5611 | if (Attr.getMinArgs() && |
| 5612 | !checkAttributeAtLeastNumArgs(S, Attr, Attr.getMinArgs())) |
| 5613 | return true; |
| 5614 | else if (!Attr.hasVariadicArg() && Attr.getMaxArgs() && |
| 5615 | !checkAttributeAtMostNumArgs(S, Attr, Attr.getMaxArgs())) |
| 5616 | return true; |
| 5617 | } |
Aaron Ballman | 74eeeae | 2013-11-27 13:27:02 +0000 | [diff] [blame] | 5618 | |
| 5619 | // Check whether the attribute appertains to the given subject. |
| 5620 | if (!Attr.diagnoseAppertainsTo(S, D)) |
| 5621 | return true; |
| 5622 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5623 | return false; |
| 5624 | } |
| 5625 | |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 5626 | static void handleOpenCLAccessAttr(Sema &S, Decl *D, |
| 5627 | const AttributeList &Attr) { |
| 5628 | if (D->isInvalidDecl()) |
| 5629 | return; |
| 5630 | |
| 5631 | // Check if there is only one access qualifier. |
| 5632 | if (D->hasAttr<OpenCLAccessAttr>()) { |
| 5633 | S.Diag(Attr.getLoc(), diag::err_opencl_multiple_access_qualifiers) |
| 5634 | << D->getSourceRange(); |
| 5635 | D->setInvalidDecl(true); |
| 5636 | return; |
| 5637 | } |
| 5638 | |
| 5639 | // OpenCL v2.0 s6.6 - read_write can be used for image types to specify that an |
| 5640 | // image object can be read and written. |
| 5641 | // OpenCL v2.0 s6.13.6 - A kernel cannot read from and write to the same pipe |
| 5642 | // object. Using the read_write (or __read_write) qualifier with the pipe |
| 5643 | // qualifier is a compilation error. |
| 5644 | if (const ParmVarDecl *PDecl = dyn_cast<ParmVarDecl>(D)) { |
| 5645 | const Type *DeclTy = PDecl->getType().getCanonicalType().getTypePtr(); |
| 5646 | if (Attr.getName()->getName().find("read_write") != StringRef::npos) { |
| 5647 | if (S.getLangOpts().OpenCLVersion < 200 || DeclTy->isPipeType()) { |
| 5648 | S.Diag(Attr.getLoc(), diag::err_opencl_invalid_read_write) |
| 5649 | << Attr.getName() << PDecl->getType() << DeclTy->isImageType(); |
| 5650 | D->setInvalidDecl(true); |
| 5651 | return; |
| 5652 | } |
| 5653 | } |
| 5654 | } |
| 5655 | |
| 5656 | D->addAttr(::new (S.Context) OpenCLAccessAttr( |
| 5657 | Attr.getRange(), S.Context, Attr.getAttributeSpellingListIndex())); |
| 5658 | } |
| 5659 | |
Ted Kremenek | 9ecdfaf | 2009-05-09 02:44:38 +0000 | [diff] [blame] | 5660 | //===----------------------------------------------------------------------===// |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 5661 | // Top Level Sema Entry Points |
| 5662 | //===----------------------------------------------------------------------===// |
| 5663 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5664 | /// ProcessDeclAttribute - Apply the specific attribute to the specified decl if |
| 5665 | /// the attribute applies to decls. If the attribute is a type attribute, just |
| 5666 | /// silently ignore it if a GNU attribute. |
| 5667 | static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, |
| 5668 | const AttributeList &Attr, |
| 5669 | bool IncludeCXX11Attributes) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5670 | if (Attr.isInvalid() || Attr.getKind() == AttributeList::IgnoredAttribute) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5671 | return; |
Abramo Bagnara | 5009937 | 2010-04-30 13:10:51 +0000 | [diff] [blame] | 5672 | |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 5673 | // Ignore C++11 attributes on declarator chunks: they appertain to the type |
| 5674 | // instead. |
| 5675 | if (Attr.isCXX11Attribute() && !IncludeCXX11Attributes) |
| 5676 | return; |
| 5677 | |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5678 | // Unknown attributes are automatically warned on. Target-specific attributes |
| 5679 | // which do not apply to the current target architecture are treated as |
| 5680 | // though they were unknown attributes. |
| 5681 | if (Attr.getKind() == AttributeList::UnknownAttribute || |
Bob Wilson | 7c73083 | 2015-07-20 22:57:31 +0000 | [diff] [blame] | 5682 | !Attr.existsInTarget(S.Context.getTargetInfo())) { |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5683 | S.Diag(Attr.getLoc(), Attr.isDeclspecAttribute() |
| 5684 | ? diag::warn_unhandled_ms_attribute_ignored |
| 5685 | : diag::warn_unknown_attribute_ignored) |
| 5686 | << Attr.getName(); |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5687 | return; |
| 5688 | } |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5689 | |
Aaron Ballman | 8ee40b7 | 2013-09-09 23:33:17 +0000 | [diff] [blame] | 5690 | if (handleCommonAttributeFeatures(S, scope, D, Attr)) |
| 5691 | return; |
| 5692 | |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5693 | switch (Attr.getKind()) { |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5694 | default: |
Richard Smith | 4f902c7 | 2016-03-08 00:32:55 +0000 | [diff] [blame] | 5695 | if (!Attr.isStmtAttr()) { |
| 5696 | // Type attributes are handled elsewhere; silently move on. |
| 5697 | assert(Attr.isTypeAttr() && "Non-type attribute not handled"); |
| 5698 | break; |
| 5699 | } |
| 5700 | S.Diag(Attr.getLoc(), diag::err_stmt_attribute_invalid_on_decl) |
| 5701 | << Attr.getName() << D->getLocation(); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5702 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5703 | case AttributeList::AT_Interrupt: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5704 | handleInterruptAttr(S, D, Attr); |
| 5705 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5706 | case AttributeList::AT_X86ForceAlignArgPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5707 | handleX86ForceAlignArgPointerAttr(S, D, Attr); |
| 5708 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5709 | case AttributeList::AT_DLLExport: |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5710 | case AttributeList::AT_DLLImport: |
Hans Wennborg | e82f19c | 2014-06-24 23:57:05 +0000 | [diff] [blame] | 5711 | handleDLLAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5712 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5713 | case AttributeList::AT_Mips16: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5714 | handleSimpleAttributeWithExclusions<Mips16Attr, MipsInterruptAttr>(S, D, |
| 5715 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5716 | break; |
Aaron Ballman | ab7691c | 2014-01-09 22:48:32 +0000 | [diff] [blame] | 5717 | case AttributeList::AT_NoMips16: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5718 | handleSimpleAttribute<NoMips16Attr>(S, D, Attr); |
| 5719 | break; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5720 | case AttributeList::AT_AMDGPUFlatWorkGroupSize: |
| 5721 | handleAMDGPUFlatWorkGroupSizeAttr(S, D, Attr); |
| 5722 | break; |
| 5723 | case AttributeList::AT_AMDGPUWavesPerEU: |
| 5724 | handleAMDGPUWavesPerEUAttr(S, D, Attr); |
Matt Arsenault | 43fae6c | 2014-12-04 20:38:18 +0000 | [diff] [blame] | 5725 | break; |
| 5726 | case AttributeList::AT_AMDGPUNumSGPR: |
| 5727 | handleAMDGPUNumSGPRAttr(S, D, Attr); |
| 5728 | break; |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 5729 | case AttributeList::AT_AMDGPUNumVGPR: |
| 5730 | handleAMDGPUNumVGPRAttr(S, D, Attr); |
| 5731 | break; |
Dylan McKay | e8232d7 | 2017-02-08 05:09:26 +0000 | [diff] [blame] | 5732 | case AttributeList::AT_AVRSignal: |
| 5733 | handleAVRSignalAttr(S, D, Attr); |
| 5734 | break; |
Aaron Ballman | 9beb517 | 2013-12-02 15:13:14 +0000 | [diff] [blame] | 5735 | case AttributeList::AT_IBAction: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5736 | handleSimpleAttribute<IBActionAttr>(S, D, Attr); |
| 5737 | break; |
| 5738 | case AttributeList::AT_IBOutlet: |
| 5739 | handleIBOutlet(S, D, Attr); |
| 5740 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5741 | case AttributeList::AT_IBOutletCollection: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5742 | handleIBOutletCollection(S, D, Attr); |
| 5743 | break; |
Dmitry Polukhin | 85eda12 | 2016-04-11 07:48:59 +0000 | [diff] [blame] | 5744 | case AttributeList::AT_IFunc: |
| 5745 | handleIFuncAttr(S, D, Attr); |
| 5746 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5747 | case AttributeList::AT_Alias: |
| 5748 | handleAliasAttr(S, D, Attr); |
| 5749 | break; |
| 5750 | case AttributeList::AT_Aligned: |
| 5751 | handleAlignedAttr(S, D, Attr); |
| 5752 | break; |
Hal Finkel | 1b0d24e | 2014-10-02 21:21:25 +0000 | [diff] [blame] | 5753 | case AttributeList::AT_AlignValue: |
| 5754 | handleAlignValueAttr(S, D, Attr); |
| 5755 | break; |
George Burgess IV | e376337 | 2016-12-22 02:50:20 +0000 | [diff] [blame] | 5756 | case AttributeList::AT_AllocSize: |
| 5757 | handleAllocSizeAttr(S, D, Attr); |
| 5758 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5759 | case AttributeList::AT_AlwaysInline: |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 5760 | handleAlwaysInlineAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5761 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5762 | case AttributeList::AT_AnalyzerNoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5763 | handleAnalyzerNoReturnAttr(S, D, Attr); |
| 5764 | break; |
| 5765 | case AttributeList::AT_TLSModel: |
| 5766 | handleTLSModelAttr(S, D, Attr); |
| 5767 | break; |
| 5768 | case AttributeList::AT_Annotate: |
| 5769 | handleAnnotateAttr(S, D, Attr); |
| 5770 | break; |
| 5771 | case AttributeList::AT_Availability: |
| 5772 | handleAvailabilityAttr(S, D, Attr); |
| 5773 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5774 | case AttributeList::AT_CarriesDependency: |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5775 | handleDependencyAttr(S, scope, D, Attr); |
| 5776 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5777 | case AttributeList::AT_Common: |
| 5778 | handleCommonAttr(S, D, Attr); |
| 5779 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5780 | case AttributeList::AT_CUDAConstant: |
Justin Lebar | e71b2fa | 2016-09-30 23:57:34 +0000 | [diff] [blame] | 5781 | handleConstantAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5782 | break; |
George Burgess IV | 3e3bb95b | 2015-12-02 21:58:08 +0000 | [diff] [blame] | 5783 | case AttributeList::AT_PassObjectSize: |
| 5784 | handlePassObjectSizeAttr(S, D, Attr); |
| 5785 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5786 | case AttributeList::AT_Constructor: |
| 5787 | handleConstructorAttr(S, D, Attr); |
| 5788 | break; |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 5789 | case AttributeList::AT_CXX11NoReturn: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5790 | handleSimpleAttribute<CXX11NoReturnAttr>(S, D, Attr); |
| 5791 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5792 | case AttributeList::AT_Deprecated: |
Aaron Ballman | 43f4010 | 2014-11-14 22:34:56 +0000 | [diff] [blame] | 5793 | handleDeprecatedAttr(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 5794 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5795 | case AttributeList::AT_Destructor: |
| 5796 | handleDestructorAttr(S, D, Attr); |
| 5797 | break; |
| 5798 | case AttributeList::AT_EnableIf: |
| 5799 | handleEnableIfAttr(S, D, Attr); |
| 5800 | break; |
George Burgess IV | 177399e | 2017-01-09 04:12:14 +0000 | [diff] [blame] | 5801 | case AttributeList::AT_DiagnoseIf: |
| 5802 | handleDiagnoseIfAttr(S, D, Attr); |
| 5803 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5804 | case AttributeList::AT_ExtVectorType: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5805 | handleExtVectorTypeAttr(S, scope, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 5806 | break; |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 5807 | case AttributeList::AT_MinSize: |
Paul Robinson | aae2fba | 2014-12-10 23:34:36 +0000 | [diff] [blame] | 5808 | handleMinSizeAttr(S, D, Attr); |
Quentin Colombet | 4e17206 | 2012-11-01 23:55:47 +0000 | [diff] [blame] | 5809 | break; |
Paul Robinson | f067435 | 2014-03-31 22:29:15 +0000 | [diff] [blame] | 5810 | case AttributeList::AT_OptimizeNone: |
| 5811 | handleOptimizeNoneAttr(S, D, Attr); |
| 5812 | break; |
Alexis Hunt | 724f14e | 2014-11-28 00:53:20 +0000 | [diff] [blame] | 5813 | case AttributeList::AT_FlagEnum: |
| 5814 | handleSimpleAttribute<FlagEnumAttr>(S, D, Attr); |
| 5815 | break; |
Peter Collingbourne | 41af7c2 | 2014-05-20 17:12:51 +0000 | [diff] [blame] | 5816 | case AttributeList::AT_Flatten: |
| 5817 | handleSimpleAttribute<FlattenAttr>(S, D, Attr); |
| 5818 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5819 | case AttributeList::AT_Format: |
| 5820 | handleFormatAttr(S, D, Attr); |
| 5821 | break; |
| 5822 | case AttributeList::AT_FormatArg: |
| 5823 | handleFormatArgAttr(S, D, Attr); |
| 5824 | break; |
| 5825 | case AttributeList::AT_CUDAGlobal: |
| 5826 | handleGlobalAttr(S, D, Attr); |
| 5827 | break; |
Aaron Ballman | f79ee27 | 2013-12-02 21:09:08 +0000 | [diff] [blame] | 5828 | case AttributeList::AT_CUDADevice: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5829 | handleSimpleAttributeWithExclusions<CUDADeviceAttr, CUDAGlobalAttr>(S, D, |
| 5830 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5831 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5832 | case AttributeList::AT_CUDAHost: |
Justin Lebar | 3eaaf86 | 2016-01-13 01:07:35 +0000 | [diff] [blame] | 5833 | handleSimpleAttributeWithExclusions<CUDAHostAttr, CUDAGlobalAttr>(S, D, |
| 5834 | Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5835 | break; |
| 5836 | case AttributeList::AT_GNUInline: |
| 5837 | handleGNUInlineAttr(S, D, Attr); |
| 5838 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5839 | case AttributeList::AT_CUDALaunchBounds: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 5840 | handleLaunchBoundsAttr(S, D, Attr); |
Peter Collingbourne | 827301e | 2010-12-12 23:03:07 +0000 | [diff] [blame] | 5841 | break; |
David Majnemer | 631a90b | 2015-02-04 07:23:21 +0000 | [diff] [blame] | 5842 | case AttributeList::AT_Restrict: |
| 5843 | handleRestrictAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5844 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5845 | case AttributeList::AT_MayAlias: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5846 | handleSimpleAttribute<MayAliasAttr>(S, D, Attr); |
| 5847 | break; |
| 5848 | case AttributeList::AT_Mode: |
| 5849 | handleModeAttr(S, D, Attr); |
| 5850 | break; |
David Majnemer | 1bf0f8e | 2015-07-20 22:51:52 +0000 | [diff] [blame] | 5851 | case AttributeList::AT_NoAlias: |
| 5852 | handleSimpleAttribute<NoAliasAttr>(S, D, Attr); |
| 5853 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5854 | case AttributeList::AT_NoCommon: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5855 | handleSimpleAttribute<NoCommonAttr>(S, D, Attr); |
| 5856 | break; |
Peter Collingbourne | b4728c1 | 2014-05-19 22:14:34 +0000 | [diff] [blame] | 5857 | case AttributeList::AT_NoSplitStack: |
| 5858 | handleSimpleAttribute<NoSplitStackAttr>(S, D, Attr); |
| 5859 | break; |
Ted Kremenek | 9aedc15 | 2014-01-17 06:24:56 +0000 | [diff] [blame] | 5860 | case AttributeList::AT_NonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5861 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(D)) |
| 5862 | handleNonNullAttrParameter(S, PVD, Attr); |
| 5863 | else |
| 5864 | handleNonNullAttr(S, D, Attr); |
| 5865 | break; |
Aaron Ballman | fc1951c | 2014-01-20 14:19:44 +0000 | [diff] [blame] | 5866 | case AttributeList::AT_ReturnsNonNull: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5867 | handleReturnsNonNullAttr(S, D, Attr); |
| 5868 | break; |
Hal Finkel | ee90a22 | 2014-09-26 05:04:30 +0000 | [diff] [blame] | 5869 | case AttributeList::AT_AssumeAligned: |
| 5870 | handleAssumeAlignedAttr(S, D, Attr); |
| 5871 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5872 | case AttributeList::AT_Overloadable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5873 | handleSimpleAttribute<OverloadableAttr>(S, D, Attr); |
| 5874 | break; |
| 5875 | case AttributeList::AT_Ownership: |
| 5876 | handleOwnershipAttr(S, D, Attr); |
| 5877 | break; |
| 5878 | case AttributeList::AT_Cold: |
| 5879 | handleColdAttr(S, D, Attr); |
| 5880 | break; |
| 5881 | case AttributeList::AT_Hot: |
| 5882 | handleHotAttr(S, D, Attr); |
| 5883 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 5884 | case AttributeList::AT_Naked: |
Charles Davis | 0e37911 | 2016-08-08 21:19:08 +0000 | [diff] [blame] | 5885 | handleNakedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5886 | break; |
| 5887 | case AttributeList::AT_NoReturn: |
| 5888 | handleNoReturnAttr(S, D, Attr); |
| 5889 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 5890 | case AttributeList::AT_NoThrow: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5891 | handleSimpleAttribute<NoThrowAttr>(S, D, Attr); |
| 5892 | break; |
Aaron Ballman | 3aff633 | 2013-12-02 19:30:36 +0000 | [diff] [blame] | 5893 | case AttributeList::AT_CUDAShared: |
Justin Lebar | 1041101 | 2016-09-30 23:57:30 +0000 | [diff] [blame] | 5894 | handleSharedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5895 | break; |
| 5896 | case AttributeList::AT_VecReturn: |
| 5897 | handleVecReturnAttr(S, D, Attr); |
| 5898 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5899 | case AttributeList::AT_ObjCOwnership: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5900 | handleObjCOwnershipAttr(S, D, Attr); |
| 5901 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5902 | case AttributeList::AT_ObjCPreciseLifetime: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5903 | handleObjCPreciseLifetimeAttr(S, D, Attr); |
| 5904 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5905 | case AttributeList::AT_ObjCReturnsInnerPointer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5906 | handleObjCReturnsInnerPointerAttr(S, D, Attr); |
| 5907 | break; |
Fariborz Jahanian | 566fff0 | 2012-09-07 23:46:23 +0000 | [diff] [blame] | 5908 | case AttributeList::AT_ObjCRequiresSuper: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5909 | handleObjCRequiresSuperAttr(S, D, Attr); |
| 5910 | break; |
Fariborz Jahanian | 0a0a397 | 2013-11-13 23:59:17 +0000 | [diff] [blame] | 5911 | case AttributeList::AT_ObjCBridge: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5912 | handleObjCBridgeAttr(S, scope, D, Attr); |
| 5913 | break; |
Fariborz Jahanian | 87c7791 | 2013-11-21 20:50:32 +0000 | [diff] [blame] | 5914 | case AttributeList::AT_ObjCBridgeMutable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5915 | handleObjCBridgeMutableAttr(S, scope, D, Attr); |
| 5916 | break; |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 5917 | case AttributeList::AT_ObjCBridgeRelated: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5918 | handleObjCBridgeRelatedAttr(S, scope, D, Attr); |
| 5919 | break; |
Argyrios Kyrtzidis | d1438b4 | 2013-12-03 21:11:25 +0000 | [diff] [blame] | 5920 | case AttributeList::AT_ObjCDesignatedInitializer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5921 | handleObjCDesignatedInitializer(S, D, Attr); |
| 5922 | break; |
Fariborz Jahanian | 451b92a | 2014-07-16 16:16:04 +0000 | [diff] [blame] | 5923 | case AttributeList::AT_ObjCRuntimeName: |
| 5924 | handleObjCRuntimeName(S, D, Attr); |
| 5925 | break; |
Douglas Gregor | 24ae22c | 2016-04-01 23:23:52 +0000 | [diff] [blame] | 5926 | case AttributeList::AT_ObjCRuntimeVisible: |
| 5927 | handleSimpleAttribute<ObjCRuntimeVisibleAttr>(S, D, Attr); |
| 5928 | break; |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 5929 | case AttributeList::AT_ObjCBoxable: |
| 5930 | handleObjCBoxable(S, D, Attr); |
| 5931 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5932 | case AttributeList::AT_CFAuditedTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5933 | handleCFAuditedTransferAttr(S, D, Attr); |
| 5934 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5935 | case AttributeList::AT_CFUnknownTransfer: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5936 | handleCFUnknownTransferAttr(S, D, Attr); |
| 5937 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5938 | case AttributeList::AT_CFConsumed: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5939 | case AttributeList::AT_NSConsumed: |
| 5940 | handleNSConsumedAttr(S, D, Attr); |
| 5941 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5942 | case AttributeList::AT_NSConsumesSelf: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5943 | handleSimpleAttribute<NSConsumesSelfAttr>(S, D, Attr); |
| 5944 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5945 | case AttributeList::AT_NSReturnsAutoreleased: |
| 5946 | case AttributeList::AT_NSReturnsNotRetained: |
| 5947 | case AttributeList::AT_CFReturnsNotRetained: |
| 5948 | case AttributeList::AT_NSReturnsRetained: |
| 5949 | case AttributeList::AT_CFReturnsRetained: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5950 | handleNSReturnsRetainedAttr(S, D, Attr); |
| 5951 | break; |
Tanya Lattner | bcffcdf | 2012-07-09 22:06:01 +0000 | [diff] [blame] | 5952 | case AttributeList::AT_WorkGroupSizeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5953 | handleWorkGroupSize<WorkGroupSizeHintAttr>(S, D, Attr); |
| 5954 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5955 | case AttributeList::AT_ReqdWorkGroupSize: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5956 | handleWorkGroupSize<ReqdWorkGroupSizeAttr>(S, D, Attr); |
| 5957 | break; |
Joey Gouly | aba589c | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 5958 | case AttributeList::AT_VecTypeHint: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5959 | handleVecTypeHint(S, D, Attr); |
| 5960 | break; |
Eric Fiselier | 341e825 | 2016-09-02 18:53:31 +0000 | [diff] [blame] | 5961 | case AttributeList::AT_RequireConstantInit: |
| 5962 | handleSimpleAttribute<RequireConstantInitAttr>(S, D, Attr); |
| 5963 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5964 | case AttributeList::AT_InitPriority: |
| 5965 | handleInitPriorityAttr(S, D, Attr); |
| 5966 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5967 | case AttributeList::AT_Packed: |
| 5968 | handlePackedAttr(S, D, Attr); |
| 5969 | break; |
| 5970 | case AttributeList::AT_Section: |
| 5971 | handleSectionAttr(S, D, Attr); |
| 5972 | break; |
Eric Christopher | 11acf73 | 2015-06-12 01:35:52 +0000 | [diff] [blame] | 5973 | case AttributeList::AT_Target: |
| 5974 | handleTargetAttr(S, D, Attr); |
| 5975 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5976 | case AttributeList::AT_Unavailable: |
Aaron Ballman | 8b8ebdd | 2013-07-18 13:13:52 +0000 | [diff] [blame] | 5977 | handleAttrWithMessage<UnavailableAttr>(S, D, Attr); |
Benjamin Kramer | f435ab4 | 2012-05-16 12:19:08 +0000 | [diff] [blame] | 5978 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5979 | case AttributeList::AT_ArcWeakrefUnavailable: |
| 5980 | handleSimpleAttribute<ArcWeakrefUnavailableAttr>(S, D, Attr); |
| 5981 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5982 | case AttributeList::AT_ObjCRootClass: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5983 | handleSimpleAttribute<ObjCRootClassAttr>(S, D, Attr); |
| 5984 | break; |
Alex Lorenz | a8c44ba | 2016-10-28 10:25:10 +0000 | [diff] [blame] | 5985 | case AttributeList::AT_ObjCSubclassingRestricted: |
| 5986 | handleSimpleAttribute<ObjCSubclassingRestrictedAttr>(S, D, Attr); |
| 5987 | break; |
Ted Kremenek | f41cf7f1 | 2013-12-10 19:43:48 +0000 | [diff] [blame] | 5988 | case AttributeList::AT_ObjCExplicitProtocolImpl: |
Ted Kremenek | 438f8db | 2014-02-22 01:06:05 +0000 | [diff] [blame] | 5989 | handleObjCSuppresProtocolAttr(S, D, Attr); |
Ted Kremenek | 28eace6 | 2013-11-23 01:01:34 +0000 | [diff] [blame] | 5990 | break; |
| 5991 | case AttributeList::AT_ObjCRequiresPropertyDefs: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5992 | handleSimpleAttribute<ObjCRequiresPropertyDefsAttr>(S, D, Attr); |
| 5993 | break; |
Aaron Ballman | 12b9f65 | 2014-01-16 13:55:42 +0000 | [diff] [blame] | 5994 | case AttributeList::AT_Unused: |
Aaron Ballman | 0bcd6c1 | 2016-03-09 16:48:08 +0000 | [diff] [blame] | 5995 | handleUnusedAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5996 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 5997 | case AttributeList::AT_ReturnsTwice: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 5998 | handleSimpleAttribute<ReturnsTwiceAttr>(S, D, Attr); |
| 5999 | break; |
Akira Hatanaka | c866762 | 2015-11-06 23:56:15 +0000 | [diff] [blame] | 6000 | case AttributeList::AT_NotTailCalled: |
| 6001 | handleNotTailCalledAttr(S, D, Attr); |
| 6002 | break; |
Akira Hatanaka | 7828b1e | 2015-11-13 00:42:21 +0000 | [diff] [blame] | 6003 | case AttributeList::AT_DisableTailCalls: |
| 6004 | handleDisableTailCallsAttr(S, D, Attr); |
| 6005 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6006 | case AttributeList::AT_Used: |
| 6007 | handleUsedAttr(S, D, Attr); |
| 6008 | break; |
John McCall | d041a9b | 2013-02-20 01:54:26 +0000 | [diff] [blame] | 6009 | case AttributeList::AT_Visibility: |
| 6010 | handleVisibilityAttr(S, D, Attr, false); |
| 6011 | break; |
| 6012 | case AttributeList::AT_TypeVisibility: |
| 6013 | handleVisibilityAttr(S, D, Attr, true); |
| 6014 | break; |
Lubos Lunak | edc1388 | 2013-07-20 15:05:36 +0000 | [diff] [blame] | 6015 | case AttributeList::AT_WarnUnused: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6016 | handleSimpleAttribute<WarnUnusedAttr>(S, D, Attr); |
| 6017 | break; |
| 6018 | case AttributeList::AT_WarnUnusedResult: |
| 6019 | handleWarnUnusedResult(S, D, Attr); |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 6020 | break; |
Aaron Ballman | 604dfec | 2013-12-02 17:07:07 +0000 | [diff] [blame] | 6021 | case AttributeList::AT_Weak: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6022 | handleSimpleAttribute<WeakAttr>(S, D, Attr); |
| 6023 | break; |
| 6024 | case AttributeList::AT_WeakRef: |
| 6025 | handleWeakRefAttr(S, D, Attr); |
| 6026 | break; |
| 6027 | case AttributeList::AT_WeakImport: |
| 6028 | handleWeakImportAttr(S, D, Attr); |
| 6029 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6030 | case AttributeList::AT_TransparentUnion: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6031 | handleTransparentUnionAttr(S, D, Attr); |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6032 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6033 | case AttributeList::AT_ObjCException: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6034 | handleSimpleAttribute<ObjCExceptionAttr>(S, D, Attr); |
| 6035 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6036 | case AttributeList::AT_ObjCMethodFamily: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6037 | handleObjCMethodFamilyAttr(S, D, Attr); |
John McCall | 86bc21f | 2011-03-02 11:33:24 +0000 | [diff] [blame] | 6038 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6039 | case AttributeList::AT_ObjCNSObject: |
| 6040 | handleObjCNSObject(S, D, Attr); |
| 6041 | break; |
Fariborz Jahanian | 7a60b6d | 2015-04-16 18:38:44 +0000 | [diff] [blame] | 6042 | case AttributeList::AT_ObjCIndependentClass: |
| 6043 | handleObjCIndependentClass(S, D, Attr); |
| 6044 | break; |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6045 | case AttributeList::AT_Blocks: |
| 6046 | handleBlocksAttr(S, D, Attr); |
| 6047 | break; |
| 6048 | case AttributeList::AT_Sentinel: |
| 6049 | handleSentinelAttr(S, D, Attr); |
| 6050 | break; |
Aaron Ballman | bf7b1ee | 2013-12-21 16:49:29 +0000 | [diff] [blame] | 6051 | case AttributeList::AT_Const: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6052 | handleSimpleAttribute<ConstAttr>(S, D, Attr); |
| 6053 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6054 | case AttributeList::AT_Pure: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6055 | handleSimpleAttribute<PureAttr>(S, D, Attr); |
| 6056 | break; |
| 6057 | case AttributeList::AT_Cleanup: |
| 6058 | handleCleanupAttr(S, D, Attr); |
| 6059 | break; |
| 6060 | case AttributeList::AT_NoDebug: |
| 6061 | handleNoDebugAttr(S, D, Attr); |
| 6062 | break; |
Aaron Ballman | 7c19ab1 | 2014-02-22 16:59:24 +0000 | [diff] [blame] | 6063 | case AttributeList::AT_NoDuplicate: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6064 | handleSimpleAttribute<NoDuplicateAttr>(S, D, Attr); |
| 6065 | break; |
Yaxun Liu | 7d07ae7 | 2016-11-01 18:45:32 +0000 | [diff] [blame] | 6066 | case AttributeList::AT_Convergent: |
| 6067 | handleSimpleAttribute<ConvergentAttr>(S, D, Attr); |
| 6068 | break; |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6069 | case AttributeList::AT_NoInline: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6070 | handleSimpleAttribute<NoInlineAttr>(S, D, Attr); |
| 6071 | break; |
| 6072 | case AttributeList::AT_NoInstrumentFunction: // Interacts with -pg. |
| 6073 | handleSimpleAttribute<NoInstrumentFunctionAttr>(S, D, Attr); |
| 6074 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6075 | case AttributeList::AT_StdCall: |
| 6076 | case AttributeList::AT_CDecl: |
| 6077 | case AttributeList::AT_FastCall: |
| 6078 | case AttributeList::AT_ThisCall: |
| 6079 | case AttributeList::AT_Pascal: |
Erich Keane | 757d317 | 2016-11-02 18:29:35 +0000 | [diff] [blame] | 6080 | case AttributeList::AT_RegCall: |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6081 | case AttributeList::AT_SwiftCall: |
Reid Kleckner | d7857f0 | 2014-10-24 17:42:17 +0000 | [diff] [blame] | 6082 | case AttributeList::AT_VectorCall: |
Charles Davis | b5a214e | 2013-08-30 04:39:01 +0000 | [diff] [blame] | 6083 | case AttributeList::AT_MSABI: |
| 6084 | case AttributeList::AT_SysVABI: |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6085 | case AttributeList::AT_Pcs: |
Guy Benyei | f0a014b | 2012-12-25 08:53:55 +0000 | [diff] [blame] | 6086 | case AttributeList::AT_IntelOclBicc: |
Roman Levenstein | 35aa5ce | 2016-03-16 18:00:46 +0000 | [diff] [blame] | 6087 | case AttributeList::AT_PreserveMost: |
| 6088 | case AttributeList::AT_PreserveAll: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6089 | handleCallConvAttr(S, D, Attr); |
John McCall | ab26cfa | 2010-02-05 21:31:56 +0000 | [diff] [blame] | 6090 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6091 | case AttributeList::AT_OpenCLKernel: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6092 | handleSimpleAttribute<OpenCLKernelAttr>(S, D, Attr); |
| 6093 | break; |
Xiuli Pan | 11e13f6 | 2016-02-26 03:13:03 +0000 | [diff] [blame] | 6094 | case AttributeList::AT_OpenCLAccess: |
| 6095 | handleOpenCLAccessAttr(S, D, Attr); |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6096 | break; |
Anastasia Stulova | fde7622 | 2016-04-01 16:05:09 +0000 | [diff] [blame] | 6097 | case AttributeList::AT_OpenCLNoSVM: |
| 6098 | handleOpenCLNoSVMAttr(S, D, Attr); |
| 6099 | break; |
John McCall | 477f2bb | 2016-03-03 06:39:32 +0000 | [diff] [blame] | 6100 | case AttributeList::AT_SwiftContext: |
| 6101 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftContext); |
| 6102 | break; |
| 6103 | case AttributeList::AT_SwiftErrorResult: |
| 6104 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftErrorResult); |
| 6105 | break; |
| 6106 | case AttributeList::AT_SwiftIndirectResult: |
| 6107 | handleParameterABIAttr(S, D, Attr, ParameterABI::SwiftIndirectResult); |
| 6108 | break; |
Evgeniy Stepanov | ae6ebd3 | 2015-11-10 21:28:44 +0000 | [diff] [blame] | 6109 | case AttributeList::AT_InternalLinkage: |
| 6110 | handleInternalLinkageAttr(S, D, Attr); |
| 6111 | break; |
Peter Collingbourne | 3afb266 | 2016-04-28 17:09:37 +0000 | [diff] [blame] | 6112 | case AttributeList::AT_LTOVisibilityPublic: |
| 6113 | handleSimpleAttribute<LTOVisibilityPublicAttr>(S, D, Attr); |
| 6114 | break; |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 6115 | |
| 6116 | // Microsoft attributes: |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6117 | case AttributeList::AT_EmptyBases: |
| 6118 | handleSimpleAttribute<EmptyBasesAttr>(S, D, Attr); |
| 6119 | break; |
| 6120 | case AttributeList::AT_LayoutVersion: |
| 6121 | handleLayoutVersion(S, D, Attr); |
| 6122 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 6123 | case AttributeList::AT_MSNoVTable: |
| 6124 | handleSimpleAttribute<MSNoVTableAttr>(S, D, Attr); |
David Majnemer | 129f417 | 2015-02-02 10:22:20 +0000 | [diff] [blame] | 6125 | break; |
David Majnemer | 8ab003a | 2015-02-02 19:30:52 +0000 | [diff] [blame] | 6126 | case AttributeList::AT_MSStruct: |
| 6127 | handleSimpleAttribute<MSStructAttr>(S, D, Attr); |
John McCall | 8d32c05 | 2012-05-22 21:28:12 +0000 | [diff] [blame] | 6128 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6129 | case AttributeList::AT_Uuid: |
Chandler Carruth | edc2c64 | 2011-07-02 00:01:44 +0000 | [diff] [blame] | 6130 | handleUuidAttr(S, D, Attr); |
Francois Pichet | a83957a | 2010-12-19 06:50:37 +0000 | [diff] [blame] | 6131 | break; |
Aaron Ballman | 8edb5c2 | 2013-12-18 23:44:18 +0000 | [diff] [blame] | 6132 | case AttributeList::AT_MSInheritance: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6133 | handleMSInheritanceAttr(S, D, Attr); |
| 6134 | break; |
Reid Kleckner | b144d36 | 2013-05-20 14:02:37 +0000 | [diff] [blame] | 6135 | case AttributeList::AT_SelectAny: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6136 | handleSimpleAttribute<SelectAnyAttr>(S, D, Attr); |
| 6137 | break; |
Reid Kleckner | 7d6d270 | 2014-05-01 03:16:47 +0000 | [diff] [blame] | 6138 | case AttributeList::AT_Thread: |
| 6139 | handleDeclspecThreadAttr(S, D, Attr); |
| 6140 | break; |
David Majnemer | cd3ebfe | 2016-05-23 17:16:12 +0000 | [diff] [blame] | 6141 | |
Dmitry Polukhin | bf17ecf | 2016-03-09 15:30:53 +0000 | [diff] [blame] | 6142 | case AttributeList::AT_AbiTag: |
| 6143 | handleAbiTagAttr(S, D, Attr); |
| 6144 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6145 | |
| 6146 | // Thread safety attributes: |
DeLesley Hutchins | b682431 | 2013-05-17 23:02:59 +0000 | [diff] [blame] | 6147 | case AttributeList::AT_AssertExclusiveLock: |
| 6148 | handleAssertExclusiveLockAttr(S, D, Attr); |
| 6149 | break; |
| 6150 | case AttributeList::AT_AssertSharedLock: |
| 6151 | handleAssertSharedLockAttr(S, D, Attr); |
| 6152 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6153 | case AttributeList::AT_GuardedVar: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6154 | handleSimpleAttribute<GuardedVarAttr>(S, D, Attr); |
| 6155 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6156 | case AttributeList::AT_PtGuardedVar: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6157 | handlePtGuardedVarAttr(S, D, Attr); |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6158 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6159 | case AttributeList::AT_ScopedLockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6160 | handleSimpleAttribute<ScopedLockableAttr>(S, D, Attr); |
| 6161 | break; |
Peter Collingbourne | 915df99 | 2015-05-15 18:33:32 +0000 | [diff] [blame] | 6162 | case AttributeList::AT_NoSanitize: |
| 6163 | handleNoSanitizeAttr(S, D, Attr); |
| 6164 | break; |
| 6165 | case AttributeList::AT_NoSanitizeSpecific: |
| 6166 | handleNoSanitizeSpecificAttr(S, D, Attr); |
Kostya Serebryany | 588d6ab | 2012-01-24 19:25:38 +0000 | [diff] [blame] | 6167 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6168 | case AttributeList::AT_NoThreadSafetyAnalysis: |
Aaron Ballman | 6f9165a | 2013-11-27 15:24:06 +0000 | [diff] [blame] | 6169 | handleSimpleAttribute<NoThreadSafetyAnalysisAttr>(S, D, Attr); |
Kostya Serebryany | 4c0fc99 | 2013-02-26 06:58:27 +0000 | [diff] [blame] | 6170 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6171 | case AttributeList::AT_GuardedBy: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6172 | handleGuardedByAttr(S, D, Attr); |
| 6173 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6174 | case AttributeList::AT_PtGuardedBy: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6175 | handlePtGuardedByAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6176 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6177 | case AttributeList::AT_ExclusiveTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6178 | handleExclusiveTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6179 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6180 | case AttributeList::AT_LockReturned: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6181 | handleLockReturnedAttr(S, D, Attr); |
| 6182 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6183 | case AttributeList::AT_LocksExcluded: |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6184 | handleLocksExcludedAttr(S, D, Attr); |
| 6185 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6186 | case AttributeList::AT_SharedTrylockFunction: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6187 | handleSharedTrylockFunctionAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6188 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6189 | case AttributeList::AT_AcquiredBefore: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6190 | handleAcquiredBeforeAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6191 | break; |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6192 | case AttributeList::AT_AcquiredAfter: |
Michael Han | 3be3b44 | 2012-07-23 18:48:41 +0000 | [diff] [blame] | 6193 | handleAcquiredAfterAttr(S, D, Attr); |
Caitlin Sadowski | 63fa667 | 2011-07-28 20:12:35 +0000 | [diff] [blame] | 6194 | break; |
Caitlin Sadowski | aac4d21 | 2011-07-28 17:21:07 +0000 | [diff] [blame] | 6195 | |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6196 | // Capability analysis attributes. |
| 6197 | case AttributeList::AT_Capability: |
| 6198 | case AttributeList::AT_Lockable: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6199 | handleCapabilityAttr(S, D, Attr); |
| 6200 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6201 | case AttributeList::AT_RequiresCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6202 | handleRequiresCapabilityAttr(S, D, Attr); |
| 6203 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6204 | |
| 6205 | case AttributeList::AT_AssertCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6206 | handleAssertCapabilityAttr(S, D, Attr); |
| 6207 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6208 | case AttributeList::AT_AcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6209 | handleAcquireCapabilityAttr(S, D, Attr); |
| 6210 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6211 | case AttributeList::AT_ReleaseCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6212 | handleReleaseCapabilityAttr(S, D, Attr); |
| 6213 | break; |
Aaron Ballman | 9e9d184 | 2014-02-21 21:05:14 +0000 | [diff] [blame] | 6214 | case AttributeList::AT_TryAcquireCapability: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6215 | handleTryAcquireCapabilityAttr(S, D, Attr); |
| 6216 | break; |
Aaron Ballman | efe348e | 2014-02-18 17:36:50 +0000 | [diff] [blame] | 6217 | |
DeLesley Hutchins | 8d41d99 | 2013-10-11 22:30:48 +0000 | [diff] [blame] | 6218 | // Consumed analysis attributes. |
DeLesley Hutchins | 5a715c4 | 2013-08-30 22:56:34 +0000 | [diff] [blame] | 6219 | case AttributeList::AT_Consumable: |
| 6220 | handleConsumableAttr(S, D, Attr); |
| 6221 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 6222 | case AttributeList::AT_ConsumableAutoCast: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6223 | handleSimpleAttribute<ConsumableAutoCastAttr>(S, D, Attr); |
| 6224 | break; |
DeLesley Hutchins | f28bbec | 2014-01-14 00:36:53 +0000 | [diff] [blame] | 6225 | case AttributeList::AT_ConsumableSetOnRead: |
Aaron Ballman | 8abdd0e | 2014-03-06 14:02:27 +0000 | [diff] [blame] | 6226 | handleSimpleAttribute<ConsumableSetOnReadAttr>(S, D, Attr); |
| 6227 | break; |
DeLesley Hutchins | 210791a | 2013-10-04 21:28:06 +0000 | [diff] [blame] | 6228 | case AttributeList::AT_CallableWhen: |
| 6229 | handleCallableWhenAttr(S, D, Attr); |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6230 | break; |
DeLesley Hutchins | 6939177 | 2013-10-17 23:23:53 +0000 | [diff] [blame] | 6231 | case AttributeList::AT_ParamTypestate: |
| 6232 | handleParamTypestateAttr(S, D, Attr); |
| 6233 | break; |
DeLesley Hutchins | fc36825 | 2013-09-03 20:11:38 +0000 | [diff] [blame] | 6234 | case AttributeList::AT_ReturnTypestate: |
| 6235 | handleReturnTypestateAttr(S, D, Attr); |
| 6236 | break; |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6237 | case AttributeList::AT_SetTypestate: |
| 6238 | handleSetTypestateAttr(S, D, Attr); |
| 6239 | break; |
Chris Wailes | 9385f9f | 2013-10-29 20:28:41 +0000 | [diff] [blame] | 6240 | case AttributeList::AT_TestTypestate: |
| 6241 | handleTestTypestateAttr(S, D, Attr); |
DeLesley Hutchins | 33a2934 | 2013-10-11 23:03:26 +0000 | [diff] [blame] | 6242 | break; |
DeLesley Hutchins | 48a3176 | 2013-08-12 21:20:55 +0000 | [diff] [blame] | 6243 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 6244 | // Type safety attributes. |
| 6245 | case AttributeList::AT_ArgumentWithTypeTag: |
| 6246 | handleArgumentWithTypeTagAttr(S, D, Attr); |
| 6247 | break; |
| 6248 | case AttributeList::AT_TypeTagForDatatype: |
| 6249 | handleTypeTagForDatatypeAttr(S, D, Attr); |
| 6250 | break; |
Pirama Arumuga Nainar | e5d2d71 | 2016-06-10 21:51:18 +0000 | [diff] [blame] | 6251 | case AttributeList::AT_RenderScriptKernel: |
| 6252 | handleSimpleAttribute<RenderScriptKernelAttr>(S, D, Attr); |
Pirama Arumuga Nainar | 8b788d0 | 2016-06-09 23:34:20 +0000 | [diff] [blame] | 6253 | break; |
Aaron Ballman | 7d2aecb | 2016-07-13 22:32:15 +0000 | [diff] [blame] | 6254 | // XRay attributes. |
| 6255 | case AttributeList::AT_XRayInstrument: |
| 6256 | handleSimpleAttribute<XRayInstrumentAttr>(S, D, Attr); |
| 6257 | break; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6258 | } |
| 6259 | } |
| 6260 | |
| 6261 | /// ProcessDeclAttributeList - Apply all the decl attributes in the specified |
| 6262 | /// attribute list to the specified decl, ignoring any type attributes. |
Eric Christopher | bc638a8 | 2010-12-01 22:13:54 +0000 | [diff] [blame] | 6263 | void Sema::ProcessDeclAttributeList(Scope *S, Decl *D, |
Peter Collingbourne | b331b26 | 2011-01-21 02:08:45 +0000 | [diff] [blame] | 6264 | const AttributeList *AttrList, |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 6265 | bool IncludeCXX11Attributes) { |
| 6266 | for (const AttributeList* l = AttrList; l; l = l->getNext()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6267 | ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6268 | |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6269 | // FIXME: We should be able to handle these cases in TableGen. |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6270 | // GCC accepts |
| 6271 | // static int a9 __attribute__((weakref)); |
| 6272 | // but that looks really pointless. We reject it. |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6273 | if (D->hasAttr<WeakRefAttr>() && !D->hasAttr<AliasAttr>()) { |
Aaron Ballman | 9f6fec4 | 2014-01-02 23:02:01 +0000 | [diff] [blame] | 6274 | Diag(AttrList->getLoc(), diag::err_attribute_weakref_without_alias) |
| 6275 | << cast<NamedDecl>(D); |
Rafael Espindola | b306900 | 2013-01-16 23:49:06 +0000 | [diff] [blame] | 6276 | D->dropAttr<WeakRefAttr>(); |
Rafael Espindola | c18086a | 2010-02-23 22:00:30 +0000 | [diff] [blame] | 6277 | return; |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6278 | } |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6279 | |
Aaron Ballman | be243a7 | 2014-12-04 22:45:31 +0000 | [diff] [blame] | 6280 | // FIXME: We should be able to handle this in TableGen as well. It would be |
| 6281 | // good to have a way to specify "these attributes must appear as a group", |
| 6282 | // for these. Additionally, it would be good to have a way to specify "these |
| 6283 | // 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] | 6284 | if (!D->hasAttr<OpenCLKernelAttr>()) { |
| 6285 | // These attributes cannot be applied to a non-kernel function. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6286 | if (Attr *A = D->getAttr<ReqdWorkGroupSizeAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 6287 | // FIXME: This emits a different error message than |
| 6288 | // diag::err_attribute_wrong_decl_type + ExpectedKernelFunction. |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6289 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6290 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 6291 | } else if (Attr *A = D->getAttr<WorkGroupSizeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6292 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6293 | D->setInvalidDecl(); |
Matt Arsenault | 43cfcbc | 2014-12-05 18:03:55 +0000 | [diff] [blame] | 6294 | } else if (Attr *A = D->getAttr<VecTypeHintAttr>()) { |
Aaron Ballman | 3e424b5 | 2013-12-26 18:30:57 +0000 | [diff] [blame] | 6295 | Diag(D->getLocation(), diag::err_opencl_kernel_attr) << A; |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6296 | D->setInvalidDecl(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6297 | } else if (Attr *A = D->getAttr<AMDGPUFlatWorkGroupSizeAttr>()) { |
| 6298 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6299 | << A << ExpectedKernelFunction; |
| 6300 | D->setInvalidDecl(); |
| 6301 | } else if (Attr *A = D->getAttr<AMDGPUWavesPerEUAttr>()) { |
Matt Arsenault | b9e9dc5 | 2014-12-05 18:03:58 +0000 | [diff] [blame] | 6302 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6303 | << A << ExpectedKernelFunction; |
| 6304 | D->setInvalidDecl(); |
| 6305 | } else if (Attr *A = D->getAttr<AMDGPUNumSGPRAttr>()) { |
| 6306 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6307 | << A << ExpectedKernelFunction; |
| 6308 | D->setInvalidDecl(); |
Konstantin Zhuravlyov | 5b48d72 | 2016-09-26 01:02:57 +0000 | [diff] [blame] | 6309 | } else if (Attr *A = D->getAttr<AMDGPUNumVGPRAttr>()) { |
| 6310 | Diag(D->getLocation(), diag::err_attribute_wrong_decl_type) |
| 6311 | << A << ExpectedKernelFunction; |
| 6312 | D->setInvalidDecl(); |
Joey Gouly | 2cd9db1 | 2013-12-13 16:15:28 +0000 | [diff] [blame] | 6313 | } |
| 6314 | } |
Chris Lattner | b632a6e | 2008-06-29 00:43:07 +0000 | [diff] [blame] | 6315 | } |
| 6316 | |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6317 | // Annotation attributes are the only attributes allowed after an access |
| 6318 | // specifier. |
| 6319 | bool Sema::ProcessAccessDeclAttributeList(AccessSpecDecl *ASDecl, |
| 6320 | const AttributeList *AttrList) { |
| 6321 | for (const AttributeList* l = AttrList; l; l = l->getNext()) { |
Alexis Hunt | 3bc72c1 | 2012-06-19 23:57:03 +0000 | [diff] [blame] | 6322 | if (l->getKind() == AttributeList::AT_Annotate) { |
David Majnemer | 706f315 | 2014-12-14 01:05:01 +0000 | [diff] [blame] | 6323 | ProcessDeclAttribute(*this, nullptr, ASDecl, *l, l->isCXX11Attribute()); |
Erik Verbruggen | ca98f2a | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 6324 | } else { |
| 6325 | Diag(l->getLoc(), diag::err_only_annotate_after_access_spec); |
| 6326 | return true; |
| 6327 | } |
| 6328 | } |
| 6329 | |
| 6330 | return false; |
| 6331 | } |
| 6332 | |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6333 | /// checkUnusedDeclAttributes - Check a list of attributes to see if it |
| 6334 | /// contains any decl attributes that we should warn about. |
| 6335 | static void checkUnusedDeclAttributes(Sema &S, const AttributeList *A) { |
| 6336 | for ( ; A; A = A->getNext()) { |
| 6337 | // Only warn if the attribute is an unignored, non-type attribute. |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 6338 | if (A->isUsedAsTypeAttr() || A->isInvalid()) continue; |
John McCall | 42856de | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 6339 | if (A->getKind() == AttributeList::IgnoredAttribute) continue; |
| 6340 | |
| 6341 | if (A->getKind() == AttributeList::UnknownAttribute) { |
| 6342 | S.Diag(A->getLoc(), diag::warn_unknown_attribute_ignored) |
| 6343 | << A->getName() << A->getRange(); |
| 6344 | } else { |
| 6345 | S.Diag(A->getLoc(), diag::warn_attribute_not_on_decl) |
| 6346 | << A->getName() << A->getRange(); |
| 6347 | } |
| 6348 | } |
| 6349 | } |
| 6350 | |
| 6351 | /// checkUnusedDeclAttributes - Given a declarator which is not being |
| 6352 | /// used to build a declaration, complain about any decl attributes |
| 6353 | /// which might be lying around on it. |
| 6354 | void Sema::checkUnusedDeclAttributes(Declarator &D) { |
| 6355 | ::checkUnusedDeclAttributes(*this, D.getDeclSpec().getAttributes().getList()); |
| 6356 | ::checkUnusedDeclAttributes(*this, D.getAttributes()); |
| 6357 | for (unsigned i = 0, e = D.getNumTypeObjects(); i != e; ++i) |
| 6358 | ::checkUnusedDeclAttributes(*this, D.getTypeObject(i).getAttrs()); |
| 6359 | } |
| 6360 | |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6361 | /// DeclClonePragmaWeak - clone existing decl (maybe definition), |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6362 | /// \#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] | 6363 | NamedDecl * Sema::DeclClonePragmaWeak(NamedDecl *ND, IdentifierInfo *II, |
| 6364 | SourceLocation Loc) { |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6365 | assert(isa<FunctionDecl>(ND) || isa<VarDecl>(ND)); |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6366 | NamedDecl *NewD = nullptr; |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6367 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6368 | FunctionDecl *NewFD; |
| 6369 | // FIXME: Missing call to CheckFunctionDeclaration(). |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6370 | // FIXME: Mangling? |
| 6371 | // FIXME: Is the qualifier info correct? |
| 6372 | // FIXME: Is the DeclContext correct? |
Alexander Kornienko | 061900f | 2015-12-03 11:37:28 +0000 | [diff] [blame] | 6373 | NewFD = FunctionDecl::Create(FD->getASTContext(), FD->getDeclContext(), |
| 6374 | Loc, Loc, DeclarationName(II), |
| 6375 | FD->getType(), FD->getTypeSourceInfo(), |
| 6376 | SC_None, false/*isInlineSpecified*/, |
| 6377 | FD->hasPrototype(), |
| 6378 | false/*isConstexprSpecified*/); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6379 | NewD = NewFD; |
| 6380 | |
| 6381 | if (FD->getQualifier()) |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 6382 | NewFD->setQualifierInfo(FD->getQualifierLoc()); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6383 | |
| 6384 | // Fake up parameter variables; they are declared as if this were |
| 6385 | // a typedef. |
| 6386 | QualType FDTy = FD->getType(); |
| 6387 | if (const FunctionProtoType *FT = FDTy->getAs<FunctionProtoType>()) { |
| 6388 | SmallVector<ParmVarDecl*, 16> Params; |
Aaron Ballman | 40bd0aa | 2014-03-17 15:23:01 +0000 | [diff] [blame] | 6389 | for (const auto &AI : FT->param_types()) { |
| 6390 | ParmVarDecl *Param = BuildParmVarDeclForTypedef(NewFD, Loc, AI); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6391 | Param->setScopeInfo(0, Params.size()); |
| 6392 | Params.push_back(Param); |
| 6393 | } |
David Blaikie | 9c70e04 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 6394 | NewFD->setParams(Params); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6395 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6396 | } else if (VarDecl *VD = dyn_cast<VarDecl>(ND)) { |
| 6397 | NewD = VarDecl::Create(VD->getASTContext(), VD->getDeclContext(), |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 6398 | VD->getInnerLocStart(), VD->getLocation(), II, |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 6399 | VD->getType(), VD->getTypeSourceInfo(), |
Rafael Espindola | 6ae7e50 | 2013-04-03 19:27:57 +0000 | [diff] [blame] | 6400 | VD->getStorageClass()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6401 | if (VD->getQualifier()) { |
| 6402 | VarDecl *NewVD = cast<VarDecl>(NewD); |
Douglas Gregor | 1445480 | 2011-02-25 02:25:35 +0000 | [diff] [blame] | 6403 | NewVD->setQualifierInfo(VD->getQualifierLoc()); |
John McCall | 3e11ebe | 2010-03-15 10:12:16 +0000 | [diff] [blame] | 6404 | } |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6405 | } |
| 6406 | return NewD; |
| 6407 | } |
| 6408 | |
James Dennett | 634962f | 2012-06-14 21:40:34 +0000 | [diff] [blame] | 6409 | /// DeclApplyPragmaWeak - A declaration (maybe definition) needs \#pragma weak |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6410 | /// applied to it, possibly with an alias. |
Ryan Flynn | d963a49 | 2009-07-31 02:52:19 +0000 | [diff] [blame] | 6411 | void Sema::DeclApplyPragmaWeak(Scope *S, NamedDecl *ND, WeakInfo &W) { |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6412 | if (W.getUsed()) return; // only do this once |
| 6413 | W.setUsed(true); |
| 6414 | if (W.getAlias()) { // clone decl, impersonate __attribute(weak,alias(...)) |
| 6415 | IdentifierInfo *NDId = ND->getIdentifier(); |
Eli Friedman | ce3e2c8 | 2011-09-07 04:05:06 +0000 | [diff] [blame] | 6416 | NamedDecl *NewD = DeclClonePragmaWeak(ND, W.getAlias(), W.getLocation()); |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6417 | NewD->addAttr(AliasAttr::CreateImplicit(Context, NDId->getName(), |
| 6418 | W.getLocation())); |
| 6419 | NewD->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6420 | WeakTopLevelDecl.push_back(NewD); |
| 6421 | // FIXME: "hideous" code from Sema::LazilyCreateBuiltin |
| 6422 | // to insert Decl at TU scope, sorry. |
| 6423 | DeclContext *SavedContext = CurContext; |
| 6424 | CurContext = Context.getTranslationUnitDecl(); |
Argyrios Kyrtzidis | 0098a4b | 2014-03-09 05:15:28 +0000 | [diff] [blame] | 6425 | NewD->setDeclContext(CurContext); |
| 6426 | NewD->setLexicalDeclContext(CurContext); |
Chris Lattner | e6eab98 | 2009-09-08 18:10:11 +0000 | [diff] [blame] | 6427 | PushOnScopeChains(NewD, S); |
| 6428 | CurContext = SavedContext; |
| 6429 | } else { // just add weak to existing |
Aaron Ballman | 36a5350 | 2014-01-16 13:03:14 +0000 | [diff] [blame] | 6430 | ND->addAttr(WeakAttr::CreateImplicit(Context, W.getLocation())); |
Ryan Flynn | 7d470f3 | 2009-07-30 03:15:39 +0000 | [diff] [blame] | 6431 | } |
| 6432 | } |
| 6433 | |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6434 | void Sema::ProcessPragmaWeak(Scope *S, Decl *D) { |
| 6435 | // It's valid to "forward-declare" #pragma weak, in which case we |
| 6436 | // have to do this. |
| 6437 | LoadExternalWeakUndeclaredIdentifiers(); |
| 6438 | if (!WeakUndeclaredIdentifiers.empty()) { |
Craig Topper | c3ec149 | 2014-05-26 06:22:03 +0000 | [diff] [blame] | 6439 | NamedDecl *ND = nullptr; |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6440 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 6441 | if (VD->isExternC()) |
| 6442 | ND = VD; |
| 6443 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 6444 | if (FD->isExternC()) |
| 6445 | ND = FD; |
| 6446 | if (ND) { |
| 6447 | if (IdentifierInfo *Id = ND->getIdentifier()) { |
Chandler Carruth | f85d982 | 2015-03-26 08:32:49 +0000 | [diff] [blame] | 6448 | auto I = WeakUndeclaredIdentifiers.find(Id); |
Rafael Espindola | de6a39f | 2013-03-02 21:41:48 +0000 | [diff] [blame] | 6449 | if (I != WeakUndeclaredIdentifiers.end()) { |
| 6450 | WeakInfo W = I->second; |
| 6451 | DeclApplyPragmaWeak(S, ND, W); |
| 6452 | WeakUndeclaredIdentifiers[Id] = W; |
| 6453 | } |
| 6454 | } |
| 6455 | } |
| 6456 | } |
| 6457 | } |
| 6458 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6459 | /// ProcessDeclAttributes - Given a declarator (PD) with attributes indicated in |
| 6460 | /// it, apply them to D. This is a bit tricky because PD can have attributes |
| 6461 | /// 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] | 6462 | void Sema::ProcessDeclAttributes(Scope *S, Decl *D, const Declarator &PD) { |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6463 | // Apply decl attributes from the DeclSpec if present. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 6464 | if (const AttributeList *Attrs = PD.getDeclSpec().getAttributes().getList()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6465 | ProcessDeclAttributeList(S, D, Attrs); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6466 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6467 | // Walk the declarator structure, applying decl attributes that were in a type |
| 6468 | // position to the decl itself. This handles cases like: |
| 6469 | // int *__attr__(x)** D; |
| 6470 | // when X is a decl attribute. |
| 6471 | for (unsigned i = 0, e = PD.getNumTypeObjects(); i != e; ++i) |
| 6472 | if (const AttributeList *Attrs = PD.getTypeObject(i).getAttrs()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6473 | ProcessDeclAttributeList(S, D, Attrs, /*IncludeCXX11Attributes=*/false); |
Mike Stump | d3bb557 | 2009-07-24 19:02:52 +0000 | [diff] [blame] | 6474 | |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6475 | // Finally, apply any attributes on the decl itself. |
| 6476 | if (const AttributeList *Attrs = PD.getAttributes()) |
Richard Smith | f8a75c3 | 2013-08-29 00:47:48 +0000 | [diff] [blame] | 6477 | ProcessDeclAttributeList(S, D, Attrs); |
Chris Lattner | 9e2aafe | 2008-06-29 00:23:49 +0000 | [diff] [blame] | 6478 | } |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6479 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6480 | /// Is the given declaration allowed to use a forbidden type? |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6481 | /// If so, it'll still be annotated with an attribute that makes it |
| 6482 | /// illegal to actually use. |
| 6483 | static bool isForbiddenTypeAllowed(Sema &S, Decl *decl, |
| 6484 | const DelayedDiagnostic &diag, |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6485 | UnavailableAttr::ImplicitReason &reason) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6486 | // Private ivars are always okay. Unfortunately, people don't |
| 6487 | // always properly make their ivars private, even in system headers. |
| 6488 | // Plus we need to make fields okay, too. |
Fariborz Jahanian | 6d5d6a2 | 2011-09-26 21:23:35 +0000 | [diff] [blame] | 6489 | if (!isa<FieldDecl>(decl) && !isa<ObjCPropertyDecl>(decl) && |
| 6490 | !isa<FunctionDecl>(decl)) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6491 | return false; |
| 6492 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6493 | // Silently accept unsupported uses of __weak in both user and system |
| 6494 | // declarations when it's been disabled, for ease of integration with |
| 6495 | // -fno-objc-arc files. We do have to take some care against attempts |
| 6496 | // to define such things; for now, we've only done that for ivars |
| 6497 | // and properties. |
| 6498 | if ((isa<ObjCIvarDecl>(decl) || isa<ObjCPropertyDecl>(decl))) { |
| 6499 | if (diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_disabled || |
| 6500 | diag.getForbiddenTypeDiagnostic() == diag::err_arc_weak_no_runtime) { |
| 6501 | reason = UnavailableAttr::IR_ForbiddenWeak; |
| 6502 | return true; |
| 6503 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6504 | } |
| 6505 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6506 | // Allow all sorts of things in system headers. |
| 6507 | if (S.Context.getSourceManager().isInSystemHeader(decl->getLocation())) { |
| 6508 | // Currently, all the failures dealt with this way are due to ARC |
| 6509 | // restrictions. |
| 6510 | reason = UnavailableAttr::IR_ARCForbiddenType; |
| 6511 | return true; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6512 | } |
| 6513 | |
| 6514 | return false; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6515 | } |
| 6516 | |
| 6517 | /// Handle a delayed forbidden-type diagnostic. |
| 6518 | static void handleDelayedForbiddenType(Sema &S, DelayedDiagnostic &diag, |
| 6519 | Decl *decl) { |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6520 | auto reason = UnavailableAttr::IR_None; |
| 6521 | if (decl && isForbiddenTypeAllowed(S, decl, diag, reason)) { |
| 6522 | assert(reason && "didn't set reason?"); |
| 6523 | decl->addAttr(UnavailableAttr::CreateImplicit(S.Context, "", reason, |
| 6524 | diag.Loc)); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6525 | return; |
| 6526 | } |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6527 | if (S.getLangOpts().ObjCAutoRefCount) |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6528 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(decl)) { |
Benjamin Kramer | 474261a | 2012-06-02 10:20:41 +0000 | [diff] [blame] | 6529 | // FIXME: we may want to suppress diagnostics for all |
Fariborz Jahanian | ed1933b | 2011-10-03 22:11:57 +0000 | [diff] [blame] | 6530 | // kind of forbidden type messages on unavailable functions. |
| 6531 | if (FD->hasAttr<UnavailableAttr>() && |
| 6532 | diag.getForbiddenTypeDiagnostic() == |
| 6533 | diag::err_arc_array_param_no_ownership) { |
| 6534 | diag.Triggered = true; |
| 6535 | return; |
| 6536 | } |
| 6537 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6538 | |
| 6539 | S.Diag(diag.Loc, diag.getForbiddenTypeDiagnostic()) |
| 6540 | << diag.getForbiddenTypeOperand() << diag.getForbiddenTypeArgument(); |
| 6541 | diag.Triggered = true; |
| 6542 | } |
| 6543 | |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 6544 | static const AvailabilityAttr *getAttrForPlatform(ASTContext &Context, |
| 6545 | const Decl *D) { |
| 6546 | // Check each AvailabilityAttr to find the one for this platform. |
| 6547 | for (const auto *A : D->attrs()) { |
| 6548 | if (const auto *Avail = dyn_cast<AvailabilityAttr>(A)) { |
| 6549 | // FIXME: this is copied from CheckAvailability. We should try to |
| 6550 | // de-duplicate. |
| 6551 | |
| 6552 | // Check if this is an App Extension "platform", and if so chop off |
| 6553 | // the suffix for matching with the actual platform. |
| 6554 | StringRef ActualPlatform = Avail->getPlatform()->getName(); |
| 6555 | StringRef RealizedPlatform = ActualPlatform; |
| 6556 | if (Context.getLangOpts().AppExt) { |
| 6557 | size_t suffix = RealizedPlatform.rfind("_app_extension"); |
| 6558 | if (suffix != StringRef::npos) |
| 6559 | RealizedPlatform = RealizedPlatform.slice(0, suffix); |
| 6560 | } |
| 6561 | |
| 6562 | StringRef TargetPlatform = Context.getTargetInfo().getPlatformName(); |
| 6563 | |
| 6564 | // Match the platform name. |
| 6565 | if (RealizedPlatform == TargetPlatform) |
| 6566 | return Avail; |
| 6567 | } |
| 6568 | } |
| 6569 | return nullptr; |
| 6570 | } |
| 6571 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6572 | /// \brief whether we should emit a diagnostic for \c K and \c DeclVersion in |
| 6573 | /// the context of \c Ctx. For example, we should emit an unavailable diagnostic |
| 6574 | /// in a deprecated context, but not the other way around. |
| 6575 | static bool ShouldDiagnoseAvailabilityInContext(Sema &S, AvailabilityResult K, |
| 6576 | VersionTuple DeclVersion, |
| 6577 | Decl *Ctx) { |
| 6578 | assert(K != AR_Available && "Expected an unavailable declaration here!"); |
| 6579 | |
| 6580 | // Checks if we should emit the availability diagnostic in the context of C. |
| 6581 | auto CheckContext = [&](const Decl *C) { |
| 6582 | if (K == AR_NotYetIntroduced) { |
| 6583 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, C)) |
| 6584 | if (AA->getIntroduced() >= DeclVersion) |
| 6585 | return true; |
| 6586 | } else if (K == AR_Deprecated) |
| 6587 | if (C->isDeprecated()) |
| 6588 | return true; |
| 6589 | |
| 6590 | if (C->isUnavailable()) |
| 6591 | return true; |
| 6592 | return false; |
| 6593 | }; |
| 6594 | |
| 6595 | // FIXME: This is a temporary workaround! Some existing Apple headers depends |
| 6596 | // on nested declarations in an @interface having the availability of the |
| 6597 | // interface when they really shouldn't: they are members of the enclosing |
| 6598 | // context, and can referenced from there. |
| 6599 | if (S.OriginalLexicalContext && cast<Decl>(S.OriginalLexicalContext) != Ctx) { |
| 6600 | auto *OrigCtx = cast<Decl>(S.OriginalLexicalContext); |
| 6601 | if (CheckContext(OrigCtx)) |
| 6602 | return false; |
| 6603 | |
| 6604 | // An implementation implicitly has the availability of the interface. |
| 6605 | if (auto *CatOrImpl = dyn_cast<ObjCImplDecl>(OrigCtx)) { |
| 6606 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 6607 | if (CheckContext(Interface)) |
| 6608 | return false; |
| 6609 | } |
| 6610 | // A category implicitly has the availability of the interface. |
| 6611 | else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(OrigCtx)) |
| 6612 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 6613 | if (CheckContext(Interface)) |
| 6614 | return false; |
| 6615 | } |
| 6616 | |
| 6617 | do { |
| 6618 | if (CheckContext(Ctx)) |
| 6619 | return false; |
| 6620 | |
| 6621 | // An implementation implicitly has the availability of the interface. |
| 6622 | if (auto *CatOrImpl = dyn_cast<ObjCImplDecl>(Ctx)) { |
| 6623 | if (const ObjCInterfaceDecl *Interface = CatOrImpl->getClassInterface()) |
| 6624 | if (CheckContext(Interface)) |
| 6625 | return false; |
| 6626 | } |
| 6627 | // A category implicitly has the availability of the interface. |
| 6628 | else if (auto *CatD = dyn_cast<ObjCCategoryDecl>(Ctx)) |
| 6629 | if (const ObjCInterfaceDecl *Interface = CatD->getClassInterface()) |
| 6630 | if (CheckContext(Interface)) |
| 6631 | return false; |
| 6632 | } while ((Ctx = cast_or_null<Decl>(Ctx->getDeclContext()))); |
| 6633 | |
| 6634 | return true; |
| 6635 | } |
| 6636 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6637 | static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K, |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6638 | Decl *Ctx, const NamedDecl *D, |
| 6639 | StringRef Message, SourceLocation Loc, |
| 6640 | const ObjCInterfaceDecl *UnknownObjCClass, |
| 6641 | const ObjCPropertyDecl *ObjCProperty, |
| 6642 | bool ObjCPropertyAccess) { |
| 6643 | // Diagnostics for deprecated or unavailable. |
| 6644 | unsigned diag, diag_message, diag_fwdclass_message; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6645 | unsigned diag_available_here = diag::note_availability_specified_here; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6646 | |
| 6647 | // Matches 'diag::note_property_attribute' options. |
| 6648 | unsigned property_note_select; |
| 6649 | |
| 6650 | // Matches diag::note_availability_specified_here. |
| 6651 | unsigned available_here_select_kind; |
| 6652 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6653 | VersionTuple DeclVersion; |
| 6654 | if (const AvailabilityAttr *AA = getAttrForPlatform(S.Context, D)) |
| 6655 | DeclVersion = AA->getIntroduced(); |
| 6656 | |
| 6657 | if (!ShouldDiagnoseAvailabilityInContext(S, K, DeclVersion, Ctx)) |
| 6658 | return; |
| 6659 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6660 | switch (K) { |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6661 | case AR_Deprecated: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6662 | diag = !ObjCPropertyAccess ? diag::warn_deprecated |
| 6663 | : diag::warn_property_method_deprecated; |
| 6664 | diag_message = diag::warn_deprecated_message; |
| 6665 | diag_fwdclass_message = diag::warn_deprecated_fwdclass_message; |
| 6666 | property_note_select = /* deprecated */ 0; |
| 6667 | available_here_select_kind = /* deprecated */ 2; |
| 6668 | break; |
| 6669 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6670 | case AR_Unavailable: |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6671 | diag = !ObjCPropertyAccess ? diag::err_unavailable |
| 6672 | : diag::err_property_method_unavailable; |
| 6673 | diag_message = diag::err_unavailable_message; |
| 6674 | diag_fwdclass_message = diag::warn_unavailable_fwdclass_message; |
| 6675 | property_note_select = /* unavailable */ 1; |
| 6676 | available_here_select_kind = /* unavailable */ 0; |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6677 | |
John McCall | c6af8c6 | 2015-10-28 05:03:19 +0000 | [diff] [blame] | 6678 | if (auto attr = D->getAttr<UnavailableAttr>()) { |
| 6679 | if (attr->isImplicit() && attr->getImplicitReason()) { |
| 6680 | // Most of these failures are due to extra restrictions in ARC; |
| 6681 | // reflect that in the primary diagnostic when applicable. |
| 6682 | auto flagARCError = [&] { |
| 6683 | if (S.getLangOpts().ObjCAutoRefCount && |
| 6684 | S.getSourceManager().isInSystemHeader(D->getLocation())) |
| 6685 | diag = diag::err_unavailable_in_arc; |
| 6686 | }; |
| 6687 | |
| 6688 | switch (attr->getImplicitReason()) { |
| 6689 | case UnavailableAttr::IR_None: break; |
| 6690 | |
| 6691 | case UnavailableAttr::IR_ARCForbiddenType: |
| 6692 | flagARCError(); |
| 6693 | diag_available_here = diag::note_arc_forbidden_type; |
| 6694 | break; |
| 6695 | |
| 6696 | case UnavailableAttr::IR_ForbiddenWeak: |
| 6697 | if (S.getLangOpts().ObjCWeakRuntime) |
| 6698 | diag_available_here = diag::note_arc_weak_disabled; |
| 6699 | else |
| 6700 | diag_available_here = diag::note_arc_weak_no_runtime; |
| 6701 | break; |
| 6702 | |
| 6703 | case UnavailableAttr::IR_ARCForbiddenConversion: |
| 6704 | flagARCError(); |
| 6705 | diag_available_here = diag::note_performs_forbidden_arc_conversion; |
| 6706 | break; |
| 6707 | |
| 6708 | case UnavailableAttr::IR_ARCInitReturnsUnrelated: |
| 6709 | flagARCError(); |
| 6710 | diag_available_here = diag::note_arc_init_returns_unrelated; |
| 6711 | break; |
| 6712 | |
| 6713 | case UnavailableAttr::IR_ARCFieldWithOwnership: |
| 6714 | flagARCError(); |
| 6715 | diag_available_here = diag::note_arc_field_with_ownership; |
| 6716 | break; |
| 6717 | } |
| 6718 | } |
John McCall | b61e14e | 2015-10-27 04:54:50 +0000 | [diff] [blame] | 6719 | } |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6720 | break; |
| 6721 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6722 | case AR_NotYetIntroduced: |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6723 | diag = diag::warn_partial_availability; |
| 6724 | diag_message = diag::warn_partial_message; |
| 6725 | diag_fwdclass_message = diag::warn_partial_fwdclass_message; |
| 6726 | property_note_select = /* partial */ 2; |
| 6727 | available_here_select_kind = /* partial */ 3; |
| 6728 | break; |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6729 | |
| 6730 | case AR_Available: |
| 6731 | llvm_unreachable("Warning for availability of available declaration?"); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6732 | } |
| 6733 | |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6734 | CharSourceRange UseRange; |
| 6735 | StringRef Replacement; |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6736 | if (K == AR_Deprecated) { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6737 | if (auto attr = D->getAttr<DeprecatedAttr>()) |
| 6738 | Replacement = attr->getReplacement(); |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 6739 | if (auto attr = getAttrForPlatform(S.Context, D)) |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 6740 | Replacement = attr->getReplacement(); |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6741 | |
| 6742 | if (!Replacement.empty()) |
| 6743 | UseRange = |
| 6744 | CharSourceRange::getCharRange(Loc, S.getLocForEndOfToken(Loc)); |
| 6745 | } |
| 6746 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6747 | if (!Message.empty()) { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6748 | S.Diag(Loc, diag_message) << D << Message |
| 6749 | << (UseRange.isValid() ? |
| 6750 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6751 | if (ObjCProperty) |
| 6752 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 6753 | << ObjCProperty->getDeclName() << property_note_select; |
| 6754 | } else if (!UnknownObjCClass) { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6755 | S.Diag(Loc, diag) << D |
| 6756 | << (UseRange.isValid() ? |
| 6757 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6758 | if (ObjCProperty) |
| 6759 | S.Diag(ObjCProperty->getLocation(), diag::note_property_attribute) |
| 6760 | << ObjCProperty->getDeclName() << property_note_select; |
| 6761 | } else { |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 6762 | S.Diag(Loc, diag_fwdclass_message) << D |
| 6763 | << (UseRange.isValid() ? |
| 6764 | FixItHint::CreateReplacement(UseRange, Replacement) : FixItHint()); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6765 | S.Diag(UnknownObjCClass->getLocation(), diag::note_forward_class); |
| 6766 | } |
| 6767 | |
Manman Ren | 45b1ab1 | 2016-05-06 19:57:16 +0000 | [diff] [blame] | 6768 | // The declaration can have multiple availability attributes, we are looking |
| 6769 | // at one of them. |
| 6770 | const AvailabilityAttr *A = getAttrForPlatform(S.Context, D); |
| 6771 | if (A && A->isInherited()) { |
| 6772 | for (const Decl *Redecl = D->getMostRecentDecl(); Redecl; |
| 6773 | Redecl = Redecl->getPreviousDecl()) { |
| 6774 | const AvailabilityAttr *AForRedecl = getAttrForPlatform(S.Context, |
| 6775 | Redecl); |
| 6776 | if (AForRedecl && !AForRedecl->isInherited()) { |
| 6777 | // If D is a declaration with inherited attributes, the note should |
| 6778 | // point to the declaration with actual attributes. |
| 6779 | S.Diag(Redecl->getLocation(), diag_available_here) << D |
| 6780 | << available_here_select_kind; |
| 6781 | break; |
| 6782 | } |
| 6783 | } |
| 6784 | } |
| 6785 | else |
| 6786 | S.Diag(D->getLocation(), diag_available_here) |
| 6787 | << D << available_here_select_kind; |
| 6788 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6789 | if (K == AR_NotYetIntroduced) |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6790 | S.Diag(Loc, diag::note_partial_availability_silence) << D; |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6791 | } |
| 6792 | |
| 6793 | static void handleDelayedAvailabilityCheck(Sema &S, DelayedDiagnostic &DD, |
| 6794 | Decl *Ctx) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 6795 | assert(DD.Kind == DelayedDiagnostic::Availability && |
| 6796 | "Expected an availability diagnostic here"); |
| 6797 | |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6798 | DD.Triggered = true; |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6799 | DoEmitAvailabilityWarning( |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 6800 | S, DD.getAvailabilityResult(), Ctx, DD.getAvailabilityDecl(), |
| 6801 | DD.getAvailabilityMessage(), DD.Loc, DD.getUnknownObjCClass(), |
| 6802 | DD.getObjCProperty(), false); |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6803 | } |
| 6804 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6805 | void Sema::PopParsingDeclaration(ParsingDeclState state, Decl *decl) { |
| 6806 | assert(DelayedDiagnostics.getCurrentPool()); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6807 | DelayedDiagnosticPool &poppedPool = *DelayedDiagnostics.getCurrentPool(); |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6808 | DelayedDiagnostics.popWithoutEmitting(state); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6809 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6810 | // When delaying diagnostics to run in the context of a parsed |
| 6811 | // declaration, we only want to actually emit anything if parsing |
| 6812 | // succeeds. |
| 6813 | if (!decl) return; |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6814 | |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6815 | // We emit all the active diagnostics in this pool or any of its |
| 6816 | // parents. In general, we'll get one pool for the decl spec |
| 6817 | // and a child pool for each declarator; in a decl group like: |
| 6818 | // deprecated_typedef foo, *bar, baz(); |
| 6819 | // only the declarator pops will be passed decls. This is correct; |
| 6820 | // we really do need to consider delayed diagnostics from the decl spec |
| 6821 | // for each of the different declarations. |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6822 | const DelayedDiagnosticPool *pool = &poppedPool; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6823 | do { |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6824 | for (DelayedDiagnosticPool::pool_iterator |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6825 | i = pool->pool_begin(), e = pool->pool_end(); i != e; ++i) { |
| 6826 | // This const_cast is a bit lame. Really, Triggered should be mutable. |
| 6827 | DelayedDiagnostic &diag = const_cast<DelayedDiagnostic&>(*i); |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6828 | if (diag.Triggered) |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6829 | continue; |
| 6830 | |
John McCall | c146582 | 2011-02-14 07:13:47 +0000 | [diff] [blame] | 6831 | switch (diag.Kind) { |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 6832 | case DelayedDiagnostic::Availability: |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 6833 | // Don't bother giving deprecation/unavailable diagnostics if |
| 6834 | // the decl is invalid. |
John McCall | 18a962b | 2012-01-26 20:04:03 +0000 | [diff] [blame] | 6835 | if (!decl->isInvalidDecl()) |
Aaron Ballman | fb23752 | 2014-10-15 15:37:51 +0000 | [diff] [blame] | 6836 | handleDelayedAvailabilityCheck(*this, diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6837 | break; |
| 6838 | |
| 6839 | case DelayedDiagnostic::Access: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6840 | HandleDelayedAccessCheck(diag, decl); |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6841 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6842 | |
| 6843 | case DelayedDiagnostic::ForbiddenType: |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6844 | handleDelayedForbiddenType(*this, diag, decl); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6845 | break; |
John McCall | 8612151 | 2010-01-27 03:50:35 +0000 | [diff] [blame] | 6846 | } |
| 6847 | } |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 6848 | } while ((pool = pool->getParent())); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6849 | } |
| 6850 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 6851 | /// Given a set of delayed diagnostics, re-emit them as if they had |
| 6852 | /// been delayed in the current context instead of in the given pool. |
| 6853 | /// Essentially, this just moves them to the current pool. |
| 6854 | void Sema::redelayDiagnostics(DelayedDiagnosticPool &pool) { |
| 6855 | DelayedDiagnosticPool *curPool = DelayedDiagnostics.getCurrentPool(); |
| 6856 | assert(curPool && "re-emitting in undelayed context not supported"); |
| 6857 | curPool->steal(pool); |
| 6858 | } |
| 6859 | |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6860 | void Sema::EmitAvailabilityWarning(AvailabilityResult AR, |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 6861 | NamedDecl *D, StringRef Message, |
| 6862 | SourceLocation Loc, |
| 6863 | const ObjCInterfaceDecl *UnknownObjCClass, |
Fariborz Jahanian | 89ea961 | 2014-06-16 17:25:41 +0000 | [diff] [blame] | 6864 | const ObjCPropertyDecl *ObjCProperty, |
| 6865 | bool ObjCPropertyAccess) { |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6866 | // Delay if we're currently parsing a declaration. |
Erik Pilkington | a800397 | 2016-10-28 21:39:27 +0000 | [diff] [blame] | 6867 | if (DelayedDiagnostics.shouldDelayDiagnostics()) { |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 6868 | DelayedDiagnostics.add(DelayedDiagnostic::makeAvailability( |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6869 | AR, Loc, D, UnknownObjCClass, ObjCProperty, Message, |
Nico Weber | 462fd1e | 2015-01-07 23:50:05 +0000 | [diff] [blame] | 6870 | ObjCPropertyAccess)); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6871 | return; |
| 6872 | } |
| 6873 | |
Ted Kremenek | b79ee57 | 2013-12-18 23:30:06 +0000 | [diff] [blame] | 6874 | Decl *Ctx = cast<Decl>(getCurLexicalContext()); |
Erik Pilkington | 796a3e2 | 2016-08-05 22:59:03 +0000 | [diff] [blame] | 6875 | DoEmitAvailabilityWarning(*this, AR, Ctx, D, Message, Loc, UnknownObjCClass, |
Nico Weber | 0055a19 | 2015-03-19 19:18:22 +0000 | [diff] [blame] | 6876 | ObjCProperty, ObjCPropertyAccess); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 6877 | } |
Erik Pilkington | 48c7cc9 | 2016-07-29 17:37:38 +0000 | [diff] [blame] | 6878 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6879 | namespace { |
| 6880 | |
| 6881 | /// \brief This class implements -Wunguarded-availability. |
| 6882 | /// |
| 6883 | /// This is done with a traversal of the AST of a function that makes reference |
| 6884 | /// to a partially available declaration. Whenever we encounter an \c if of the |
| 6885 | /// form: \c if(@available(...)), we use the version from the condition to visit |
| 6886 | /// the then statement. |
| 6887 | class DiagnoseUnguardedAvailability |
| 6888 | : public RecursiveASTVisitor<DiagnoseUnguardedAvailability> { |
| 6889 | typedef RecursiveASTVisitor<DiagnoseUnguardedAvailability> Base; |
| 6890 | |
| 6891 | Sema &SemaRef; |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6892 | Decl *Ctx; |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6893 | |
| 6894 | /// Stack of potentially nested 'if (@available(...))'s. |
| 6895 | SmallVector<VersionTuple, 8> AvailabilityStack; |
| 6896 | |
| 6897 | void DiagnoseDeclAvailability(NamedDecl *D, SourceRange Range); |
| 6898 | |
| 6899 | public: |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6900 | DiagnoseUnguardedAvailability(Sema &SemaRef, Decl *Ctx) |
| 6901 | : SemaRef(SemaRef), Ctx(Ctx) { |
| 6902 | AvailabilityStack.push_back( |
| 6903 | SemaRef.Context.getTargetInfo().getPlatformMinVersion()); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | void IssueDiagnostics(Stmt *S) { TraverseStmt(S); } |
| 6907 | |
| 6908 | bool TraverseIfStmt(IfStmt *If); |
| 6909 | |
| 6910 | bool VisitObjCMessageExpr(ObjCMessageExpr *Msg) { |
| 6911 | if (ObjCMethodDecl *D = Msg->getMethodDecl()) |
| 6912 | DiagnoseDeclAvailability( |
| 6913 | D, SourceRange(Msg->getSelectorStartLoc(), Msg->getLocEnd())); |
| 6914 | return true; |
| 6915 | } |
| 6916 | |
| 6917 | bool VisitDeclRefExpr(DeclRefExpr *DRE) { |
| 6918 | DiagnoseDeclAvailability(DRE->getDecl(), |
| 6919 | SourceRange(DRE->getLocStart(), DRE->getLocEnd())); |
| 6920 | return true; |
| 6921 | } |
| 6922 | |
| 6923 | bool VisitMemberExpr(MemberExpr *ME) { |
| 6924 | DiagnoseDeclAvailability(ME->getMemberDecl(), |
| 6925 | SourceRange(ME->getLocStart(), ME->getLocEnd())); |
| 6926 | return true; |
| 6927 | } |
| 6928 | |
| 6929 | bool VisitTypeLoc(TypeLoc Ty); |
| 6930 | }; |
| 6931 | |
| 6932 | void DiagnoseUnguardedAvailability::DiagnoseDeclAvailability( |
| 6933 | NamedDecl *D, SourceRange Range) { |
| 6934 | |
| 6935 | VersionTuple ContextVersion = AvailabilityStack.back(); |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6936 | if (AvailabilityResult Result = |
| 6937 | SemaRef.ShouldDiagnoseAvailabilityOfDecl(D, nullptr)) { |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6938 | // All other diagnostic kinds have already been handled in |
| 6939 | // DiagnoseAvailabilityOfDecl. |
| 6940 | if (Result != AR_NotYetIntroduced) |
| 6941 | return; |
| 6942 | |
| 6943 | const AvailabilityAttr *AA = getAttrForPlatform(SemaRef.getASTContext(), D); |
| 6944 | VersionTuple Introduced = AA->getIntroduced(); |
| 6945 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 6946 | if (ContextVersion >= Introduced) |
| 6947 | return; |
| 6948 | |
| 6949 | // If the context of this function is less available than D, we should not |
| 6950 | // emit a diagnostic. |
| 6951 | if (!ShouldDiagnoseAvailabilityInContext(SemaRef, Result, Introduced, Ctx)) |
| 6952 | return; |
| 6953 | |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 6954 | SemaRef.Diag(Range.getBegin(), diag::warn_unguarded_availability) |
| 6955 | << Range << D |
| 6956 | << AvailabilityAttr::getPrettyPlatformName( |
| 6957 | SemaRef.getASTContext().getTargetInfo().getPlatformName()) |
| 6958 | << Introduced.getAsString(); |
| 6959 | |
| 6960 | SemaRef.Diag(D->getLocation(), diag::note_availability_specified_here) |
| 6961 | << D << /* partial */ 3; |
| 6962 | |
| 6963 | // FIXME: Replace this with a fixit diagnostic. |
| 6964 | SemaRef.Diag(Range.getBegin(), diag::note_unguarded_available_silence) |
| 6965 | << Range << D; |
| 6966 | } |
| 6967 | } |
| 6968 | |
| 6969 | bool DiagnoseUnguardedAvailability::VisitTypeLoc(TypeLoc Ty) { |
| 6970 | const Type *TyPtr = Ty.getTypePtr(); |
| 6971 | SourceRange Range{Ty.getBeginLoc(), Ty.getEndLoc()}; |
| 6972 | |
| 6973 | if (const TagType *TT = dyn_cast<TagType>(TyPtr)) { |
| 6974 | TagDecl *TD = TT->getDecl(); |
| 6975 | DiagnoseDeclAvailability(TD, Range); |
| 6976 | |
| 6977 | } else if (const TypedefType *TD = dyn_cast<TypedefType>(TyPtr)) { |
| 6978 | TypedefNameDecl *D = TD->getDecl(); |
| 6979 | DiagnoseDeclAvailability(D, Range); |
| 6980 | |
| 6981 | } else if (const auto *ObjCO = dyn_cast<ObjCObjectType>(TyPtr)) { |
| 6982 | if (NamedDecl *D = ObjCO->getInterface()) |
| 6983 | DiagnoseDeclAvailability(D, Range); |
| 6984 | } |
| 6985 | |
| 6986 | return true; |
| 6987 | } |
| 6988 | |
| 6989 | bool DiagnoseUnguardedAvailability::TraverseIfStmt(IfStmt *If) { |
| 6990 | VersionTuple CondVersion; |
| 6991 | if (auto *E = dyn_cast<ObjCAvailabilityCheckExpr>(If->getCond())) { |
| 6992 | CondVersion = E->getVersion(); |
| 6993 | |
| 6994 | // If we're using the '*' case here or if this check is redundant, then we |
| 6995 | // use the enclosing version to check both branches. |
| 6996 | if (CondVersion.empty() || CondVersion <= AvailabilityStack.back()) |
| 6997 | return Base::TraverseStmt(If->getThen()) && |
| 6998 | Base::TraverseStmt(If->getElse()); |
| 6999 | } else { |
| 7000 | // This isn't an availability checking 'if', we can just continue. |
| 7001 | return Base::TraverseIfStmt(If); |
| 7002 | } |
| 7003 | |
| 7004 | AvailabilityStack.push_back(CondVersion); |
| 7005 | bool ShouldContinue = TraverseStmt(If->getThen()); |
| 7006 | AvailabilityStack.pop_back(); |
| 7007 | |
| 7008 | return ShouldContinue && TraverseStmt(If->getElse()); |
| 7009 | } |
| 7010 | |
| 7011 | } // end anonymous namespace |
| 7012 | |
| 7013 | void Sema::DiagnoseUnguardedAvailabilityViolations(Decl *D) { |
| 7014 | Stmt *Body = nullptr; |
| 7015 | |
| 7016 | if (auto *FD = D->getAsFunction()) { |
| 7017 | // FIXME: We only examine the pattern decl for availability violations now, |
| 7018 | // but we should also examine instantiated templates. |
| 7019 | if (FD->isTemplateInstantiation()) |
| 7020 | return; |
| 7021 | |
| 7022 | Body = FD->getBody(); |
| 7023 | } else if (auto *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 7024 | Body = MD->getBody(); |
| 7025 | |
| 7026 | assert(Body && "Need a body here!"); |
| 7027 | |
Erik Pilkington | f35114c | 2016-10-25 19:05:50 +0000 | [diff] [blame] | 7028 | DiagnoseUnguardedAvailability(*this, D).IssueDiagnostics(Body); |
Erik Pilkington | 5cd5717 | 2016-08-16 17:44:11 +0000 | [diff] [blame] | 7029 | } |